added anonymous statistics generation and save

This commit is contained in:
sqshq 2019-05-21 00:41:03 -04:00
parent 334c39da64
commit 6bc8b580cb
6 changed files with 69 additions and 15 deletions

View File

@ -3,5 +3,5 @@ package config
type Options struct { type Options struct {
ConfigFile string `short:"c" long:"config" required:"true" description:"path to YAML config file"` 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" ` 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"`
} }

View File

@ -49,7 +49,7 @@ func main() {
width, height := ui.TerminalDimensions() width, height := ui.TerminalDimensions()
license := storage.GetLicense() 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)) lout := layout.NewLayout(width, height, component.NewStatusLine(opt.ConfigFile, palette, license), component.NewMenu(palette), component.NewIntro(palette))
starter := &Starter{lout, player, opt, cfg} starter := &Starter{lout, player, opt, cfg}

View File

@ -47,7 +47,3 @@ func InitLicense() {
initStorage() initStorage()
saveStorageFile(file, getPlatformStoragePath(licenseFileName)) saveStorageFile(file, getPlatformStoragePath(licenseFileName))
} }
func SaveLicense() {
// TODO
}

66
storage/statistics.go Normal file
View File

@ -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
}

View File

@ -1,8 +0,0 @@
package storage
// TODO
// version
// launch count
// components count by type
// OS
// window size

View File

@ -49,7 +49,7 @@ func readStorageFile(path string) []byte {
} }
func saveStorageFile(file []byte, fileName string) { func saveStorageFile(file []byte, fileName string) {
err := ioutil.WriteFile(fileName, file, os.ModePerm) err := ioutil.WriteFile(getPlatformStoragePath(fileName), file, os.ModePerm)
if err != nil { if err != nil {
log.Fatalf("Failed to save the storage file: %v", err) log.Fatalf("Failed to save the storage file: %v", err)
} }