Skip to content

Commit

Permalink
Update json-legacy.tmpl (#9)
Browse files Browse the repository at this point in the history
* Update json-legacy.tmpl

* Updates to template and add timeRfc3339

---------

Co-authored-by: Remigiusz Orlowski <[email protected]>
  • Loading branch information
sulfurious and remi00 authored Dec 7, 2023
1 parent f4551bb commit d554e34
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
54 changes: 52 additions & 2 deletions pkg/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"encoding/json"
"fmt"
"reflect"
"strings"
"text/template"
"time"
)

var TextTemplateFuncMap = template.FuncMap{
Expand All @@ -17,9 +20,56 @@ var TextTemplateFuncMap = template.FuncMap{
"uglifyJSON": compactJSON,
"explodeJSONKeys": explodeJSONKeys,
"x": explodeJSONKeys,
"timeRfc3339": timeRfc3339,
"join": join,
"joinWith": joinWith,
}

func tryParseTime(input string) (time.Time, error) {
formats := []string{
"2006-01-02 15:04:05 MST",
"2006-01-02 15:04:05",
time.RFC3339,
time.RFC3339Nano,
time.RFC822,
time.RFC822Z,
time.ANSIC,
time.UnixDate,
}

var parsed time.Time
var err error
for _, format := range formats {
parsed, err = time.Parse(format, input)
if err == nil {
return parsed, nil
}
}

return time.Time{}, fmt.Errorf("unable to parse time: %s", input)
}

func timeRfc3339(input interface{}) string {
var t time.Time
var err error

switch v := input.(type) {
case string:
t, err = tryParseTime(v)
if err != nil {
return v
}
case int, int8, int16, int32, int64:
t = time.Unix(int64(reflect.ValueOf(v).Int()), 0)
case uint, uint8, uint16, uint32, uint64:
t = time.Unix(int64(reflect.ValueOf(v).Uint()), 0)
case time.Time:
t = v
default:
return fmt.Sprintf("(%t:%+v)", input, input)
}

"join": join,
"joinWith": joinWith,
return t.Format(time.RFC3339)
}

func toJSON(v interface{}) string {
Expand Down
12 changes: 12 additions & 0 deletions pkg/samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ var TestAlarm = &NotificationViewModel{
Value: 14444,
Tag: "",
},
&EventViewModelDetail{
Name: "Baseline",
Label: "Baseline Value",
Value: 777.654,
Tag: "",
},
&EventViewModelDetail{
Name: "AlarmBaselineSource",
Label: "Baseline Source",
Expand Down Expand Up @@ -393,6 +399,12 @@ var TestSynth = &NotificationViewModel{
Value: 1228,
Tag: "",
},
&EventViewModelDetail{
Name: "AlarmID",
Label: "",
Value: 1222228,
Tag: "",
},
&EventViewModelDetail{
Name: "TestName",
Label: "Test Name",
Expand Down
35 changes: 31 additions & 4 deletions templates/json-legacy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ See template documentation here: https://golang.org/pkg/text/template/
"AlarmState": "{{ .CurrentState }}",
"PolicyID": {{ .Details.GetValue "AlarmPolicyID" }},
"ThresholdID": {{ .Details.GetValue "AlarmThresholdID" }},
"MitigationID": "0",
"ActivateSeverity": "{{.Details.GetValue "AlarmSeverity"}}",
"AlarmStart": "{{ .StartTime }}",
"AlarmEnd": "{{ .EndTime }}",
"AlarmStart": "{{ timeRfc3339 .StartTime }}",
"AlarmEnd": "{{ if eq .EndTime "ongoing" }}0001-01-01T00:00:00Z{{ else }}{{ timeRfc3339 .EndTime }}{{ end }}",
"LastActivate": "{{$.NowRFC3339}}",
"AlertPolicyName": "{{ .Details.GetValue "AlarmPolicyName" }}",
"AlarmsStateOld": "{{ .PreviousState }}",
"AlertDimensions": {{(.Details.WithTag "dimension").Names | toJSON}},
Expand Down Expand Up @@ -45,6 +47,15 @@ See template documentation here: https://golang.org/pkg/text/template/
{{- end -}}
{{- end -}}
{{- end -}}
{{- if gt (len (.Details.WithTag "metric")) 0 -}}
"AlertBaseline": {
{{- with $metric := index (.Details.WithTag "metric") 0 -}}
"Unit": "{{- $metric.Name -}}",
{{- end -}}
"Value": {{ if .Details.Has "Baseline" }}{{ .Details.GetValue "Baseline" | toJSON }}{{ else }}0{{ end }}
},
{{- end -}}
"AlertBaselineSource": "{{ .Details.GetValue "AlarmBaselineSource" }}",
"AlertKey": [
{{- range $index, $dimension := .Details.WithTag "dimension" -}}
{{- join $index -}}
Expand All @@ -54,11 +65,21 @@ See template documentation here: https://golang.org/pkg/text/template/
}
{{- end -}}
],
"Links": {
"Dashboard": {
"Text": "Open in Dashboard",
"Value": "{{ .Details.GetValue "DashboardAlarmURL" }}"
},
"Explorer": {
"Text": "Open in Explorer",
"Value": "{{ .Details.GetValue "DetailsAlarmURL" }}"
}
},
{{- else if .IsMitigation -}}
"EventType": "MITIGATION_STATE_CHANGE",
"MitigationID": {{ .Details.GetValue "MitigationID" }},
"MitigationStart": "{{.StartTime}}",
"MitigationEnd": "{{.EndTime}}",
"MitigationStart": "{{timeRfc3339 .StartTime}}",
"MitigationEnd": "{{timeRfc3339 .EndTime}}",
"MitigationStateNew": "{{.CurrentState}}",
"MitigationState": "{{.CurrentState}}",
"MitigationStateOld": "{{.PreviousState}}",
Expand All @@ -68,6 +89,12 @@ See template documentation here: https://golang.org/pkg/text/template/
"MitigationMethodName": "{{.Details.GetValue "MitigationMethodName"}}",
"MitigationPlatformName": "{{.Details.GetValue "MitigationPlatformName"}}",
"MitigationAlertIP": "{{.Details.GetValue "MitigationAlertIP"}}",
{{- else -}}
{{- . | toJSON | explodeJSONKeys -}},
{{- .Details.General.ToMap | toJSON | explodeJSONKeys -}},
"Metrics": {{- (.Details.WithTag "metric").ToMap | toJSON -}},
"Dimensions": {{- (.Details.WithTag "dimension").ToMap | toJSON -}},
"Links": {{- (.Details.WithTag "url").ToMap | toJSON -}},
{{- end -}}
"CompanyID": {{ $.CompanyID }}
{{- end -}}
Expand Down

0 comments on commit d554e34

Please sign in to comment.