From 72665f55a411e049cd65d42fefaa25894f16bb73 Mon Sep 17 00:00:00 2001 From: sqshq Date: Fri, 7 Jun 2019 01:39:11 -0400 Subject: [PATCH] stop all samplers on pause --- component/statusbar.go | 2 +- data/sampler.go | 18 ++++++++++++++---- event/handler.go | 18 +++++++++++++++--- main.go | 24 +++++++++++++----------- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/component/statusbar.go b/component/statusbar.go index 18c9d70..a109f86 100644 --- a/component/statusbar.go +++ b/component/statusbar.go @@ -9,8 +9,8 @@ import ( ) const ( - bindingsIndent = 3 pauseText = " P A U S E D " + bindingsIndent = 3 ) type StatusBar struct { diff --git a/data/sampler.go b/data/sampler.go index 6bdff16..12022f6 100644 --- a/data/sampler.go +++ b/data/sampler.go @@ -12,24 +12,28 @@ type Sampler struct { triggers []*Trigger triggersChannel chan *Sample variables []string + pause bool } -func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options config.Options, fileVariables map[string]string, rateMs int) Sampler { +func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options config.Options, fileVariables map[string]string, rateMs int) *Sampler { ticker := time.NewTicker(time.Duration(rateMs * int(time.Millisecond))) - sampler := Sampler{ + sampler := &Sampler{ consumer, items, triggers, make(chan *Sample), mergeVariables(fileVariables, options.Environment), + false, } go func() { for ; true; <-ticker.C { for _, item := range sampler.items { - go sampler.sample(item, options) + if !sampler.pause { + go sampler.sample(item, options) + } } } }() @@ -39,7 +43,9 @@ func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options select { case sample := <-sampler.triggersChannel: for _, t := range sampler.triggers { - t.Execute(sample) + if !sampler.pause { + t.Execute(sample) + } } } } @@ -77,3 +83,7 @@ func mergeVariables(fileVariables map[string]string, optionsVariables []string) return result } + +func (s *Sampler) Pause(pause bool) { + s.pause = pause +} diff --git a/event/handler.go b/event/handler.go index f658a56..da8aa11 100644 --- a/event/handler.go +++ b/event/handler.go @@ -5,6 +5,7 @@ import ( "github.com/sqshq/sampler/component/layout" "github.com/sqshq/sampler/config" "github.com/sqshq/sampler/console" + "github.com/sqshq/sampler/data" "time" ) @@ -13,21 +14,23 @@ const ( ) type Handler struct { + samplers []*data.Sampler + options config.Options layout *layout.Layout renderTicker *time.Ticker consoleEvents <-chan ui.Event renderRate time.Duration - options config.Options } -func NewHandler(layout *layout.Layout, options config.Options) *Handler { +func NewHandler(samplers []*data.Sampler, options config.Options, layout *layout.Layout) *Handler { renderRate := calcMinRenderRate(layout) return &Handler{ + samplers: samplers, + options: options, layout: layout, consoleEvents: ui.PollEvents(), renderTicker: time.NewTicker(renderRate), renderRate: renderRate, - options: options, } } @@ -71,10 +74,19 @@ func (h *Handler) handleModeChange(m layout.Mode) { switch m { case layout.ModeDefault: h.renderTicker = time.NewTicker(h.renderRate) + h.pause(false) case layout.ModePause: + h.pause(true) // proceed with stopped timer default: h.renderTicker = time.NewTicker(console.MinRenderInterval) + h.pause(false) + } +} + +func (h *Handler) pause(pause bool) { + for _, s := range h.samplers { + s.Pause(pause) } } diff --git a/main.go b/main.go index 71528be..f2a6d1c 100644 --- a/main.go +++ b/main.go @@ -30,40 +30,42 @@ type Starter struct { cfg config.Config } -func (s *Starter) startAll() { +func (s *Starter) startAll() []*data.Sampler { + samplers := make([]*data.Sampler, 0) for _, c := range s.cfg.RunCharts { cpt := runchart.NewRunChart(c, s.palette) - s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) + samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)) } for _, c := range s.cfg.SparkLines { cpt := sparkline.NewSparkLine(c, s.palette) - s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) + samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers)) } for _, c := range s.cfg.BarCharts { cpt := barchart.NewBarChart(c, s.palette) - s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) + samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers)) } for _, c := range s.cfg.Gauges { cpt := gauge.NewGauge(c, s.palette) - s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers) + samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers)) } for _, c := range s.cfg.AsciiBoxes { cpt := asciibox.NewAsciiBox(c, s.palette) - s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) + samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers)) } for _, c := range s.cfg.TextBoxes { cpt := textbox.NewTextBox(c, s.palette) - s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) + samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers)) } + return samplers } -func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, componentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) { +func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, componentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) *data.Sampler { cpt := component.NewComponent(drawable, consumer, componentConfig) triggers := data.NewTriggers(triggersConfig, consumer, s.opt, s.player) items := data.NewItems(itemsConfig, *componentConfig.RateMs) - data.NewSampler(consumer, items, triggers, s.opt, s.cfg.Variables, *componentConfig.RateMs) s.lout.AddComponent(cpt) time.Sleep(10 * time.Millisecond) // desync coroutines + return data.NewSampler(consumer, items, triggers, s.opt, s.cfg.Variables, *componentConfig.RateMs) } func main() { @@ -103,9 +105,9 @@ func main() { metadata.PersistStatistics(cfg) starter := &Starter{player, lout, palette, opt, *cfg} - starter.startAll() + samplers := starter.startAll() - handler := event.NewHandler(lout, opt) + handler := event.NewHandler(samplers, opt, lout) handler.HandleEvents() }