Initial support for #686. #705

Open
fdcastel wants to merge 3 commits from fdcastel/push-api-tags into master
  1. 35
      server/routers/api-router.js

35
server/routers/api-router.js

@ -18,10 +18,11 @@ 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 ping = request.query.ping || null; let {msg, ping, ...requestTags} = request.query;
msg = msg || "OK";
ping = ping || null;
let monitor = await R.findOne("monitor", " push_token = ? AND active = 1 ", [ let monitor = await R.findOne("monitor", " push_token = ? AND active = 1 ", [
pushToken pushToken
@ -38,6 +39,28 @@ router.get("/api/push/:pushToken", async (request, response) => {
monitor.id monitor.id
]); ]);
const tagKeys = Object.keys(requestTags)
if (tagKeys.length > 0) {
// Request has additional tags. Fetch all tags for this monitor.
// For multiple tags with same name, get the oldest one.
const monitorTags = await R.getAll("SELECT tag.name, MIN(monitor_tag.id) id FROM monitor_tag JOIN tag ON tag.id = monitor_tag.tag_id AND monitor_tag.monitor_id = ? GROUP BY tag.name ORDER BY 1", [monitor.id]);
// Update monitor_tag, ignoring non-existing request tags.
monitorTags
.filter(mt => tagKeys.includes(mt.name))
.forEach(async mt => {
const tagValue = requestTags[mt.name];
await R.exec("UPDATE monitor_tag SET value = ? WHERE id = ?", [
tagValue,
mt.id
]);
// FixMe: Not working. What to emit here?
io.to(monitor.user_id).emit("addMonitorTag", mt.id, monitor.id, tagValue);
});
}
let status = UP; let status = UP;
if (monitor.isUpsideDown()) { if (monitor.isUpsideDown()) {
status = flipStatus(status); status = flipStatus(status);
@ -66,11 +89,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 +105,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…
Cancel
Save