date picker
[scannr.git] / viewcalls.php
blob:a/viewcalls.php -> blob:b/viewcalls.php
<?php <?php
include('common.inc.php'); include('common.inc.php');
$tgid = 44028; $tgid = 44028;
$from = (isset($_REQUEST['from']) ? $_REQUEST['from'] : strtotime("2012-09-12")); $from = (isset($_REQUEST['from']) ? $_REQUEST['from'] : strtotime("2012-09-12"));
$to = (isset($_REQUEST['to']) ? $_REQUEST['to'] : strtotime("2012-12-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 = $conn->prepare('select distinct date_trunc(\'day\', call_timestamp) as rdate from recordings order by rdate');
   
$sth->execute(); $sth->execute();
foreach ($sth->fetchAll() as $row) { foreach ($sth->fetchAll() as $row) {
echo '<a href="?from=' . strtotime($row['rdate']) . '&amp;to=' . strtotime($row['rdate'] . ' +1 day') . '">' . $row['rdate'] . '</a> <br>'; echo '<a href="?from=' . strtotime($row['rdate']) . '&amp;to=' . strtotime($row['rdate'] . ' +1 day') . '">' . $row['rdate'] . '</a> <br>';
} }
   
?> ?>
  <form class="form-horizontal">
  <fieldset>
  <div class="input-prepend">
  <span class="add-on"><i class="icon-calendar"></i></span><input type="text" name="range" id="range" />
  </div>
  </fieldset>
  </form>
<div class="span12"> <div class="span12">
   
<table width="100%" height="775px"> <table width="100%" height="775px">
<tr> <tr>
<td valign="middle"><span class="arrow-w" style="font-size:2em;">&lt;</span></td> <td valign="middle"><span class="arrow-w" style="font-size:2em;">&lt;</span></td>
<td width="95%"> <td width="95%">
<div id="placeholder" style="width:100%;height:575px;"></div> <div id="placeholder" style="width:100%;height:575px;"></div>
</td> </td>
<td valign="middle"><span class="arrow-e" style="font-size:2em;">&gt;</span></td> <td valign="middle"><span class="arrow-e" style="font-size:2em;">&gt;</span></td>
</tr> </tr>
</table> </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 () { Flotr.EventAdapter.observe(placeholder, 'flotr:click', function () {
drawGraph(); drawGraph();
}); });
   
   
  // Set the default dates
  var startDate = Date.create().addDays(-6), // 7 days ago
  endDate = Date.create(); // today
   
  var range = $('#range');
   
  // Show the dates in the range input
  range.val(startDate.format('{MM}/{dd}/{yyyy}') + ' - ' + endDate.format('{MM}/{dd}/{yyyy}'));
   
  range.daterangepicker({
   
  startDate: startDate,
  endDate: endDate,
   
  ranges: {
  'Today': ['today', 'today'],
  'Yesterday': ['yesterday', 'yesterday'],
  'Last 7 Days': [Date.create().addDays(-6), 'today'],
  'Last 30 Days': [Date.create().addDays(-29), 'today']
  }
  },function(start, end){
   
  ajaxLoadChart(start, end);
   
  });
  function ajaxLoadChart(startDate,endDate) {
   
  // If no data is passed (the chart was cleared)
   
  if(!startDate || !endDate){
  chart.setData({
  "xScale" : "time",
  "yScale" : "linear",
  "main" : [{
  className : ".stats",
  data : []
  }]
  });
   
  return;
  }
   
  // Otherwise, issue an AJAX request
   
  $.getJSON('ajax.php', {
   
  start: startDate.format('{yyyy}-{MM}-{dd}'),
  end: endDate.format('{yyyy}-{MM}-{dd}')
   
  }, function(data) {
   
  var set = [];
  $.each(data, function() {
  set.push({
  x : this.label,
  y : parseInt(this.value, 10)
  });
  });
   
  chart.setData({
  "xScale" : "time",
  "yScale" : "linear",
  "main" : [{
  className : ".stats",
  data : set
  }]
  });
   
  });
  }
getData('<?php echo $tgid; ?>', '<?php echo $from ?>', '<?php echo $to ?>'); 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();
?> ?>