package metrics import ( "encoding/json" "text/template" "edge-infra.dev/pkg/lib/gcp/monitoring/monutil" ) const ( metricDescriptor = `{{ "Metric Descriptor" | white }} {{ "================================================================================" | white }} {{ "Name :" | white }} {{ .Name | green }} {{ "Type :" | white }} {{ .Type | cyan }} {{ "Metric Kind :" | white }} {{ .MetricKind | magenta }} {{ "Value Type :" | white }} {{ .ValueType | blue }} {{ "Labels :" | white }} {{ range .Labels }}{{ . | yellow }} {{ end }} ` metricList = `{{ "Metric Descriptors Listed by Metric Type" | white }} {{ "================================================================================" | white }} {{ range . }} {{ "Metric Type :" | white | bold }} {{ .Type | cyan }} {{ "Projects" | white }}{{ range .Projects }} {{ " - Project :" | white }} {{ .ID | green }} {{ " Labels :" | white }} {{ $s := separator ", " }}{{ range .Labels }}{{ call $s | white }}{{ . | yellow }}{{ end }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` projectList = `{{ "Metric Descriptors Listed by Project ID" | white }} {{ "================================================================================" | white }} {{ range . }} {{ "Project ID :" | white | bold }} {{ .ID | green }} {{ "Metric Types" | white }}{{ range .Metrics }} {{ " - Type :" | white }} {{ .Type | cyan }} {{ " Labels :" | white }} {{ $s := separator ", " }}{{ range .Labels }}{{ call $s | white }}{{ . | yellow }}{{ end }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` labelList = `{{ range . }} {{ "Label Name :" | white | bold }} {{ .Name | yellow }} {{ "Metric Types" | white }}{{ range .Metrics }} {{ " - Type :" | white }} {{ .Type | cyan }} {{ " Projects :" | white }} {{ range .Projects }}{{ .ID | green }} {{ end }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` descriptorList = `{{ "Metric Descriptors List" | white }} {{ "================================================================================" | white }} {{ range .Descriptor }} {{ "Name :" | white }} {{ .Name | green }} {{ "Type :" | white }} {{ .Type | cyan }} {{ "Metric Kind :" | white }} {{ .MetricKind | magenta }} {{ "Value Type :" | white }} {{ .ValueType | blue }} {{ "Labels :" | white }} {{ range .Labels }}{{ . | yellow }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` metricCompare = `{{ "Metric Descriptors Compared by Metric Type" | white }} {{ "================================================================================" | white }} {{ range . }} {{ "Metric Type :" | white | bold }} {{ .Type | cyan }}{{ if .CommonLabels }} {{ "Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}{{ end }} {{ "Diff Labels :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }} {{ "Projects" | white }}{{ range .Projects }} {{ " - Project ID :" | white }} {{ .ID | green }}{{ if .DiffLabels }} {{ " Diff Labels :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }}{{ end }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` projectCompare = `{{ "Metric Descriptors Compared by Project ID" | white }} {{ "================================================================================" | white }} {{ range . }} {{ "Project ID :" | white | bold }} {{ .ID | green }} {{ "Metric Types" | white }}{{ range .Metrics }} {{ " - Type :" | white }} {{ .Type | cyan }}{{ if .CommonLabels }} {{ " Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}{{ end }}{{ if .DiffLabels }} {{ " Diff Labels :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }}{{ end }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` descriptorCompare = `{{ "Metric Descriptors Compared" | white }} {{ "================================================================================" | white }} {{ range .Descriptor }} {{ "Name :" | white }} {{ .Name | green }} {{ "Type :" | white }} {{ .Type | cyan }} {{ "Metric Kind :" | white }} {{ .MetricKind | magenta }} {{ "Value Type :" | white }} {{ .ValueType | blue }}{{ if .CommonLabels }} {{ "Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}{{ end }}{{ if .DiffLabels }} {{ "Diff Labels :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }} {{ end }} {{ "--------------------------------------------------" | magenta }} {{ end }} ` alignReport = `{{ "Metric Descriptor Align - Post Action Report" | white }} {{ "================================================================================" | white }} {{ "Misaligned :" | white }} {{ .Misaligned | yellow }} {{ "Attempted :" | white }} {{ .Attempted | blue }} {{ "Completed :" | white }} {{ .Completed | green }} {{ if .Skipped }}{{ "Skipped : " | white }}{{ range .Skipped }}{{ . | yellow }} {{ end }}{{ end }} {{ if .Aligned }}{{ "Aligned : " | white }}{{ range .Aligned }}{{ . | green }} {{ end }}{{ end }} {{ if .Failed }}{{ "Failed : " | white }}{{ range .Failed }}{{ . | red }} {{ end }}{{ end }} ` deleteReport = `{{ "Metric Descriptor Delete - Post Action Report" | white }} {{ "================================================================================" | white }} {{ "Attempted :" | white }} {{ .Attempted | cyan }} {{ "Completed :" | white }} {{ .Completed | green }} {{ if .Skipped }}{{ "--------------------------------------------------" | magenta }} {{ "Skipped : " }}{{ range .Skipped }}{{ . | yellow }} {{ end }}{{ end }} {{ if .Deleted }}{{ "--------------------------------------------------" | magenta }} {{ "Deleted : " }}{{ range .Deleted }}{{ . | green }} {{ end }}{{ end }} {{ if .Failed }}{{ "--------------------------------------------------" | magenta }} {{ "Failed : " }}{{ range .Failed }}{{ . | red }} {{ end }}{{ end }} ` alignConfirm = ` {{ "Descriptor :" | white }} {{ .Name | cyan }} {{ "Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }} {{ "Add Labels :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | green }}{{ end }} {{"Do you wish to create a timeSeries entry to align above metric descriptor with the additional labels listed? " | white }}` ) var ( tFuncMap = template.FuncMap{ "bold": monutil.Bold, "italic": monutil.Italic, "underline": monutil.Underline, "white": monutil.White, "red": monutil.Red, "blue": monutil.Blue, "green": monutil.Green, "yellow": monutil.Yellow, "cyan": monutil.Cyan, "magenta": monutil.Magenta, "separator": separator, "jsonFormat": jsonFormat, } // example usage: _ = tmpl.Execute(os.Stdout, data) TmplJSON = template.Must(template. New("templateJSON"). Funcs(tFuncMap). Parse(`{{ . | jsonFormat }}`)) TmplDesc = template.Must(template. New("templateMetricDescriptor"). Funcs(tFuncMap). Parse(metricDescriptor)) TmplMetricList = template.Must(template. New("templateMetricList"). Funcs(tFuncMap). Parse(metricList)) TmplProjectList = template.Must(template. New("templateProjectList"). Funcs(tFuncMap). Parse(projectList)) TmplLabelList = template.Must(template. New("templateLabelList"). Funcs(tFuncMap). Parse(labelList)) TmplDescList = template.Must(template. New("templateDescriptorList"). Funcs(tFuncMap). Parse(descriptorList)) TmplMetricCompare = template.Must(template. New("templateMetricCompare"). Funcs(tFuncMap). Parse(metricCompare)) TmplProjectCompare = template.Must(template. New("templateProjectCompare"). Funcs(tFuncMap). Parse(projectCompare)) TmplDescCompare = template.Must(template. New("templateDescriptorCompare"). Funcs(tFuncMap). Parse(descriptorCompare)) TmplAlignReport = template.Must(template. New("templateAlignReport"). Funcs(tFuncMap). Parse(alignReport)) TmplDeleteReport = template.Must(template. New("templateDeleteReport"). Funcs(tFuncMap). Parse(deleteReport)) TmplAlignConfirm = template.Must(template. New("templateAlignConfirm"). Funcs(tFuncMap). Parse(alignConfirm)) ) // returns the separator string for a template func separator(s string) func() string { i := -1 return func() string { i++ if i == 0 { return "" } return s } } // formats the data into json layout func jsonFormat(v interface{}) (string, error) { b, err := json.MarshalIndent(v, "", " ") if err != nil { return "", err } return string(b), nil }