added usage time statistics

This commit is contained in:
sqshq 2019-07-28 20:27:14 -04:00
parent a506bb92eb
commit d7d3831d7d
4 changed files with 37 additions and 31 deletions

View File

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

View File

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

View File

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

View File

@ -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) {