graph date filter
graph date filter

<?php <?php
include('common.inc.php'); include('common.inc.php');
function getTGIDValuesByHour($TGID, $timeFrom, $timeTo) { function getTGIDValuesByHour($TGID, $timeFrom, $timeTo) {
global $conn; global $conn;
$sth = $conn->prepare( 'select tgid, min(call_timestamp) as time, count(*), min(length), max(length), avg(length), stddev(length) from recordings $sth = $conn->prepare( 'select tgid, min(call_timestamp) as time, count(*), min(length), max(length), avg(length), stddev(length) from recordings
  where call_timestamp between to_timestamp(?) and to_timestamp(?)
group by tgid, date_trunc(\'hour\', call_timestamp) order by time'); group by tgid, date_trunc(\'hour\', call_timestamp) order by time');
   
$sth->execute( ); $sth->execute(Array($timeFrom, $timeTo));
//Array($TGID, $timeFrom, $timeTo)  
return $sth->fetchAll(); return $sth->fetchAll();
   
   
} }
   
function getTGIDValuesByDay($TGID, $dayFrom, $dayTo) { function getTGIDValuesByDay($TGID, $dayFrom, $dayTo) {
global $conn; global $conn;
$sth = $conn->prepare('select min(time) as time, min(value), max(value), avg(value), stddev(value) from sensor_values where sensor_id = ? $sth = $conn->prepare('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'); group by sensor_id, date_trunc(\'day\', time) order by time');
   
$sth->execute( Array($TGID)); $sth->execute( Array($TGID));
return $sth->fetchAll(); return $sth->fetchAll();
} }
function getTGIDDataYears($TGID, $timeFrom, $timeTo) { function getTGIDDataYears($TGID, $timeFrom, $timeTo) {
global $conn; global $conn;
$sth = $conn->prepare("select distinct extract('year' from call_timestamp) as year from recordings where tgid = ? order by year"); $sth = $conn->prepare("select distinct extract('year' from call_timestamp) as year from recordings where tgid = ? order by year");
   
$sth->execute(Array($TGID)); $sth->execute(Array($TGID));
return $sth->fetchAll(); return $sth->fetchAll();
} }
   
function getTGIDDataMonths($TGID, $timeFrom, $timeTo) { function getTGIDDataMonths($TGID, $timeFrom, $timeTo) {
global $conn; global $conn;
$sth = $conn->prepare("select distinct extract('month' from call_timestamp) as month, extract('year' from call_timestamp) as year from recordings where tgid = ? order by year, month"); $sth = $conn->prepare("select distinct extract('month' from call_timestamp) as month, extract('year' from call_timestamp) as year from recordings where tgid = ? order by year, month");
   
$sth->execute(Array($TGID)); $sth->execute(Array($TGID));
return $sth->fetchAll(); return $sth->fetchAll();
} }
   
function getTGIDDataDays($TGID, $timeFrom, $timeTo) { function getTGIDDataDays($TGID, $timeFrom, $timeTo) {
global $conn; global $conn;
$sth = $conn->prepare("select distinct extract('day' from call_timestamp) as day, extract('month' from call_timestamp) as month, extract('year' from call_timestamp) as year from recordings where tgid = ? order by year,month,day"); $sth = $conn->prepare("select distinct extract('day' from call_timestamp) as day, extract('month' from call_timestamp) as month, extract('year' from call_timestamp) as year from recordings where tgid = ? order by year,month,day");
   
   
$sth->execute(Array($TGID)); $sth->execute(Array($TGID));
return $sth->fetchAll(); return $sth->fetchAll();
} }
$action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : ''); $action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
$TGID = (isset($_REQUEST['tgid']) ? $_REQUEST['tgid'] : ''); $TGID = (isset($_REQUEST['tgid']) ? $_REQUEST['tgid'] : '');
$timefrom = (isset($_REQUEST['from']) ? $_REQUEST['from'] : ''); $timefrom = (isset($_REQUEST['from']) ? $_REQUEST['from'] : '');
$timeto = (isset($_REQUEST['to']) ? $_REQUEST['to'] : ''); $timeto = (isset($_REQUEST['to']) ? $_REQUEST['to'] : '');
   
if ($action == "data_description") { if ($action == "data_description") {
$timefrom = strtotime($timefrom); $timefrom = strtotime($timefrom);
$timeto = strtotime($timeto); $timeto = strtotime($timeto);
$years = getTGIDDataYears($TGID, $timefrom, $timeto); $years = getTGIDDataYears($TGID, $timefrom, $timeto);
   
$months = getTGIDDataMonths($TGID, $timefrom, $timeto); $months = getTGIDDataMonths($TGID, $timefrom, $timeto);
$days = getTGIDDataDays($TGID, $timefrom, $timeto); $days = getTGIDDataDays($TGID, $timefrom, $timeto);
   
echo json_encode(Array("years" => $years, "months" => $months, "days" => $days echo json_encode(Array("years" => $years, "months" => $months, "days" => $days
)); ));
} }
   
   
if (strpos($action,"graph") !== false) { if (strpos($action,"graph") !== false) {
$values = getTGIDValuesByHour($TGID, $timefrom, $timeto); $values = getTGIDValuesByHour($TGID, $timefrom, $timeto);
$label = $TGID; $label = $TGID;
$data = Array(); $data = Array();
$tzoffset = get_timezone_offset("UTC"); $tzoffset = get_timezone_offset("UTC");
foreach ($values as $value) { foreach ($values as $value) {
if ($action == "graphlength") { if ($action == "graphlength") {
$data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg'])); $data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg']));
} else if ($action == "graphcount") { } else if ($action == "graphcount") {
$data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['count'])); $data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['count']));
} }
} }
echo json_encode(Array("label" => $label, "data" => $data, echo json_encode(Array("label" => $label, "data" => $data,
"previous" => Array( "previous" => Array(
"from" => $timefrom - (24 * 60 * 60), "from" => $timefrom - (24 * 60 * 60),
"to" => $timefrom) "to" => $timefrom)
, ,
"next" => Array( "next" => Array(
"to" => $timeto + (24 * 60 * 60), "to" => $timeto + (24 * 60 * 60),
"from" => $timeto) "from" => $timeto)
) )
); );
} }
   
   
   
?> ?>
   
<?php <?php
include('common.inc.php'); include('common.inc.php');
$tgid = 44028; $tgid = 44028;
  $from = (isset($_REQUEST['from']) ? $_REQUEST['from'] : strtotime("2012-09-12"));
  $to = (isset($_REQUEST['to']) ? $_REQUEST['to'] : strtotime("2012-12-12"));
include_header("fdds"); include_header("fdds");
  $sth = $conn->prepare('select distinct date_trunc(\'day\', call_timestamp) as rdate from recordings order by rdate');
   
  $sth->execute();
  foreach( $sth->fetchAll() as $row) {
  echo '<a href="?from='.strtotime($row['rdate']).'&amp;to='.strtotime($row['rdate'].' +1 day').'">'.$row['rdate'].'</a> <br>';
  }
   
?> ?>
<div class="span12"> <div class="span12">
   
<table width="100%" height="775px"><tr><td valign="middle"><span class="arrow-w" style="font-size:2em;">&lt;</span></td><td width="95%"><div id="placeholder" style="width:100%;height:575px;"></div></td><td valign="middle"><span class="arrow-e" style="font-size:2em;">&gt;</span></td></tr></table> <table width="100%" height="775px"><tr><td valign="middle"><span class="arrow-w" style="font-size:2em;">&lt;</span></td><td width="95%"><div id="placeholder" style="width:100%;height:575px;"></div></td><td valign="middle"><span class="arrow-e" style="font-size:2em;">&gt;</span></td></tr></table>
<script> <script>
var data = []; var data = [];
var plot; var plot;
var options = { var options = {
lines: { show: true }, lines: { show: true },
points: { show: true }, points: { show: true },
xaxis : { xaxis : {
mode : 'time', mode : 'time',
labelsAngle : 45 labelsAngle : 45
}, },
selection : { mode : 'x', fps : 30 }, selection : { mode : 'x', fps : 30 },
series: { series: {
lines: { show: true }, lines: { show: true },
points: { show: true } points: { show: true }
}, },
mouse : { mouse : {
track : true, track : true,
relative : true relative : true
} }
}; };
$(function () { $(function () {
// graph // graph
var placeholder = document.getElementById("placeholder"); var placeholder = document.getElementById("placeholder");
   
drawGraph (options); drawGraph (options);
   
// Hook into the 'flotr:select' event. // Hook into the 'flotr:select' event.
Flotr.EventAdapter.observe(placeholder, 'flotr:select', function (area) { Flotr.EventAdapter.observe(placeholder, 'flotr:select', function (area) {
   
// Draw graph with new area // Draw graph with new area
graph = drawGraph({ graph = drawGraph({
xaxis: {min:area.x1, max:area.x2, mode : 'time', labelsAngle : 45}, xaxis: {min:area.x1, max:area.x2, mode : 'time', labelsAngle : 45},
yaxis: {min:area.y1, max:area.y2} yaxis: {min:area.y1, max:area.y2}
}); });
}); });
   
// When graph is clicked, draw the graph with default area. // When graph is clicked, draw the graph with default area.
Flotr.EventAdapter.observe(placeholder, 'flotr:click', function () { drawGraph(); }); Flotr.EventAdapter.observe(placeholder, 'flotr:click', function () { drawGraph(); });
   
getData('<?php echo $tgid; ?>','<?php echo strtotime("10/09/2012") ?>','<?php echo strtotime("10/11/2012") ?>'); getData('<?php echo $tgid; ?>','<?php echo $from ?>','<?php echo $to ?>');
}); });
   
// Draw graph with default options, overwriting with passed options // Draw graph with default options, overwriting with passed options
function drawGraph (opts) { function drawGraph (opts) {
   
// Clone the options, so the 'options' variable always keeps intact. // Clone the options, so the 'options' variable always keeps intact.
var o = Flotr._.extend(Flotr._.clone(options), opts || {}); var o = Flotr._.extend(Flotr._.clone(options), opts || {});
   
// Return a new graph. // Return a new graph.
return Flotr.draw( return Flotr.draw(
placeholder, placeholder,
data, data,
o o
); );
} }
   
   
function onDataReceived(series) { function onDataReceived(series) {
data =[] data =[]
for (var key in series.data) { for (var key in series.data) {
data[data.length] = {label: key, data: series.data[key]}; data[data.length] = {label: key, data: series.data[key]};
} }
drawGraph (options); drawGraph (options);
} }
function getData(sensorID,from,to) { function getData(sensorID,from,to) {
$.ajax({ $.ajax({
url: "<?php echo $basePath; ?>calls.json.php?action=graphcount&tgid="+sensorID+"&from="+from+"&to="+to, url: "<?php echo $basePath; ?>calls.json.php?action=graphcount&tgid="+sensorID+"&from="+from+"&to="+to,
method: 'GET', method: 'GET',
dataType: 'json', dataType: 'json',
success: onDataReceived success: onDataReceived
}); });
} }
   
   
   
</script> </script>
</div> </div>
<?php <?php
include_footer(); include_footer();
?> ?>