stop all samplers on pause

This commit is contained in:
sqshq 2019-06-07 01:39:11 -04:00
parent ac75e7e891
commit 72665f55a4
4 changed files with 43 additions and 19 deletions

View File

@ -9,8 +9,8 @@ import (
)
const (
bindingsIndent = 3
pauseText = " P A U S E D "
bindingsIndent = 3
)
type StatusBar struct {

View File

@ -12,26 +12,30 @@ 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 {
if !sampler.pause {
go sampler.sample(item, options)
}
}
}
}()
go func() {
@ -39,10 +43,12 @@ func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options
select {
case sample := <-sampler.triggersChannel:
for _, t := range sampler.triggers {
if !sampler.pause {
t.Execute(sample)
}
}
}
}
}()
return sampler
@ -77,3 +83,7 @@ func mergeVariables(fileVariables map[string]string, optionsVariables []string)
return result
}
func (s *Sampler) Pause(pause bool) {
s.pause = pause
}

View File

@ -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)
}
}

24
main.go
View File

@ -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()
}