Skip to content

Commit

Permalink
Fix #922
Browse files Browse the repository at this point in the history
  • Loading branch information
louislam committed Dec 8, 2021
1 parent 76611ec commit dcc91d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 12 additions & 0 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ class Monitor extends BeanModel {
debug("heartbeatCount" + heartbeatCount + " " + time);

if (heartbeatCount <= 0) {
// Fix #922, since previous heartbeat could be inserted by api, it should get from database
previousBeat = await Monitor.getPreviousHeartbeat(this.id);

throw new Error("No heartbeat in the time window");
} else {
// No need to insert successful heartbeat for push type, so end here
Expand Down Expand Up @@ -751,6 +754,15 @@ class Monitor extends BeanModel {
debug("No notification, no need to send cert notification");
}
}

static async getPreviousHeartbeat(monitorID) {
return await R.getRow(`
SELECT status, time FROM heartbeat
WHERE id = (select MAX(id) from heartbeat where monitor_id = ?)
`, [
monitorID
]);
}
}

module.exports = Monitor;
12 changes: 4 additions & 8 deletions server/routers/api-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ router.get("/api/push/:pushToken", async (request, response) => {
throw new Error("Monitor not found or not active.");
}

const previousHeartbeat = await R.getRow(`
SELECT status, time FROM heartbeat
WHERE id = (select MAX(id) from heartbeat where monitor_id = ?)
`, [
monitor.id
]);
const previousHeartbeat = await Monitor.getPreviousHeartbeat(monitor.id);

let status = UP;
if (monitor.isUpsideDown()) {
Expand Down Expand Up @@ -157,8 +152,9 @@ router.get("/api/status-page/monitor-list", cache("5 minutes"), async (_request,
JOIN tag
ON monitor_tag.tag_id = tag.id
WHERE monitor_tag.monitor_id = ?`, [monitor.id]
);
return {...monitor, tags: tags}
);
return { ...monitor,
tags: tags };
}));
}

Expand Down

0 comments on commit dcc91d6

Please sign in to comment.