-
Notifications
You must be signed in to change notification settings - Fork 11
/
scorekeeper.go
68 lines (60 loc) · 1.45 KB
/
scorekeeper.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
package ik
import (
"errors"
"fmt"
)
type Scorekeeper struct {
logger Logger
topics map[Plugin]map[string]ScorekeeperTopic
}
func (sk *Scorekeeper) GetPlugins() []Plugin {
plugins := make([]Plugin, len(sk.topics))
i := 0
for plugin, _ := range sk.topics {
plugins[i] = plugin
i += 1
}
return plugins
}
func (sk *Scorekeeper) GetTopics(plugin Plugin) []ScorekeeperTopic {
entries, ok := sk.topics[plugin]
if !ok {
return []ScorekeeperTopic{}
}
topics := make([]ScorekeeperTopic, len(entries))
i := 0
for _, entry := range entries {
topics[i] = entry
i += 1
}
return topics
}
func (sk *Scorekeeper) AddTopic(topic ScorekeeperTopic) {
sk.logger.Info("AddTopic: plugin=%s, name=%s", topic.Plugin.Name(), topic.Name)
entries, ok := sk.topics[topic.Plugin]
if !ok {
entries = make(map[string]ScorekeeperTopic)
sk.topics[topic.Plugin] = entries
}
entries[topic.Name] = topic
}
func (sk *Scorekeeper) Fetch(plugin Plugin, name string) (ScoreValueFetcher, error) {
var ok bool
var entries map[string]ScorekeeperTopic
var entry ScorekeeperTopic
entries, ok = sk.topics[plugin]
if ok {
entry, ok = entries[name]
}
if !ok {
return nil, errors.New(fmt.Sprintf("unknown topic: plugin=%s, name=%s", plugin.Name(), name))
}
return entry.Fetcher, nil
}
func (sk *Scorekeeper) Dispose() {}
func NewScorekeeper(logger Logger) *Scorekeeper {
return &Scorekeeper{
logger: logger,
topics: make(map[Plugin]map[string]ScorekeeperTopic),
}
}