Skip to content

Commit

Permalink
Add JSON legacy format (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
remi00 authored Nov 27, 2023
1 parent ab2604d commit f4551bb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand Down
75 changes: 75 additions & 0 deletions templates/json-legacy.tmpl
Original file line number Diff line number Diff line change
@@ -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 -}}
}

0 comments on commit f4551bb

Please sign in to comment.