Skip to content

Commit

Permalink
refactor(IntervalUploadStatus): Rename from TripHistoryUploadStatus, …
Browse files Browse the repository at this point in the history
…move persistence code.
  • Loading branch information
binh-dam-ibigroup committed Nov 14, 2024
1 parent b2190fc commit b6c8a08
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bson.conversions.Bson;
import org.opentripplanner.middleware.bugsnag.BugsnagReporter;
import org.opentripplanner.middleware.controllers.api.OtpRequestProcessor;
import org.opentripplanner.middleware.models.IntervalUpload;
import org.opentripplanner.middleware.models.TripHistoryUpload;
import org.opentripplanner.middleware.models.TripRequest;
import org.opentripplanner.middleware.models.TripSummary;
Expand All @@ -27,7 +28,6 @@
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -131,7 +131,9 @@ public static void removeUsersTripHistory(String userId) {
}
// Get all hourly windows that have already been earmarked for uploading.
Set<LocalDateTime> incompleteUploadHours = new HashSet<>();
getIncompleteUploads().forEach(tripHistoryUpload -> incompleteUploadHours.add(tripHistoryUpload.uploadHour));
IntervalUpload
.getIncompleteUploads(Persistence.tripHistoryUploads)
.forEach(tripHistoryUpload -> incompleteUploadHours.add(tripHistoryUpload.uploadHour));
// Save all new hourly windows for uploading.
Set<LocalDateTime> newHourlyWindows = Sets.difference(userTripHourlyWindows, incompleteUploadHours);
TripHistoryUpload first = TripHistoryUpload.getFirst();
Expand Down Expand Up @@ -444,16 +446,6 @@ public static boolean isAnonymizedInterval(String reportingMode) {
return reportingModes.contains("interval") && reportingModes.contains("anonymized");
}

/**
* Get all incomplete trip history uploads.
*/
private static List<TripHistoryUpload> getIncompleteUploads() {
FindIterable<TripHistoryUpload> tripHistoryUploads = Persistence.tripHistoryUploads.getFiltered(
Filters.ne("status", TripHistoryUploadStatus.COMPLETED.getValue())
);
return tripHistoryUploads.into(new ArrayList<>());
}

public static String getDailyFileName(LocalDateTime date, String fileNameSuffix) {
return formatFileName(date, "yyyy-MM-dd", fileNameSuffix);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.opentripplanner.middleware.connecteddataplatform;

import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.conversions.Bson;
import org.opentripplanner.middleware.models.IntervalUpload;
import org.opentripplanner.middleware.persistence.TypedPersistence;
import org.opentripplanner.middleware.utils.DateTimeUtils;
import org.slf4j.Logger;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

import static org.opentripplanner.middleware.connecteddataplatform.ConnectedDataManager.isReportingDaily;
Expand All @@ -24,7 +18,6 @@
public abstract class IntervalUploadJob<T extends IntervalUpload> implements Runnable {

private static final int HISTORIC_UPLOAD_HOURS_BACK_STOP = 24;
public static final String STATUS_FIELD_NAME = "status";

protected final ReportingInterval reportingInterval;
private final Logger logger;
Expand All @@ -51,7 +44,7 @@ public void run() {
}

public void runInnerLogic() {
getIncompleteUploads().forEach(this::processInterval);
IntervalUpload.getIncompleteUploads(persistence).forEach(this::processInterval);
}

/**
Expand All @@ -78,7 +71,7 @@ public void stageUploadDays() {
* up to HISTORIC_UPLOAD_HOURS_BACK_STOP hours, and add the latest upload hour/day if not already accounted for.
*/
private void stageUploadTimes(LocalDateTime previousTime, ChronoUnit chronoUnit) {
IntervalUpload lastCreated = getLastUploadCreated();
IntervalUpload lastCreated = IntervalUpload.getLastUploadCreated(persistence);
if (lastCreated == null) {
// Stage first ever upload hour/day (will use 'hour' throughout whether referring to hours or days).
createUpload(previousTime);
Expand Down Expand Up @@ -117,43 +110,4 @@ private void stageUploadTimes(LocalDateTime previousTime, ChronoUnit chronoUnit)
private static LocalDateTime getHistoricDateTimeBackStop() {
return LocalDateTime.now().minusHours(HISTORIC_UPLOAD_HOURS_BACK_STOP);
}

/**
* Get all incomplete uploads.
*/
public List<T> getIncompleteUploads() {
FindIterable<T> incompleteUploads = persistence.getFiltered(
Filters.ne(STATUS_FIELD_NAME, TripHistoryUploadStatus.COMPLETED.getValue())
);
return incompleteUploads.into(new ArrayList<>());
}

/**
* Get the last created trip history upload regardless of status.
*/
@BsonIgnore
public T getLastUploadCreated() {
return getOneOrdered(Sorts.descending("dateCreated"));
}

/**
* Get the first created trip history upload regardless of status.
*/
@BsonIgnore
public T getFirstUpload() {
return getOneOrdered(Sorts.ascending("dateCreated"));
}

/**
* Get one upload based on the sort order.
*/
private T getOneOrdered(Bson sortBy) {
return persistence.getOneFiltered(
Filters.or(
Filters.eq(STATUS_FIELD_NAME, TripHistoryUploadStatus.COMPLETED.getValue()),
Filters.eq(STATUS_FIELD_NAME, TripHistoryUploadStatus.PENDING.getValue())
),
sortBy
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.opentripplanner.middleware.connecteddataplatform;

/**
* Used to define a trip data upload status. Trip uploads will remain 'pending' until successfully uploaded, at which
* Used to define an upload status. Uploads will remain 'pending' until successfully uploaded, at which
* point the status is set to 'completed'.
*/
public enum TripHistoryUploadStatus {
public enum IntervalUploadStatus {
/**
* Once a trip data upload for a day has been completed it's status is set to completed.
*/
Expand All @@ -16,7 +16,7 @@ public enum TripHistoryUploadStatus {

private final String value;

TripHistoryUploadStatus(String value) {
IntervalUploadStatus(String value) {
this.value = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void processInterval(TripHistoryUpload upload) {
if (numRecordsToUpload != Integer.MIN_VALUE) {
// If successfully compiled and updated, update the status to 'completed' and record the number of trip
// requests uploaded (if any).
upload.status = TripHistoryUploadStatus.COMPLETED.getValue();
upload.status = IntervalUploadStatus.COMPLETED;
upload.numTripRequestsUploaded = numRecordsToUpload;
Persistence.tripHistoryUploads.replace(upload.id, upload);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package org.opentripplanner.middleware.models;

import org.opentripplanner.middleware.connecteddataplatform.TripHistoryUploadStatus;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.conversions.Bson;
import org.opentripplanner.middleware.connecteddataplatform.IntervalUploadStatus;
import org.opentripplanner.middleware.persistence.TypedPersistence;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -11,19 +19,19 @@
* uploaded.
*/
public class IntervalUpload extends Model {
// TODO: rename??
public LocalDateTime uploadHour;
public static final String STATUS_FIELD_NAME = "status";

// TODO: Can this be just the enum type?
public String status = TripHistoryUploadStatus.PENDING.getValue();
public LocalDateTime uploadHour; // Regardless of whether dealing with hours or days.

public IntervalUploadStatus status = IntervalUploadStatus.PENDING;

public IntervalUpload() {
// Empty constructor for deserialization.
}

public IntervalUpload(LocalDateTime uploadHour) {
this.uploadHour = uploadHour;
this.status = TripHistoryUploadStatus.PENDING.getValue();
this.status = IntervalUploadStatus.PENDING;
}

@Override
Expand All @@ -39,4 +47,43 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(super.hashCode(), uploadHour, status);
}

/**
* Get the last created trip history upload regardless of status.
*/
@BsonIgnore
public static <U extends IntervalUpload> U getLastUploadCreated(TypedPersistence<U> persistence) {
return getOneOrdered(persistence, Sorts.descending("dateCreated"));
}

/**
* Get the first created trip history upload regardless of status.
*/
@BsonIgnore
public static <U extends IntervalUpload> U getFirstUpload(TypedPersistence<U> persistence) {
return getOneOrdered(persistence, Sorts.ascending("dateCreated"));
}

/**
* Get one upload based on the sort order.
*/
public static <U extends IntervalUpload> U getOneOrdered(TypedPersistence<U> persistence, Bson sortBy) {
return persistence.getOneFiltered(
Filters.or(
Filters.eq(STATUS_FIELD_NAME, IntervalUploadStatus.COMPLETED.getValue()),
Filters.eq(STATUS_FIELD_NAME, IntervalUploadStatus.PENDING.getValue())
),
sortBy
);
}

/**
* Get all incomplete uploads.
*/
public static <U extends IntervalUpload> List<U> getIncompleteUploads(TypedPersistence<U> persistence) {
FindIterable<U> incompleteUploads = persistence.getFiltered(
Filters.ne(IntervalUpload.STATUS_FIELD_NAME, IntervalUploadStatus.COMPLETED.getValue())
);
return incompleteUploads.into(new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mongodb.client.model.Sorts;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.conversions.Bson;
import org.opentripplanner.middleware.connecteddataplatform.TripHistoryUploadStatus;
import org.opentripplanner.middleware.connecteddataplatform.IntervalUploadStatus;
import org.opentripplanner.middleware.persistence.Persistence;

import java.time.LocalDateTime;
Expand All @@ -26,14 +26,6 @@ public TripHistoryUpload(LocalDateTime uploadHour) {
super(uploadHour);
}

/**
* Get the last created trip history upload regardless of status.
*/
@BsonIgnore
public static TripHistoryUpload getLastCreated() {
return getOneOrdered(Sorts.descending("dateCreated"));
}

/**
* Get the first created trip history upload regardless of status.
*/
Expand All @@ -48,8 +40,8 @@ public static TripHistoryUpload getFirst() {
private static TripHistoryUpload getOneOrdered(Bson sortBy) {
return Persistence.tripHistoryUploads.getOneFiltered(
Filters.or(
Filters.eq("status", TripHistoryUploadStatus.COMPLETED.getValue()),
Filters.eq("status", TripHistoryUploadStatus.PENDING.getValue())
Filters.eq("status", IntervalUploadStatus.COMPLETED.getValue()),
Filters.eq("status", IntervalUploadStatus.PENDING.getValue())
),
sortBy
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.opentripplanner.middleware.models;

import org.opentripplanner.middleware.connecteddataplatform.TripHistoryUploadStatus;
import org.opentripplanner.middleware.connecteddataplatform.IntervalUploadStatus;

import java.time.LocalDateTime;

Expand All @@ -19,10 +19,10 @@ public TripSurveyUpload(LocalDateTime uploadHour) {
super(uploadHour);
}

public TripSurveyUpload(String id, LocalDateTime uploadHour, TripHistoryUploadStatus status) {
public TripSurveyUpload(String id, LocalDateTime uploadHour, IntervalUploadStatus status) {
super(uploadHour);
this.id = id;
this.uploadHour = uploadHour;
this.status = status.toString();
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.opentripplanner.middleware.connecteddataplatform.IntervalUploadFiles;
import org.opentripplanner.middleware.connecteddataplatform.IntervalUploadJob;
import org.opentripplanner.middleware.connecteddataplatform.ReportingInterval;
import org.opentripplanner.middleware.connecteddataplatform.TripHistoryUploadStatus;
import org.opentripplanner.middleware.connecteddataplatform.IntervalUploadStatus;
import org.opentripplanner.middleware.models.TripSurveyUpload;
import org.opentripplanner.middleware.models.typeform.Responses;
import org.opentripplanner.middleware.models.typeform.Form;
Expand Down Expand Up @@ -76,7 +76,7 @@ protected void processInterval(TripSurveyUpload upload) {

if (success) {
// If successfully compiled and updated, update the status to 'completed'.
upload.status = TripHistoryUploadStatus.COMPLETED.getValue();
upload.status = IntervalUploadStatus.COMPLETED;
Persistence.tripSurveyUploads.replace(upload.id, upload);

// Attempt to delete the responses that were downloaded above
Expand Down
Loading

0 comments on commit b6c8a08

Please sign in to comment.