sampler-fork/config/default.go

248 lines
4.6 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-04-07 15:17:28 +00:00
defaultRateMs = 1000
defaultScale = 1
defaultTheme = console.ThemeDark
2019-02-01 00:02:34 +00:00
)
func (c *Config) setDefaults() {
c.setDefaultValues()
c.setDefaultItemSettings()
c.setDefaultArrangement()
}
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
2019-04-07 15:17:28 +00:00
if chart.RateMs == nil {
r := defaultRateMs
chart.RateMs = &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
2019-04-07 15:17:28 +00:00
if line.RateMs == nil {
r := defaultRateMs
line.RateMs = &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
2019-04-07 15:17:28 +00:00
if chart.RateMs == nil {
r := defaultRateMs
chart.RateMs = &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-04-07 15:17:28 +00:00
if g.RateMs == nil {
r := defaultRateMs
g.RateMs = &r
2019-02-26 04:36:23 +00:00
}
if g.Scale == nil {
p := defaultScale
g.Scale = &p
}
cur := "cur"
max := "max"
min := "min"
g.Cur.Label = &cur
g.Max.Label = &max
g.Min.Label = &min
2019-02-26 04:36:23 +00:00
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-04-07 15:17:28 +00:00
if box.RateMs == nil {
r := defaultRateMs
box.RateMs = &r
2019-02-17 23:00:00 +00:00
}
if box.Label == nil {
label := string(i)
box.Label = &label
}
if box.Font == nil {
2019-04-25 00:48:16 +00:00
font := console.AsciiFont2D
2019-02-17 23:00:00 +00:00
box.Font = &font
}
if box.Border == nil {
border := true
box.Border = &border
}
2019-02-17 23:00:00 +00:00
c.AsciiBoxes[i] = box
}
2019-04-10 02:30:21 +00:00
for i, box := range c.TextBoxes {
setDefaultTriggersValues(box.Triggers)
box.ComponentConfig.Type = TypeTextBox
if box.RateMs == nil {
r := defaultRateMs
box.RateMs = &r
}
if box.Label == nil {
label := string(i)
box.Label = &label
}
if box.Border == nil {
border := true
box.Border = &border
}
c.TextBoxes[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) setDefaultItemSettings() {
2019-02-01 00:02:34 +00:00
palette := console.GetPalette(*c.Theme)
colorsCount := len(palette.ContentColors)
defaultPty := false
defaultPercentOnly := false
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]
2019-02-01 00:02:34 +00:00
}
if item.Pty == nil {
item.Pty = &defaultPty
}
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]
}
if item.Pty == nil {
item.Pty = &defaultPty
}
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 {
2019-04-01 04:35:55 +00:00
s.Gradient = &palette.GradientColors[i%(len(palette.GradientColors))]
if s.Item.Pty == nil {
s.Item.Pty = &defaultPty
}
2019-04-01 04:35:55 +00:00
c.SparkLines[i] = s
2019-03-26 03:29:23 +00:00
}
2019-02-26 04:36:23 +00:00
for i, g := range c.Gauges {
if g.Min.Pty == nil {
g.Min.Pty = &defaultPty
}
if g.Max.Pty == nil {
g.Max.Pty = &defaultPty
}
if g.Cur.Pty == nil {
g.Cur.Pty = &defaultPty
}
2019-02-26 04:36:23 +00:00
if g.Color == nil {
g.Color = &palette.ContentColors[i%colorsCount]
2019-02-26 04:36:23 +00:00
}
if g.PercentOnly == nil {
g.PercentOnly = &defaultPercentOnly
}
c.Gauges[i] = g
}
for i, a := range c.AsciiBoxes {
if a.Item.Pty == nil {
a.Item.Pty = &defaultPty
}
c.AsciiBoxes[i] = a
}
for i, t := range c.TextBoxes {
if t.Item.Pty == nil {
t.Item.Pty = &defaultPty
}
c.TextBoxes[i] = t
2019-02-26 04:36:23 +00:00
}
2019-02-01 00:02:34 +00:00
}