From: Maxious Date: Mon, 08 Oct 2012 11:17:20 +0000 Subject: add example db X-Git-Url: http://maxious.lambdacomplex.org/git/?p=scannr.git&a=commitdiff&h=cf9ff0eadf9531ecb19b202ace6d92cd1c5daf31 --- add example db --- --- /dev/null +++ b/.gitignore @@ -1,1 +1,3 @@ - +*.wav +*.pyc +/nbproject/private/ --- /dev/null +++ b/.gitmodules @@ -1,1 +1,7 @@ +[submodule "pynma"] + path = pynma + url = git://github.com/uskr/pynma.git +[submodule "js/flotr2"] + path = js/flotr2 + url = git://github.com/HumbleSoftware/Flotr2.git --- /dev/null +++ b/calls.json.php @@ -1,1 +1,68 @@ + $sensorID, + ); + $timefrom = 1; + $timeto = 1; + $years = $this->Sensor_model->getSensorDataYears($sensorID, $timefrom, $timeto); + foreach ($years as $year) { + $yearsItem[] = Array("title" => $year['year'], "link" => "#", + "onclick" => "getData('$sensorID'," . mktime(0, 0, 0, 1, 1, $year['year']) . "," . mktime(0, 0, 0, 12, 31, $year['year']) . ");"); + } + $months = $this->Sensor_model->getSensorDataMonths($sensorID, $timefrom, $timeto); + foreach ($months as $month) { + $monthsItem[] = Array("title" => date("F", mktime(0, 0, 0, $month['month'], 10)) . ' ' . $month['year'], "link" => "#", + "onclick" => "getData('$sensorID'," . mktime(0, 0, 0, $month['month'], 1, $month['year']) . "," . strtotime("+1 month", mktime(0, 0, 0, $month['month'], 1, $month['year'])) . ");"); + } + $days = $this->Sensor_model->getSensorDataDays($sensorID, $timefrom, $timeto); + foreach ($days as $day) { + $daysItem[] = Array("title" => date("jS F", mktime(0, 0, 0, $day['month'], $day['day'])), "link" => "#", + "onclick" => "getData('$sensorID'," . mktime(0, 0, 0, $day['month'], $day['day'], $day['year']) . "," . mktime(23, 59, 59, $day['month'], $day['day'], $day['year']) . ");"); + } + + $data['sidebar'] = Array("years" => $yearsItem, "months" => $monthsItem, "days" => $daysItem + ); + $this->javascript = Array("flot/jquery.flot.min.js", "flot/jquery.flot.resize.min.js", "flot/jquery.flot.crosshair.min.js"); + $this->content = $this->load->view('pages/sensor/view', $data, true); + $this->render_page(); +} + +function json_graph($sensorID, $timefrom, $timeto) { + $values = $this->Sensor_model->getSensorValuesByHour($sensorID, $timefrom, $timeto); + $label = $sensorID; + $data = Array(); + $tzoffset = $this->get_timezone_offset("UTC"); + foreach ($values as $value) { + $data[] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg'])); + } + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(Array("label" => $label, "data" => $data, + "previous" => Array( + "from" => $timefrom - (24 * 60 * 60), + "to" => $timefrom) + , + "next" => Array( + "to" => $timeto + (24 * 60 * 60), + "from" => $timeto) + ) + )); +} + +function json_data_description($sensorID, $timefrom, $timeto) { + $timefrom = strtotime($timefrom); + $timeto = strtotime($timeto); + $years = $this->Sensor_model->getSensorDataYears($sensorID, $timefrom, $timeto); + + $months = $this->Sensor_model->getSensorDataMonths($sensorID, $timefrom, $timeto); + $days = $this->Sensor_model->getSensorDataDays($sensorID, $timefrom, $timeto); + + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(Array("years" => $years, "months" => $months, "days" => $days + ))); +} + +?> + --- /dev/null +++ b/common.inc.php @@ -1,1 +1,159 @@ +getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); + return $offset; +} + +function getSensorValuesByHour($sensorID, $timeFrom, $timeTo) { + $sql = 'select min(time) as time, min(value), max(value), avg(value), stddev(value) from sensor_values where sensor_id = ? + and extract(epoch from time) > ? and extract(epoch from time) < ? + group by sensor_id, date_trunc(\'hour\', time) order by time'; + + $query = $this->db->query($sql, Array($sensorID, $timeFrom, $timeTo)); + return $query->result_array(); +} + +function getSensorValuesByDay($sensorID, $dayFrom, $dayTo) { + $sql = 'select min(time) as time, min(value), max(value), avg(value), stddev(value) from sensor_values where sensor_id = ? + group by sensor_id, date_trunc(\'day\', time) order by time'; + + $query = $this->db->query($sql, Array($sensorID)); + return $query->result_array(); +} + +function getSensorDataYears($sensorID, $timeFrom, $timeTo) { + $sql = "select distinct extract('year' from time) as year from sensor_values where sensor_id = ? order by year"; + + $query = $this->db->query($sql, Array($sensorID)); + return $query->result_array(); +} + +function getSensorDataMonths($sensorID, $timeFrom, $timeTo) { + $sql = "select distinct extract('month' from time) as month, extract('year' from time) as year from sensor_values where sensor_id = ? order by year, month"; + + $query = $this->db->query($sql, Array($sensorID)); + return $query->result_array(); +} + +function getSensorDataDays($sensorID, $timeFrom, $timeTo) { + $sql = "select distinct extract('day' from time) as day, extract('month' from time) as month, extract('year' from time) as year from sensor_values where sensor_id = ? order by year,month,day"; + + $query = $this->db->query($sql, Array($sensorID)); + return $query->result_array(); +} + +function include_header($title) { + global $basePath; + ?> + + + + + + + + + + + + + + <?php echo $title; ?> - Scannr + + + + + + + + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + + + + + +