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 sound: false
visual: true visual: true
script: say ${lavel} initiated with value ${cur} script: say ${lavel} initiated with value ${cur}
scale: 3
items: items:
- label: GOOGLE - label: GOOGLE
value: curl -o /dev/null -s -w '%{time_total}' https://www.google.com 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() { func (s *Sampler) sample() {
value, err := s.item.nextValue() value, err := s.item.nextValue()
if err == nil { if err == nil {
sample := Sample{Value: value, Label: s.item.Label} sample := Sample{Value: value, Label: s.item.Label}
s.consumer.SampleChannel <- sample s.consumer.SampleChannel <- sample
for _, t := range s.triggers {
t.Execute(s.item.Label, value)
}
} else { } else {
s.consumer.AlertChannel <- Alert{Text: err.Error()} s.consumer.AlertChannel <- Alert{Text: err.Error()}
} }

View File

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