trigger implementation

This commit is contained in:
sqshq 2019-03-04 22:25:17 -05:00
parent 0de4d07a02
commit 9b22083af8
6 changed files with 117 additions and 23 deletions

View File

@ -1,7 +1,6 @@
package asset
import (
"fmt"
"github.com/hajimehoshi/go-mp3"
"github.com/hajimehoshi/oto"
"io"
@ -31,7 +30,5 @@ func Beep() error {
return err
}
fmt.Print("\a")
return nil
}

View File

@ -2,6 +2,7 @@ package runchart
import (
"fmt"
"github.com/sqshq/sampler/component/trigger"
"github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data"
"image"
@ -36,6 +37,7 @@ const (
type RunChart struct {
ui.Block
triggers []trigger.Trigger
lines []TimeLine
grid ChartGrid
timescale time.Duration

View File

@ -0,0 +1,99 @@
package trigger
import (
"fmt"
"github.com/sqshq/sampler/asset"
"github.com/sqshq/sampler/config"
"os"
"os/exec"
)
const (
TrueIndicator = "1"
BellCharacter = "\a"
)
type Trigger struct {
title string
condition string
actions Actions
data map[string]Data
}
type Actions struct {
terminalBell bool
sound bool
visual bool
script *string
}
type Data struct {
previousValue interface{}
currentValue interface{}
}
func NewTrigger(config config.TriggerConfig) Trigger {
return Trigger{
title: config.Title,
condition: config.Condition,
actions: Actions{
terminalBell: *config.Actions.TerminalBell,
sound: *config.Actions.Sound,
visual: *config.Actions.Visual,
script: config.Actions.Script,
},
}
}
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) evaluate(label string, data Data) {
output, err := runScript(t.condition, label, data)
if err != nil {
println(err) // TODO visual notification
}
if string(output) != TrueIndicator {
return
}
if t.actions.terminalBell {
fmt.Print(BellCharacter)
}
if t.actions.sound {
_ = asset.Beep()
}
if t.actions.visual {
// TODO visual notification
}
if t.actions.script != nil {
_, _ = runScript(*t.actions.script, label, data)
}
}
func runScript(script, label string, data Data) ([]byte, error) {
cmd := exec.Command("sh", "-c", script)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env,
fmt.Sprintf("prev=%v", data.previousValue),
fmt.Sprintf("cur=%v", data.currentValue),
fmt.Sprintf("label=%v", label))
return cmd.Output()
}

View File

@ -6,7 +6,6 @@ runcharts:
size:
w: 53
h: 16
scale: 3
triggers:
- title: PROCESSING STARTED # ${prev} ${cur} ${lavel} echo $(( 3 < 4 && 1 > 2 ))
condition: ((${prev} == 0 && ${cur} > 0))

View File

@ -11,6 +11,7 @@ type ComponentConfig struct {
RefreshRateMs *int `yaml:"refresh-rate-ms,omitempty"`
Position Position `yaml:"position"`
Size Size `yaml:"size"`
Triggers []TriggerConfig `yaml:"triggers,omitempty"`
}
type TriggerConfig struct {
@ -28,7 +29,6 @@ type ActionsConfig struct {
type GaugeConfig struct {
ComponentConfig `yaml:",inline"`
Triggers []TriggerConfig `yaml:"triggers"`
Scale *int `yaml:"scale,omitempty"`
Color *ui.Color `yaml:"color,omitempty"`
Values map[string]string `yaml:"values"`
@ -37,21 +37,18 @@ type GaugeConfig struct {
type BarChartConfig struct {
ComponentConfig `yaml:",inline"`
Triggers []TriggerConfig `yaml:"triggers"`
Scale *int `yaml:"scale,omitempty"`
Items []data.Item `yaml:"items"`
}
type AsciiBoxConfig struct {
ComponentConfig `yaml:",inline"`
Triggers []TriggerConfig `yaml:"triggers"`
data.Item `yaml:",inline"`
Font *asciibox.AsciiFont `yaml:"font,omitempty"`
}
type RunChartConfig struct {
ComponentConfig `yaml:",inline"`
Triggers []TriggerConfig `yaml:"triggers"`
Legend *LegendConfig `yaml:"legend,omitempty"`
Scale *int `yaml:"scale,omitempty"`
Items []data.Item `yaml:"items"`

View File

@ -24,16 +24,16 @@ type Flags struct {
func Load() (Config, Flags) {
//if len(os.Args) < 2 {
// println("Please specify config file location. See www.github.com/sqshq/sampler for the reference")
// os.Exit(0)
//}
if len(os.Args) < 2 {
println("Please specify config file location. See www.github.com/sqshq/sampler for the reference")
os.Exit(0)
}
cfg := readFile("config.yml")
cfg := readFile(os.Args[1])
cfg.validate()
cfg.setDefaults()
flg := Flags{ConfigFileName: "config.yml"}
flg := Flags{ConfigFileName: os.Args[1]}
return *cfg, flg
}