Skip to content

Commit

Permalink
added usage time statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
sqshq committed Jul 29, 2019
1 parent a506bb9 commit d7d3831
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
48 changes: 24 additions & 24 deletions component/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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() {
Expand Down Expand 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
}

Expand All @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions component/sparkline/sparkline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -108,7 +109,6 @@ func main() {
}
}

metadata.PersistStatistics(cfg)
starter := &Starter{player, lout, palette, opt, *cfg}
samplers := starter.startAll()

Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions metadata/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v3"
"log"
"runtime"
"time"
)

// Statistics represents anonymous usage data, which we collect for analyses and improvements
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -66,7 +69,6 @@ func PersistStatistics(config *config.Config) *Statistics {
return statistics
}

// GetStatistics from file
func GetStatistics(cfg *config.Config) *Statistics {

if !fileExists(statisticsFileName) {
Expand Down

0 comments on commit d7d3831

Please sign in to comment.