From b03329fd5829142a607df1868a000ed448d9a0a1 Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Wed, 8 Mar 2023 18:33:16 +0100 Subject: [PATCH 1/3] fix(tests): init data at first request --- src/main/java/telraam/monitoring/BatonStatusHolder.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/telraam/monitoring/BatonStatusHolder.java b/src/main/java/telraam/monitoring/BatonStatusHolder.java index db59421..9966cee 100644 --- a/src/main/java/telraam/monitoring/BatonStatusHolder.java +++ b/src/main/java/telraam/monitoring/BatonStatusHolder.java @@ -19,14 +19,16 @@ public class BatonStatusHolder { private BatonDAO batonDAO; private DetectionDAO detectionDAO; + private boolean initialized = false; public BatonStatusHolder(BatonDAO BDAO, DetectionDAO DDAO) { batonDAO = BDAO; detectionDAO = DDAO; - this.initStatus(); } private void initStatus() { + if (initialized) return; + initialized = true; var batons = batonDAO.getAll(); for (Baton baton : batons) { BatonStatus batonStatus = new BatonStatus( @@ -45,6 +47,7 @@ private void initStatus() { } public List GetAllBatonStatuses() { + this.initStatus(); // For each baton, fetch latest detection var batons = batonDAO.getAll(); for (Baton baton : batons) { @@ -78,6 +81,7 @@ private void updateState(Detection msg) { } public BatonStatus GetBatonStatus(Integer batonId) { + this.initStatus(); if (!batonIdToMac.containsKey(batonId)) { var baton = batonDAO.getById(batonId); baton.ifPresent(value -> batonIdToMac.put(batonId, value.getMac().toLowerCase())); @@ -87,6 +91,7 @@ public BatonStatus GetBatonStatus(Integer batonId) { } public BatonStatus createBatonStatus(Integer batonId) { + this.initStatus(); String batonMac = batonIdToMac.get(batonId); if (batonMac != null) { return batonStatusMap.get(batonMac); @@ -112,6 +117,7 @@ public BatonStatus createBatonStatus(Integer batonId) { } public void resetRebooted(int batonId) { + this.initStatus(); var batonStatus = GetBatonStatus(batonId); if (batonStatus == null) { return; From 9db4acaa98783f08342a4b0e9053ab1682631967 Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Mon, 25 Mar 2024 20:22:35 +0100 Subject: [PATCH 2/3] feat(BatonStatusHolder): implement default dict behavior in a custom getter --- .../java/telraam/database/daos/BatonDAO.java | 4 +++ .../telraam/monitoring/BatonStatusHolder.java | 29 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/telraam/database/daos/BatonDAO.java b/src/main/java/telraam/database/daos/BatonDAO.java index f0859d1..2aefda9 100644 --- a/src/main/java/telraam/database/daos/BatonDAO.java +++ b/src/main/java/telraam/database/daos/BatonDAO.java @@ -28,6 +28,10 @@ public interface BatonDAO extends DAO { @RegisterBeanMapper(Baton.class) Optional getById(@Bind("id") int id); + @SqlQuery("SELECT * FROM baton WHERE mac = :mac") + @RegisterBeanMapper(Baton.class) + Optional getByMac(@Bind("mac") String mac); + @Override @SqlUpdate("DELETE FROM baton WHERE id = :id") @RegisterBeanMapper(Baton.class) diff --git a/src/main/java/telraam/monitoring/BatonStatusHolder.java b/src/main/java/telraam/monitoring/BatonStatusHolder.java index 9966cee..e3e4f36 100644 --- a/src/main/java/telraam/monitoring/BatonStatusHolder.java +++ b/src/main/java/telraam/monitoring/BatonStatusHolder.java @@ -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 @@ -43,6 +44,30 @@ private void initStatus() { ); batonStatusMap.put(baton.getMac().toLowerCase(), batonStatus); } + } + + private BatonStatus getStatusForBaton(String batonMac) { + BatonStatus batonStatus = batonStatusMap.get(batonMac); + if (batonStatus == null) { + Optional optionalBaton = batonDAO.getByMac(batonMac); + if (optionalBaton.isEmpty()) { + return null; + } + Baton baton = optionalBaton.get(); + + batonStatus = new BatonStatus( + baton.getMac().toLowerCase(), + baton.getId(), + baton.getName(), + 0, + 0, + false, + null, + -1 + ); + batonStatusMap.put(baton.getMac().toLowerCase(), batonStatus); + } + return batonStatus; } @@ -87,14 +112,14 @@ 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) { this.initStatus(); String batonMac = batonIdToMac.get(batonId); if (batonMac != null) { - return batonStatusMap.get(batonMac); + return getStatusForBaton(batonMac); } var baton = batonDAO.getById(batonId); if (baton.isEmpty()) { From 57e998a8ff03b70cffada2e625d7d67f841b3605 Mon Sep 17 00:00:00 2001 From: NuttyShrimp Date: Mon, 25 Mar 2024 20:37:54 +0100 Subject: [PATCH 3/3] chore(BatonStatusHolder): go all-in on lazy-loading the baton statuses --- .../telraam/monitoring/BatonStatusHolder.java | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/src/main/java/telraam/monitoring/BatonStatusHolder.java b/src/main/java/telraam/monitoring/BatonStatusHolder.java index e3e4f36..b791e0e 100644 --- a/src/main/java/telraam/monitoring/BatonStatusHolder.java +++ b/src/main/java/telraam/monitoring/BatonStatusHolder.java @@ -20,32 +20,12 @@ public class BatonStatusHolder { private BatonDAO batonDAO; private DetectionDAO detectionDAO; - private boolean initialized = false; public BatonStatusHolder(BatonDAO BDAO, DetectionDAO DDAO) { batonDAO = BDAO; detectionDAO = DDAO; } - private void initStatus() { - if (initialized) return; - initialized = true; - var batons = batonDAO.getAll(); - for (Baton baton : batons) { - BatonStatus batonStatus = new BatonStatus( - baton.getMac().toLowerCase(), - baton.getId(), - baton.getName(), - 0, - 0, - false, - null, - -1 - ); - batonStatusMap.put(baton.getMac().toLowerCase(), batonStatus); - } - } - private BatonStatus getStatusForBaton(String batonMac) { BatonStatus batonStatus = batonStatusMap.get(batonMac); if (batonStatus == null) { @@ -72,7 +52,6 @@ private BatonStatus getStatusForBaton(String batonMac) { } public List GetAllBatonStatuses() { - this.initStatus(); // For each baton, fetch latest detection var batons = batonDAO.getAll(); for (Baton baton : batons) { @@ -106,7 +85,6 @@ private void updateState(Detection msg) { } public BatonStatus GetBatonStatus(Integer batonId) { - this.initStatus(); if (!batonIdToMac.containsKey(batonId)) { var baton = batonDAO.getById(batonId); baton.ifPresent(value -> batonIdToMac.put(batonId, value.getMac().toLowerCase())); @@ -116,7 +94,6 @@ public BatonStatus GetBatonStatus(Integer batonId) { } public BatonStatus createBatonStatus(Integer batonId) { - this.initStatus(); String batonMac = batonIdToMac.get(batonId); if (batonMac != null) { return getStatusForBaton(batonMac); @@ -142,7 +119,6 @@ public BatonStatus createBatonStatus(Integer batonId) { } public void resetRebooted(int batonId) { - this.initStatus(); var batonStatus = GetBatonStatus(batonId); if (batonStatus == null) { return;