From: maxious Date: Thu, 18 Aug 2011 06:07:33 +0000 Subject: New db import method X-Git-Url: https://maxious.lambdacomplex.org/git/?p=busui.git&a=commitdiff&h=0128893b41cf4e40748565022bd9f36d83f08ac1 --- New db import method --- --- /dev/null +++ b/.box @@ -1,1 +1,5 @@ +shared_writable_dirs: + - /labs/tiles + - /lib/staticmaplite/cache +php_extensions: [pgsql, pdo, pdo_pgsql, curl] --- a/include/common-template.inc.php +++ b/include/common-template.inc.php @@ -34,6 +34,8 @@ ' . $pageTitle . ' + + '; if (isDebugServer()) { $jqmcss = $labsPath . 'css/jquery.mobile-1.0b1.css'; @@ -41,9 +43,9 @@ $jqmjs = $labsPath . 'js/jquery.mobile-1.0b1.js'; } else { - $jqmcss = "http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css"; - $jqjs = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"; - $jqmjs = "http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"; + $jqmcss = "//code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css"; + $jqjs = "//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"; + $jqmjs = "//code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"; } echo ' --- a/include/common.inc.php +++ b/include/common.inc.php @@ -31,7 +31,8 @@ function isDebugServer() { - return !isset($_SERVER['SERVER_NAME']) || $_SERVER['SERVER_NAME'] == "10.0.1.154" || $_SERVER['SERVER_NAME'] == "10.1.0.4" || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1" ; + return php_sapi_name() == "cli" || isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "10.0.1.154" || $_SERVER['SERVER_NAME'] == "10.1.0.4" || $_SERVER['SERVER_NAME'] == +"localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1") ; } include_once ("common-geo.inc.php"); --- a/include/db/trip-dao.inc.php +++ b/include/db/trip-dao.inc.php @@ -173,6 +173,22 @@ $r = $query->fetch(PDO::FETCH_ASSOC); return $r['arrival_time']; } +function getTripEndTime($tripID) +{ + global $conn; + $query = "SELECT trip_id,max(arrival_time) as arrival_time from stop_times + WHERE stop_times.arrival_time IS NOT NULL and trip_id = :tripID group by trip_id"; + debug($query, "database"); + $query = $conn->prepare($query); + $query->bindParam(":tripID", $tripID); + $query->execute(); + if (!$query) { + databaseError($conn->errorInfo()); + return Array(); + } + $r = $query->fetch(PDO::FETCH_ASSOC); + return $r['arrival_time']; +} function getActiveTrips($time) { global $conn; --- a/labs/myway_timeliness_calculate.php +++ b/labs/myway_timeliness_calculate.php @@ -1,6 +1,8 @@ "; var_dump($stopRoutes); + flush(); + } } // lowest delta is recorded delta @@ -119,7 +125,7 @@ $stmt->bindParam(':route_full_name', $route_full_name); $stmt->bindParam(':stop_code', $stop_code); $stmt->bindParam(':timing_delta', $lowestDelta); - $stmt->bindParam(':time', $time); + $stmt->bindParam(':time', $time_tz); $stmt->bindParam(':date', $date); $stmt->bindParam(':timing_period', $timing_period); $stmt->bindParam(':stop_sequence', $stop_sequence); @@ -129,6 +135,7 @@ echo "Recorded.
"; } var_dump($conn->errorInfo()); + flush(); } flush(); } --- a/labs/myway_timeliness_freqdist.php +++ b/labs/myway_timeliness_freqdist.php @@ -38,41 +38,8 @@ grid: { hoverable: true, clickable: true, labelMargin: 17 }, }); - /* var o; - o = plot.pointOffset({ x: midnight+ (9*60*60*1000), y: -1.2}); - placeholder.append('
9am
'); - o = plot.pointOffset({ x: midnight+ (16*60*60*1000), y: -1.2}); - placeholder.append('
4pm
'); - */ + }); - /* - var previousPoint = null; - $("#placeholder").bind("plothover", function (event, pos, item) { - $("#x").text(pos.x.toFixed(2)); - $("#y").text(pos.y.toFixed(2)); - if (item) { - if (previousPoint != item.dataIndex) { - previousPoint = item.dataIndex; - - $("#tooltip").remove(); - var x = item.datapoint[0].toFixed(2), - y = item.datapoint[1].toFixed(2); - - var d = new Date(); -d.setTime(x); -var time = d.getUTCHours() +':'+ (d.getUTCMinutes().toString().length == 1 ? '0'+ d.getMinutes(): d.getUTCMinutes()) - - - showTooltip(item.pageX, item.pageY, - item.series.label + " at "+ time +" = " + Math.abs(new Number(y/60).toFixed(2))+" minutes "+(y >0 ? "early":"late")); - } - } - else { - $("#tooltip").remove(); - previousPoint = null; - } - }); - */ --- a/labs/myway_timeliness_overview.php +++ b/labs/myway_timeliness_overview.php @@ -18,6 +18,20 @@ }; ?> + + Hour of Day +prepare($query); +$query->execute(); +if (!$query) { + databaseError($conn->errorInfo()); + return Array(); +} +foreach ($query->fetchAll() as $row) { + echo "{$row[0]}" . floor($row[1]) . "" . floor($row[2]) . "{$row[3]}"; +}; +?> Day of Week + --- a/labs/myway_timeliness_stop.json.php +++ b/labs/myway_timeliness_stop.json.php @@ -29,3 +29,4 @@ else echo "[".implode(",",$points)."]"; ?> } + --- a/labs/myway_timeliness_stop.php +++ b/labs/myway_timeliness_stop.php @@ -40,6 +40,10 @@ tickFormatter: yformatter }, grid: { hoverable: true, clickable: true, labelMargin: 32 }, + series: { + lines: { show: false }, + points: { show: true } + } }; var plot = $.plot(placeholder, data, options); @@ -129,3 +133,4 @@ }); + --- /dev/null +++ b/labs/travelAllRoutes.php @@ -1,1 +1,23 @@ +prepare($query); + $query->execute(); +echo ""; +$total = 0; +$count = 0; +foreach($query->fetchAll() as $r) { + $trips = getRouteTrips($r['route_id']); + $startTime = $trips[0]['arrival_time']; + $endTime = getTripEndTime($trips[0]['trip_id']); + $timeDiff = strtotime($endTime) - strtotime($startTime); + $total += $timeDiff; + $count ++; + echo ""; +} +echo "
Route NumberFirst Trip StartFirst Trip EndLength
{$r['route_short_name']}$startTime$endTime$timeDiff seconds ie. ". ($timeDiff/60). " minutes
"; +echo "Total time: $total seconds ie. " .($total/60/60). " hours
"; +echo "$count Routes"; +?> --- a/updatedb.php +++ b/updatedb.php @@ -2,6 +2,9 @@ if ( php_sapi_name() == "cli") { include ('include/common.inc.php'); $conn = pg_connect("dbname=transitdata user=postgres password=snmc host=localhost") or die('connection failed'); +$pdconn = new PDO("pgsql:dbname=transitdata;user=postgres;password=snmc;host=localhost"); + + // Unzip cbrfeed.zip, import all csv files to database $unzip = true; $zip = zip_open(dirname(__FILE__) . "/cbrfeed.zip"); @@ -31,8 +34,18 @@ echo "Opening $file \n"; $line = 0; $handle = fopen($tmpdir . $file, "r"); + if ($tablename =="stop_times") { + $stmt = $pdconn->prepare("insert into stop_times (trip_id,stop_id,stop_sequence) values(:trip_id, :stop_id, :stop_sequence);"); + $stmt->bindParam(':trip_id',$trip_id); + $stmt->bindParam(':stop_id',$stop_id); + $stmt->bindParam(':stop_sequence',$stop_sequence); + } + + while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { - if ($line > 0) { + if ($line == 0) { + + } else { $query = "insert into $tablename values("; $valueCount = 0; foreach ($data as $value) { @@ -44,14 +57,22 @@ } else { $query.= "');"; } - if ($tablename =="stop_times" && $data[1] == "") { - $query = "insert into $tablename (trip_id,stop_id,stop_sequence) values('{$data[0]}','{$data[3]}','{$data[4]}');"; - } + if ($tablename =="stop_times" && $data[1] == "") { + // $query = "insert into $tablename (trip_id,stop_id,stop_sequence) values('{$data[0]}','{$data[3]}','{$data[4]}');"; + $trip_id=$data[0]; + $stop_id=$data[3]; + $stop_sequence=$data[4]; + } } - $result = pg_query($conn, $query); + if ($tablename =="stop_times") { + $stmt->execute(); + } + else { + $result = pg_query($conn, $query); + } $line++; - if ($line % 10000 == 0) echo "$line records... \n"; + if ($line % 10000 == 0) echo "$line records... ".date('c')."\n"; } fclose($handle); echo "Found a total of $line records in $file.\n";