From 6bc8b580cb0e22a6876f433fcc240980fafcbb66 Mon Sep 17 00:00:00 2001 From: sqshq Date: Tue, 21 May 2019 00:41:03 -0400 Subject: [PATCH] added anonymous statistics generation and save --- config/options.go | 2 +- main.go | 2 +- storage/license.go | 4 --- storage/statistics.go | 66 +++++++++++++++++++++++++++++++++++++++++++ storage/stats.go | 8 ------ storage/storage.go | 2 +- 6 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 storage/statistics.go delete mode 100644 storage/stats.go diff --git a/config/options.go b/config/options.go index 3ef033d..c315126 100644 --- a/config/options.go +++ b/config/options.go @@ -3,5 +3,5 @@ package config type Options struct { ConfigFile string `short:"c" long:"config" required:"true" description:"path to YAML config file"` Variables []string `short:"v" long:"variable" required:"false" description:"specify name=value variable to use in script placeholder as $name. This flag takes precedence over the same name variables, specified in config yml" long-description:"one or more variables can be specified as flags, in order to replace repeated patterns in the scripts, which can be replaced with {$variable-name} placeholder" ` - License []string `short:"l" long:"license" required:"false" description:"provide license key. see www.sampler.dev for details"` + License []string `short:"l" long:"license" required:"false" description:"provide license key. visit www.sampler.dev for details"` } diff --git a/main.go b/main.go index 779f95f..3780e8a 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ func main() { width, height := ui.TerminalDimensions() license := storage.GetLicense() - // TODO storage.UpdateStats() + _ = storage.UpdateStatistics(cfg, width, height) lout := layout.NewLayout(width, height, component.NewStatusLine(opt.ConfigFile, palette, license), component.NewMenu(palette), component.NewIntro(palette)) starter := &Starter{lout, player, opt, cfg} diff --git a/storage/license.go b/storage/license.go index 8bcb8de..391c1af 100644 --- a/storage/license.go +++ b/storage/license.go @@ -47,7 +47,3 @@ func InitLicense() { initStorage() saveStorageFile(file, getPlatformStoragePath(licenseFileName)) } - -func SaveLicense() { - // TODO -} diff --git a/storage/statistics.go b/storage/statistics.go new file mode 100644 index 0000000..2142805 --- /dev/null +++ b/storage/statistics.go @@ -0,0 +1,66 @@ +package storage + +import ( + "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" + +func UpdateStatistics(config config.Config, width int, height int) *Statistics { + + statistics := new(Statistics) + + if fileExists(statisticsFileName) { + file := readStorageFile(getPlatformStoragePath(statisticsFileName)) + err := yaml.Unmarshal(file, statistics) + if err != nil { + log.Fatalf("Failed to read statistics file: %v", err) + } + statistics.ComponentsCount = countComponentsPerType(config) + statistics.WindowWidth = width + statistics.WindowWidth = height + statistics.LaunchCount += 1 + } else { + statistics = &Statistics{ + Version: console.AppVersion, + OS: runtime.GOOS, + LaunchCount: 1, + WindowWidth: width, + WindowHeight: height, + 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 +} + +func countComponentsPerType(config config.Config) map[string]int { + m := make(map[string]int) + 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) + return m +} diff --git a/storage/stats.go b/storage/stats.go deleted file mode 100644 index 962dcf1..0000000 --- a/storage/stats.go +++ /dev/null @@ -1,8 +0,0 @@ -package storage - -// TODO -// version -// launch count -// components count by type -// OS -// window size diff --git a/storage/storage.go b/storage/storage.go index 2a6a2fb..acbe214 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -49,7 +49,7 @@ func readStorageFile(path string) []byte { } func saveStorageFile(file []byte, fileName string) { - err := ioutil.WriteFile(fileName, file, os.ModePerm) + err := ioutil.WriteFile(getPlatformStoragePath(fileName), file, os.ModePerm) if err != nil { log.Fatalf("Failed to save the storage file: %v", err) }