From ebd099280e611edf827626fd6b046f5a1227a6d5 Mon Sep 17 00:00:00 2001 From: jacob6838 Date: Wed, 3 Jan 2024 23:49:26 -0700 Subject: [PATCH] Adding roadRegulatorId to APIs and fixing infinite live data loop --- jpo-conflictmonitor | 2 +- .../accessors/reports/ReportRepository.java | 8 +- .../reports/ReportRepositoryImpl.java | 24 +- .../ode/api/controllers/ReportController.java | 326 ++++++++++-------- 4 files changed, 198 insertions(+), 162 deletions(-) diff --git a/jpo-conflictmonitor b/jpo-conflictmonitor index e8187d152..8b615d390 160000 --- a/jpo-conflictmonitor +++ b/jpo-conflictmonitor @@ -1 +1 @@ -Subproject commit e8187d152935334cb98e4a0579eb4f891c2d5091 +Subproject commit 8b615d3908e5877480d652723a2ddb227552852c diff --git a/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepository.java b/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepository.java index 9426b878c..ee58b4256 100644 --- a/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepository.java +++ b/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepository.java @@ -1,17 +1,17 @@ package us.dot.its.jpo.ode.api.accessors.reports; - import java.util.List; import org.springframework.data.mongodb.core.query.Query; import us.dot.its.jpo.ode.api.models.DataLoader; import us.dot.its.jpo.ode.api.models.ReportDocument; -public interface ReportRepository extends DataLoader{ - Query getQuery(String reportName, Integer intersectionID, String roadRegulatorID, Long startTime, Long endTime, boolean includeReportContents, boolean latest); +public interface ReportRepository extends DataLoader { + Query getQuery(String reportName, Integer intersectionID, Integer roadRegulatorID, Long startTime, Long endTime, + boolean includeReportContents, boolean latest); long getQueryResultCount(Query query); - + List find(Query query); } \ No newline at end of file diff --git a/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepositoryImpl.java b/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepositoryImpl.java index 4df08df4a..ae4cc5e7d 100644 --- a/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepositoryImpl.java +++ b/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/reports/ReportRepositoryImpl.java @@ -1,6 +1,5 @@ package us.dot.its.jpo.ode.api.accessors.reports; - import java.time.Instant; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -13,10 +12,8 @@ import us.dot.its.jpo.ode.api.ConflictMonitorApiProperties; import us.dot.its.jpo.ode.api.models.ReportDocument; - - @Component -public class ReportRepositoryImpl implements ReportRepository{ +public class ReportRepositoryImpl implements ReportRepository { @Autowired private MongoTemplate mongoTemplate; @@ -27,38 +24,39 @@ public class ReportRepositoryImpl implements ReportRepository{ private String collectionName = "CmReport"; @Override - public Query getQuery(String reportName, Integer intersectionID, String roadRegulatorID, Long startTime, Long endTime, boolean includeReportContents, boolean latest) { + public Query getQuery(String reportName, Integer intersectionID, Integer roadRegulatorID, Long startTime, + Long endTime, boolean includeReportContents, boolean latest) { Query query = new Query(); - if(reportName != null){ + if (reportName != null) { query.addCriteria(Criteria.where("reportName").is(reportName)); } - if(intersectionID != null){ + if (intersectionID != null) { query.addCriteria(Criteria.where("intersectionID").is(intersectionID)); } - if(roadRegulatorID != null && roadRegulatorID.length() > 0){ - query.addCriteria(Criteria.where("intersectionID").is(intersectionID)); + if (roadRegulatorID != null) { + query.addCriteria(Criteria.where("roadRegulatorID").is(intersectionID)); } - if(startTime == null){ + if (startTime == null) { startTime = 0L; } - if(endTime == null){ + if (endTime == null) { endTime = Instant.now().toEpochMilli(); } query.addCriteria(Criteria.where("reportGeneratedAt").gte(startTime).lte(endTime)); - if(!includeReportContents){ + if (!includeReportContents) { query.fields().exclude("reportContents"); } if (latest) { query.with(Sort.by(Sort.Direction.DESC, "reportGeneratedAt")); query.limit(1); - }else{ + } else { query.limit(props.getMaximumResponseSize()); } diff --git a/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/ReportController.java b/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/ReportController.java index 8ca44f05c..ed8b0a1fe 100644 --- a/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/ReportController.java +++ b/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/ReportController.java @@ -26,21 +26,18 @@ public class ReportController { private static final Logger logger = LoggerFactory.getLogger(ReportController.class); - ObjectMapper objectMapper = new ObjectMapper(); @Autowired ConflictMonitorApiProperties props; - @Autowired ReportService reportService; @Autowired ReportRepository reportRepo; - public String getCurrentTime() { return ZonedDateTime.now().toInstant().toEpochMilli() + ""; } @@ -50,18 +47,19 @@ public String getCurrentTime() { @PreAuthorize("hasRole('USER') || hasRole('ADMIN')") public byte[] generateReport( @RequestParam(name = "intersection_id", required = true) int intersectionID, - @RequestParam(name = "road_regulator_id", required = false) String roadRegulatorID, + @RequestParam(name = "road_regulator_id", required = false) Integer roadRegulatorID, @RequestParam(name = "start_time_utc_millis", required = true) long startTime, @RequestParam(name = "end_time_utc_millis", required = true) long endTime) { - logger.info("Generating Report"); + logger.info("Generating Report"); + + if (roadRegulatorID == null) { + roadRegulatorID = -1; + } - if(roadRegulatorID == null){ - roadRegulatorID = "-1"; - } - - ReportDocument document = reportService.buildReport(intersectionID, roadRegulatorID, startTime, endTime); + ReportDocument document = reportService.buildReport(intersectionID, roadRegulatorID.toString(), startTime, + endTime); - return document.getReportContents(); + return document.getReportContents(); } @CrossOrigin(origins = "http://localhost:3000") @@ -70,160 +68,200 @@ public byte[] generateReport( public ResponseEntity> listReports( @RequestParam(name = "report_name", required = false) String reportName, @RequestParam(name = "intersection_id", required = false) int intersectionID, - @RequestParam(name = "road_regulator_id", required = false) String roadRegulatorID, + @RequestParam(name = "road_regulator_id", required = false) Integer roadRegulatorID, @RequestParam(name = "start_time_utc_millis", required = true) long startTime, @RequestParam(name = "end_time_utc_millis", required = true) long endTime, - @RequestParam(name = "latest", required = true) boolean latest - ) { - - Query query = reportRepo.getQuery(reportName, intersectionID, roadRegulatorID, startTime, endTime, false, latest); - long count = reportRepo.getQueryResultCount(query); - - logger.info("Returning Report List with Size: " + count); - return ResponseEntity.ok(reportRepo.find(query)); + @RequestParam(name = "latest", required = true) boolean latest) { + + if (roadRegulatorID == null) { + roadRegulatorID = -1; + } + + Query query = reportRepo.getQuery(reportName, intersectionID, roadRegulatorID, startTime, endTime, + false, + latest); + long count = reportRepo.getQueryResultCount(query); + + logger.info("Returning Report List with Size: " + count); + return ResponseEntity.ok(reportRepo.find(query)); } @CrossOrigin(origins = "http://localhost:3000") @RequestMapping(value = "/reports/download", method = RequestMethod.GET, produces = "application/octet-stream") @PreAuthorize("hasRole('USER') || hasRole('ADMIN')") public ResponseEntity downloadReport( - @RequestParam(name = "report_name", required = true) String reportName - ) { - - Query query = reportRepo.getQuery(reportName, null, null, null, null, true, true); - - logger.info("Returning archived report for download"); - - List reports = reportRepo.find(query); - if(reports.size() >0 ){ - return ResponseEntity.ok() - .header("Content-Disposition", "attachment; filename=\"" + reportName + "\"") - .body(reports.get(0).getReportContents()); - } - else{ - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); - } + @RequestParam(name = "report_name", required = true) String reportName) { + + Query query = reportRepo.getQuery(reportName, null, null, null, null, true, true); + + logger.info("Returning archived report for download"); + + List reports = reportRepo.find(query); + if (reports.size() > 0) { + return ResponseEntity.ok() + .header("Content-Disposition", "attachment; filename=\"" + reportName + "\"") + .body(reports.get(0).getReportContents()); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } } // @Bean // public void testReportDownload(){ + // // Query query = reportRepo.getQuery(null, null, null, null, null, false, + // false); + // // long count = reportRepo.getQueryResultCount(query); - // // Query query = reportRepo.getQuery(null, null, null, null, null, false, false); - // // long count = reportRepo.getQueryResultCount(query); - - // // System.out.println("Returning Report List with Size: " + count); - - // // for(ReportDocument doc : reportRepo.find(query)){ - // // System.out.println(doc); + // // System.out.println("Returning Report List with Size: " + count); - // // } - - // // Query query = reportRepo.getQuery("CmReport_1234_-1_1698710400000_1699315200000", null, null, null, null, true, true); - // // long count = reportRepo.getQueryResultCount(query); - // // System.out.println("Returning Report List with Size: " + count); - // // System.out.println(reportRepo.find(query)); + // // for(ReportDocument doc : reportRepo.find(query)){ + // // System.out.println(doc); + // // } + // // Query query = + // reportRepo.getQuery("CmReport_1234_-1_1698710400000_1699315200000", null, + // null, null, null, true, true); + // // long count = reportRepo.getQueryResultCount(query); + // // System.out.println("Returning Report List with Size: " + count); + // // System.out.println(reportRepo.find(query)); // } - - - // @Bean // public void test(){ - // System.out.println("Generating Test PDF"); - - // int intersectionID = 6311; - // long startTime = 0; - // // long startTime = 1678233600000L; - // long endTime = Instant.now().toEpochMilli(); - - // ReportDocument document = buildReport(intersectionID, roadRegulatorID, startTime, endTime); - - // // List laneDirectionOfTravelEventCounts = laneDirectionOfTravelEventRepo.getLaneDirectionOfTravelEventsByDay(intersectionID, startTime, endTime); - // // List laneDirectionOfTravelMedianDistanceDistribution = laneDirectionOfTravelEventRepo.getMedianDistanceByFoot(intersectionID, startTime, endTime); - // // List laneDirectionOfTravelMedianHeadingDistribution = laneDirectionOfTravelEventRepo.getMedianDistanceByDegree(intersectionID, startTime, endTime); - // // List laneDirectionOfTravelAssessmentCount = laneDirectionOfTravelAssessmentRepo.getLaneDirectionOfTravelOverTime(intersectionID, startTime, endTime); - - - // // List connectionOfTravelEventCounts = connectionOfTravelEventRepo.getConnectionOfTravelEventsByDay(intersectionID, startTime, endTime); - // // List laneConnectionCounts = connectionOfTravelEventRepo.getConnectionOfTravelEventsByConnection(intersectionID, startTime, endTime); - - // // List signalstateEventCounts = signalStateEventRepo.getSignalStateEventsByDay(intersectionID, startTime, endTime); - - // // List signalStateStopEventCounts = signalStateStopEventRepo.getSignalStateStopEventsByDay(intersectionID, startTime, endTime); - - // // List signalStateConflictEventCounts = signalStateConflictEventRepo.getSignalStateConflictEventsByDay(intersectionID, startTime, endTime); - - // // List timeChangeDetailsEventCounts= timeChangeDetailsEventRepo.getTimeChangeDetailsEventsByDay(intersectionID, startTime, endTime); - - // // List mapCounts = processedMapRepo.getMapBroadcastRates(intersectionID, startTime, endTime); - // // List spatCounts = processedSpatRepo.getSpatBroadcastRates(intersectionID, startTime, endTime); - - // // List spatCountDistribution = processedSpatRepo.getSpatBroadcastRateDistribution(intersectionID, startTime, endTime); - // // List mapCountDistribution = processedMapRepo.getMapBroadcastRateDistribution(intersectionID, startTime, endTime); - - - - // try { - - // ReportBuilder builder = new ReportBuilder(new FileOutputStream("test.pdf")); - // List dateStrings = builder.getDayStringsInRange(startTime, endTime); - // builder.addTitlePage("Conflict Monitor Report", startTime, endTime); - - // // // Add Lane Direction of Travel Information - // // builder.addTitle("Lane Direction of Travel"); - // // builder.addLaneDirectionOfTravelEvent(DailyData.fromIDCountDays(laneDirectionOfTravelEventCounts, dateStrings)); - // // builder.addLaneDirectionOfTravelMedianDistanceDistribution(ChartData.fromIDCountList(laneDirectionOfTravelMedianDistanceDistribution)); - // // builder.addLaneDirectionOfTravelMedianHeadingDistribution(ChartData.fromIDCountList(laneDirectionOfTravelMedianHeadingDistribution)); - // // builder.addDistanceFromCenterlineOverTime(laneDirectionOfTravelAssessmentCount); - // // builder.addHeadingOverTime(laneDirectionOfTravelAssessmentCount); - // // builder.addPageBreak(); - - // // // Add Lane Connection of Travel Information - // // builder.addTitle("Connection of Travel"); - // // builder.addConnectionOfTravelEvent(DailyData.fromIDCountDays(connectionOfTravelEventCounts, dateStrings)); - // // builder.addLaneConnectionOfTravelMap(laneConnectionCounts); - // // builder.addPageBreak(); - - // // // Add Signal State Events - // // builder.addTitle("Signal State Events"); - // // builder.addSignalStateEvents(DailyData.fromIDCountDays(signalstateEventCounts, dateStrings)); - // // builder.addSignalStateStopEvents(DailyData.fromIDCountDays(signalStateStopEventCounts, dateStrings)); - // // builder.addSignalStateConflictEvent(DailyData.fromIDCountDays(signalStateConflictEventCounts, dateStrings)); - // // builder.addPageBreak(); - - // // // Add Time Change Details - // // builder.addSpatTimeChangeDetailsEvent(DailyData.fromIDCountDays(timeChangeDetailsEventCounts, dateStrings)); - // // builder.addPageBreak(); - - - // // builder.addTitle("Map"); - // // builder.addMapBroadcastRate(mapCounts); - // // builder.addMapBroadcastRateDistribution(mapCountDistribution, startTime, endTime); - // // builder.addPageBreak(); - - // // builder.addTitle("SPaT"); - // // builder.addSpatBroadcastRate(spatCounts); - // // builder.addSpatBroadcastRateDistribution(spatCountDistribution, startTime, endTime); - // // builder.addPageBreak(); - - // // List secondStrings = builder.getSecondsStringInRange(startTime, endTime); - - // // builder.addSpatMinimumDataEventErrors(latestSpatMinimumdataEvent); - // // builder.addMapMinimumDataEventErrors(latestMapMinimumdataEvent); - - // builder.write(); - - - // } catch (FileNotFoundException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } - // System.out.println("Test PDF Generation Complete"); - - + // System.out.println("Generating Test PDF"); + + // int intersectionID = 6311; + // long startTime = 0; + // // long startTime = 1678233600000L; + // long endTime = Instant.now().toEpochMilli(); + + // ReportDocument document = buildReport(intersectionID, roadRegulatorID, + // startTime, endTime); + + // // List laneDirectionOfTravelEventCounts = + // laneDirectionOfTravelEventRepo.getLaneDirectionOfTravelEventsByDay(intersectionID, + // startTime, endTime); + // // List laneDirectionOfTravelMedianDistanceDistribution = + // laneDirectionOfTravelEventRepo.getMedianDistanceByFoot(intersectionID, + // startTime, endTime); + // // List laneDirectionOfTravelMedianHeadingDistribution = + // laneDirectionOfTravelEventRepo.getMedianDistanceByDegree(intersectionID, + // startTime, endTime); + // // List laneDirectionOfTravelAssessmentCount + // = + // laneDirectionOfTravelAssessmentRepo.getLaneDirectionOfTravelOverTime(intersectionID, + // startTime, endTime); + + // // List connectionOfTravelEventCounts = + // connectionOfTravelEventRepo.getConnectionOfTravelEventsByDay(intersectionID, + // startTime, endTime); + // // List laneConnectionCounts = + // connectionOfTravelEventRepo.getConnectionOfTravelEventsByConnection(intersectionID, + // startTime, endTime); + + // // List signalstateEventCounts = + // signalStateEventRepo.getSignalStateEventsByDay(intersectionID, startTime, + // endTime); + + // // List signalStateStopEventCounts = + // signalStateStopEventRepo.getSignalStateStopEventsByDay(intersectionID, + // startTime, endTime); + + // // List signalStateConflictEventCounts = + // signalStateConflictEventRepo.getSignalStateConflictEventsByDay(intersectionID, + // startTime, endTime); + + // // List timeChangeDetailsEventCounts= + // timeChangeDetailsEventRepo.getTimeChangeDetailsEventsByDay(intersectionID, + // startTime, endTime); + + // // List mapCounts = + // processedMapRepo.getMapBroadcastRates(intersectionID, startTime, endTime); + // // List spatCounts = + // processedSpatRepo.getSpatBroadcastRates(intersectionID, startTime, endTime); + + // // List spatCountDistribution = + // processedSpatRepo.getSpatBroadcastRateDistribution(intersectionID, startTime, + // endTime); + // // List mapCountDistribution = + // processedMapRepo.getMapBroadcastRateDistribution(intersectionID, startTime, + // endTime); + + // try { + + // ReportBuilder builder = new ReportBuilder(new FileOutputStream("test.pdf")); + // List dateStrings = builder.getDayStringsInRange(startTime, endTime); + // builder.addTitlePage("Conflict Monitor Report", startTime, endTime); + + // // // Add Lane Direction of Travel Information + // // builder.addTitle("Lane Direction of Travel"); + // // + // builder.addLaneDirectionOfTravelEvent(DailyData.fromIDCountDays(laneDirectionOfTravelEventCounts, + // dateStrings)); + // // + // builder.addLaneDirectionOfTravelMedianDistanceDistribution(ChartData.fromIDCountList(laneDirectionOfTravelMedianDistanceDistribution)); + // // + // builder.addLaneDirectionOfTravelMedianHeadingDistribution(ChartData.fromIDCountList(laneDirectionOfTravelMedianHeadingDistribution)); + // // + // builder.addDistanceFromCenterlineOverTime(laneDirectionOfTravelAssessmentCount); + // // builder.addHeadingOverTime(laneDirectionOfTravelAssessmentCount); + // // builder.addPageBreak(); + + // // // Add Lane Connection of Travel Information + // // builder.addTitle("Connection of Travel"); + // // + // builder.addConnectionOfTravelEvent(DailyData.fromIDCountDays(connectionOfTravelEventCounts, + // dateStrings)); + // // builder.addLaneConnectionOfTravelMap(laneConnectionCounts); + // // builder.addPageBreak(); + + // // // Add Signal State Events + // // builder.addTitle("Signal State Events"); + // // + // builder.addSignalStateEvents(DailyData.fromIDCountDays(signalstateEventCounts, + // dateStrings)); + // // + // builder.addSignalStateStopEvents(DailyData.fromIDCountDays(signalStateStopEventCounts, + // dateStrings)); + // // + // builder.addSignalStateConflictEvent(DailyData.fromIDCountDays(signalStateConflictEventCounts, + // dateStrings)); + // // builder.addPageBreak(); + + // // // Add Time Change Details + // // + // builder.addSpatTimeChangeDetailsEvent(DailyData.fromIDCountDays(timeChangeDetailsEventCounts, + // dateStrings)); + // // builder.addPageBreak(); + + // // builder.addTitle("Map"); + // // builder.addMapBroadcastRate(mapCounts); + // // builder.addMapBroadcastRateDistribution(mapCountDistribution, startTime, + // endTime); + // // builder.addPageBreak(); + + // // builder.addTitle("SPaT"); + // // builder.addSpatBroadcastRate(spatCounts); + // // builder.addSpatBroadcastRateDistribution(spatCountDistribution, startTime, + // endTime); + // // builder.addPageBreak(); + + // // List secondStrings = builder.getSecondsStringInRange(startTime, + // endTime); + + // // builder.addSpatMinimumDataEventErrors(latestSpatMinimumdataEvent); + // // builder.addMapMinimumDataEventErrors(latestMapMinimumdataEvent); + + // builder.write(); + + // } catch (FileNotFoundException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // System.out.println("Test PDF Generation Complete"); + // } } \ No newline at end of file