-
-
Notifications
You must be signed in to change notification settings - Fork 544
/
statistics.go
113 lines (90 loc) · 2.67 KB
/
statistics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package metadata
import (
ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console"
"gopkg.in/yaml.v3"
"log"
"runtime"
"time"
)
// Statistics represents anonymous usage data, which we collect for analyses and improvements
// User can disable it, along with crash reports, using --disable-telemetry flag
type Statistics struct {
Version string
OS string
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"
func PersistStatistics(config *config.Config, uptime time.Duration) *Statistics {
statistics := new(Statistics)
w, h := ui.TerminalDimensions()
if fileExists(statisticsFileName) {
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
err := yaml.Unmarshal(file, statistics)
if err != nil {
log.Fatalf("Failed to read statistics file: %v", err)
}
if config != nil {
statistics.ComponentsCount = countComponentsPerType(config)
}
statistics.Version = console.AppVersion
statistics.WindowWidth = w
statistics.WindowHeight = h
statistics.LaunchCount += 1
statistics.UsageTime += int(uptime.Seconds())
} else {
statistics = &Statistics{
Version: console.AppVersion,
OS: runtime.GOOS,
WindowWidth: w,
WindowHeight: h,
LaunchCount: 1,
UsageTime: 0,
ComponentsCount: countComponentsPerType(config),
}
initStorage()
}
file, err := yaml.Marshal(statistics)
if err != nil {
log.Fatalf("Failed to marshal statistics file: %v", err)
}
saveStorageFile(file, statisticsFileName)
return statistics
}
func GetStatistics(cfg *config.Config) *Statistics {
if !fileExists(statisticsFileName) {
return &Statistics{
Version: console.AppVersion,
OS: runtime.GOOS,
LaunchCount: 0,
WindowWidth: 0,
WindowHeight: 0,
ComponentsCount: countComponentsPerType(cfg),
}
}
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
license := new(Statistics)
err := yaml.Unmarshal(file, license)
if err != nil {
log.Fatalf("Failed to read statistics file: %v", err)
}
return license
}
func countComponentsPerType(config *config.Config) map[string]int {
m := make(map[string]int)
if config == nil {
return m
}
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
}