Skip to content

Commit

Permalink
chore(BatonStatusHolder): go all-in on lazy-loading the baton statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
NuttyShrimp committed Mar 25, 2024
1 parent 9db4aca commit 57e998a
Showing 1 changed file with 0 additions and 24 deletions.
24 changes: 0 additions & 24 deletions src/main/java/telraam/monitoring/BatonStatusHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -72,7 +52,6 @@ private BatonStatus getStatusForBaton(String batonMac) {
}

public List<BatonStatus> GetAllBatonStatuses() {
this.initStatus();
// For each baton, fetch latest detection
var batons = batonDAO.getAll();
for (Baton baton : batons) {
Expand Down Expand Up @@ -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()));
Expand All @@ -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);
Expand All @@ -142,7 +119,6 @@ public BatonStatus createBatonStatus(Integer batonId) {
}

public void resetRebooted(int batonId) {
this.initStatus();
var batonStatus = GetBatonStatus(batonId);
if (batonStatus == null) {
return;
Expand Down

0 comments on commit 57e998a

Please sign in to comment.