Changeset

4619:b11001bd915d

mod_pubsub_alertmanager: Add a plain text summary
author Kim Alvefur <zash@zash.se>
date Thu, 01 Jul 2021 00:07:27 +0200
parents 4618:48132b6e1b16
children 4620:9b253cce7d88
files mod_pubsub_alertmanager/mod_pubsub_alertmanager.lua
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pubsub_alertmanager/mod_pubsub_alertmanager.lua	Wed Jun 09 01:14:46 2021 +0200
+++ b/mod_pubsub_alertmanager/mod_pubsub_alertmanager.lua	Thu Jul 01 00:07:27 2021 +0200
@@ -1,5 +1,7 @@
 local st = require "util.stanza";
 local json = require "util.json";
+local filters = {};
+local render = require "util.interpolation".new("%b{}", tostring, filters);
 local uuid_generate = require "util.uuid".generate;
 
 module:depends("http");
@@ -55,6 +57,37 @@
 	return 202;
 end
 
+local template = [[
+*ALARM!*
+Status: {status}
+Starts at: {startsAt}{endsAt&
+Ends at: {endsAt}}
+Labels: {labels%
+  {idx}: {item}}
+Annotations: {annotations%
+  {idx}: {item}}
+]]
+
+module:hook("pubsub-summary/urn:uuid:e3bec775-c607-4e9b-9a3f-94de1316d861:v4", function(event)
+	local payload = event.payload;
+
+	local data = {
+		status = payload.attr.status,
+		annotations = {},
+		labels = {},
+		endsAt = payload:find("ends/@at"),
+		startsAt = payload:find("starts/@at"),
+	};
+	for label in payload:childtags("label") do
+		data.labels[tostring(label.attr.name)] = label:get_text();
+	end
+	for annotation in payload:childtags("annotation") do
+		data.annotations[tostring(annotation.attr.name)] = annotation:get_text();
+	end
+
+	return render(template, data);
+end);
+
 module:provides("http", {
 	route = {
 		["POST /*"] = handle_POST;