Make NewGroup and Group.Eval public

Allows callers to execute evaluate lists of rules without first writing
them to disk.
This commit is contained in:
Jonathan Lange 2016-11-18 16:25:58 +00:00
parent 2a2da40223
commit 31fc357cd8
1 changed files with 12 additions and 12 deletions

View File

@ -125,9 +125,11 @@ type Group struct {
terminated chan struct{} terminated chan struct{}
} }
func newGroup(name string, opts *ManagerOptions) *Group { // NewGroup makes a new Group with the given name, options, and rules.
func NewGroup(name string, rules []Rule, opts *ManagerOptions) *Group {
return &Group{ return &Group{
name: name, name: name,
rules: rules,
opts: opts, opts: opts,
done: make(chan struct{}), done: make(chan struct{}),
terminated: make(chan struct{}), terminated: make(chan struct{}),
@ -151,7 +153,7 @@ func (g *Group) run() {
return return
} }
start := time.Now() start := time.Now()
g.eval() g.Eval()
iterationDuration.Observe(time.Since(start).Seconds()) iterationDuration.Observe(time.Since(start).Seconds())
} }
@ -234,10 +236,10 @@ func typeForRule(r Rule) ruleType {
panic(fmt.Errorf("unknown rule type: %T", r)) panic(fmt.Errorf("unknown rule type: %T", r))
} }
// eval runs a single evaluation cycle in which all rules are evaluated in parallel. // Eval runs a single evaluation cycle in which all rules are evaluated in parallel.
// In the future a single group will be evaluated sequentially to properly handle // In the future a single group will be evaluated sequentially to properly handle
// rule dependency. // rule dependency.
func (g *Group) eval() { func (g *Group) Eval() {
var ( var (
now = model.Now() now = model.Now()
wg sync.WaitGroup wg sync.WaitGroup
@ -443,13 +445,7 @@ func (m *Manager) ApplyConfig(conf *config.Config) error {
// As there's currently no group syntax a single group named "default" containing // As there's currently no group syntax a single group named "default" containing
// all rules will be returned. // all rules will be returned.
func (m *Manager) loadGroups(filenames ...string) (map[string]*Group, error) { func (m *Manager) loadGroups(filenames ...string) (map[string]*Group, error) {
groups := map[string]*Group{} rules := []Rule{}
// Currently there is no group syntax implemented. Thus all rules
// are read into a single default group.
g := newGroup("default", m.opts)
groups[g.name] = g
for _, fn := range filenames { for _, fn := range filenames {
content, err := ioutil.ReadFile(fn) content, err := ioutil.ReadFile(fn)
if err != nil { if err != nil {
@ -473,10 +469,14 @@ func (m *Manager) loadGroups(filenames ...string) (map[string]*Group, error) {
default: default:
panic("retrieval.Manager.LoadRuleFiles: unknown statement type") panic("retrieval.Manager.LoadRuleFiles: unknown statement type")
} }
g.rules = append(g.rules, rule) rules = append(rules, rule)
} }
} }
// Currently there is no group syntax implemented. Thus all rules
// are read into a single default group.
g := NewGroup("default", rules, m.opts)
groups := map[string]*Group{g.name: g}
return groups, nil return groups, nil
} }