[Push Type] Fix missing duration calculation (#685)

This commit is contained in:
Louis Lam 2021-10-14 14:42:34 +08:00
parent 4bee57ea7f
commit 5ff9a64e5e
1 changed files with 9 additions and 0 deletions

View File

@ -18,6 +18,7 @@ router.get("/api/entry-page", async (_, response) => {
router.get("/api/push/:pushToken", async (request, response) => { router.get("/api/push/:pushToken", async (request, response) => {
try { try {
let pushToken = request.params.pushToken; let pushToken = request.params.pushToken;
let msg = request.query.msg || "OK"; let msg = request.query.msg || "OK";
let ping = request.query.ping; let ping = request.query.ping;
@ -30,12 +31,20 @@ router.get("/api/push/:pushToken", async (request, response) => {
throw new Error("Monitor not found or not active."); throw new Error("Monitor not found or not active.");
} }
const previousHeartbeatTime = await R.getCell(`
SELECT time FROM heartbeat
WHERE id = (select MAX(id) from heartbeat where monitor_id = ?)
`, [
monitor.id
]);
let bean = R.dispense("heartbeat"); let bean = R.dispense("heartbeat");
bean.monitor_id = monitor.id; bean.monitor_id = monitor.id;
bean.time = R.isoDateTime(dayjs.utc()); bean.time = R.isoDateTime(dayjs.utc());
bean.status = UP; bean.status = UP;
bean.msg = msg; bean.msg = msg;
bean.ping = ping; bean.ping = ping;
bean.duration = dayjs(bean.time).diff(dayjs(previousHeartbeatTime), "second");
await R.store(bean); await R.store(bean);