Skip to content

Commit

Permalink
Adding Bsm Event Count by Minute
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Wiens committed Mar 18, 2024
1 parent db6c03f commit 3166c80
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public Query getQuery(Integer intersectionID, Long startTime, Long endTime, bool
endTimeDate = new Date(endTime);
}

query.addCriteria(Criteria.where("eventGeneratedAt").gte(startTimeDate).lte(endTimeDate));
query.addCriteria(Criteria.where("recordGeneratedAt").gte(startTimeDate).lte(endTimeDate));
if (latest) {
query.with(Sort.by(Sort.Direction.DESC, "eventGeneratedAt"));
query.with(Sort.by(Sort.Direction.DESC, "recordGeneratedAt"));
query.limit(1);
}else{
query.limit(props.getMaximumResponseSize());
Expand Down Expand Up @@ -82,10 +82,10 @@ public List<IDCount> getBsmEventsByDay(int intersectionID, Long startTime, Long

Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("intersectionID").is(intersectionID)),
Aggregation.match(Criteria.where("timestamp").gte(startTime).lte(endTime)),
Aggregation.project("timestamp"),
Aggregation.match(Criteria.where("").gte(startTime).lte(endTime)),
Aggregation.project("startingBsmTimestamp"),
Aggregation.project()
.and(ConvertOperators.ToDate.toDate("$timestamp")).as("date"),
.and(ConvertOperators.ToDate.toDate("$startingBsmTimestamp")).as("date"),
Aggregation.project()
.and(DateOperators.DateToString.dateOf("date").toString("%Y-%m-%d")).as("dateStr"),
Aggregation.group("dateStr").count().as("count")
Expand All @@ -96,5 +96,4 @@ public List<IDCount> getBsmEventsByDay(int intersectionID, Long startTime, Long

return results;
}

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

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

if (startTime == null) {
startTime = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,6 +23,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import us.dot.its.jpo.conflictmonitor.monitor.models.bsm.BsmEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.ConnectionOfTravelEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.IntersectionReferenceAlignmentEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.LaneDirectionOfTravelEvent;
Expand All @@ -30,6 +37,7 @@
import us.dot.its.jpo.conflictmonitor.monitor.models.events.minimum_data.MapMinimumDataEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.minimum_data.SpatMinimumDataEvent;
import us.dot.its.jpo.ode.api.ConflictMonitorApiProperties;
import us.dot.its.jpo.ode.api.accessors.events.BsmEvent.BsmEventRepository;
import us.dot.its.jpo.ode.api.accessors.events.ConnectionOfTravelEvent.ConnectionOfTravelEventRepository;
import us.dot.its.jpo.ode.api.accessors.events.IntersectionReferenceAlignmentEvent.IntersectionReferenceAlignmentEventRepository;
import us.dot.its.jpo.ode.api.accessors.events.LaneDirectionOfTravelEvent.LaneDirectionOfTravelEventRepository;
Expand All @@ -43,8 +51,11 @@
import us.dot.its.jpo.ode.api.accessors.events.SpatMinimumDataEvent.SpatMinimumDataEventRepository;
import us.dot.its.jpo.ode.api.accessors.events.TimeChangeDetailsEvent.TimeChangeDetailsEventRepository;
import us.dot.its.jpo.ode.api.models.IDCount;
import us.dot.its.jpo.ode.api.models.MinuteCount;
import us.dot.its.jpo.ode.mockdata.MockEventGenerator;
import us.dot.its.jpo.ode.mockdata.MockIDCountGenerator;
import us.dot.its.jpo.ode.model.OdeBsmPayload;
import us.dot.its.jpo.ode.plugin.j2735.J2735Bsm;

@RestController
public class EventController {
Expand Down Expand Up @@ -85,6 +96,9 @@ public class EventController {
@Autowired
MapBroadcastRateEventRepository mapBroadcastRateEventRepo;

@Autowired
BsmEventRepository bsmEventRepo;



@Autowired
Expand Down Expand Up @@ -476,6 +490,92 @@ public ResponseEntity<List<SpatBroadcastRateEvent>> findSpatBroadcastRateEvents(
}
}

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/events/bsm_events", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
public ResponseEntity<List<BsmEvent>> findBsmEvents(
@RequestParam(name = "intersection_id", required = false) Integer intersectionID,
@RequestParam(name = "start_time_utc_millis", required = false) Long startTime,
@RequestParam(name = "end_time_utc_millis", required = false) Long endTime,
@RequestParam(name = "latest", required = false, defaultValue = "false") boolean latest,
@RequestParam(name = "test", required = false, defaultValue = "false") boolean testData) {

if (testData) {
List<BsmEvent> list = new ArrayList<>();
list.add(MockEventGenerator.getBsmEvent());
return ResponseEntity.ok(list);
} else {
Query query = bsmEventRepo.getQuery(intersectionID, startTime, endTime, latest);
long count = bsmEventRepo.getQueryResultCount(query);
logger.info("Returning Bsm Event Repo Response with Size: " + count);
System.out.println("Spat Broadcast Data Event");
return ResponseEntity.ok(bsmEventRepo.find(query));
}
}

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/events/bsm_events_by_minute", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
public ResponseEntity<List<MinuteCount>> getBsmActivityByMinuteInRange(
@RequestParam(name = "intersection_id", required = false) Integer intersectionID,
@RequestParam(name = "start_time_utc_millis", required = false) Long startTime,
@RequestParam(name = "end_time_utc_millis", required = false) Long endTime,
@RequestParam(name = "latest", required = false, defaultValue = "false") boolean latest,
@RequestParam(name = "test", required = false, defaultValue = "false") boolean testData) {

if (testData) {
List<MinuteCount> list = new ArrayList<>();
Random rand = new Random();
for(int i=0; i< 10; i++){
int offset = rand.nextInt((int)(endTime - startTime));

MinuteCount count = new MinuteCount();
count.setMinute(Math.round((startTime + offset) / 60000) * 60000);
count.setCount(rand.nextInt(10) + 1);
list.add(count);
}

return ResponseEntity.ok(list);
} else {
Query query = bsmEventRepo.getQuery(intersectionID, startTime, endTime, latest);

List<BsmEvent> events = bsmEventRepo.find(query);

Map<Long, Set<String>> bsmEventMap = new HashMap<>();

System.out.println("Test" + events.size());

for(BsmEvent event: events){
Long eventStartMinute = event.getStartingBsmTimestamp() / (60 * 1000);
Long eventEndMinute = event.getEndingBsmTimestamp()/ (60 * 1000);

if(eventStartMinute != null && eventEndMinute != null){
for (Long i = eventStartMinute; i< eventEndMinute; i++){
String bsmID = ((J2735Bsm)event.getStartingBsm().getPayload().getData()).getCoreData().getId();
if(bsmEventMap.get(i) != null){
bsmEventMap.get(i).add(bsmID);
}else{
Set<String> newSet = new HashSet<>();
newSet.add(bsmID);

bsmEventMap.put(i, newSet);
}
}
}
}

List<MinuteCount> outputEvents = new ArrayList<>();
for(Long key: bsmEventMap.keySet()){
MinuteCount count = new MinuteCount();
count.setMinute(key * 60000);
count.setCount(bsmEventMap.get(key).size());
outputEvents.add(count);
}

return ResponseEntity.ok(outputEvents);
}
}




Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package us.dot.its.jpo.ode.api.models;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.EqualsAndHashCode;

@ToString
@Setter
@EqualsAndHashCode
@Getter
public class MinuteCount {
public long minute; //utc millisecond indicating the start of the minute
public int count;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package us.dot.its.jpo.ode.mockdata;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import us.dot.its.jpo.conflictmonitor.monitor.models.RegulatorIntersectionId;
import us.dot.its.jpo.conflictmonitor.monitor.models.bsm.BsmEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.ConnectionOfTravelEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.IntersectionReferenceAlignmentEvent;
import us.dot.its.jpo.conflictmonitor.monitor.models.events.LaneDirectionOfTravelEvent;
Expand Down Expand Up @@ -175,4 +177,18 @@ public static MapBroadcastRateEvent getMapBroadcastRateEvent() {
event.setTimePeriod(new ProcessingTimePeriod());
return event;
}

public static BsmEvent getBsmEvent() {
BsmEvent event = new BsmEvent();
event.setIntersectionID(12109);
event.setStartingBsm(MockBsmGenerator.getJsonBsms().getFirst());
event.setEndingBsm(MockBsmGenerator.getJsonBsms().getLast());
event.setStartingBsmTimestamp(Instant.parse(event.getStartingBsm().getMetadata().getOdeReceivedAt()).toEpochMilli());
event.setEndingBsmTimestamp(Instant.parse(event.getEndingBsm().getMetadata().getOdeReceivedAt()).toEpochMilli());
event.setWktMapBoundingBox("LINESTRING (-105.09071084163995 39.587773371787485, -105.09071620693672 39.58779610924971, -105.09072266805292 39.58781264558122, -105.09072836868071 39.587833057609934)");
event.setInMapBoundingBox(true);
event.setWallClockTimestamp(Instant.now().toEpochMilli());
event.setWktPath("LINESTRING (-105.09071084163995 39.587773371787485, -105.09071620693672 39.58779610924971, -105.09072266805292 39.58781264558122, -105.09072836868071 39.587833057609934)");
return event;
}
}

0 comments on commit 3166c80

Please sign in to comment.