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

--- a/feedback.php
+++ b/feedback.php
@@ -31,7 +31,7 @@
 } else {
 $stopid = "";
 $stopcode = "";
-$urlparts = explode("?",$_SERVER["HTTP_REFERER"]);
+if (isset($_SERVER["HTTP_REFERER"])) $urlparts = explode("?",$_SERVER["HTTP_REFERER"]);
 if (isset($urlparts[1])) {
     $getparams = explode("&",$urlparts[1]);
     foreach ($getparams as $param) {
@@ -60,7 +60,7 @@
 </textarea>
 <textarea name="extrainfo" id="extrainfo">
 <?php
-  echo "Referrer URL: ".$_SERVER["HTTP_REFERER"];
+  echo "Referrer URL: ".($_SERVER["HTTP_REFERER"] ? $_SERVER["HTTP_REFERER"] : "");
   echo "\nCurrent page URL: ".curPageURL();
   echo "\nUser Agent: ".$_SERVER["HTTP_USER_AGENT"];
   echo "\nUser host/IP: ".$_SERVER["HTTP_X_FORWARDED_FOR"]." ".$_SERVER["REMOTE_ADDR"]; 

--- a/include/common-db.inc.php
+++ b/include/common-db.inc.php
@@ -15,7 +15,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
  */
-if (php_uname('n') == "actbus-www") {
+if (strstr(php_uname('n'),"actbus")) {
     $conn = new PDO("pgsql:dbname=transitdata;user=transitdata;password=transitdata;host=bus-main.lambdacomplex.org");
 } else if (isDebugServer()) {
     $conn = new PDO("pgsql:dbname=transitdata;user=postgres;password=snmc;host=localhost");

--- a/include/common-request.inc.php
+++ b/include/common-request.inc.php
@@ -58,7 +58,7 @@
     $stopids = explode(",", filter_var($_REQUEST['stopids'], FILTER_SANITIZE_STRING));
 }
 if (isset($_REQUEST['tripid'])) {
-    $tripid = filter_var($_REQUEST['tripid'], FILTER_SANITIZE_NUMBER_INT);
+    $tripid = filter_var($_REQUEST['tripid'], FILTER_SANITIZE_STRING);
 }
 if (isset($_REQUEST['stopid'])) {
     $stopid = filter_var($_REQUEST['stopid'], FILTER_SANITIZE_NUMBER_INT);

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -27,7 +27,7 @@
     $url.= $GA_PIXEL . "?";
     $url.= "utmac=" . $GA_ACCOUNT;
     $url.= "&utmn=" . rand(0, 0x7fffffff);
-    $referer = $_SERVER["HTTP_REFERER"];
+    $referer = (isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "");
     $query = $_SERVER["QUERY_STRING"];
     $path = $_SERVER["REQUEST_URI"];
     if (empty($referer)) {
@@ -169,6 +169,7 @@
 	</div><!-- /header -->
         <a name="maincontent" id="maincontent"></a>
         <div data-role="content"> ';
+        if ($GTFSREnabled) {
         $overrides = getServiceOverride();
         if ($overrides['service_id']) {
             if ($overrides['service_id'] == "noservice") {
@@ -178,7 +179,6 @@
                 echo '<div id="servicewarning">Buses are running on an altered timetable today due to industrial action/public holiday. See <a href="http://www.action.act.gov.au">http://www.action.act.gov.au</a> for details.</div>';
             }
         }
-        if ($GTFSREnabled) {
             $serviceAlerts = getServiceAlertsAsArray("agency", "0");
             if (isset($serviceAlerts['entity']) && sizeof($serviceAlerts['entity']) > 0) {
                 foreach ($serviceAlerts['entity'] as $entity) {
@@ -241,5 +241,36 @@
     }
 }
 
+//stop list collapsing
+function stopCompare($stopName) {
+    return substr(trim(preg_replace("/\(Platform.*/", "", $stopName)),0,9);
+}
+function stopGroupTitle($stopName,$stopdesc) {
+    if (preg_match("/Dr |Cct |Cir |Av |St |Cr |Parade |Way |Bank /",$stopName)) {
+        $descParts =  explode("<br>",$stopdesc);
+         return trim(str_replace("Street: ","",$descParts[0]));
+    } else {
+        return trim(preg_replace("/\(Platform.*/", "",$stopName));
+    }
+}
+
+function viaPointNames($tripid, $stop_sequence = "") {
+    $viaPointNames = Array();
+    foreach (viaPoints($tripid, $stop_sequence) as $point) {
+        if (strstr($point['stop_name'], "Station")
+                || strstr($point['stop_name'], "Shops")
+                || strstr($point['stop_name'], "CIT")
+                || strstr($point['stop_name'], "School")
+                || strstr($point['stop_name'], "University")
+        ) {
+            $viaPointNames[] = $point['stop_name'];
+        }
+    }
+    if (sizeof($viaPointNames) > 0) {
+        return r_implode(", ", $viaPointNames);
+    } else {
+        return "";
+    }
+}
 ?>
 

--- 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/common.inc.php
+++ b/include/common.inc.php
@@ -31,7 +31,7 @@
 $cloudmadeAPIkey = "daa03470bb8740298d4b10e3f03d63e6";
 $googleMapsAPIkey = "ABQIAAAA95XYXN0cki3Yj_Sb71CFvBTPaLd08ONybQDjcH_VdYtHHLgZvRTw2INzI_m17_IoOUqH3RNNmlTk1Q";
 $otpAPIurl = 'http://localhost:8080/opentripplanner-api-webapp/';
-if (isDebug("awsotp") || php_uname('n') == "maxious.xen.prgmr.com") {
+if (isDebug("awsotp") || php_uname('n') == "maxious.xen.prgmr.com" || strstr(php_uname('n'),"actbus")) {
     $otpAPIurl = 'http://bus-main.lambdacomplex.org:8080/opentripplanner-api-webapp/';
 }
 if (isDebug("dotcloudotp") || php_uname('n') == "actbus-www") {
@@ -56,14 +56,16 @@
 
 function isDebugServer() {
     
-    return php_sapi_name() == "cli" || isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "azusa" || $_SERVER['SERVER_NAME'] == "vanille"
+    return php_sapi_name() == "cli" || strstr(php_uname('n'),"actbus") || isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "azusa" || $_SERVER['SERVER_NAME'] == "vanille"
             || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1" ||  $_SERVER['SERVER_NAME'] == "192.168.1.8");
 }
 
 include_once ("common-geo.inc.php");
 include_once ("common-net.inc.php");
 include_once ("common-transit.inc.php");
-include_once ("common-db.inc.php");
+if (!strstr($_SERVER['PHP_SELF'], "feedback")) {
+	include_once ("common-db.inc.php");
+}
 
 include_once ("common-request.inc.php");
 include_once ("common-session.inc.php");

--- a/include/db/stop-dao.inc.php
+++ b/include/db/stop-dao.inc.php
@@ -101,10 +101,10 @@
 
 function getStopsBySuburb($suburb) {
     global $conn;
-    $query = "Select * from stops where zone_id LIKE :suburb order by stop_name;";
-    debug($query, "database");
-    $query = $conn->prepare($query);
-    $suburb = "%" . $suburb . ";%";
+    $query = "Select * from stops where stop_desc LIKE :suburb order by stop_name;";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $suburb = "%<br>Suburb: %" . $suburb . "%";
     $query->bindParam(":suburb", $suburb);
     $query->execute();
     if (!$query) {
@@ -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/include/db/trip-dao.inc.php
+++ b/include/db/trip-dao.inc.php
@@ -28,6 +28,7 @@
     $query->execute();
     if (!$query) {
         databaseError($conn->errorInfo());
+
         return Array();
     }
     return $query->fetch(PDO :: FETCH_ASSOC);
@@ -56,7 +57,8 @@
 
 function getTripStopTimes($tripID) {
     global $conn;
-    $query = "SELECT stop_times.trip_id,trip_headsign,arrival_time,stop_times.stop_id,stop_lat,stop_lon,stop_name,stop_code,
+    $query = "SELECT stop_times.trip_id,trip_headsign,arrival_time,stop_times.stop_id
+    ,stop_lat,stop_lon,stop_name,stop_desc,stop_code,
 	stop_sequence,service_id,trips.route_id,route_short_name,route_long_name
 FROM stop_times
 join trips on trips.trip_id = stop_times.trip_id
@@ -83,6 +85,7 @@
     }
     return Array();
 }
+
 function getTripStartTime($tripID) {
     global $conn;
     $query = "Select * from stop_times
@@ -154,16 +157,5 @@
     return $query->fetchAll();
 }
 
-function viaPointNames($tripid, $stop_sequence = "") {
-    $viaPointNames = Array();
-    foreach (viaPoints($tripid, $stop_sequence) as $point) {
-        $viaPointNames[] = $point['stop_name'];
-    }
-    if (sizeof($viaPointNames) > 0) {
-        return r_implode(", ", $viaPointNames);
-    } else {
-        return "";
-    }
-}
 
 ?>

--- 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

--- a/stopList.php
+++ b/stopList.php
@@ -17,17 +17,7 @@
  */
 include ('include/common.inc.php');
 $stops = Array();
-function stopCompare($stopName) {
-    return substr(trim(preg_replace("/\(Platform.*/", "", $stopName)),0,9);
-}
-function stopGroupTitle($stopName,$stopdesc) {
-    if (preg_match("/Dr |Cct |Cir |Av |St |Cr |Parade |Way |Bank /",$stopName)) {
-        $descParts =  explode("<br>",$stopdesc);
-         return trim(str_replace("Street: ","",$descParts[0]));
-    } else {
-        return trim(preg_replace("/\(Platform.*/", "",$stopName));
-    }
-}
+
 function navbar() {
     echo '
 		<div data-role="navbar">
@@ -149,7 +139,7 @@
                 } else {
                     // subsequent duplicates
                     $stopsGrouped["stop_ids"][] = $stop['stop_id'];
-                    ;
+                    
                 }
             }
         }

file:a/trip.php -> file:b/trip.php
--- a/trip.php
+++ b/trip.php
@@ -24,7 +24,6 @@
     $trip = getTrip($tripid);
     $routeid = $trip["route_id"];
 }
-
 include_header("Stops on " . $trip['route_short_name'] . ' ' . $trip['route_long_name'], "trip");
 trackEvent("Route/Trip View", "View Route", $trip['route_short_name'] . ' ' . $trip['route_long_name'], $routeid);
 echo '<span class="content-secondary">';
@@ -69,9 +68,9 @@
 $tripStopTimes = getTripStopTimes($tripid);
 echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' ' . $trip['route_long_name'] . ' (' . ucwords($tripStopTimes[0]['service_id']) . ')</li>';
 foreach ($tripStopTimes as $key => $tripStopTime) {
-    if ($key + 1 > sizeof($tripStopTimes) || ($tripStopTimes[$key]["stop_name"] != $tripStopTimes[$key + 1]["stop_name"])) {
+    if ($key + 1 > sizeof($tripStopTimes) || stopCompare($tripStopTimes[$key]["stop_name"]) != stopCompare($tripStopTimes[$key + 1]["stop_name"])) {
         echo '<li>';
-       
+
         if (sizeof($stopsGrouped) > 0) {
             // print and empty grouped stops
             // subsequent duplicates
@@ -83,7 +82,8 @@
                 echo '<br>' . distance($tripStopTime['stop_lat'], $tripStopTime['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away';
             }
             echo '</p>';
-            echo $tripStopTime["stop_name"];
+            echo stopGroupTitle($tripStopTime['stop_name'],$tripStopTime['stop_desc']) . '<br><small>' . sizeof($stopsGrouped["stop_ids"]) . ' stops</small>';
+                    
             echo '</a></li>';
             flush();
             @ob_flush();
@@ -103,10 +103,10 @@
         }
     } else {
         // this is a duplicated line item
-        if ($key - 1 <= 0 || ($tripStopTimes[$key]['stop_name'] != $tripStopTimes[$key - 1]['stop_name'])) {
+        if ($key - 1 <= 0 || stopCompare($tripStopTimes[$key]['stop_name']) != stopCompare($tripStopTimes[$key - 1]['stop_name'])) {
             // first duplicate
             $stopsGrouped = Array(
-                "name" => $tripStopTime['stop_name'],
+                "name" => trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])),
                 "startTime" => $tripStopTime['arrival_time'],
                 "stop_ids" => Array(
                     $tripStopTime['stop_id']