Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lapCountResource): add lap count per source & team #131

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/telraam/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
32 changes: 31 additions & 1 deletion src/main/java/telraam/api/LapCountResource.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
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.*;
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;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -18,9 +23,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;
}
NuttyShrimp marked this conversation as resolved.
Show resolved Hide resolved

@GET
Expand Down Expand Up @@ -53,4 +60,27 @@ public Map<String, Integer> getLapCounts() {

return perName;
}

@GET
@Path("/{lapSourceId}")
public List<LapCount> getLapCountForLapSource(@PathParam("lapSourceId") Integer id, @QueryParam("end") Optional<String> endTimestamp) {
LocalDateTime dateTime = LocalDateTime.now();
if (endTimestamp.isPresent()) {
dateTime = LocalDateTime.parse(endTimestamp.get());
}
List<LapCount> laps = lapDAO.getAllBeforeTime(id, Timestamp.valueOf(dateTime));
return laps;
}

// 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<String> endTimestamp) {
LocalDateTime dateTime = LocalDateTime.now();
if (endTimestamp.isPresent()) {
dateTime = LocalDateTime.parse(endTimestamp.get());
}
List<Lap> laps = lapDAO.getAllForTeamBeforeTime(id, teamId, Timestamp.valueOf(dateTime));
return laps.size();
}
NuttyShrimp marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 9 additions & 0 deletions src/main/java/telraam/database/daos/LapDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,14 @@ public interface LapDAO extends DAO<Lap> {
@RegisterBeanMapper(Lap.class)
List<Lap> 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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")
@SqlQuery("SELECT team_id, COUNT(*) FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp GROUP BY team_id")

@RegisterBeanMapper(LapCount.class)
List<LapCount> 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<Lap> getAllForTeamBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("teamId") Integer teamId, @Bind("timestamp") Timestamp timestamp);
NuttyShrimp marked this conversation as resolved.
Show resolved Hide resolved

@SqlUpdate("INSERT INTO lap (team_id, lap_source_id, timestamp) " +
"VALUES (:teamId, :lapSourceId, :timestamp)")
@GetGeneratedKeys({"id"})
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/telraam/database/models/LapCount.java
Original file line number Diff line number Diff line change
@@ -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;
}
Loading