forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
135 lines (112 loc) · 3.6 KB
/
store.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package configdb
import (
"context"
"errors"
"github.com/frelon/loki/v2/pkg/configs/client"
"github.com/frelon/loki/v2/pkg/configs/userconfig"
"github.com/frelon/loki/v2/pkg/ruler/rulespb"
)
const (
Name = "configdb"
)
// ConfigRuleStore is a concrete implementation of RuleStore that sources rules from the config service
type ConfigRuleStore struct {
configClient client.Client
since userconfig.ID
ruleGroupList map[string]rulespb.RuleGroupList
}
func (c *ConfigRuleStore) SupportsModifications() bool {
return false
}
// NewConfigRuleStore constructs a ConfigRuleStore
func NewConfigRuleStore(c client.Client) *ConfigRuleStore {
return &ConfigRuleStore{
configClient: c,
since: 0,
ruleGroupList: make(map[string]rulespb.RuleGroupList),
}
}
func (c *ConfigRuleStore) ListAllUsers(ctx context.Context) ([]string, error) {
m, err := c.ListAllRuleGroups(ctx)
// TODO: this should be optimized, if possible.
result := []string(nil)
for u := range m {
result = append(result, u)
}
return result, err
}
// ListAllRuleGroups implements RuleStore
func (c *ConfigRuleStore) ListAllRuleGroups(ctx context.Context) (map[string]rulespb.RuleGroupList, error) {
configs, err := c.configClient.GetRules(ctx, c.since)
if err != nil {
return nil, err
}
for user, cfg := range configs {
userRules := rulespb.RuleGroupList{}
if cfg.IsDeleted() {
delete(c.ruleGroupList, user)
continue
}
rMap, err := cfg.Config.ParseFormatted()
if err != nil {
return nil, err
}
for file, rgs := range rMap {
for _, rg := range rgs.Groups {
userRules = append(userRules, rulespb.ToProto(user, file, rg))
}
}
c.ruleGroupList[user] = userRules
}
c.since = getLatestConfigID(configs, c.since)
return c.ruleGroupList, nil
}
// getLatestConfigID gets the latest configs ID.
// max [latest, max (map getID cfgs)]
func getLatestConfigID(cfgs map[string]userconfig.VersionedRulesConfig, latest userconfig.ID) userconfig.ID {
ret := latest
for _, config := range cfgs {
if config.ID > ret {
ret = config.ID
}
}
return ret
}
func (c *ConfigRuleStore) ListRuleGroupsForUserAndNamespace(ctx context.Context, userID, namespace string) (rulespb.RuleGroupList, error) {
r, err := c.ListAllRuleGroups(ctx)
if err != nil {
return nil, err
}
if namespace == "" {
return r[userID], nil
}
list := r[userID]
for ix := 0; ix < len(list); {
if list[ix].GetNamespace() != namespace {
list = append(list[:ix], list[ix+1:]...)
} else {
ix++
}
}
return list, nil
}
func (c *ConfigRuleStore) LoadRuleGroups(ctx context.Context, groupsToLoad map[string]rulespb.RuleGroupList) error {
// Since ConfigRuleStore already Loads the rules in the List methods, there is nothing left to do here.
return nil
}
// GetRuleGroup is not implemented
func (c *ConfigRuleStore) GetRuleGroup(ctx context.Context, userID, namespace, group string) (*rulespb.RuleGroupDesc, error) {
return nil, errors.New("not implemented by the config service rule store")
}
// SetRuleGroup is not implemented
func (c *ConfigRuleStore) SetRuleGroup(ctx context.Context, userID, namespace string, group *rulespb.RuleGroupDesc) error {
return errors.New("not implemented by the config service rule store")
}
// DeleteRuleGroup is not implemented
func (c *ConfigRuleStore) DeleteRuleGroup(ctx context.Context, userID, namespace, group string) error {
return errors.New("not implemented by the config service rule store")
}
// DeleteNamespace is not implemented
func (c *ConfigRuleStore) DeleteNamespace(ctx context.Context, userID, namespace string) error {
return errors.New("not implemented by the config service rule store")
}