...

Source file src/edge-infra.dev/pkg/lib/gcp/monitoring/metrics/templates.go

Documentation: edge-infra.dev/pkg/lib/gcp/monitoring/metrics

     1  package metrics
     2  
     3  import (
     4  	"encoding/json"
     5  	"text/template"
     6  
     7  	"edge-infra.dev/pkg/lib/gcp/monitoring/monutil"
     8  )
     9  
    10  const (
    11  	metricDescriptor = `{{ "Metric Descriptor" | white }}
    12  {{ "================================================================================" | white }}
    13  {{ "Name        :" | white }} {{ .Name | green }}
    14  {{ "Type        :" | white }} {{ .Type | cyan }}
    15  {{ "Metric Kind :" | white }} {{ .MetricKind | magenta }}
    16  {{ "Value Type  :" | white }} {{ .ValueType | blue }}
    17  {{ "Labels      :" | white }} {{ range .Labels }}{{ . | yellow }}
    18                {{ end }}
    19  `
    20  
    21  	metricList = `{{ "Metric Descriptors Listed by Metric Type" | white }}
    22  {{ "================================================================================" | white }}
    23  {{ range . }}
    24  {{ "Metric Type :" | white | bold }} {{ .Type | cyan }}
    25  {{ "Projects" | white }}{{ range .Projects }}
    26  {{ "  - Project :" | white }} {{ .ID | green }}
    27  {{ "    Labels  :" | white }} {{ $s := separator ", " }}{{ range .Labels }}{{ call $s | white }}{{ . | yellow }}{{ end }}
    28  {{ end }}
    29  {{ "--------------------------------------------------" | magenta }}
    30  {{ end }}
    31  `
    32  
    33  	projectList = `{{ "Metric Descriptors Listed by Project ID" | white }}
    34  {{ "================================================================================" | white }}
    35  {{ range . }}
    36  {{ "Project ID :" | white | bold }} {{ .ID | green }}
    37  {{ "Metric Types" | white }}{{ range .Metrics }}
    38  {{ "  - Type   :" | white }} {{ .Type | cyan }}
    39  {{ "    Labels :" | white }} {{ $s := separator ", " }}{{ range .Labels }}{{ call $s | white }}{{ . | yellow }}{{ end }}
    40  {{ end }}
    41  {{ "--------------------------------------------------" | magenta }}
    42  {{ end }}
    43  `
    44  
    45  	labelList = `{{ range . }}
    46  {{ "Label Name  :" | white | bold }} {{ .Name | yellow }}
    47  {{ "Metric Types" | white }}{{ range .Metrics }}
    48  {{ " - Type     :" | white }} {{ .Type | cyan }}
    49  {{ "   Projects :" | white }} {{ range .Projects }}{{ .ID | green }}
    50                   {{ end }}
    51  {{ end }}
    52  {{ "--------------------------------------------------" | magenta }}
    53  {{ end }}
    54  `
    55  
    56  	descriptorList = `{{ "Metric Descriptors List" | white }}
    57  {{ "================================================================================" | white }}
    58  {{ range .Descriptor }}
    59  {{ "Name        :" | white }} {{ .Name | green }}
    60  {{ "Type        :" | white }} {{ .Type | cyan }}
    61  {{ "Metric Kind :" | white }} {{ .MetricKind | magenta }}
    62  {{ "Value Type  :" | white }} {{ .ValueType | blue }}
    63  {{ "Labels      :" | white }} {{ range .Labels }}{{ . | yellow }}
    64                {{ end }}
    65  {{ "--------------------------------------------------" | magenta }}
    66  {{ end }}
    67  `
    68  
    69  	metricCompare = `{{ "Metric Descriptors Compared by Metric Type" | white }}
    70  {{ "================================================================================" | white }}
    71  {{ range . }}
    72  {{ "Metric Type   :" | white | bold }} {{ .Type | cyan }}{{ if .CommonLabels }}
    73  {{ "Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}{{ end }}
    74  {{ "Diff Labels   :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }}
    75  {{ "Projects" | white }}{{ range .Projects }}
    76  {{ "  - Project ID  :" | white }} {{ .ID | green }}{{ if .DiffLabels }}
    77  {{ "    Diff Labels :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }}{{ end }}
    78  {{ end }}
    79  {{ "--------------------------------------------------" | magenta }}
    80  {{ end }}
    81  `
    82  
    83  	projectCompare = `{{ "Metric Descriptors Compared by Project ID" | white }}
    84  {{ "================================================================================" | white }}
    85  {{ range . }}
    86  {{ "Project ID       :" | white | bold }} {{ .ID | green }}
    87  {{ "Metric Types" | white }}{{ range .Metrics }}
    88  {{ " - Type          :" | white }} {{ .Type | cyan }}{{ if .CommonLabels }}
    89  {{ "   Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}{{ end }}{{ if .DiffLabels }}
    90  {{ "   Diff Labels   :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }}{{ end }}
    91  {{ end }}
    92  {{ "--------------------------------------------------" | magenta }}
    93  {{ end }}
    94  `
    95  
    96  	descriptorCompare = `{{ "Metric Descriptors Compared" | white }}
    97  {{ "================================================================================" | white }}
    98  {{ range .Descriptor }}
    99  {{ "Name          :" | white }} {{ .Name | green }}
   100  {{ "Type          :" | white }} {{ .Type | cyan }}
   101  {{ "Metric Kind   :" | white }} {{ .MetricKind | magenta }}
   102  {{ "Value Type    :" | white }} {{ .ValueType | blue }}{{ if .CommonLabels }}
   103  {{ "Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}{{ end }}{{ if .DiffLabels }}
   104  {{ "Diff Labels   :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | red }}{{ end }}
   105  {{ end }}
   106  {{ "--------------------------------------------------" | magenta }}
   107  {{ end }}
   108  `
   109  
   110  	alignReport = `{{ "Metric Descriptor Align - Post Action Report" | white }}
   111  {{ "================================================================================" | white }}
   112  {{ "Misaligned :" | white }} {{ .Misaligned | yellow }}
   113  {{ "Attempted  :" | white }} {{ .Attempted | blue }}
   114  {{ "Completed  :" | white }} {{ .Completed | green }}
   115  
   116  {{ if .Skipped }}{{ "Skipped : " | white }}{{ range .Skipped }}{{ . | yellow }}
   117            {{ end }}{{ end }}
   118  {{ if .Aligned }}{{ "Aligned : " | white }}{{ range .Aligned }}{{ . | green }}
   119            {{ end }}{{ end }}
   120  {{ if .Failed }}{{ "Failed  : " | white }}{{ range .Failed }}{{ . | red }}
   121            {{ end }}{{ end }}
   122  `
   123  
   124  	deleteReport = `{{ "Metric Descriptor Delete - Post Action Report" | white }}
   125  {{ "================================================================================" | white }}
   126  {{ "Attempted  :" | white }} {{ .Attempted | cyan }}
   127  {{ "Completed  :" | white }} {{ .Completed | green }}
   128  {{ if .Skipped }}{{ "--------------------------------------------------" | magenta }}
   129  {{ "Skipped : " }}{{ range .Skipped }}{{ . | yellow }}
   130            {{ end }}{{ end }}
   131  {{ if .Deleted }}{{ "--------------------------------------------------" | magenta }}
   132  {{ "Deleted : " }}{{ range .Deleted }}{{ . | green }}
   133            {{ end }}{{ end }}
   134  {{ if .Failed }}{{ "--------------------------------------------------" | magenta }}
   135  {{ "Failed  : " }}{{ range .Failed }}{{ . | red }}
   136            {{ end }}{{ end }}
   137  `
   138  
   139  	alignConfirm = `
   140  {{ "Descriptor    :" | white }} {{ .Name | cyan }}
   141  {{ "Common Labels :" | white }} {{ $s := separator ", " }}{{ range .CommonLabels }}{{ call $s | white }}{{ . | yellow }}{{ end }}
   142  {{ "Add Labels    :" | white }} {{ $s := separator ", " }}{{ range .DiffLabels }}{{ call $s | white }}{{ . | green }}{{ end }}
   143  
   144  {{"Do you wish to create a timeSeries entry to align above metric descriptor with the additional labels listed? " | white }}`
   145  )
   146  
   147  var (
   148  	tFuncMap = template.FuncMap{
   149  		"bold":       monutil.Bold,
   150  		"italic":     monutil.Italic,
   151  		"underline":  monutil.Underline,
   152  		"white":      monutil.White,
   153  		"red":        monutil.Red,
   154  		"blue":       monutil.Blue,
   155  		"green":      monutil.Green,
   156  		"yellow":     monutil.Yellow,
   157  		"cyan":       monutil.Cyan,
   158  		"magenta":    monutil.Magenta,
   159  		"separator":  separator,
   160  		"jsonFormat": jsonFormat,
   161  	}
   162  
   163  	// example usage: _ = tmpl.Execute(os.Stdout, data)
   164  	TmplJSON = template.Must(template.
   165  			New("templateJSON").
   166  			Funcs(tFuncMap).
   167  			Parse(`{{ . | jsonFormat }}`))
   168  
   169  	TmplDesc = template.Must(template.
   170  			New("templateMetricDescriptor").
   171  			Funcs(tFuncMap).
   172  			Parse(metricDescriptor))
   173  
   174  	TmplMetricList = template.Must(template.
   175  			New("templateMetricList").
   176  			Funcs(tFuncMap).
   177  			Parse(metricList))
   178  
   179  	TmplProjectList = template.Must(template.
   180  			New("templateProjectList").
   181  			Funcs(tFuncMap).
   182  			Parse(projectList))
   183  
   184  	TmplLabelList = template.Must(template.
   185  			New("templateLabelList").
   186  			Funcs(tFuncMap).
   187  			Parse(labelList))
   188  
   189  	TmplDescList = template.Must(template.
   190  			New("templateDescriptorList").
   191  			Funcs(tFuncMap).
   192  			Parse(descriptorList))
   193  
   194  	TmplMetricCompare = template.Must(template.
   195  				New("templateMetricCompare").
   196  				Funcs(tFuncMap).
   197  				Parse(metricCompare))
   198  
   199  	TmplProjectCompare = template.Must(template.
   200  				New("templateProjectCompare").
   201  				Funcs(tFuncMap).
   202  				Parse(projectCompare))
   203  
   204  	TmplDescCompare = template.Must(template.
   205  			New("templateDescriptorCompare").
   206  			Funcs(tFuncMap).
   207  			Parse(descriptorCompare))
   208  
   209  	TmplAlignReport = template.Must(template.
   210  			New("templateAlignReport").
   211  			Funcs(tFuncMap).
   212  			Parse(alignReport))
   213  
   214  	TmplDeleteReport = template.Must(template.
   215  				New("templateDeleteReport").
   216  				Funcs(tFuncMap).
   217  				Parse(deleteReport))
   218  
   219  	TmplAlignConfirm = template.Must(template.
   220  				New("templateAlignConfirm").
   221  				Funcs(tFuncMap).
   222  				Parse(alignConfirm))
   223  )
   224  
   225  // returns the separator string for a template
   226  func separator(s string) func() string {
   227  	i := -1
   228  	return func() string {
   229  		i++
   230  		if i == 0 {
   231  			return ""
   232  		}
   233  		return s
   234  	}
   235  }
   236  
   237  // formats the data into json layout
   238  func jsonFormat(v interface{}) (string, error) {
   239  	b, err := json.MarshalIndent(v, "", "  ")
   240  	if err != nil {
   241  		return "", err
   242  	}
   243  	return string(b), nil
   244  }
   245  

View as plain text