From 22fbf3e796ff66b075d551668434857d4d090e66 Mon Sep 17 00:00:00 2001 From: sqshq Date: Fri, 8 Mar 2019 20:00:13 -0500 Subject: [PATCH] triggers mechanism integration --- config.yml | 1 + data/sampler.go | 6 ++++++ trigger/trigger.go | 23 ++++++++++++----------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/config.yml b/config.yml index 70b49b7..1d54c76 100644 --- a/config.yml +++ b/config.yml @@ -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 diff --git a/data/sampler.go b/data/sampler.go index 21dfedd..81356f6 100644 --- a/data/sampler.go +++ b/data/sampler.go @@ -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()} } diff --git a/trigger/trigger.go b/trigger/trigger.go index c670507..47330a8 100644 --- a/trigger/trigger.go +++ b/trigger/trigger.go @@ -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 {