mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-24 20:45:57 +00:00
Initial support for https://github.com/louislam/uptime-kuma/issues/686.
This commit is contained in:
parent
8a481a1be0
commit
a2f1d61d31
1 changed files with 36 additions and 7 deletions
|
@ -17,13 +17,15 @@ router.get("/api/entry-page", async (_, response) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/api/push/:pushToken", async (request, response) => {
|
router.get("/api/push/:pushToken", async (request, response) => {
|
||||||
|
const trx = await R.begin();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
let pushToken = request.params.pushToken;
|
let pushToken = request.params.pushToken;
|
||||||
let msg = request.query.msg || "OK";
|
|
||||||
let ping = request.query.ping || null;
|
|
||||||
|
|
||||||
let monitor = await R.findOne("monitor", " push_token = ? AND active = 1 ", [
|
let {msg, ping, ...requestTags} = request.query;
|
||||||
|
msg = msg || "OK";
|
||||||
|
ping = ping || null;
|
||||||
|
|
||||||
|
let monitor = await trx.findOne("monitor", " push_token = ? AND active = 1 ", [
|
||||||
pushToken
|
pushToken
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -31,13 +33,36 @@ 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 previousHeartbeat = await R.getRow(`
|
const previousHeartbeat = await trx.getRow(`
|
||||||
SELECT status, time FROM heartbeat
|
SELECT status, time FROM heartbeat
|
||||||
WHERE id = (select MAX(id) from heartbeat where monitor_id = ?)
|
WHERE id = (select MAX(id) from heartbeat where monitor_id = ?)
|
||||||
`, [
|
`, [
|
||||||
monitor.id
|
monitor.id
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const tagKeys = Object.keys(requestTags)
|
||||||
|
if (tagKeys.length > 0) {
|
||||||
|
// Request has additional tags. Fetch all tags for this monitor.
|
||||||
|
const dbTags = await trx.getAll("SELECT tag.id, tag.name FROM tag JOIN monitor_tag ON monitor_tag.tag_id = tag.id AND monitor_tag.monitor_id = ?", [monitor.id]);
|
||||||
|
|
||||||
|
// Update monitor_tag, ignoring non-existing request tags.
|
||||||
|
dbTags
|
||||||
|
.filter(tag => tagKeys.includes(tag.name))
|
||||||
|
.forEach(async tag => {
|
||||||
|
const tagValue = requestTags[tag.name];
|
||||||
|
|
||||||
|
await trx.exec("UPDATE monitor_tag SET value = ? WHERE tag_id = ? AND monitor_id = ?", [
|
||||||
|
tagValue,
|
||||||
|
tag.id,
|
||||||
|
monitor.id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// FixMe: Not working. What to emit here?
|
||||||
|
io.to(monitor.user_id).emit("addMonitorTag", tag.id, monitor.id, tagValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// FixMe: Bean returned by trx.dispense() has no .toJSON() method (!?).
|
||||||
let status = UP;
|
let status = UP;
|
||||||
if (monitor.isUpsideDown()) {
|
if (monitor.isUpsideDown()) {
|
||||||
status = flipStatus(status);
|
status = flipStatus(status);
|
||||||
|
@ -47,7 +72,7 @@ router.get("/api/push/:pushToken", async (request, response) => {
|
||||||
let previousStatus = status;
|
let previousStatus = status;
|
||||||
let duration = 0;
|
let duration = 0;
|
||||||
|
|
||||||
let bean = R.dispense("heartbeat");
|
let bean = R.dispense("heartbeat"); // Should be trx.dispense();
|
||||||
bean.time = R.isoDateTime(dayjs.utc());
|
bean.time = R.isoDateTime(dayjs.utc());
|
||||||
|
|
||||||
if (previousHeartbeat) {
|
if (previousHeartbeat) {
|
||||||
|
@ -66,11 +91,13 @@ router.get("/api/push/:pushToken", async (request, response) => {
|
||||||
bean.ping = ping;
|
bean.ping = ping;
|
||||||
bean.duration = duration;
|
bean.duration = duration;
|
||||||
|
|
||||||
await R.store(bean);
|
await trx.store(bean);
|
||||||
|
|
||||||
io.to(monitor.user_id).emit("heartbeat", bean.toJSON());
|
io.to(monitor.user_id).emit("heartbeat", bean.toJSON());
|
||||||
Monitor.sendStats(io, monitor.id, monitor.user_id);
|
Monitor.sendStats(io, monitor.id, monitor.user_id);
|
||||||
|
|
||||||
|
await trx.commit();
|
||||||
|
|
||||||
response.json({
|
response.json({
|
||||||
ok: true,
|
ok: true,
|
||||||
});
|
});
|
||||||
|
@ -80,6 +107,8 @@ router.get("/api/push/:pushToken", async (request, response) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
await trx.rollback();
|
||||||
|
|
||||||
response.json({
|
response.json({
|
||||||
ok: false,
|
ok: false,
|
||||||
msg: e.message
|
msg: e.message
|
||||||
|
|
Loading…
Add table
Reference in a new issue