forked from c3sr/go-cupti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
available_metrics.go
56 lines (47 loc) · 1.11 KB
/
available_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cupti
import (
"encoding/json"
"sync"
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
)
// easyjson:json
type Metric struct {
Metric int `json:"metric,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ShortDescription string `json:"short_description,omitempty"`
LongDescription string `json:"long_description,omitempty"`
}
// easyjson:json
type metrics []Metric
var AvailableMetrics metrics = nil
func loadAvailableMetrics() {
bts, err := ReadFile("/metric_mapping.json")
if err != nil {
panic(err)
}
var res metrics
err = json.Unmarshal(bts, &res)
if err != nil {
panic(err)
}
AvailableMetrics = res
}
func initAvailableMetrics() {
var once sync.Once
once.Do(loadAvailableMetrics)
}
func GetAvailableMetrics() metrics {
initAvailableMetrics()
return AvailableMetrics
}
func FindMetricByName(s0 string) (Metric, error) {
s := strcase.ToSnake(s0)
for _, metric := range AvailableMetrics {
if metric.Name == s {
return metric, nil
}
}
return Metric{}, errors.Errorf("cannot find metric with name %s", s0)
}