Skip to content

Commit

Permalink
Added mongoDB timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Wiens committed Feb 16, 2024
1 parent 13370fa commit 888ad50
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ConflictMonitorApiProperties {
private String cmServerURL = "";
private String emailBroker = "";
private String emailFromAddress = "noreply@cimms.com";
private long mongoTimeoutMs = 5000;
private int importProcessorBufferSize = OdePlugin.INPUT_STREAM_BUFFER_SIZE;
private String hostId;
private List<Path> uploadLocations = new ArrayList<>();
Expand Down Expand Up @@ -166,6 +167,15 @@ public void setEmailFromAddress(String emailFromAddress) {
this.emailFromAddress = emailFromAddress;
}

public long getMongoTimeoutMs() {
return mongoTimeoutMs;
}

@Value("${mongoTimeoutMs}")
public void setMongoTimeoutMs(long mongoTimeoutMs) {
this.mongoTimeoutMs = mongoTimeoutMs;
}

public String getKafkaBrokers() {
return kafkaBrokers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
Expand All @@ -21,16 +24,19 @@
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.MongoException;
import com.mongodb.client.DistinctIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;


import static com.mongodb.client.model.Filters.eq;

import us.dot.its.jpo.geojsonconverter.pojos.geojson.map.ProcessedMap;
import us.dot.its.jpo.ode.api.ConflictMonitorApiProperties;
import us.dot.its.jpo.ode.api.controllers.AssessmentController;
import us.dot.its.jpo.ode.api.models.IDCount;
import us.dot.its.jpo.ode.api.models.IntersectionReferenceData;
import us.dot.its.jpo.geojsonconverter.DateJsonMapper;
Expand All @@ -47,6 +53,7 @@ public class ProcessedMapRepositoryImpl implements ProcessedMapRepository {

private String collectionName = "ProcessedMap";
private ObjectMapper mapper = DateJsonMapper.getInstance();
private Logger logger = LoggerFactory.getLogger(ProcessedMapRepositoryImpl.class);

public Query getQuery(Integer intersectionID, Long startTime, Long endTime, boolean latest) {
Query query = new Query();
Expand Down Expand Up @@ -97,30 +104,40 @@ public List<IntersectionReferenceData> getIntersectionIDs() {
DistinctIterable<Integer> docs = collection.distinct("properties.intersectionId", Integer.class);
MongoCursor<Integer> results = docs.iterator();
List<IntersectionReferenceData> referenceDataList = new ArrayList<>();

while (results.hasNext()) {
Integer intersectionId = results.next();
if (intersectionId != null){

Bson projectionFields = Projections.fields(
Projections.include("properties.intersectionId", "properties.originIp",
"properties.refPoint.latitude", "properties.refPoint.longitude"),
Projections.excludeId());
Document document = collection.find(eq("properties.intersectionId", intersectionId))
.projection(projectionFields).sort(Sorts.descending("properties.timeStamp")).first();
IntersectionReferenceData data = new IntersectionReferenceData();
Document properties = document.get("properties", Document.class);

if (properties != null) {
Document refPoint = properties.get("refPoint", Document.class);
data.setIntersectionID(intersectionId);
data.setRoadRegulatorID("-1");
data.setRsuIP(properties.getString("originIp"));
if (refPoint != null) {
data.setLatitude(refPoint.getDouble("latitude"));
data.setLongitude(refPoint.getDouble("longitude"));
try {
Document document = collection.find(eq("properties.intersectionId", intersectionId))
.projection(projectionFields).sort(Sorts.descending("properties.timeStamp")).maxTime(props.getMongoTimeoutMs(), TimeUnit.MILLISECONDS).first();

if(document != null){
IntersectionReferenceData data = new IntersectionReferenceData();
Document properties = document.get("properties", Document.class);

if (properties != null) {
Document refPoint = properties.get("refPoint", Document.class);
data.setIntersectionID(intersectionId);
data.setRoadRegulatorID("-1");
data.setRsuIP(properties.getString("originIp"));
if (refPoint != null) {
data.setLatitude(refPoint.getDouble("latitude"));
data.setLongitude(refPoint.getDouble("longitude"));
}
}
referenceDataList.add(data);
}
} catch (MongoException e){
logger.error("MongoDB Intersection Query Did not finish in allowed time window");
} catch (Exception e) {
logger.error("");
}
referenceDataList.add(data);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public Query getQuery(Integer intersectionID, Integer roadRegulatorID, String no
query.addCriteria(Criteria.where("intersectionID").is(intersectionID));
}

if(roadRegulatorID != null){
query.addCriteria(Criteria.where("roadRegulatorID").is(roadRegulatorID));
}
// if(roadRegulatorID != null){
// query.addCriteria(Criteria.where("roadRegulatorID").is(roadRegulatorID));
// }

if(notificationType != null){
query.addCriteria(Criteria.where("notificationType").is(notificationType));
Expand Down

0 comments on commit 888ad50

Please sign in to comment.