-
Notifications
You must be signed in to change notification settings - Fork 3
/
watch_list.go
67 lines (60 loc) · 1.92 KB
/
watch_list.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
package config
import (
"errors"
"strings"
"github.com/waltzofpearls/reckon/logs"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
// WatchList can decode either a YAML config or a comma separated list
//
// - YAML config has a list of metric queries and model names for each everyone of them.
// When using YAML, MODELS (config.Models) env var is not needed.
// - comma separated list doesn't specify model names for each metric query, models names
// come from MODELS (config.Models) env var and it will be set with fillEmpty method.
// When using comma separated list, MODELS env var is required
type WatchList struct {
logger logs.Logger
// map{
// "metric1": {"model1", "model2"},
// "metric2": {"model1"},
// "metric3": {"model2"},
// }
list map[string][]string
}
func newWatchList(lg logs.Logger) *WatchList {
return &WatchList{
logger: lg,
}
}
// Decode either a YAML config or a comma separated list for backward compatibility
//
// WATCH_LIST comma separated list or inline yaml
// sensehat_temperature,sensehat_humidity,sensehat_pressure
// {sensehat_temperature: [Prophet, LSTM], sensehat_humidity: [Prophet], sensehat_pressure: [LSTM]}
func (w *WatchList) Decode(value string) error {
if len(value) == 0 {
return errors.New("WATCH_LIST cannot be empty")
}
w.list = make(map[string][]string)
if err := yaml.Unmarshal([]byte(value), &w.list); err != nil {
w.logger.Info("received comma separated list from WATCH_LIST", zap.String("value", value))
watchList := strings.Split(value, ",")
for _, metricQuery := range watchList {
w.list[metricQuery] = []string{}
}
} else {
w.logger.Info("received YAML config from WATCH_LIST", zap.String("value", value))
}
return nil
}
func (w *WatchList) List() map[string][]string {
return w.list
}
func (w *WatchList) fillEmpty(models []string) {
for metricQuery, modelNames := range w.list {
if len(modelNames) == 0 {
w.list[metricQuery] = models
}
}
}