triggers mechanism integration

This commit is contained in:
sqshq 2019-03-08 20:00:13 -05:00
parent 9b487ada89
commit 22fbf3e796
3 changed files with 19 additions and 11 deletions

View File

@ -14,6 +14,7 @@ runcharts:
sound: false
visual: true
script: say ${lavel} initiated with value ${cur}
scale: 3
items:
- label: GOOGLE
value: curl -o /dev/null -s -w '%{time_total}' https://www.google.com

View File

@ -29,8 +29,14 @@ func NewSampler(consumer Consumer, item Item, triggers []trigger.Trigger, rateMs
func (s *Sampler) sample() {
value, err := s.item.nextValue()
if err == nil {
sample := Sample{Value: value, Label: s.item.Label}
s.consumer.SampleChannel <- sample
for _, t := range s.triggers {
t.Execute(s.item.Label, value)
}
} else {
s.consumer.AlertChannel <- Alert{Text: err.Error()}
}

View File

@ -47,6 +47,7 @@ func NewTrigger(config config.TriggerConfig) Trigger {
return Trigger{
title: config.Title,
condition: config.Condition,
data: make(map[string]Data),
actions: Actions{
terminalBell: *config.Actions.TerminalBell,
sound: *config.Actions.Sound,
@ -56,16 +57,16 @@ func NewTrigger(config config.TriggerConfig) Trigger {
}
}
func (t Trigger) execute(label string, value interface{}) {
go func() {
if data, ok := t.data[label]; ok {
data.previousValue = data.currentValue
data.currentValue = value
} else {
t.data[label] = Data{previousValue: nil, currentValue: value}
}
t.evaluate(label, t.data[label])
}()
func (t Trigger) Execute(label string, value interface{}) {
if data, ok := t.data[label]; ok {
data.previousValue = data.currentValue
data.currentValue = value
} else {
t.data[label] = Data{previousValue: nil, currentValue: value}
}
t.evaluate(label, t.data[label])
}
func (t Trigger) evaluate(label string, data Data) {
@ -73,7 +74,7 @@ func (t Trigger) evaluate(label string, data Data) {
output, err := runScript(t.condition, label, data)
if err != nil {
println(err) // TODO visual notification
//println(err) // TODO visual notification
}
if string(output) != TrueIndicator {