Skip to content

Commit

Permalink
#14 sort all slice data used in tf templates
Browse files Browse the repository at this point in the history
  • Loading branch information
agaurav committed Mar 4, 2022
1 parent 7d46a28 commit 9cc3f59
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
44 changes: 13 additions & 31 deletions libs/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libs

import (
"bytes"
"sort"
"text/template"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions libs/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func giveMostCommonVarsFromSLOSLice(slos []SLO, n int) []string {
varList = append(varList, k)
}

sort.Strings(varList)
if len(varList) <= n {
return varList
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 17 additions & 2 deletions libs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package libs

import (
"errors"
"github.com/mitchellh/hashstructure/v2"
"os"
"sort"
"strings"
)

Expand Down Expand Up @@ -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))

Expand All @@ -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
}

0 comments on commit 9cc3f59

Please sign in to comment.