sampler-fork/config/default.go

189 lines
3.7 KiB
Go
Raw Normal View History

2019-02-01 00:02:34 +00:00
package config
import (
2019-02-02 14:45:53 +00:00
"github.com/sqshq/sampler/console"
)
2019-02-01 00:02:34 +00:00
const (
2019-02-25 00:08:36 +00:00
defaultRefreshRateMs = 1000
2019-02-19 04:07:32 +00:00
defaultScale = 1
defaultTheme = console.ThemeDark
2019-02-01 00:02:34 +00:00
)
func (c *Config) setDefaults() {
c.setDefaultValues()
c.setDefaultColors()
c.setDefaultLayout()
}
func (c *Config) setDefaultValues() {
2019-02-01 00:02:34 +00:00
if c.Theme == nil {
t := defaultTheme
c.Theme = &t
2019-02-01 00:02:34 +00:00
}
for i, chart := range c.RunCharts {
2019-03-03 23:35:32 +00:00
setDefaultTriggersValues(chart.Triggers)
2019-03-17 23:55:24 +00:00
chart.ComponentConfig.Type = TypeRunChart
2019-03-03 23:35:32 +00:00
if chart.RefreshRateMs == nil {
r := defaultRefreshRateMs
chart.RefreshRateMs = &r
}
2019-02-19 04:07:32 +00:00
if chart.Scale == nil {
p := defaultScale
chart.Scale = &p
2019-02-01 00:02:34 +00:00
}
if chart.Legend == nil {
chart.Legend = &LegendConfig{true, true}
c.RunCharts[i] = chart
2019-02-04 04:03:59 +00:00
}
c.RunCharts[i] = chart
2019-02-01 00:02:34 +00:00
}
2019-02-17 23:00:00 +00:00
for i, line := range c.SparkLines {
setDefaultTriggersValues(line.Triggers)
line.ComponentConfig.Type = TypeSparkLine
line.Item.Label = &line.Title
if line.RefreshRateMs == nil {
r := defaultRefreshRateMs
line.RefreshRateMs = &r
}
if line.Scale == nil {
p := defaultScale
line.Scale = &p
}
c.SparkLines[i] = line
}
for i, chart := range c.BarCharts {
2019-03-03 23:35:32 +00:00
setDefaultTriggersValues(chart.Triggers)
2019-03-17 23:55:24 +00:00
chart.ComponentConfig.Type = TypeBarChart
2019-03-03 23:35:32 +00:00
if chart.RefreshRateMs == nil {
r := defaultRefreshRateMs
chart.RefreshRateMs = &r
}
if chart.Scale == nil {
p := defaultScale
chart.Scale = &p
}
c.BarCharts[i] = chart
}
2019-02-26 04:36:23 +00:00
for i, g := range c.Gauges {
2019-03-03 23:35:32 +00:00
setDefaultTriggersValues(g.Triggers)
2019-03-17 23:55:24 +00:00
g.ComponentConfig.Type = TypeGauge
2019-03-03 23:35:32 +00:00
2019-02-26 04:36:23 +00:00
if g.RefreshRateMs == nil {
r := defaultRefreshRateMs
g.RefreshRateMs = &r
}
if g.Scale == nil {
p := defaultScale
g.Scale = &p
}
2019-03-08 04:04:46 +00:00
var items []Item
2019-02-26 04:36:23 +00:00
for label, script := range g.Values {
l := label
2019-03-08 04:04:46 +00:00
items = append(items, Item{Label: &l, Script: script})
2019-02-26 04:36:23 +00:00
}
g.Items = items
c.Gauges[i] = g
}
2019-02-17 23:00:00 +00:00
for i, box := range c.AsciiBoxes {
2019-03-03 23:35:32 +00:00
setDefaultTriggersValues(box.Triggers)
2019-03-17 23:55:24 +00:00
box.ComponentConfig.Type = TypeAsciiBox
2019-03-03 23:35:32 +00:00
2019-02-17 23:00:00 +00:00
if box.RefreshRateMs == nil {
r := defaultRefreshRateMs
box.RefreshRateMs = &r
}
if box.Label == nil {
label := string(i)
box.Label = &label
}
if box.Font == nil {
2019-03-08 04:04:46 +00:00
font := console.AsciiFontFlat
2019-02-17 23:00:00 +00:00
box.Font = &font
}
c.AsciiBoxes[i] = box
}
2019-02-01 00:02:34 +00:00
}
2019-03-03 23:35:32 +00:00
func setDefaultTriggersValues(triggers []TriggerConfig) {
defaultTerminalBell := false
2019-03-03 23:35:32 +00:00
defaultSound := false
defaultVisual := false
2019-03-03 23:35:32 +00:00
for i, trigger := range triggers {
if trigger.Actions == nil {
trigger.Actions = &ActionsConfig{TerminalBell: &defaultTerminalBell, Sound: &defaultSound, Visual: &defaultVisual, Script: nil}
} else {
if trigger.Actions.TerminalBell == nil {
trigger.Actions.TerminalBell = &defaultTerminalBell
}
if trigger.Actions.Sound == nil {
trigger.Actions.Sound = &defaultSound
}
if trigger.Actions.Visual == nil {
trigger.Actions.Visual = &defaultVisual
}
}
triggers[i] = trigger
}
}
func (c *Config) setDefaultLayout() {
// TODO auto-arrange components
2019-02-01 00:02:34 +00:00
}
func (c *Config) setDefaultColors() {
2019-02-01 00:02:34 +00:00
palette := console.GetPalette(*c.Theme)
colorsCount := len(palette.ContentColors)
2019-02-01 00:02:34 +00:00
for _, ch := range c.RunCharts {
for j, item := range ch.Items {
if item.Color == nil {
item.Color = &palette.ContentColors[j%colorsCount]
ch.Items[j] = item
2019-02-01 00:02:34 +00:00
}
}
}
for _, b := range c.BarCharts {
for j, item := range b.Items {
if item.Color == nil {
item.Color = &palette.ContentColors[j%colorsCount]
b.Items[j] = item
}
}
}
2019-02-26 04:36:23 +00:00
2019-03-26 03:29:23 +00:00
for i, s := range c.SparkLines {
if s.Item.Color == nil {
s.Item.Color = &palette.ContentColors[i%colorsCount]
c.SparkLines[i] = s
}
}
2019-02-26 04:36:23 +00:00
for i, g := range c.Gauges {
if g.Color == nil {
g.Color = &palette.ContentColors[i%colorsCount]
2019-02-26 04:36:23 +00:00
c.Gauges[i] = g
}
}
2019-02-01 00:02:34 +00:00
}