Beginnings of ETA calc based on actual data
Beginnings of ETA calc based on actual data

--- a/include/common-request.inc.php
+++ b/include/common-request.inc.php
@@ -29,6 +29,9 @@
 }
 if (isset($_REQUEST['nearby'])) {
     $nearby = true;
+}
+if (isset($_REQUEST['labs'])) {
+    $labs = true;
 }
 if (isset($_REQUEST['suburb'])) {
     $suburb = $_REQUEST['suburb'];

--- a/labs/busdelay.csv.php
+++ b/labs/busdelay.csv.php
@@ -25,7 +25,7 @@
         foreach ($headers as $i => $fieldName) {
             switch ($fieldName) {
                 case "date":
-                    $row[] = date("dm",strtotime($r['date'])).date("Hi",strtotime($r['time']));
+                    $row[] = date("r",strtotime($r['date']." ".$r['time']));
                     break;
                 case "delay":
                     $row[] = $r['timing_delta'];

--- a/labs/busdelay.php
+++ b/labs/busdelay.php
@@ -166,9 +166,10 @@
   dates = date.group(),
   hour = flight.dimension(function(d) { return d.date.getHours() + d.date.getMinutes() / 60; }),
   hours = hour.group(Math.floor),
-  delay = flight.dimension(function(d) { return Math.max(-60, Math.min(149, d.delay)); }),
+  //delay = flight.dimension(function(d) { return Math.max(-60, Math.min(149, d.delay)); }),
+  delay = flight.dimension(function(d) { return d.delay; }),
   delays = delay.group(function(d) { return Math.floor(d / 10) * 10; }),
-  distance = flight.dimension(function(d) { return Math.min(90, d.distance); }),
+  distance = flight.dimension(function(d) { return Math.min(60, d.distance); }),
   distances = distance.group(function(d) { return Math.floor(d / 50) * 50; });
 
   var charts = [
@@ -184,14 +185,14 @@
         .dimension(delay)
         .group(delays)
       .x(d3.scale.linear()
-        .domain([-60, 150])
+        .domain([-650, 650])
         .rangeRound([0, 10 * 21])),
 
     barChart()
         .dimension(distance)
         .group(distances)
       .x(d3.scale.linear()
-        .domain([0, 90])
+        .domain([0, 60])
         .rangeRound([0, 10 * 40])),
 
     barChart()
@@ -199,9 +200,9 @@
         .group(dates)
         .round(d3.time.day.round)
       .x(d3.time.scale()
-        .domain([new Date(2001, 0, 1), new Date(2001, 3, 1)])
+        .domain([new Date(2011, 4, 1), new Date(2012, 1, 4)])
         .rangeRound([0, 10 * 90]))
-        .filter([new Date(2001, 1, 1), new Date(2001, 2, 1)])
+        .filter([new Date(2011, 4, 4), new Date(2012, 4, 4)])
 
   ];
 
@@ -236,11 +237,7 @@
 
   // Like d3.time.format, but faster.
   function parseDate(d) {
-    return new Date(2001,
-        d.substring(0, 2) - 1,
-        d.substring(2, 4),
-        d.substring(4, 6),
-        d.substring(6, 8));
+    return new Date(d);
   }
 
   window.filter = function(filters) {

file:a/stop.php -> file:b/stop.php
--- a/stop.php
+++ b/stop.php
@@ -141,6 +141,30 @@
 if (sizeof($trips) == 0) {
     echo "<li style='text-align: center;'>No trips in the near future.</li>";
 } else {
+            if ($labs) {
+// ETA calculation
+                
+                $tripETA = Array();
+                // max/min delay instead of stddev?
+                $query = $query = "select 'lol', avg(timing_delta), stddev(timing_delta), count(*) from myway_timingdeltas where extract(hour from time) between ".date("H", $earlierTime)." and ".date("H", $laterTime);
+       //select 'lol', stop_id,extract(hour from time), avg(timing_delta), stddev(timing_delta), count(*) from myway_timingdeltas where stop_id = '5501' group by stop_id, extract(hour from time) order by extract(hour from time)
+                $query = $conn->prepare($query);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    $ETAparams = Array();
+    foreach ($query->fetchAll() as $row) {
+        $ETAparams[$row[0]] = Array("avg"=> $row[1], "stddev"=>floor($row[2]),"count"=>$row[3]);
+    };
+    //print_r($ETAparams);
+    foreach ($trips as $trip) {
+        $tripETA[$trip['trip_id']] = date("H:i",strtotime($trip['arrival_time']." - ".(floor($ETAparams['lol']['stddev']))." seconds"))." to ".
+        date("H:i",strtotime($trip['arrival_time']." + ".(floor($ETAparams['lol']['stddev']))." seconds"));
+    }
+    //print_r($tripETA);
+}
     foreach ($trips as $trip) {
         if (
                 isset($filterHasStop) && (getTripHasStop($trip['trip_id'], $filterHasStop) == 1)
@@ -152,6 +176,9 @@
             $destination = getTripDestination($trip['trip_id']);
             echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " towards " . $destination['stop_name'] . "</h3><p>";
             $viaPoints = viaPointNames($trip['trip_id'], $trip['stop_sequence']);
+if ($labs) {
+                echo '<br><span class="eta">ETA: ' . $tripETA[$trip['trip_id']] . '</span>';
+            }
             if ($viaPoints != "")
                 echo '<br><span class="viaPoints">Via: ' . $viaPoints . '</span>';
             if (sizeof($tripStopNumbers) > 0) {