/
alerting_usage.go
112 lines (90 loc) · 2.48 KB
/
alerting_usage.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package alerting
import (
"encoding/json"
"github.com/Fenrislol/grafana/pkg/models"
)
// DatasourceAlertUsage is a hash where the key represents the
// Datasource type and the value represents how many alerts
// that use the datasources.
type DatasourceAlertUsage map[string]int
// UsageStats contains stats about alert rules configured in
// Grafana.
type UsageStats struct {
DatasourceUsage DatasourceAlertUsage
}
// UsageStatsQuerier returns usage stats about alert rules
// configured in Grafana.
type UsageStatsQuerier interface {
QueryUsageStats() (*UsageStats, error)
}
// QueryUsageStats returns usage stats about alert rules
// configured in Grafana.
func (ae *AlertEngine) QueryUsageStats() (*UsageStats, error) {
cmd := &models.GetAllAlertsQuery{}
err := ae.Bus.Dispatch(cmd)
if err != nil {
return nil, err
}
dsUsage, err := ae.mapRulesToUsageStats(cmd.Result)
if err != nil {
return nil, err
}
return &UsageStats{
DatasourceUsage: dsUsage,
}, nil
}
func (ae *AlertEngine) mapRulesToUsageStats(rules []*models.Alert) (DatasourceAlertUsage, error) {
// map of datasourceId type and frequency
typeCount := map[int64]int{}
for _, a := range rules {
dss, err := ae.parseAlertRuleModel(a.Settings)
if err != nil {
ae.log.Debug("could not parse settings for alert rule", "id", a.Id)
continue
}
for _, d := range dss {
// aggregated datasource usage based on datasource id
typeCount[d]++
}
}
// map of datsource types and frequency
result := map[string]int{}
for k, v := range typeCount {
query := &models.GetDataSourceByIdQuery{Id: k}
err := ae.Bus.Dispatch(query)
if err != nil {
return map[string]int{}, nil
}
// aggregate datasource usages based on datasource type
result[query.Result.Type] += v
}
return result, nil
}
func (ae *AlertEngine) parseAlertRuleModel(settings json.Marshaler) ([]int64, error) {
datasourceIDs := []int64{}
model := alertJSONModel{}
if settings == nil {
return datasourceIDs, nil
}
bytes, err := settings.MarshalJSON()
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &model)
if err != nil {
return datasourceIDs, err
}
for _, condition := range model.Conditions {
datasourceIDs = append(datasourceIDs, condition.Query.DatasourceID)
}
return datasourceIDs, nil
}
type alertCondition struct {
Query *conditionQuery `json:"query"`
}
type conditionQuery struct {
DatasourceID int64 `json:"datasourceId"`
}
type alertJSONModel struct {
Conditions []*alertCondition `json:"conditions"`
}