triggers mechanism integration
This commit is contained in:
parent
9b487ada89
commit
22fbf3e796
|
@ -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
|
||||
|
|
|
@ -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()}
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue