From 9cc3f5932c277d26bbd5a5236263d4a638c2049d Mon Sep 17 00:00:00 2001 From: Ashish Gaurav Date: Fri, 4 Mar 2022 09:45:02 +0530 Subject: [PATCH] #14 sort all slice data used in tf templates --- libs/monitor.go | 44 +++++++++++++------------------------------- libs/overview.go | 15 ++++++++++++--- libs/util.go | 19 +++++++++++++++++-- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/libs/monitor.go b/libs/monitor.go index e7a2eff..37129e8 100644 --- a/libs/monitor.go +++ b/libs/monitor.go @@ -2,6 +2,7 @@ package libs import ( "bytes" + "sort" "text/template" ) @@ -123,7 +124,13 @@ func MonitorConfigFromOpenSLO(sloConf SLO) (*SLOMonitorConfig, error) { for _, alert := range alertTmplParams { - alert.View = sloConf.ViewName + sortedNotifs := make([]Notification, len(alert.Notifications)) + copy(sortedNotifs, alert.Notifications) + sort.Slice(sortedNotifs, func(i, j int) bool { + return GiveStructCompare(sortedNotifs[i], sortedNotifs[j]) + }) + + alert.View = sloConf.ViewName() buf := bytes.Buffer{} err = tmpl.Execute(&buf, alert) if err != nil { @@ -140,40 +147,15 @@ func MonitorConfigFromOpenSLO(sloConf SLO) (*SLOMonitorConfig, error) { } objectives = append(objectives, obj) } + + sort.Slice(objectives, func(i, j int) bool { + return objectives[i].Suffix < objectives[j].Suffix + }) + mConf.Objectives = objectives return mConf, nil } -//func GiveBurnMonitorConf(sloConf SLO) (*SLOMonitorConfig, error) { -// -// tmpl, err := template.New("monitor").Parse(GoodByTotalQueryTmpl) -// -// if err != nil { -// return nil, err -// } -// -// buf := bytes.Buffer{} -// err = tmpl.Execute(&buf, sloConf.Spec.Objectives[0].RatioMetrics) -// if err != nil { -// return nil, err -// } -// m := &SLOMonitorConfig{ -// Name: sloConf.ObjectHeader.Metadata.Name, -// Desc: sloConf.Spec.Description, -// Service: sloConf.Spec.Service, -// Objectives: []SLOObjective{ -// { -// Field: "SLO", -// Query: buf.String(), -// TimeRange: "24h", -// ValueWarning: (*sloConf.Spec.Objectives[0].BudgetTarget + 1.0) / 2, -// ValueCritical: *sloConf.Spec.Objectives[0].BudgetTarget, -// }, -// }, -// } -// return m, err -//} - type BurnAlertTmplParams struct { BurnRate Target float64 // SLO goal i.e. the slo percent age in [0.0,1.0] decimal form diff --git a/libs/overview.go b/libs/overview.go index 6b9d616..503b1ee 100644 --- a/libs/overview.go +++ b/libs/overview.go @@ -111,6 +111,7 @@ func giveMostCommonVarsFromSLOSLice(slos []SLO, n int) []string { varList = append(varList, k) } + sort.Strings(varList) if len(varList) <= n { return varList } @@ -121,7 +122,10 @@ func giveMostCommonVarsFromSLOSLice(slos []SLO, n int) []string { return vCount[ki] > vCount[kj] }) - return varList[:n] + trimmedList := varList[:n] + + sort.Strings(trimmedList) + return trimmedList } // giveMostCommonVars top n most common label or fields found @@ -155,6 +159,7 @@ func GenServiceOverviewDashboard(sloPathMap map[string]*SLO, outDir string) erro for srv, slos := range srvMap { rows, err := getOverviewRows(slos) + if err != nil { return err } @@ -165,10 +170,10 @@ func GenServiceOverviewDashboard(sloPathMap map[string]*SLO, outDir string) erro Service: srv, Rows: rows, Layout: layout, - Vars: giveMostCommonVarsFromSLOSLice(slos,4), + Vars: giveMostCommonVarsFromSLOSLice(slos, 4), } - path := filepath.Join(outDir, DashboardsFolder, "overview-" +srv +".tf") + path := filepath.Join(outDir, DashboardsFolder, "overview-"+srv+".tf") err = FileFromTmpl(NameServiceTrackerTmpl, path, conf) if err != nil { return err @@ -202,6 +207,10 @@ func getOverviewRows(s []SLO) ([]ServiceOverviewRow, error) { rows = append(rows, r) } + sort.Slice(rows, func(i, j int) bool { + return rows[i].SLOName < rows[j].SLOName + }) + return rows, nil } diff --git a/libs/util.go b/libs/util.go index f0a724e..8d4df9e 100644 --- a/libs/util.go +++ b/libs/util.go @@ -2,7 +2,9 @@ package libs import ( "errors" + "github.com/mitchellh/hashstructure/v2" "os" + "sort" "strings" ) @@ -73,7 +75,6 @@ func dirExists(path string) (bool, error) { return false, err } - func giveMapKeys(m map[string]string) []string { keys := make([]string, len(m)) @@ -82,12 +83,26 @@ func giveMapKeys(m map[string]string) []string { keys[i] = k i++ } + sort.Strings(keys) return keys } -func giveFieldsGroupByStr(m map[string]string) string { +func giveFieldsGroupByStr(m map[string]string) string { k := giveMapKeys(m) return strings.Join(k, ",") } + +func GiveStructCompare(a, b interface{}) bool { + aHash, err := hashstructure.Hash(a, hashstructure.FormatV2, nil) + if err != nil { + log.Fatal(err) + } + bHash, err := hashstructure.Hash(b, hashstructure.FormatV2, nil) + + if err != nil { + log.Fatal(err) + } + return aHash == bHash +}