From f4551bb4f69af0f495122437e8f07676132c1d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remigiusz=20Or=C5=82owski?= Date: Mon, 27 Nov 2023 09:43:22 +0100 Subject: [PATCH] Add JSON legacy format (#8) --- pkg/samples.go | 20 +++++++++- templates/json-legacy.tmpl | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 templates/json-legacy.tmpl diff --git a/pkg/samples.go b/pkg/samples.go index 698ed09..8727dda 100644 --- a/pkg/samples.go +++ b/pkg/samples.go @@ -293,6 +293,24 @@ var TestAlarm = &NotificationViewModel{ Value: 12345, Tag: "device", }, + &EventViewModelDetail{ + Name: "bits", + Label: "", + Value: 58555.9140625, + Tag: "metric", + }, + &EventViewModelDetail{ + Name: "packets", + Label: "", + Value: 11.200035095214844, + Tag: "metric", + }, + &EventViewModelDetail{ + Name: "unique_src_ip", + Label: "", + Value: 1, + Tag: "metric", + }, &EventViewModelDetail{ Name: "DeviceName", Label: "Device", @@ -433,7 +451,7 @@ var TestSynth = &NotificationViewModel{ }, &EventViewModelDetail{ Name: "TestLabel1", - Value: map[string]interface{}{"Name": "MyTestLabel", "Color": "#00ffffff", "IsDark": false}, + Value: map[string]interface{}{"Name": "Foo: MyTestLabel", "Color": "#00ffffff", "IsDark": false}, Tag: "label", }, &EventViewModelDetail{ diff --git a/templates/json-legacy.tmpl b/templates/json-legacy.tmpl new file mode 100644 index 0000000..ad72500 --- /dev/null +++ b/templates/json-legacy.tmpl @@ -0,0 +1,75 @@ +{{- /* +This is a template that produces JSON in a legacy format. +The output must be well-formed JSON, properly escaped. +See template documentation here: https://golang.org/pkg/text/template/ +*/ -}} + +{ +{{ if .IsSingleEvent -}} + {{- with .Event -}} + {{- if .IsAlarm -}} + "EventType": "ALARM_STATE_CHANGE", + "AlarmID": {{ .Details.GetValue "AlarmID" }}, + "AlarmState": "{{ .CurrentState }}", + "PolicyID": {{ .Details.GetValue "AlarmPolicyID" }}, + "ThresholdID": {{ .Details.GetValue "AlarmThresholdID" }}, + "ActivateSeverity": "{{.Details.GetValue "AlarmSeverity"}}", + "AlarmStart": "{{ .StartTime }}", + "AlarmEnd": "{{ .EndTime }}", + "AlertPolicyName": "{{ .Details.GetValue "AlarmPolicyName" }}", + "AlarmsStateOld": "{{ .PreviousState }}", + "AlertDimensions": {{(.Details.WithTag "dimension").Names | toJSON}}, + {{- with $metrics := .Details.WithTag "metric" -}} + {{- if gt (len $metrics) 0 -}} + {{- with $metric := index $metrics 0 -}} + "AlertValue": { + "Unit": "{{- $metric.Name -}}", + "Value": {{- $metric.Value | toJSON -}} + }, + {{- end -}} + {{- end -}} + {{- if gt (len $metrics) 1 -}} + {{- with $metric := index $metrics 1 -}} + "AlertValueSecond": { + "Unit": "{{- $metric.Name -}}", + "Value": {{- $metric.Value | toJSON -}} + }, + {{- end -}} + {{- end -}} + {{- if gt (len $metrics) 2 -}} + {{- with $metric := index $metrics 2 -}} + "AlertValueThird": { + "Unit": "{{- $metric.Name -}}", + "Value": {{- $metric.Value | toJSON -}} + }, + {{- end -}} + {{- end -}} + {{- end -}} + "AlertKey": [ + {{- range $index, $dimension := .Details.WithTag "dimension" -}} + {{- join $index -}} + { + "DimensionName": "{{- $dimension.Name -}}", + "DimensionValue": {{- $dimension.Value | toJSON -}} + } + {{- end -}} + ], + {{- else if .IsMitigation -}} + "EventType": "MITIGATION_STATE_CHANGE", + "MitigationID": {{ .Details.GetValue "MitigationID" }}, + "MitigationStart": "{{.StartTime}}", + "MitigationEnd": "{{.EndTime}}", + "MitigationStateNew": "{{.CurrentState}}", + "MitigationState": "{{.CurrentState}}", + "MitigationStateOld": "{{.PreviousState}}", + "MitigationMethodID": {{.Details.GetValue "MitigationMethodID"}}, + "MitigationPlatformID": {{.Details.GetValue "MitigationPlatformID"}}, + "MitigationPolicyID": {{ .Details.GetValue "MitigationPolicyID" }}, + "MitigationMethodName": "{{.Details.GetValue "MitigationMethodName"}}", + "MitigationPlatformName": "{{.Details.GetValue "MitigationPlatformName"}}", + "MitigationAlertIP": "{{.Details.GetValue "MitigationAlertIP"}}", + {{- end -}} + "CompanyID": {{ $.CompanyID }} + {{- end -}} +{{- end -}} +}