sampler-fork/metadata/statistics.go

106 lines
2.5 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"
)
// Anonymous usage data, which we collect for analyses and improvements
// User can disable it, along with crash reports, using --telemetry flag
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.WindowHeight = 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),
}
initStorage()
}
file, err := yaml.Marshal(statistics)
if err != nil {
log.Fatalf("Failed to marshal statistics file: %v", err)
}
saveStorageFile(file, statisticsFileName)
return statistics
}
func GetStatistics(cfg *config.Config) *Statistics {
if !fileExists(statisticsFileName) {
return &Statistics{
Version: console.AppVersion,
OS: runtime.GOOS,
LaunchCount: 0,
WindowWidth: 0,
WindowHeight: 0,
ComponentsCount: countComponentsPerType(cfg),
}
} else {
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
license := new(Statistics)
err := yaml.Unmarshal(file, license)
if err != nil {
log.Fatalf("Failed to read statistics file: %v", err)
}
return license
}
}
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
}