Skip to content

Commit

Permalink
Merge pull request ibi-group#192 from conveyal/normalize-stop_times
Browse files Browse the repository at this point in the history
Add normalize stop times API endpoint
  • Loading branch information
Landon Reed committed Apr 2, 2019
2 parents 566e4da + e4f089e commit 54b0c0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.conveyal.datatools.editor.controllers.api;

import com.conveyal.datatools.common.utils.S3Utils;
import com.conveyal.datatools.common.utils.SparkUtils;
import com.conveyal.datatools.editor.controllers.EditorLockController;
import com.conveyal.datatools.manager.auth.Auth0UserProfile;
import com.conveyal.datatools.manager.models.FeedSource;
Expand Down Expand Up @@ -86,6 +87,7 @@ private void registerRoutes() {
// Handle update useFrequency field. Hitting this endpoint will delete all trips for a pattern and update the
// useFrequency field.
if ("pattern".equals(classToLowercase)) {
put(ROOT_ROUTE + ID_PARAM + "/stop_times", this::updateStopTimesFromPatternStops, json::write);
delete(ROOT_ROUTE + ID_PARAM + "/trips", this::deleteTripsForPattern, json::write);
}
}
Expand All @@ -112,7 +114,7 @@ private String deleteTripsForPattern(Request req, Response res) {
logMessageAndHalt(req, 500, "Error deleting entity", e);
return null;
} finally {
LOG.info("Delete operation took {} msec", System.currentTimeMillis() - startTime);
LOG.info("Delete trips for pattern operation took {} msec", System.currentTimeMillis() - startTime);
}
}

Expand All @@ -124,8 +126,9 @@ private String deleteMultipleTrips(Request req, Response res) {
long startTime = System.currentTimeMillis();
String namespace = getNamespaceAndValidateSession(req);
String[] tripIds = req.queryParams("tripIds").split(",");
JdbcTableWriter tableWriter = null;
try {
JdbcTableWriter tableWriter = new JdbcTableWriter(table, datasource, namespace);
tableWriter = new JdbcTableWriter(table, datasource, namespace);
for (String tripId: tripIds) {
// Delete each trip ID found in query param WITHOUT auto-committing.
int result = tableWriter.delete(Integer.parseInt(tripId), false);
Expand All @@ -135,14 +138,15 @@ private String deleteMultipleTrips(Request req, Response res) {
throw new SQLException(message);
}
}
// Commit the transaction after iterating over trip IDs (because the deletes where made without autocommit).
// Commit the transaction after iterating over trip IDs (because the deletes were made without autocommit).
tableWriter.commit();
LOG.info("Deleted {} trips", tripIds.length);
} catch (InvalidNamespaceException e) {
logMessageAndHalt(req, 400, "Invalid namespace");
} catch (Exception e) {
logMessageAndHalt(req, 500, "Error deleting entity", e);
} finally {
if (tableWriter != null) tableWriter.close();
LOG.info("Delete operation took {} msec", System.currentTimeMillis() - startTime);
}
return formatJSON(String.format("Deleted %d.", tripIds.length), 200);
Expand All @@ -159,7 +163,7 @@ private String deleteOne(Request req, Response res) {
JdbcTableWriter tableWriter = new JdbcTableWriter(table, datasource, namespace);
if (tableWriter.delete(id, true) == 1) {
// FIXME: change return message based on result value
return formatJSON(String.valueOf("Deleted one."), 200);
return formatJSON("Deleted one.", 200);
}
} catch (Exception e) {
logMessageAndHalt(req, 400, "Error deleting entity", e);
Expand All @@ -169,17 +173,37 @@ private String deleteOne(Request req, Response res) {
return null;
}

/**
* For a given pattern ID, update all its trips' stop times to conform to the default travel and dwell times. This
* is used, for instance, when a new pattern stop is added or inserted into an existing pattern that has trips which
* need the updated travel times applied in bulk.
*/
private String updateStopTimesFromPatternStops (Request req, Response res) {
long startTime = System.currentTimeMillis();
String namespace = getNamespaceAndValidateSession(req);
int patternId = getIdFromRequest(req);
try {
int beginStopSequence = Integer.parseInt(req.queryParams("stopSequence"));
JdbcTableWriter tableWriter = new JdbcTableWriter(table, datasource, namespace);
int stopTimesUpdated = tableWriter.normalizeStopTimesForPattern(patternId, beginStopSequence);
return SparkUtils.formatJSON("updateResult", stopTimesUpdated + " stop times updated.");
} catch (Exception e) {
logMessageAndHalt(req, 400, "Error normalizing stop times", e);
return null;
} finally {
LOG.info("Normalize stop times operation took {} msec", System.currentTimeMillis() - startTime);
}
}

/**
* HTTP endpoint to upload branding image to S3 for either agency or route entities. The endpoint also handles
* updating the branding URL field to match the S3 URL.
*/
private String uploadEntityBranding (Request req, Response res) {
int id = getIdFromRequest(req);
String url = null;
String url;
try {
// FIXME: remove cast to string.
String idAsString = String.valueOf(id);
url = S3Utils.uploadBranding(req, String.join("_", classToLowercase, idAsString));
url = S3Utils.uploadBranding(req, String.format("%s_%d", classToLowercase, id));
} catch (HaltException e) {
// Do not re-catch halts thrown for exceptions that have already been caught.
throw e;
Expand Down Expand Up @@ -300,10 +324,4 @@ private Integer getIdFromRequest(Request req) {
}
return id;
}

// TODO add hooks
abstract void getEntityHook(T entity);
abstract void createEntityHook(T entity);
abstract void updateEntityHook(T entity);
abstract void deleteEntityHook(T entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,4 @@ public class EditorControllerImpl extends EditorController {
public EditorControllerImpl(String apiPrefix, Table table, DataSource dataSource){
super(apiPrefix, table, dataSource);
}

@Override
void getEntityHook(Entity entity) {

}

@Override
void createEntityHook(Entity entity) {

}

@Override
void updateEntityHook(Entity entity) {

}

@Override
void deleteEntityHook(Entity entity) {

}
}

0 comments on commit 54b0c0e

Please sign in to comment.