From d7d3831d7d7b4c584c963c60807cc043fd5a0d51 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 28 Jul 2019 20:27:14 -0400 Subject: [PATCH] added usage time statistics --- component/menu.go | 48 ++++++++++++++++---------------- component/sparkline/sparkline.go | 4 +-- main.go | 6 +++- metadata/statistics.go | 10 ++++--- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/component/menu.go b/component/menu.go index cd06aaa..7a8b943 100644 --- a/component/menu.go +++ b/component/menu.go @@ -10,29 +10,29 @@ import ( type Menu struct { *ui.Block - options []MenuOption + options []menuOption component Component - mode MenuMode - option MenuOption + mode menuMode + option menuOption palette console.Palette } -type MenuMode rune +type menuMode rune const ( - MenuModeIdle MenuMode = 0 - MenuModeHighlight MenuMode = 1 - MenuModeOptionSelect MenuMode = 2 - MenuModeMoveAndResize MenuMode = 3 + menuModeIdle menuMode = 0 + menuModeHighlight menuMode = 1 + menuModeOptionSelect menuMode = 2 + menuModeMoveAndResize menuMode = 3 ) -type MenuOption string +type menuOption string const ( - MenuOptionMove MenuOption = "MOVE" - MenuOptionResize MenuOption = "RESIZE" - MenuOptionPinpoint MenuOption = "PINPOINT" - MenuOptionResume MenuOption = "RESUME" + MenuOptionMove menuOption = "MOVE" + MenuOptionResize menuOption = "RESIZE" + MenuOptionPinpoint menuOption = "PINPOINT" + MenuOptionResume menuOption = "RESUME" ) const ( @@ -42,30 +42,30 @@ const ( func NewMenu(palette console.Palette) *Menu { return &Menu{ Block: NewBlock("", true, palette), - options: []MenuOption{MenuOptionMove, MenuOptionResize, MenuOptionPinpoint, MenuOptionResume}, - mode: MenuModeIdle, + options: []menuOption{MenuOptionMove, MenuOptionResize, MenuOptionPinpoint, MenuOptionResume}, + mode: menuModeIdle, option: MenuOptionMove, palette: palette, } } -func (m *Menu) GetSelectedOption() MenuOption { +func (m *Menu) GetSelectedOption() menuOption { return m.option } func (m *Menu) Highlight(component *Component) { m.component = *component m.updateDimensions() - m.mode = MenuModeHighlight + m.mode = menuModeHighlight m.Title = component.Title } func (m *Menu) Choose() { - m.mode = MenuModeOptionSelect + m.mode = menuModeOptionSelect } func (m *Menu) Idle() { - m.mode = MenuModeIdle + m.mode = menuModeIdle } func (m *Menu) Up() { @@ -93,12 +93,12 @@ func (m *Menu) Down() { } func (m *Menu) MoveOrResize() { - m.mode = MenuModeMoveAndResize + m.mode = menuModeMoveAndResize } func (m *Menu) Draw(buffer *ui.Buffer) { - if m.mode == MenuModeIdle { + if m.mode == menuModeIdle { return } @@ -112,11 +112,11 @@ func (m *Menu) Draw(buffer *ui.Buffer) { m.Block.Draw(buffer) switch m.mode { - case MenuModeHighlight: + case menuModeHighlight: m.renderHighlight(buffer) - case MenuModeMoveAndResize: + case menuModeMoveAndResize: m.renderMoveAndResize(buffer) - case MenuModeOptionSelect: + case menuModeOptionSelect: m.renderOptions(buffer) } } diff --git a/component/sparkline/sparkline.go b/component/sparkline/sparkline.go index 27428a3..eaca2cb 100644 --- a/component/sparkline/sparkline.go +++ b/component/sparkline/sparkline.go @@ -56,10 +56,10 @@ func (s *SparkLine) consumeSample(sample *data.Sample) { if err != nil { s.HandleConsumeFailure("Failed to parse a number", err, sample) return - } else { - s.HandleConsumeSuccess() } + s.HandleConsumeSuccess() + s.values = append(s.values, float) max, min := s.values[0], s.values[0] diff --git a/main.go b/main.go index 2245814..ecb7609 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,7 @@ func main() { license := metadata.GetLicense() defer handleCrash(statistics, opt, bc) + defer updateStatistics(cfg, time.Now()) if opt.LicenseKey != nil { registerLicense(statistics, opt, bc) @@ -108,7 +109,6 @@ func main() { } } - metadata.PersistStatistics(cfg) starter := &Starter{player, lout, palette, opt, *cfg} samplers := starter.startAll() @@ -126,6 +126,10 @@ func handleCrash(statistics *metadata.Statistics, opt config.Options, bc *client } } +func updateStatistics(cfg *config.Config, startTime time.Time) { + metadata.PersistStatistics(cfg, time.Since(startTime)) +} + func registerLicense(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) { lc, err := bc.RegisterLicenseKey(*opt.LicenseKey, statistics) if err != nil { diff --git a/metadata/statistics.go b/metadata/statistics.go index 1662793..2007aa6 100644 --- a/metadata/statistics.go +++ b/metadata/statistics.go @@ -7,6 +7,7 @@ import ( "gopkg.in/yaml.v3" "log" "runtime" + "time" ) // Statistics represents anonymous usage data, which we collect for analyses and improvements @@ -17,13 +18,13 @@ type Statistics struct { WindowWidth int `yaml:"ww"` WindowHeight int `yaml:"wh"` LaunchCount int `yaml:"lc"` + UsageTime int `yaml:"ut"` ComponentsCount map[string]int `yaml:"cc"` } const statisticsFileName = "statistics.yml" -// PersistStatistics in file -func PersistStatistics(config *config.Config) *Statistics { +func PersistStatistics(config *config.Config, uptime time.Duration) *Statistics { statistics := new(Statistics) w, h := ui.TerminalDimensions() @@ -43,14 +44,16 @@ func PersistStatistics(config *config.Config) *Statistics { statistics.WindowWidth = w statistics.WindowHeight = h statistics.LaunchCount += 1 + statistics.UsageTime += int(uptime.Seconds()) } else { statistics = &Statistics{ Version: console.AppVersion, OS: runtime.GOOS, - LaunchCount: 1, WindowWidth: w, WindowHeight: h, + LaunchCount: 1, + UsageTime: 0, ComponentsCount: countComponentsPerType(config), } initStorage() @@ -66,7 +69,6 @@ func PersistStatistics(config *config.Config) *Statistics { return statistics } -// GetStatistics from file func GetStatistics(cfg *config.Config) *Statistics { if !fileExists(statisticsFileName) {