Skip to content

Commit

Permalink
grafana endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tgxn committed Mar 12, 2024
1 parent 2e1f044 commit 5435c6b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/module/grafana.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { getTopicBySecretKey } = require("../services/topic");

const { createPushToTopic, pushToTopicDevices } = require("../services/push");

const { appLogger } = require("../middleware/logging.js");

const createGrafanaPushRequest = async (request, response, next) => {
try {
console.log("inputPayload", request.body);
let { title, state, message } = request.body;
console.log("inputPayload", { title, state, message });

const pushPayload = {
categoryId: "simple.push",
title: `Grafana: ${title}`,
body: message,
};

const foundTopic = await getTopicBySecretKey(request.params.topicSecret);
if (!foundTopic) {
return next(new Error("Topic secret key does not exist"));
}
appLogger.debug("foundTopic", foundTopic.dataValues.id);

const createdPush = await createPushToTopic(foundTopic);
appLogger.info("createPush", createdPush.dataValues);

// respond early
response
.status(200)
.json({
success: true,
pushIdent: createdPush.dataValues.pushIdent,
})
.send();

await pushToTopicDevices(foundTopic, createdPush, pushPayload);
} catch (error) {
next(error);
}
};

module.exports = {
createGrafanaPushRequest,
};
2 changes: 2 additions & 0 deletions src/routes/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
} = require("../controllers/push");

const { createUpTimeKumaPushRequest } = require("../module/uptimekuma");
const { createGrafanaPushRequest } = require("../module/grafana");

const router = express.Router();

Expand All @@ -21,6 +22,7 @@ router.get("/", authorize(), getUserPushHistory);
// push to topic with secret
router.post("/:topicSecret", createPushRequest);
router.post("/:topicSecret/uptimekuma", createUpTimeKumaPushRequest);
router.post("/:topicSecret/grafana", createGrafanaPushRequest);

// get information on a push request
router.post("/:pushIdent/receipt", recordPushReceipt); // when the notification is recieved
Expand Down

0 comments on commit 5435c6b

Please sign in to comment.