move files to data folder
[scannr.git] / calls.json.php
blob:a/calls.json.php -> blob:b/calls.json.php
<?php <?php
  include('common.inc.php');
  function getTGIDValuesByHour($TGID, $timeFrom, $timeTo) {
  global $conn;
  $sth = $conn->prepare( 'select tgid, min(call_timestamp) as time, count(*), min(length), max(length), avg(length), stddev(length) from recordings
  group by tgid, date_trunc(\'hour\', call_timestamp) order by time');
   
  $sth->execute( );
  //Array($TGID, $timeFrom, $timeTo)
  return $sth->fetchAll();
   
   
function json_graph($sensorID, $timefrom, $timeto) { }
$values = getSensorValuesByHour($sensorID, $timefrom, $timeto);  
$label = $sensorID; function getTGIDValuesByDay($TGID, $dayFrom, $dayTo) {
$data = Array(); global $conn;
$tzoffset = $this->get_timezone_offset("UTC"); $sth = $conn->prepare('select min(time) as time, min(value), max(value), avg(value), stddev(value) from sensor_values where sensor_id = ?
foreach ($values as $value) { group by sensor_id, date_trunc(\'day\', time) order by time');
$data[] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg']));  
} $sth->execute( Array($TGID));
$this->output return $sth->fetchAll();
->set_content_type('application/json') }
->set_output(json_encode(Array("label" => $label, "data" => $data, function getTGIDDataYears($TGID, $timeFrom, $timeTo) {
"previous" => Array( global $conn;
"from" => $timefrom - (24 * 60 * 60), $sth = $conn->prepare("select distinct extract('year' from call_timestamp) as year from recordings where tgid = ? order by year");
"to" => $timefrom)  
, $sth->execute(Array($TGID));
"next" => Array( return $sth->fetchAll();
"to" => $timeto + (24 * 60 * 60), }
"from" => $timeto)  
) function getTGIDDataMonths($TGID, $timeFrom, $timeTo) {
  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->execute(Array($TGID));
  return $sth->fetchAll();
  }
   
  function getTGIDDataDays($TGID, $timeFrom, $timeTo) {
  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->execute(Array($TGID));
  return $sth->fetchAll();
  }
  $action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
  $TGID = (isset($_REQUEST['tgid']) ? $_REQUEST['tgid'] : '');
  $timefrom = (isset($_REQUEST['from']) ? $_REQUEST['from'] : '');
  $timeto = (isset($_REQUEST['to']) ? $_REQUEST['to'] : '');
   
  if ($action == "data_description") {
  $timefrom = strtotime($timefrom);
  $timeto = strtotime($timeto);
  $years = getTGIDDataYears($TGID, $timefrom, $timeto);
   
  $months = getTGIDDataMonths($TGID, $timefrom, $timeto);
  $days = getTGIDDataDays($TGID, $timefrom, $timeto);
   
  echo json_encode(Array("years" => $years, "months" => $months, "days" => $days
)); ));
} }
   
function json_data_description($sensorID, $timefrom, $timeto) {  
$timefrom = strtotime($timefrom);  
$timeto = strtotime($timeto);  
$years = getSensorDataYears($sensorID, $timefrom, $timeto);  
   
$months = getSensorDataMonths($sensorID, $timefrom, $timeto); if (strpos($action,"graph") !== false) {
$days = getSensorDataDays($sensorID, $timefrom, $timeto); $values = getTGIDValuesByHour($TGID, $timefrom, $timeto);
  $label = $TGID;
  $data = Array();
  $tzoffset = get_timezone_offset("UTC");
  foreach ($values as $value) {
  if ($action == "graphlength") {
  $data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg']));
  } else if ($action == "graphcount") {
  $data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['count']));
  }
  }
  echo 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)
  )
  );
  }
   
return json_encode(Array("years" => $years, "months" => $months, "days" => $days  
));  
}  
   
?> ?>