From 3509c556a13cdf49944621bd90bf2db3bc072ac7 Mon Sep 17 00:00:00 2001 From: js0ny Date: Sat, 6 Dec 2025 02:44:03 +0000 Subject: [PATCH] fix: Allow passing timestamp --- drone-black-box/main.go | 2 -- flake.nix | 1 + .../data/common/DroneEvent.java | 24 +++++++++++++++++++ .../service/PathFinderService.java | 7 +++++- .../service/TelemetryService.java | 9 +++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/drone-black-box/main.go b/drone-black-box/main.go index 76cca90..62b0e89 100644 --- a/drone-black-box/main.go +++ b/drone-black-box/main.go @@ -56,8 +56,6 @@ func (s *Server) snapshotHandler(w http.ResponseWriter, r *http.Request) { return } - // Optimized query using window function (requires SQLite 3.25+) or standard group by max - // Using correlated subquery which is standard and works well with the index query := ` SELECT drone_id, latitude, longitude, timestamp FROM drone_events t1 diff --git a/flake.nix b/flake.nix index cfb17de..4dc25de 100644 --- a/flake.nix +++ b/flake.nix @@ -37,6 +37,7 @@ go bun svelte-language-server + typescript-language-server ]; shellHook = '' export JAVA_HOME=${pkgs.jdk21} diff --git a/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/data/common/DroneEvent.java b/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/data/common/DroneEvent.java index 9132afe..5060221 100644 --- a/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/data/common/DroneEvent.java +++ b/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/data/common/DroneEvent.java @@ -2,6 +2,7 @@ package io.github.js0ny.ilp_coursework.data.common; import java.time.LocalDateTime; import java.util.List; +import java.util.Map; import io.github.js0ny.ilp_coursework.data.response.DeliveryPathResponse; @@ -62,4 +63,27 @@ public record DroneEvent( } return events; } + + public static List fromPathResponseWithTimestamps( + DeliveryPathResponse resp, Map deliveryTimestamps) { + List events = new java.util.ArrayList<>(); + for (var p : resp.dronePaths()) { + String id = String.valueOf(p.droneId()); + for (var d : p.deliveries()) { + LocalDateTime timestamp = deliveryTimestamps.get(d.deliveryId()); + // Fallback to current time if the delivery does not carry a timestamp. + System.out.println("Generated event for drone " + id + " at " + timestamp.toString()); + LocalDateTime current = timestamp != null ? timestamp : LocalDateTime.now(); + for (var coord : d.flightPath()) { + events.add(new DroneEvent( + id, + coord.lat(), + coord.lng(), + current.toString())); + current = current.plusSeconds(1); + } + } + } + return events; + } } diff --git a/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/PathFinderService.java b/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/PathFinderService.java index 44b52b8..464b21a 100644 --- a/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/PathFinderService.java +++ b/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/PathFinderService.java @@ -31,6 +31,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.time.LocalDateTime; /** * Class that handles calculations about deliverypath @@ -126,6 +127,7 @@ public class PathFinderService { return new DeliveryPathResponse(0f, 0, new DronePath[0]); } + Map deliveryTimestamps = new HashMap<>(); for (var r : records) { if (isRestricted(r.delivery())) { throw new IllegalStateException( @@ -133,6 +135,9 @@ public class PathFinderService { + r.id() + " is located within a restricted area and cannot be fulfilled"); } + if (r.date() != null && r.time() != null) { + deliveryTimestamps.put(r.id(), LocalDateTime.of(r.date(), r.time())); + } } Map> assigned = assignDeliveries(records); @@ -177,7 +182,7 @@ public class PathFinderService { var resp = new DeliveryPathResponse(totalCost, totalMoves, paths.toArray(new DronePath[0])); - telemetryService.sendEventAsyncByPathResponse(resp); + telemetryService.sendEventAsyncByPathResponse(resp, deliveryTimestamps); return resp; } diff --git a/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/TelemetryService.java b/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/TelemetryService.java index 9d2e19e..1e8e61a 100644 --- a/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/TelemetryService.java +++ b/ilp-rest-service/src/main/java/io/github/js0ny/ilp_coursework/service/TelemetryService.java @@ -4,6 +4,7 @@ import java.net.http.HttpClient; import java.time.Duration; import java.time.LocalDateTime; import java.util.concurrent.CompletableFuture; +import java.util.Map; import org.springframework.stereotype.Service; @@ -40,6 +41,14 @@ public class TelemetryService { } } + public void sendEventAsyncByPathResponse(DeliveryPathResponse resp, + Map deliveryTimestamps) { + var events = DroneEvent.fromPathResponseWithTimestamps(resp, deliveryTimestamps); + for (var event : events) { + sendEventAsync(event); + } + } + public void sendEventAsync(DroneEvent event) { CompletableFuture.runAsync(() -> { try {