-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstats.go
More file actions
145 lines (119 loc) · 3.01 KB
/
Copy pathstats.go
File metadata and controls
145 lines (119 loc) · 3.01 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"os"
"sync/atomic"
"time"
log "unknwon.dev/clog/v2"
)
// NOTE: all int64 must be manipulated atomically.
type stats struct {
totalView int64
totalGet int64
pkgsView map[string]*int64
pkgsGet map[string]*int64
lastUpdated int64
lastSynced int64
}
func (s *stats) TotalView() int64 {
return atomic.LoadInt64(&s.totalView)
}
func (s *stats) TotalGet() int64 {
return atomic.LoadInt64(&s.totalGet)
}
func (s *stats) PkgView(improtPath string) int64 {
return atomic.LoadInt64(s.pkgsView[improtPath])
}
func (s *stats) PkgGet(improtPath string) int64 {
return atomic.LoadInt64(s.pkgsGet[improtPath])
}
func (s *stats) PkgViewIncr(improtPath string, n int64) {
atomic.AddInt64(s.pkgsView[improtPath], n)
atomic.AddInt64(&s.totalView, n)
atomic.StoreInt64(&s.lastUpdated, time.Now().Unix())
}
func (s *stats) PkgGetIncr(improtPath string, n int64) {
atomic.AddInt64(s.pkgsGet[improtPath], n)
atomic.AddInt64(&s.totalGet, n)
atomic.StoreInt64(&s.lastUpdated, time.Now().Unix())
}
// statsData is the structure for JSON serialization
type statsData struct {
TotalView int64 `json:"total_view"`
TotalGet int64 `json:"total_get"`
PkgsView map[string]int64 `json:"pkgs_view"`
PkgsGet map[string]int64 `json:"pkgs_get"`
}
// NOTE: atomic operation is not needed in this method since it is currently only
// being called at init time.
func (s *stats) loadFromJSON(path string) error {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// File doesn't exist yet, which is fine
return nil
}
return err
}
var sd statsData
if err := json.Unmarshal(data, &sd); err != nil {
return err
}
s.totalView = sd.TotalView
s.totalGet = sd.TotalGet
for importPath, view := range sd.PkgsView {
v := view
s.pkgsView[importPath] = &v
}
for importPath, get := range sd.PkgsGet {
g := get
s.pkgsGet[importPath] = &g
}
return nil
}
func (s *stats) start(path string, done chan struct{}) {
defer func() {
log.Info("Exiting stats syncing goroutine...")
done <- struct{}{}
}()
t := time.NewTicker(time.Minute)
for {
select {
case <-t.C:
s.syncToJSON(path)
case <-done:
s.syncToJSON(path)
return
}
}
}
func (s *stats) syncToJSON(path string) {
lastSynced := atomic.LoadInt64(&s.lastSynced)
lastUpdated := atomic.LoadInt64(&s.lastUpdated)
if lastSynced == lastUpdated {
log.Trace("stats.syncToJSON: nothing changed, file is up-to-date")
return
}
sd := statsData{
TotalView: s.TotalView(),
TotalGet: s.TotalGet(),
PkgsView: make(map[string]int64),
PkgsGet: make(map[string]int64),
}
for p := range s.pkgsView {
sd.PkgsView[p] = s.PkgView(p)
}
for p := range s.pkgsGet {
sd.PkgsGet[p] = s.PkgGet(p)
}
data, err := json.MarshalIndent(sd, "", " ")
if err != nil {
log.Error("Failed to marshal stats: %v", err)
return
}
if err := os.WriteFile(path, data, 0644); err != nil {
log.Error("Failed to write stats file: %v", err)
return
}
atomic.StoreInt64(&s.lastSynced, lastUpdated)
}