Improve myway timeliness based on new GTFS feed accuracy
Improve myway timeliness based on new GTFS feed accuracy

--- a/include/common-transit.inc.php
+++ b/include/common-transit.inc.php
@@ -27,7 +27,8 @@
         return $_SESSION['service_period'];
     $override = getServiceOverride($date);
     if ($override['service_id']) {
-        return $override['service_id'];
+        $idParts = explode("-",$override['service_id']);
+        return strtolower($idParts[2]);
     }
 
     switch (date('w', ($date != "" ? $date : time()))) {

--- a/include/db/stop-dao.inc.php
+++ b/include/db/stop-dao.inc.php
@@ -163,19 +163,20 @@
     return $query->fetchAll();
 }
 
-function getStopTrips($stopID, $service_period = "", $afterTime = "", $limit = "") {
+function getStopTrips($stopID, $service_period = "", $afterTime = "", $limit = "", $route_short_name = "") {
     if ($service_period == "") {
         $service_period = service_period();
     }
         $service_ids = service_ids($service_period);
-
     $sidA = $service_ids[0];
     $sidB = $service_ids[1];
+    $limitSQL = "";
     if ($limit != "")
-        $limitSQL = " LIMIT :limit ";
+        $limitSQL .= " LIMIT :limit ";
+        
     global $conn;
     if ($afterTime != "") {
-        $query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name, end_times.arrival_time as end_time
+        $query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name,end_times.arrival_time as end_time
 FROM stop_times
 join trips on trips.trip_id =
 stop_times.trip_id
@@ -183,7 +184,7 @@
 	WHERE stop_times.arrival_time IS NOT NULL group by trip_id) as end_times 
 WHERE stop_times.stop_id = :stopID
 AND stop_times.trip_id = end_times.trip_id
-AND (service_id=:service_periodA OR service_id=:service_periodB)
+AND (service_id=:service_periodA OR service_id=:service_periodB) ".($route_short_name != "" ? " AND route_short_name = :route_short_name ":"")." 
 AND end_times.arrival_time > :afterTime
 ORDER BY end_time $limitSQL";
     } else {
@@ -193,7 +194,7 @@
 stop_times.trip_id
 join routes on trips.route_id = routes.route_id
 WHERE stop_times.stop_id = :stopID
-AND (service_id=:service_periodA OR service_id=:service_periodB)
+AND (service_id=:service_periodA OR service_id=:service_periodB) ".($route_short_name != "" ? " AND route_short_name = :route_short_name ":"")." 
 ORDER BY arrival_time $limitSQL";
     }
     debug($query, "database");
@@ -205,6 +206,8 @@
         $query->bindParam(":limit", $limit);
     if ($afterTime != "")
         $query->bindParam(":afterTime", $afterTime);
+    if ($route_short_name != "")
+        $query->bindParam(":route_short_name", $route_short_name);
     $query->execute();
     if (!$query) {
         databaseError($conn->errorInfo());

--- a/myway/myway_timeliness_calculate.php
+++ b/myway/myway_timeliness_calculate.php
@@ -29,7 +29,8 @@
 
 //collect all observation not in delta
 $query = "select * from myway_observations INNER JOIN myway_stops
-ON myway_observations.myway_stop=myway_stops.myway_stop 
+ON myway_observations.myway_stop=myway_stops.myway_stop INNER JOIN myway_routes
+ON myway_observations.myway_route=myway_routes.myway_route 
  WHERE observation_id NOT IN
 (
 SELECT  observation_id
@@ -54,10 +55,10 @@
 // timezones from http://www.postgresql.org/docs/8.0/static/datetime-keywords.html
     $time = date("H:i:s", strtotime($obsv['time']));
     $time_tz = date("H:i:s", strtotime($obsv['time'])) . " AESST";
-    $search_time = date("H:i:s", strtotime($obsv['time']) - (30 * 60)); // 30 minutes margin
+    $search_time = date("H:i:s", strtotime($obsv['time']) - (60 * 60)); // 30 minutes margin
     $date = date("c", strtotime($obsv['time']));
     $timing_period = service_period(strtotime($date));
-    if (isset($obsv["stop_id"]) && $obsv["stop_id"] != "" ) {
+    if (isset($obsv["stop_id"]) && $obsv["stop_id"] != "" && $obsv["stop_id"] != "*") {
     $potentialStops = Array(getStop($obsv["stop_id"]));
     } else {
         echo "Potential stops are a bus station<br>";
@@ -79,7 +80,7 @@
     //:get myway_route record
     //no result, skip and display error
     //print out route
-    $potentialRoutes = getRoutesByShortName(preg_replace("/[A-Z]/", "", $obsv["myway_route"]));
+    $potentialRoutes = getRoutesByShortName($obsv["route_short_name"]);
     if (sizeof($potentialRoutes) < 1) {
         echo "error, route '{$obsv["myway_route"]}' unknown";
         continue;
@@ -96,7 +97,7 @@
                     echo "Matching route {$stopRoute['route_id']} found at stop #{$potentialStop['stop_id']}<br>";
                     $foundRoute = $stopRoute;
                     //if does get tripstoptimes for this route
-                    $trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time);
+                    $trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time, 10, $potentialRoute['route_short_name']);
                     foreach ($trips as $trip) {
                         //echo $trip['route_id']." ".$stopRoute['route_id'].";";
                         if ($trip['route_id'] == $stopRoute['route_id']) {
@@ -109,13 +110,16 @@
                                 "timeDiff" => $timeDiff,
                                 "stop_id" => $potentialStop['stop_id'],
                                 "stop_sequence" => $trip['stop_sequence'],
-                                "route_name" => "{$potentialRoute['route_short_name']} {$potentialRoute['route_long_name']} {$trip['direction']}",
+                                "route_name" => "{$trip['route_short_name']} {$trip['route_long_name']} {$trip['direction']}",
                                 "route_id" => $trip['route_id']
                             );
                             echo "Found trip {$trip['trip_id']} at stop {$potentialStop['stop_id']} (#{$potentialStop['stop_name']}, sequence #{$trip['stop_sequence']})<br>";
                             echo "Arriving at {$timedTrip['arrival_time']}, difference of " . round($timeDiff / 60, 2) . " minutes<br>";
+                        } else {
+                            echo "{$trip['route_id']} != {$stopRoute['route_id']}<br>";
                         }
                     }
+                    if (sizeof($timeDeltas) == 0) echo "Error, no trips found.<bR>";
                     break; // because have found route
                 }
             }
@@ -163,6 +167,7 @@
         }
         var_dump($conn->errorInfo());
         flush();
+       
         }
     }
     flush();

--- a/myway/myway_timeliness_route.json.php
+++ b/myway/myway_timeliness_route.json.php
@@ -25,7 +25,8 @@
 "data": <?php
 $query = "select * from myway_timingdeltas where route_name = :route_name AND abs(timing_delta) < 2*(select stddev(timing_delta) from myway_timingdeltas)  order by stop_sequence;";
 $query = $conn->prepare($query);
-$query->bindParam(':route_name', $_REQUEST['routeid'], PDO::PARAM_STR, 42);
+$_REQUEST['routeid'].=" ";
+$query->bindParam(':route_name', $_REQUEST['routeid'], PDO::PARAM_STR);
 
 $query->execute();
 if (!$query) {

--- a/myway/myway_timeliness_stop.php
+++ b/myway/myway_timeliness_stop.php
@@ -46,9 +46,7 @@
             var data = [];
             var options = {
                 xaxis: {
-                    mode: "time",
-                    min: midnight + (1000*60*60*8),
-                    max: midnight + (1000*60*60*23.5)
+                    mode: "time"
                 },
                 yaxis: {
                     tickFormatter: yformatter