From: Maxious Date: Fri, 23 Mar 2012 08:28:19 +0000 Subject: Beginnings of ETA calc based on actual data X-Git-Url: https://maxious.lambdacomplex.org/git/?p=busui.git&a=commitdiff&h=b895ed0fe961eb254ee2b2f21b2f6e2666024455 --- 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/include/db/servicealert-dao.inc.php +++ b/include/db/servicealert-dao.inc.php @@ -106,7 +106,19 @@ function getFutureAlerts() { global $conn; - $query = "SELECT id,extract('epoch' from start) as start, extract('epoch' from \"end\") as \"end\",cause,effect,header,description,url from servicealerts_alerts where NOW() > start or NOW() < \"end\""; + $query = "SELECT id,extract('epoch' from start) as start, extract('epoch' from \"end\") as \"end\",cause,effect,header,description,url from servicealerts_alerts where NOW() < \"end\""; + // debug($query, "database"); + $query = $conn->prepare($query); + $query->execute(); + if (!$query) { + databaseError($conn->errorInfo()); + return Array(); + } + return $query->fetchAll(); +} +function getAllAlerts() { + global $conn; + $query = "SELECT id,extract('epoch' from start) as start, extract('epoch' from \"end\") as \"end\",cause,effect,header,description,url from servicealerts_alerts"; // debug($query, "database"); $query = $conn->prepare($query); $query->execute(); --- 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) { --- a/rtpis/servicealert_editor.php +++ b/rtpis/servicealert_editor.php @@ -82,7 +82,7 @@ '; + echo "'; } ?>
" . date("c", $alert['start']) . "" . date("c", $alert['end']) . "" . substr($alert['description'], 0, 999) . 'edit
" . date("c", $alert['start']) . "
to
" . date("c", $alert['end']) . "
" .$alert['header']."
". $alert['description'] . '
edit
--- a/stop.php +++ b/stop.php @@ -141,6 +141,30 @@ if (sizeof($trips) == 0) { echo "
  • No trips in the near future.
  • "; } 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 '

    ' . $trip['route_short_name'] . " towards " . $destination['stop_name'] . "

    "; $viaPoints = viaPointNames($trip['trip_id'], $trip['stop_sequence']); +if ($labs) { + echo '
    ETA: ' . $tripETA[$trip['trip_id']] . ''; + } if ($viaPoints != "") echo '
    Via: ' . $viaPoints . ''; if (sizeof($tripStopNumbers) > 0) {