sampler-fork/metadata/statistics.go

82 lines
1.8 KiB
Go
Raw Normal View History

2019-05-24 02:58:46 +00:00
package metadata
import (
2019-05-24 02:58:46 +00:00
ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console"
"gopkg.in/yaml.v2"
"log"
"runtime"
)
type Statistics struct {
Version string
OS string
WindowWidth int `yaml:"ww"`
WindowHeight int `yaml:"wh"`
LaunchCount int `yaml:"lc"`
ComponentsCount map[string]int `yaml:"cc"`
}
const statisticsFileName = "statistics.yml"
2019-05-24 02:58:46 +00:00
func PersistStatistics(config *config.Config) *Statistics {
statistics := new(Statistics)
2019-05-24 02:58:46 +00:00
w, h := ui.TerminalDimensions()
if fileExists(statisticsFileName) {
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
err := yaml.Unmarshal(file, statistics)
2019-05-24 02:58:46 +00:00
if err != nil {
log.Fatalf("Failed to read statistics file: %v", err)
}
2019-05-24 02:58:46 +00:00
if config != nil {
statistics.ComponentsCount = countComponentsPerType(config)
}
statistics.WindowWidth = w
statistics.WindowWidth = h
statistics.LaunchCount += 1
2019-05-24 02:58:46 +00:00
} else {
statistics = &Statistics{
Version: console.AppVersion,
OS: runtime.GOOS,
LaunchCount: 1,
2019-05-24 02:58:46 +00:00
WindowWidth: w,
WindowHeight: h,
ComponentsCount: countComponentsPerType(config),
}
}
file, err := yaml.Marshal(statistics)
if err != nil {
log.Fatalf("Failed to marshal statistics file: %v", err)
}
saveStorageFile(file, statisticsFileName)
return statistics
}
2019-05-24 02:58:46 +00:00
func countComponentsPerType(config *config.Config) map[string]int {
m := make(map[string]int)
2019-05-24 02:58:46 +00:00
if config == nil {
return m
}
m["runcharts"] = len(config.RunCharts)
m["sparkLines"] = len(config.SparkLines)
m["barcharts"] = len(config.BarCharts)
m["gauges"] = len(config.Gauges)
m["asciiboxes"] = len(config.AsciiBoxes)
m["textboxes"] = len(config.TextBoxes)
2019-05-24 02:58:46 +00:00
return m
}