2019-05-24 02:58:46 +00:00
|
|
|
package metadata
|
2019-05-21 04:41:03 +00:00
|
|
|
|
|
|
|
import (
|
2019-05-24 02:58:46 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2019-05-21 04:41:03 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
|
|
|
"github.com/sqshq/sampler/console"
|
2019-06-21 04:45:32 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-05-21 04:41:03 +00:00
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
// Anonymous usage data, which we collect for analyses and improvements
|
|
|
|
// User can disable it, along with crash reports, using --telemetry flag
|
2019-05-21 04:41:03 +00:00
|
|
|
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 {
|
2019-05-21 04:41:03 +00:00
|
|
|
|
|
|
|
statistics := new(Statistics)
|
2019-05-24 02:58:46 +00:00
|
|
|
w, h := ui.TerminalDimensions()
|
2019-05-21 04:41:03 +00:00
|
|
|
|
|
|
|
if fileExists(statisticsFileName) {
|
|
|
|
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
|
|
|
|
err := yaml.Unmarshal(file, statistics)
|
2019-05-24 02:58:46 +00:00
|
|
|
|
2019-05-21 04:41:03 +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
|
2019-05-28 01:44:06 +00:00
|
|
|
statistics.WindowHeight = h
|
2019-05-21 04:41:03 +00:00
|
|
|
statistics.LaunchCount += 1
|
2019-05-24 02:58:46 +00:00
|
|
|
|
2019-05-21 04:41:03 +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,
|
2019-05-21 04:41:03 +00:00
|
|
|
ComponentsCount: countComponentsPerType(config),
|
|
|
|
}
|
2019-05-28 01:44:06 +00:00
|
|
|
initStorage()
|
2019-05-21 04:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := yaml.Marshal(statistics)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to marshal statistics file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
saveStorageFile(file, statisticsFileName)
|
|
|
|
|
|
|
|
return statistics
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
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 {
|
|
|
|
|
2019-05-21 04:41:03 +00:00
|
|
|
m := make(map[string]int)
|
2019-05-24 02:58:46 +00:00
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-05-21 04:41:03 +00:00
|
|
|
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
|
|
|
|
2019-05-21 04:41:03 +00:00
|
|
|
return m
|
|
|
|
}
|