Skip to content

Commit

Permalink
changing version pre MET-213 branch
Browse files Browse the repository at this point in the history
Inspired by work done here:
TheTransitClock#255 (comment)

Needs bug fix MET-214
  • Loading branch information
sheldonabrown committed Apr 5, 2022
1 parent a5937cd commit 495c45a
Show file tree
Hide file tree
Showing 25 changed files with 417 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,12 @@ static private void populateCaches() throws Exception
if(cacheReloadStartTimeStr.getValue().length()>0&&cacheReloadEndTimeStr.getValue().length()>0)
{
Criteria criteria = session.createCriteria(ArrivalDeparture.class);
logger.info("querying from {} to {}", cacheReloadStartTimeStr, cacheReloadEndTimeStr);

List<ArrivalDeparture> results = StopArrivalDepartureCache.createArrivalDeparturesCriteriaMultiDay(criteria,
new Date(Time.parse(cacheReloadStartTimeStr.getValue()).getTime()),
new Date(Time.parse(cacheReloadEndTimeStr.getValue()).getTime()));

logger.info("query complete from {} to {}", cacheReloadStartTimeStr, cacheReloadEndTimeStr);
if(TripDataHistoryCacheFactory.getInstance()!=null)
{
logger.info("Populating TripDataHistoryCache cache for period {} to {}",cacheReloadStartTimeStr.getValue(),cacheReloadEndTimeStr.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,11 @@ public int getScheduledWaitStopTimeSecs() {
logger.error("no scheduled wait stop time for {} {}", tripIndex, stopPathIndex);
return -1;
}
if (scheduleTime.getDepartureTime() == null) {
// timepoints dont have departure times by convention
// this isn't an error
return -1;
}
return scheduleTime.getDepartureTime();
} catch (Exception e) {
logger.error("Tried to get wait stop time for a stop that didn't "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static int adjustTravelTimeForWaitStop(int timeOfDaySecs,
/**
* Returns the scheduled epoch time vehicle is scheduled leave a wait stop.
* Does not take into account whether vehicle can make it to the wait stop
* in time.
* in time. A wait stop now includes timepoint stops.
*
* @param indices
* Describes which stop
Expand Down Expand Up @@ -192,7 +192,9 @@ public static long scheduledDepartureTime(Indices indices,
Integer scheduledDepartureTimeSecs =
scheduleTime.getDepartureTime();
if (scheduledDepartureTimeSecs == null) {
logger.error("Called scheduledDepartureTimePlusWaitTime() for stop "
// timepoints will not have departure time set via convention
// this is not an error
logger.debug("Called scheduledDepartureTimePlusWaitTime() for stop "
+ "that doesn't have a scheduled departure time. {}",
indices);
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,39 @@ public String toString() {

@Override
public void run() throws Exception {
logger.error("in run for task with results=" + futureResults);
logger.info("in run for task with results=" + futureResults);
Session session = null;
List<ArrivalDeparture> results = null;
if (futureResults != null) {
// block here until we have input ready
logger.error("async retrieval of {} to {}", startDate, endDate);
logger.info("async retrieval of {} to {}", startDate, endDate);
try {
results = (List<ArrivalDeparture>) futureResults.get();
} catch (Throwable t) {
// this will catch any hydration issues in the future query
logger.error("futureResult retrieval failed with {}", t, t);
}
if (results == null) {
logger.error("async retrieval of {} to {} failed!", startDate, endDate);
logger.info("async retrieval of {} to {} failed!", startDate, endDate);
} else {
logger.error("async retrieval of {} to {} finished with {} results", startDate, endDate, results.size());
logger.info("async retrieval of {} to {} finished with {} results", startDate, endDate, results.size());
}
}
try {
if (this.futureResults == null) {
session = HibernateUtils.getSession();
Criteria criteria = session.createCriteria(ArrivalDeparture.class);
logger.error("async / future results null, performing manual retrieval now");
logger.info("async / future results null, performing manual retrieval now");
results = criteria.add(Restrictions.between("time", startDate, endDate)).list();
}

logger.info("Populating {} cache for period {} to {}", type, startDate, endDate);
switch (type) {
case TripDataHistoryCacheFactory:
if (results != null) {
logger.error("populating TripDataHistoryCache with " + results.size() + "records");
logger.info("populating TripDataHistoryCache with " + results.size() + "records");
} else {
logger.error("populating TripDataHistoryCache with NuLl records");
logger.info("populating TripDataHistoryCache with NuLl records");
}
TripDataHistoryCacheFactory.getInstance().populateCacheFromDb(results);
break;
Expand Down Expand Up @@ -117,7 +117,7 @@ public void run() throws Exception {
} catch (Throwable t) {
logger.error("Error Populating {} cache for period {} to {}, {}", type, startDate, endDate, t, t);
} finally {
logger.error("Finished Populating {} cache for period {} to {}", type, startDate, endDate);
logger.info("Finished Populating {} cache for period {} to {}", type, startDate, endDate);
if (session != null) {
// this session is in a separate thread and needs to be reclaimed
// as it counts against the connection pool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,100 @@
package org.transitclock.core.dataCache;

import org.transitclock.core.Indices;
import org.transitclock.db.structs.Trip;

/**
* @author Sean Og Crudden
* TODO This is the same as StopPathCacheKey but left seperate in case we might use block_id as well.
*/
public class KalmanErrorCacheKey implements java.io.Serializable {


private String routeId;
private String directionId;
private Integer startTimeSecondsIntoDay;
private String originStopId;
private String destinationStopId;

public String getTripId() {
return tripId;
// The vehicleId is only used for debug purposed we know in log which vehicle set the error value
private String vehicleId;

public String getRouteId() {
return routeId;
}

public void setTripId(String tripId) {
this.tripId = tripId;
public String getDirectionId() {
return directionId;
}

public void setStopPathIndex(Integer stopPathIndex) {
this.stopPathIndex = stopPathIndex;
public Integer getStartTimeSecondsIntoDay() {
return startTimeSecondsIntoDay;
}

private String tripId;
private Integer stopPathIndex;

// The vehicleId is only used for debug purposed we know in log which vehicle set the error value
private String vehiceId;


public String getVehiceId() {
return vehiceId;
public String getOriginStopId() {
return originStopId;
}

public String getDestinationStopId() {
return destinationStopId;
}

public void setVehiceId(String vehiceId) {
this.vehiceId = vehiceId;
public String getVehicleId() {
return vehicleId;
}

public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}

/**
* Needs to be serializable to add to cache
*/
private static final long serialVersionUID = 5029823633051153716L;
private static final long serialVersionUID = 5029823633051153717L;


public KalmanErrorCacheKey(Indices indices, String vehicleId) {
super();

this.tripId=indices.getBlock().getTrip(indices.getTripIndex()).getId();
this.stopPathIndex=indices.getStopPathIndex();
this.vehiceId=vehicleId;

Trip trip = indices.getBlock().getTrip(indices.getTripIndex());
this.routeId = trip.getRouteId();
this.directionId = trip.getDirectionId();
this.startTimeSecondsIntoDay = trip.getStartTime();
this.originStopId = trip.getTripPattern().getStopIds().get(0);
this.destinationStopId = trip.getLastStopId();
this.vehicleId =vehicleId;

}
public KalmanErrorCacheKey(Indices indices) {
super();

this.tripId=indices.getBlock().getTrip(indices.getTripIndex()).getId();
this.stopPathIndex=indices.getStopPathIndex();



Trip trip = indices.getBlock().getTrip(indices.getTripIndex());
this.routeId = trip.getRouteId();
this.directionId = trip.getDirectionId();
this.startTimeSecondsIntoDay = trip.getStartTime();
this.originStopId = trip.getTripPattern().getStopIds().get(0);
this.destinationStopId = trip.getLastStopId();

}
@Override
public String toString() {
return "KalmanErrorCacheKey [tripId=" + tripId + ", stopPathIndex=" + stopPathIndex + "]";
return "KalmanErrorCacheKey [routeId=" + routeId
+ ", directionId=" + directionId
+ ", startTime=" + startTimeSecondsIntoDay
+ ", originStopId=" + originStopId
+ ", destinationStopId=" + destinationStopId
+ "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((stopPathIndex == null) ? 0 : stopPathIndex.hashCode());
result = prime * result + ((tripId == null) ? 0 : tripId.hashCode());
result = prime * result + ((routeId == null) ? 0 : routeId.hashCode());
result = prime * result + ((directionId == null) ? 0 : directionId.hashCode());
result = prime * result + ((originStopId == null) ? 0 : originStopId.hashCode());
result = prime * result + ((destinationStopId == null) ? 0 : destinationStopId.hashCode());
result = prime * result + ((startTimeSecondsIntoDay == null) ? 0 : startTimeSecondsIntoDay.hashCode());
return result;
}

Expand All @@ -81,41 +107,48 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
KalmanErrorCacheKey other = (KalmanErrorCacheKey) obj;
if (stopPathIndex == null) {
if (other.stopPathIndex != null)
if (routeId == null) {
if (other.routeId != null)
return false;
} else if (!routeId.equals(other.routeId))
return false;
if (directionId == null) {
if (other.directionId != null)
return false;
} else if (!directionId.equals(other.directionId))
return false;
if (startTimeSecondsIntoDay == null) {
if (other.startTimeSecondsIntoDay != null)
return false;
} else if (!startTimeSecondsIntoDay.equals(other.startTimeSecondsIntoDay))
return false;
if (originStopId == null) {
if (other.originStopId != null)
return false;
} else if (!stopPathIndex.equals(other.stopPathIndex))
} else if (!originStopId.equals(other.originStopId))
return false;
if (tripId == null) {
if (other.tripId != null)
if (destinationStopId == null) {
if (other.destinationStopId != null)
return false;
} else if (!tripId.equals(other.tripId))
} else if (!destinationStopId.equals(other.destinationStopId))
return false;

return true;
}

public KalmanErrorCacheKey(String tripId, Integer stopPathIndex) {
public KalmanErrorCacheKey(String routeId, String directionId,
Integer startTimeSecondsIntoDay, String originStopId,
String destinationStopId) {
super();

this.tripId = tripId;
this.stopPathIndex = stopPathIndex;
this.routeId = routeId;
this.directionId = directionId;
this.startTimeSecondsIntoDay = startTimeSecondsIntoDay;
this.originStopId = originStopId;
this.destinationStopId = destinationStopId;
}


/**
* @return the stopPathIndex
*/
public int getStopPathIndex() {
return stopPathIndex;
}

/**
* @param stopPathIndex the stopPathIndex to set
*/
public void setStopPathIndex(int stopPathIndex) {
this.stopPathIndex = stopPathIndex;
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.transitclock.core.dataCache;

import java.util.Objects;

/**
* StopPatch caching based in TripInstance (Route/Direction/time) instead of tripId.
*/
public class StopPathKey extends TripKey implements java.io.Serializable {

private static long serialVersionUID = 1L;

private String originStopId;
private String destinationStopId;
private boolean travelTime;

public StopPathKey(String routeId,
String directionId,
Integer startTimeSecondsIntoDay,
Long tripStartTime,
String originStopId,
String destinationStopId,
boolean travelTime) {
super(routeId, directionId, tripStartTime, startTimeSecondsIntoDay);
this.originStopId = originStopId;
this.destinationStopId = destinationStopId;
this.travelTime = travelTime;
}

public String getOriginStopId() {
return originStopId;
}

public String getDestinationStopId() {
return destinationStopId;
}

public boolean isTravelTime() {
return travelTime;
}

@Override
public int hashCode() {
return Objects.hash(getRouteId(), getDirectionId(), getStartTimeSecondsIntoDay(),
getTripStartTime(), originStopId, destinationStopId, travelTime);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return true;
StopPathKey that = (StopPathKey) o;
return Objects.equals(getRouteId(), that.getRouteId())
&& Objects.equals(getDirectionId(), that.getDirectionId())
&& Objects.equals(getStartTimeSecondsIntoDay(), that.getStartTimeSecondsIntoDay())
&& Objects.equals(getTripStartTime(), that.getTripStartTime())
&& Objects.equals(originStopId, that.originStopId)
&& Objects.equals(destinationStopId, that.destinationStopId)
&& Objects.equals(travelTime, that.travelTime);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.transitclock.core.dataCache;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.transitclock.db.structs.ArrivalDeparture;
import org.transitclock.ipc.data.IpcArrivalDeparture;

Expand Down
Loading

0 comments on commit 495c45a

Please sign in to comment.