From b314cc50527ba4ef0ea24cc1639bfe4431645a3a Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Fri, 29 Mar 2024 18:05:31 +0100 Subject: [PATCH 1/4] feat(lapCountResource): add lap count per source & team End date should be the date of the postgres DB so in our normal settingg, the local time (UTC + 2) --- src/main/java/telraam/App.java | 2 +- .../java/telraam/api/LapCountResource.java | 20 ++++++++++++++++++- .../java/telraam/database/daos/LapDAO.java | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/telraam/App.java b/src/main/java/telraam/App.java index 4e6eb03..27be8c2 100644 --- a/src/main/java/telraam/App.java +++ b/src/main/java/telraam/App.java @@ -103,7 +103,7 @@ public void run(AppConfiguration configuration, Environment environment) throws jersey.register(new LapSourceSwitchoverResource(database.onDemand(LapSourceSwitchoverDAO.class))); jersey.register(new AcceptedLapsResource()); jersey.register(new TimeResource()); - jersey.register(new LapCountResource(database.onDemand(TeamDAO.class))); + jersey.register(new LapCountResource(database.onDemand(TeamDAO.class), database.onDemand(LapDAO.class))); jersey.register(new MonitoringResource(database)); environment.healthChecks().register("template", new TemplateHealthCheck(configuration.getTemplate())); diff --git a/src/main/java/telraam/api/LapCountResource.java b/src/main/java/telraam/api/LapCountResource.java index b8ca04d..697bfa6 100644 --- a/src/main/java/telraam/api/LapCountResource.java +++ b/src/main/java/telraam/api/LapCountResource.java @@ -1,5 +1,6 @@ package telraam.api; +import telraam.database.daos.LapDAO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.ws.rs.*; @@ -9,7 +10,10 @@ import telraam.database.models.Team; import telraam.util.AcceptedLapsUtil; +import java.sql.Timestamp; +import java.time.LocalDateTime; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; @@ -18,9 +22,11 @@ @Produces(MediaType.APPLICATION_JSON) public class LapCountResource { TeamDAO teamDAO; + LapDAO lapDAO; - public LapCountResource(TeamDAO teamDAO) { + public LapCountResource(TeamDAO teamDAO, LapDAO lapDAO) { this.teamDAO = teamDAO; + this.lapDAO = lapDAO; } @GET @@ -53,4 +59,16 @@ public Map getLapCounts() { return perName; } + + // EndTimestamp should be a ISO formatted date timestamp + @GET + @Path("/{lapSourceId}/{teamId}") + public Integer getLapCountForLapSource(@PathParam("lapSourceId") Integer id, @PathParam("teamId") Integer teamId, @QueryParam("end") Optional endTimestamp) { + LocalDateTime dateTime = LocalDateTime.now(); + if (endTimestamp.isPresent()) { + dateTime = LocalDateTime.parse(endTimestamp.get()); + } + List laps = lapDAO.getAllForTeamBeforeTime(id, teamId, Timestamp.valueOf(dateTime)); + return laps.size(); + } } diff --git a/src/main/java/telraam/database/daos/LapDAO.java b/src/main/java/telraam/database/daos/LapDAO.java index f484f6f..078f35c 100644 --- a/src/main/java/telraam/database/daos/LapDAO.java +++ b/src/main/java/telraam/database/daos/LapDAO.java @@ -29,6 +29,10 @@ public interface LapDAO extends DAO { @RegisterBeanMapper(Lap.class) List getAllBySourceSorted(@Bind("lapSourceId") Integer lapSourceId); + @SqlQuery("SELECT * FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp AND team_id = :teamId") + @RegisterBeanMapper(Lap.class) + List getAllForTeamBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("teamId") Integer teamId, @Bind("timestamp") Timestamp timestamp); + @SqlUpdate("INSERT INTO lap (team_id, lap_source_id, timestamp) " + "VALUES (:teamId, :lapSourceId, :timestamp)") @GetGeneratedKeys({"id"}) From 4ad04ca2fb8f9cf7bcc246ad09ea16dcbb14deb8 Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Wed, 3 Apr 2024 18:15:53 +0200 Subject: [PATCH 2/4] feat(LapCount): add endpoint for count for all teams for specific lapSource --- src/main/java/telraam/api/LapCountResource.java | 12 ++++++++++++ src/main/java/telraam/database/daos/LapDAO.java | 5 +++++ src/main/java/telraam/database/models/LapCount.java | 11 +++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/main/java/telraam/database/models/LapCount.java diff --git a/src/main/java/telraam/api/LapCountResource.java b/src/main/java/telraam/api/LapCountResource.java index 697bfa6..62be1d1 100644 --- a/src/main/java/telraam/api/LapCountResource.java +++ b/src/main/java/telraam/api/LapCountResource.java @@ -7,6 +7,7 @@ import jakarta.ws.rs.core.MediaType; import telraam.database.daos.TeamDAO; import telraam.database.models.Lap; +import telraam.database.models.LapCount; import telraam.database.models.Team; import telraam.util.AcceptedLapsUtil; @@ -59,6 +60,17 @@ public Map getLapCounts() { return perName; } + + @GET + @Path("/{lapSourceId}") + public List getLapCountForLapSource(@PathParam("lapSourceId") Integer id, @QueryParam("end") Optional endTimestamp) { + LocalDateTime dateTime = LocalDateTime.now(); + if (endTimestamp.isPresent()) { + dateTime = LocalDateTime.parse(endTimestamp.get()); + } + List laps = lapDAO.getAllBeforeTime(id, Timestamp.valueOf(dateTime)); + return laps; + } // EndTimestamp should be a ISO formatted date timestamp @GET diff --git a/src/main/java/telraam/database/daos/LapDAO.java b/src/main/java/telraam/database/daos/LapDAO.java index 078f35c..490058a 100644 --- a/src/main/java/telraam/database/daos/LapDAO.java +++ b/src/main/java/telraam/database/daos/LapDAO.java @@ -8,6 +8,7 @@ import org.jdbi.v3.sqlobject.statement.SqlQuery; import org.jdbi.v3.sqlobject.statement.SqlUpdate; import telraam.database.models.Lap; +import telraam.database.models.LapCount; import telraam.database.models.TeamLapCount; import java.sql.Timestamp; @@ -29,6 +30,10 @@ public interface LapDAO extends DAO { @RegisterBeanMapper(Lap.class) List getAllBySourceSorted(@Bind("lapSourceId") Integer lapSourceId); + @SqlQuery("SELECT t.id as team_id, (SELECT COUNT(*) FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp and team_id = t.id) as count FROM team t") + @RegisterBeanMapper(LapCount.class) + List getAllBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("timestamp") Timestamp timestamp); + @SqlQuery("SELECT * FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp AND team_id = :teamId") @RegisterBeanMapper(Lap.class) List getAllForTeamBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("teamId") Integer teamId, @Bind("timestamp") Timestamp timestamp); diff --git a/src/main/java/telraam/database/models/LapCount.java b/src/main/java/telraam/database/models/LapCount.java new file mode 100644 index 0000000..a3831a5 --- /dev/null +++ b/src/main/java/telraam/database/models/LapCount.java @@ -0,0 +1,11 @@ +package telraam.database.models; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter @Setter @NoArgsConstructor +public class LapCount { + private int teamId; + private int count; +} From 9fffdd0270f4d0bf152431ee431f1b4206ce256b Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Fri, 5 Apr 2024 00:30:02 +0200 Subject: [PATCH 3/4] refactor(LapDao): unify getAllBeforeTime query results --- src/main/java/telraam/api/LapCountResource.java | 4 ++-- src/main/java/telraam/database/daos/LapDAO.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/telraam/api/LapCountResource.java b/src/main/java/telraam/api/LapCountResource.java index 62be1d1..18a5c0b 100644 --- a/src/main/java/telraam/api/LapCountResource.java +++ b/src/main/java/telraam/api/LapCountResource.java @@ -80,7 +80,7 @@ public Integer getLapCountForLapSource(@PathParam("lapSourceId") Integer id, @Pa if (endTimestamp.isPresent()) { dateTime = LocalDateTime.parse(endTimestamp.get()); } - List laps = lapDAO.getAllForTeamBeforeTime(id, teamId, Timestamp.valueOf(dateTime)); - return laps.size(); + LapCount lapInfo = lapDAO.getAllForTeamBeforeTime(id, teamId, Timestamp.valueOf(dateTime)); + return lapInfo.getCount(); } } diff --git a/src/main/java/telraam/database/daos/LapDAO.java b/src/main/java/telraam/database/daos/LapDAO.java index 490058a..8afd163 100644 --- a/src/main/java/telraam/database/daos/LapDAO.java +++ b/src/main/java/telraam/database/daos/LapDAO.java @@ -34,9 +34,9 @@ public interface LapDAO extends DAO { @RegisterBeanMapper(LapCount.class) List getAllBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("timestamp") Timestamp timestamp); - @SqlQuery("SELECT * FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp AND team_id = :teamId") - @RegisterBeanMapper(Lap.class) - List getAllForTeamBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("teamId") Integer teamId, @Bind("timestamp") Timestamp timestamp); + @SqlQuery("SELECT t.id as team_id, (SELECT COUNT(*) FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp and team_id = t.id) as count FROM team t where id = :teamId") + @RegisterBeanMapper(LapCount.class) + LapCount getAllForTeamBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("teamId") Integer teamId, @Bind("timestamp") Timestamp timestamp); @SqlUpdate("INSERT INTO lap (team_id, lap_source_id, timestamp) " + "VALUES (:teamId, :lapSourceId, :timestamp)") From 2e8bc9acb6220fad7d820851b6478bb058128155 Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Fri, 5 Apr 2024 00:31:21 +0200 Subject: [PATCH 4/4] fix(LapCountResource): make DAO attributes private --- src/main/java/telraam/api/LapCountResource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/telraam/api/LapCountResource.java b/src/main/java/telraam/api/LapCountResource.java index 18a5c0b..edb7b03 100644 --- a/src/main/java/telraam/api/LapCountResource.java +++ b/src/main/java/telraam/api/LapCountResource.java @@ -22,8 +22,8 @@ @Tag(name="Lap Counts") @Produces(MediaType.APPLICATION_JSON) public class LapCountResource { - TeamDAO teamDAO; - LapDAO lapDAO; + private TeamDAO teamDAO; + private LapDAO lapDAO; public LapCountResource(TeamDAO teamDAO, LapDAO lapDAO) { this.teamDAO = teamDAO;