Skip to content

Commit

Permalink
time zone updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kpwebb committed Jun 30, 2015
1 parent d31a176 commit 72952b5
Show file tree
Hide file tree
Showing 12 changed files with 3,907 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<dependency>
<groupId>com.conveyal</groupId>
<artifactId>traffic-engine</artifactId>
<version>0.1</version>
<version>0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mapdb</groupId>
Expand Down
84 changes: 68 additions & 16 deletions src/main/java/com/conveyal/traffic/app/TrafficEngineApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.conveyal.traffic.app.data.WeekObject;
import com.conveyal.traffic.app.data.WeeklyStatsObject;
import com.conveyal.traffic.app.engine.Engine;
import com.conveyal.traffic.data.SpatialDataItem;
import com.conveyal.traffic.stats.SegmentStatistics;
import com.vividsolutions.jts.geom.Envelope;
import org.opentripplanner.api.model.TripPlan;
import org.opentripplanner.common.model.GenericLocation;
import org.opentripplanner.routing.core.RoutingRequest;
Expand All @@ -23,7 +30,7 @@

import com.conveyal.traffic.data.ExchangeFormat;
import com.conveyal.traffic.geom.GPSPoint;
import com.conveyal.traffic.app.controllers.StatsObject;
import com.conveyal.traffic.app.data.StatsObject;
import com.conveyal.traffic.app.routing.Routing;
import com.conveyal.traffic.app.tiles.TrafficTileRequest.DataTile;
import com.conveyal.traffic.app.tiles.TrafficTileRequest.SegmentTile;
Expand Down Expand Up @@ -54,20 +61,66 @@ public static void main(String[] args) {
engine = new Engine();

get("/stats", (request, response) -> new StatsObject(), mapper::writeValueAsString);

get("/weeks", (request, response) -> {

List<Long> weeks = engine.getTrafficEngine().getWeekList();
List<WeekObject> weekObjects = new ArrayList();
for(Long week : weeks) {
WeekObject weekObj = new WeekObject();
weekObj.weekId = week;
weekObj.weekStartTime = SegmentStatistics.getTimeForWeek(week);
weekObjects.add(weekObj);
}
return weekObjects;
}, mapper::writeValueAsString);

get("/weeklyStats", (request, response) -> {

response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Request-Method", "*");
response.header("Access-Control-Allow-Headers", "*");

double x1 = request.queryMap("x1").doubleValue();
double x2 = request.queryMap("x2").doubleValue();
double y1 = request.queryMap("y1").doubleValue();
double y2 = request.queryMap("y2").doubleValue();

Integer week = request.queryMap("week").integerValue();

Envelope env1 = new Envelope(x1, x2, y1, y2);

SegmentStatistics segmentStatistics = new SegmentStatistics();

for (SpatialDataItem segment : TrafficEngineApp.engine.getStreetSegments(env1)) {
SegmentStatistics stats = TrafficEngineApp.engine.getTrafficEngine().getSegmentStatistics(segment.id);
if (stats != null) {
segmentStatistics.avgStats(stats);
}
}

return new WeeklyStatsObject(segmentStatistics);
}, mapper::writeValueAsString);

post("/locationUpdate", (request, response) -> {
ExchangeFormat.VehicleMessage vm = ExchangeFormat.VehicleMessage.parseFrom(request.bodyAsBytes());

long vehicleId = getUniqueIdFromString(vm.getSourceId() + "_" + vm.getVehicleId());

for(ExchangeFormat.VehicleLocation location : vm.getLocationsList()) {

GPSPoint gpsPoint = new GPSPoint(location.getTimestamp(), vehicleId, location.getLon(), location.getLat());

TrafficEngineApp.engine.locationUpdate(gpsPoint);
}

return response;

ExchangeFormat.VehicleMessageEnvelope vmEnvelope = ExchangeFormat.VehicleMessageEnvelope.parseFrom(request.bodyAsBytes());

long sourceId = vmEnvelope.getSourceId();

for(ExchangeFormat.VehicleMessage vm : vmEnvelope.getMessagesList()) {

long vehicleId = getUniqueIdFromString(sourceId + "_" + vm.getVehicleId());

for (ExchangeFormat.VehicleLocation location : vm.getLocationsList()) {

GPSPoint gpsPoint = new GPSPoint(location.getTimestamp(), vehicleId, location.getLon(), location.getLat());

if(gpsPoint.lat != 0.0 && gpsPoint.lon != 0.0)
TrafficEngineApp.engine.locationUpdate(gpsPoint);
}
}
return response;
});

// routing requests
Expand All @@ -81,7 +134,7 @@ public static void main(String[] args) {
double toLat = request.queryMap("toLat").doubleValue();
double toLon = request.queryMap("toLon").doubleValue();
int day = request.queryMap("day").integerValue();
int time = request.queryMap("time").integerValue();
int time = request.queryMap("time").integerValue();
boolean useTraffic = request.queryMap("useTraffic").booleanValue();

routing.buildIfUnbuilt();
Expand All @@ -104,8 +157,6 @@ public static void main(String[] args) {
return mapper.writeValueAsString(tp);
});

// tile requests

get("/tile/data", (request, response) -> {

int x = request.queryMap("x").integerValue();
Expand Down Expand Up @@ -170,4 +221,5 @@ public static void loadSettings() {
e.printStackTrace();
}
}

}

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/com/conveyal/traffic/app/data/StatsObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.conveyal.traffic.app.data;

import com.conveyal.traffic.app.TrafficEngineApp;

public class StatsObject{
public Double locationsPerSecond;
public Double samplesPerSecond;
public Long locationsQueued;
public Long locationsProcessed;
public Long samplesProcessed;
public Integer vehicleCount;
public Long lastUpdate;

public StatsObject() {
locationsPerSecond = TrafficEngineApp.engine.getLocationProcessingRate();
samplesPerSecond = TrafficEngineApp.engine.getSampleProcessingRate();
locationsProcessed = TrafficEngineApp.engine.getTotalLocationsProcessed();
samplesProcessed = TrafficEngineApp.engine.getTotalSamplesProcessed();
locationsQueued = TrafficEngineApp.engine.getQueueSize();
vehicleCount = TrafficEngineApp.engine.getVehicleCount();
lastUpdate = TrafficEngineApp.engine.getLastUpdate();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/conveyal/traffic/app/data/WeekObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.conveyal.traffic.app.data;

public class WeekObject {

public long weekStartTime;
public long weekId;

}
48 changes: 48 additions & 0 deletions src/main/java/com/conveyal/traffic/app/data/WeeklyStatsObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.conveyal.traffic.app.data;

import com.conveyal.traffic.stats.SegmentStatistics;

import java.util.Arrays;

public class WeeklyStatsObject {

public static double MS_TO_KMH = 3.6d;

public Double[] dailyStats = new Double[7];
public Double[] hourlyStats = new Double[7 * 24];

public WeeklyStatsObject(SegmentStatistics stats) {

Arrays.fill(dailyStats, 0.0);
Arrays.fill(hourlyStats, 0.0);

long dailyCount = 0l;
double dailySum = 0.0;

int day = 0;
for(int hour = 0; hour < (24 * 7); hour++) {

if(stats.hourSampleCount[hour] > 0) {
hourlyStats[hour] = (stats.hourSampleSum[hour] / stats.hourSampleCount[hour]) * MS_TO_KMH;

dailyCount += stats.hourSampleCount[hour];
dailySum += stats.hourSampleSum[hour];
}

if(day != ((hour - (hour % 24)) / 24)) {

if(dailyCount > 0)
dailyStats[day] = (dailySum / dailyCount) * MS_TO_KMH;

dailySum = 0.0;
dailyCount = 0l;

day = ((hour - (hour % 24)) / 24);
}
}

if(dailyCount > 0)
dailyStats[day] = dailySum / dailyCount * MS_TO_KMH;

}
}
67 changes: 61 additions & 6 deletions src/main/java/com/conveyal/traffic/app/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ public class Engine {
private ArrayList<Long> workerIdList = new ArrayList<Long>();
private int nextWorkerIndex = 0;

private long lastUpdate;
private long lastUpdate;

private long lastStatsCheck;

private long totalSamplesProcessedLastStatsCheck;
private long totalUpdatesProcessedLastStatsCheck;

private double lastLocationProcessingRate;
private double lastSampleProcessingRate;

public Engine() {

Expand Down Expand Up @@ -104,17 +112,33 @@ public void collectStatistics() {
public void locationUpdate(GPSPoint gpsPoint) {
if(!vehicleWorkerMap.containsKey(gpsPoint.vehicleId))
vehicleWorkerMap.put(gpsPoint.vehicleId, getNextWorkerId());

te.checkOsm(gpsPoint.lat, gpsPoint.lon);

GeometryFactory gf = new GeometryFactory();

lastUpdate = gpsPoint.time;

locationMap.put(gpsPoint.vehicleId, gf.createPoint(new Coordinate(gpsPoint.lon, gpsPoint.lat)));

workerMap.get(vehicleWorkerMap.get(gpsPoint.vehicleId)).enqueueLocationUpdate(gpsPoint);



if(lastStatsCheck > 0) {
long currentTime = System.currentTimeMillis();
long delta = currentTime - lastStatsCheck;

if(delta > 5000) {
long totalLocationsProcessed = getTotalLocationsProcessed();
long totalSamplesProcessed = getTotalSamplesProcessed();

lastSampleProcessingRate = (double)(totalSamplesProcessed - totalSamplesProcessedLastStatsCheck) / ((double)delta / 1000.0);
lastLocationProcessingRate = (double)(totalLocationsProcessed - totalUpdatesProcessedLastStatsCheck) / ((double)delta / 1000.0);

totalSamplesProcessedLastStatsCheck = totalSamplesProcessed;
totalUpdatesProcessedLastStatsCheck = totalLocationsProcessed;
lastStatsCheck = currentTime;
}
}
else
lastStatsCheck = System.currentTimeMillis();
}

public List<SpatialDataItem> getStreetSegments(Envelope env) {
Expand All @@ -128,7 +152,38 @@ public long getLastUpdate() {
public int getVehicleCount() {
return te.getVehicleCount();
}


public long getQueueSize() {
Long queuedUpdates = 0l;
for(EngineWorker worker : workerMap.values()) {
queuedUpdates += worker.getQueueSize();
}
return queuedUpdates;
}

public long getTotalLocationsProcessed() {
Long totalProcessed = 0l;
for(EngineWorker worker : workerMap.values()) {
totalProcessed += worker.getTotalProcessed();
}
return totalProcessed;
}

public long getTotalSamplesProcessed() {
Long totalSamples = 0l;
for(EngineWorker worker : workerMap.values()) {
totalSamples += worker.getTotalSamples();
}
return totalSamples;
}

public double getLocationProcessingRate() {
return this.lastLocationProcessingRate;
}
public double getSampleProcessingRate() {
return this.lastSampleProcessingRate;
}

public Collection<Point> getCurrentVehicleLocations() {
return locationMap.values();
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/conveyal/traffic/app/engine/EngineWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class EngineWorker implements Runnable {
private static final Logger log = Logger.getLogger( EngineWorker.class.getName());

private ConcurrentLinkedQueue<GPSPoint> locationQueue = new ConcurrentLinkedQueue<GPSPoint>();

private Long totalProcessed = 0l;
private Long totalSamples = 0l;

private final Long id;

Expand All @@ -31,7 +34,19 @@ public Long getId() {
public void enqueueLocationUpdate(GPSPoint gpsPoint) {
locationQueue.add(gpsPoint);
}


public long getQueueSize() {
return locationQueue.size();
}

public long getTotalProcessed() {
return totalProcessed;
}

public long getTotalSamples() {
return totalSamples;
}

@Override
public void run() {

Expand All @@ -42,7 +57,8 @@ public void run() {

if(gpsPoint != null) {
try {
int sampleCount = engine.getTrafficEngine().update(gpsPoint);
totalSamples += engine.getTrafficEngine().update(gpsPoint);
totalProcessed++;
} catch (Exception e) {
log.log(Level.WARNING, e.getMessage());
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<html>
<head>
<title>Traffic Engine</title>
<!-- Latest compiled and minified CSS -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>

<script src="javascripts/underscore.js"></script>
Expand All @@ -17,6 +19,7 @@
<script src="javascripts/backbone.wreqr.min.js"></script>
<script src="javascripts/backbone.marionette.min.js"></script>
<script src="javascripts/Polyline.encoded.js"></script>
<script src="javascripts/Chart.js"></script>

<script src="javascripts/handlebars.js"></script>

Expand Down
Loading

0 comments on commit 72952b5

Please sign in to comment.