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

fix(tests): lazy load baton statuses #105

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/main/java/telraam/database/daos/BatonDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public interface BatonDAO extends DAO<Baton> {
@RegisterBeanMapper(Baton.class)
Optional<Baton> getById(@Bind("id") int id);

@SqlQuery("SELECT * FROM baton WHERE mac = :mac")
@RegisterBeanMapper(Baton.class)
Optional<Baton> getByMac(@Bind("mac") String mac);

@Override
@SqlUpdate("DELETE FROM baton WHERE id = :id")
@RegisterBeanMapper(Baton.class)
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/telraam/monitoring/BatonStatusHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

public class BatonStatusHolder {
// Map from batonMac to batonStatus
Expand All @@ -23,13 +24,18 @@ public class BatonStatusHolder {
public BatonStatusHolder(BatonDAO BDAO, DetectionDAO DDAO) {
batonDAO = BDAO;
detectionDAO = DDAO;
this.initStatus();
}

private void initStatus() {
var batons = batonDAO.getAll();
for (Baton baton : batons) {
BatonStatus batonStatus = new BatonStatus(
private BatonStatus getStatusForBaton(String batonMac) {
BatonStatus batonStatus = batonStatusMap.get(batonMac);
if (batonStatus == null) {
Optional<Baton> optionalBaton = batonDAO.getByMac(batonMac);
if (optionalBaton.isEmpty()) {
return null;
}
Baton baton = optionalBaton.get();

batonStatus = new BatonStatus(
baton.getMac().toLowerCase(),
baton.getId(),
baton.getName(),
Expand All @@ -41,6 +47,7 @@ private void initStatus() {
);
batonStatusMap.put(baton.getMac().toLowerCase(), batonStatus);
}
return batonStatus;

}

Expand Down Expand Up @@ -83,13 +90,13 @@ public BatonStatus GetBatonStatus(Integer batonId) {
baton.ifPresent(value -> batonIdToMac.put(batonId, value.getMac().toLowerCase()));
}
String batonMac = batonIdToMac.get(batonId);
return batonStatusMap.get(batonMac);
return getStatusForBaton(batonMac);
}

public BatonStatus createBatonStatus(Integer batonId) {
String batonMac = batonIdToMac.get(batonId);
if (batonMac != null) {
return batonStatusMap.get(batonMac);
return getStatusForBaton(batonMac);
}
var baton = batonDAO.getById(batonId);
if (baton.isEmpty()) {
Expand Down
Loading