/
stats.go
282 lines (264 loc) · 8.12 KB
/
stats.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package stats
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"sort"
"sync"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/sagan/ptool/util"
)
type TorrentTraffic struct {
Client string `gorm:"primaryKey"`
Day string `gorm:"primaryKey"`
Site string `gorm:"primaryKey"`
Downloaded int64
Uploaded int64
}
type TorrentStat struct {
Client string `json:"client"`
Site string `json:"site"`
Category string `json:"category"`
InfoHash string `json:"infoHash"`
Name string `json:"name"`
Size int64 `json:"size"`
Atime int64 `json:"atime"`
Uploaded int64 `json:"uploaded"`
Downloaded int64 `json:"downloaded"`
Msg string `json:"msg"`
}
type Stat struct {
Ts int64 `json:"ts"`
Event int64 `json:"event"`
Data *TorrentStat `json:"data"`
}
type Statistics struct {
Downloaded int64
Uploaded int64
}
type StatDb struct {
statFilename string
file *os.File
mu sync.Mutex
sqldb *gorm.DB
}
func (db *StatDb) AddTorrentStat(ts int64, event int64, torrentStat *TorrentStat) {
buf := bytes.NewBuffer(nil)
json.NewEncoder(buf).Encode(Stat{
Ts: ts,
Event: event,
Data: torrentStat,
})
db.mu.Lock()
db.file.Write(buf.Bytes())
db.mu.Unlock()
}
func (db *StatDb) ShowTrafficStats(client string) {
now := util.Now()
today := util.FormatDate(now)
yesterday := util.FormatDate(now - 86400)
yesterdayMinus7day := util.FormatDate(now - 86400*8)
yesterdayMinus30day := util.FormatDate(now - 86400*31)
timespans := []struct {
name string
startday string
endday string
}{
{"<all time>", "", ""},
{"last 30d", yesterdayMinus30day, yesterday},
{"last 7d", yesterdayMinus7day, yesterday},
{"yesterday", yesterday, yesterday},
{"today", today, today},
}
var clientObjs []TorrentTraffic
db.sqldb.Distinct("client").Order("day desc").Limit(1000).Find(&clientObjs)
sort.Slice(clientObjs, func(i, j int) bool {
if clientObjs[i].Uploaded != clientObjs[j].Uploaded {
return clientObjs[i].Uploaded > clientObjs[j].Uploaded
}
return clientObjs[i].Downloaded+clientObjs[i].Uploaded > clientObjs[j].Downloaded+clientObjs[j].Uploaded
})
if len(clientObjs) > 3 {
clientObjs = clientObjs[:3]
}
clients := util.Map(clientObjs, func(c TorrentTraffic) string {
return c.Client
})
if client == "" {
fmt.Printf("%-15s ", `time\clients`)
for _, client := range clients {
fmt.Printf("%20s / ", client+"(↓, ↑)")
}
fmt.Printf("%20s\n", "<all>")
for _, timespan := range timespans {
records := []TorrentTraffic{}
tx := db.sqldb.Table("torrent_traffics").
Select("client", "ifnull(sum(downloaded),0) as downloaded", "ifnull(sum(uploaded),0) as uploaded").Group("client")
if timespan.startday != "" {
tx = tx.Where("day >= ?", timespan.startday)
}
if timespan.endday != "" {
tx = tx.Where("day <= ?", timespan.endday)
}
tx.Find(&records)
allDownloaded := int64(0)
allUploaded := int64(0)
clientsDownloaded := map[string]int64{}
clientsUploaded := map[string]int64{}
for _, record := range records {
if record.Client != "" {
clientsDownloaded[record.Client] = record.Downloaded
clientsUploaded[record.Client] = record.Uploaded
}
allDownloaded += record.Downloaded
allUploaded += record.Uploaded
}
fmt.Printf("%-15s ", timespan.name)
for _, client := range clients {
fmt.Printf("%20s / ", "↓"+util.BytesSize(float64(clientsDownloaded[client]))+", ↑"+util.BytesSize(float64(clientsUploaded[client])))
}
fmt.Printf("%20s\n", "↓"+util.BytesSize(float64(allDownloaded))+", ↑"+util.BytesSize(float64(allUploaded)))
}
return
}
var siteObjs []TorrentTraffic
db.sqldb.Distinct("site").Where("site != ?", "").Order("day desc").Limit(1000).Find(&siteObjs)
if len(siteObjs) > 3 {
siteObjs = siteObjs[:3]
}
sort.Slice(siteObjs, func(i, j int) bool {
if siteObjs[i].Uploaded != siteObjs[j].Uploaded {
return siteObjs[i].Uploaded > siteObjs[j].Uploaded
}
return siteObjs[i].Downloaded+siteObjs[i].Uploaded > siteObjs[j].Downloaded+siteObjs[j].Uploaded
})
sites := util.Map(siteObjs, func(c TorrentTraffic) string {
return c.Site
})
fmt.Printf("%-15s ", client+`\sites`)
for _, site := range sites {
fmt.Printf("%20s / ", site+"(↓, ↑)")
}
fmt.Printf("%20s\n", "<all>")
for _, timespan := range timespans {
records := []TorrentTraffic{}
tx := db.sqldb.Table("torrent_traffics").
Select("site", "ifnull(sum(downloaded),0) downloaded", "ifnull(sum(uploaded),0) uploaded").
Group("site").
Where("client = ?", client)
if timespan.startday != "" {
tx = tx.Where("day >= ?", timespan.startday)
}
if timespan.endday != "" {
tx = tx.Where("day <= ?", timespan.endday)
}
tx.Find(&records)
allDownloaded := int64(0)
allUploaded := int64(0)
sitesDownloaded := map[string]int64{}
siteUploaded := map[string]int64{}
for _, record := range records {
if record.Site != "" {
sitesDownloaded[record.Site] = record.Downloaded
siteUploaded[record.Site] = record.Uploaded
}
allDownloaded += record.Downloaded
allUploaded += record.Uploaded
}
fmt.Printf("%-15s ", timespan.name)
for _, site := range sites {
fmt.Printf("%20s / ", "↓"+util.BytesSize(float64(sitesDownloaded[site]))+", ↑"+util.BytesSize(float64(siteUploaded[site])))
}
fmt.Printf("%20s\n", "↓"+util.BytesSize(float64(allDownloaded))+", ↑"+util.BytesSize(float64(allUploaded)))
}
}
func NewDb(statFilename string) (*StatDb, error) {
db := &StatDb{}
f, err := os.OpenFile(statFilename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("failed to open stats file %s: %v", statFilename, err)
}
db.statFilename = statFilename
db.file = f
sqldb, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("error create stats sqldb: %v", err)
}
err = sqldb.AutoMigrate(&TorrentTraffic{})
if err != nil {
return nil, fmt.Errorf("sql schema init error: %v", err)
}
db.sqldb = sqldb
db.file.Seek(0, 0)
fileScanner := bufio.NewScanner(db.file)
fileScanner.Split(bufio.ScanLines)
flagMap := map[string]bool{}
for fileScanner.Scan() {
statRecord := Stat{}
err := json.Unmarshal([]byte(fileScanner.Text()), &statRecord)
if err != nil || statRecord.Event != 1 {
continue
}
timespan := statRecord.Ts - statRecord.Data.Atime
if timespan == 0 {
continue // just skip it
}
id := fmt.Sprint(statRecord.Data.Client, statRecord.Data.InfoHash, statRecord.Data.Atime)
if flagMap[id] {
continue // duplicate records
}
flagMap[id] = true
aDownloadSpeed := statRecord.Data.Downloaded / timespan
aUploadSpeed := statRecord.Data.Uploaded / timespan
dailyDownloaded := 86400 * aDownloadSpeed
dailyUploaded := 86400 * aUploadSpeed
time := statRecord.Data.Atime
day := util.FormatDate(time)
nexydayTime, _ := util.ParseLocalDateTime(day)
nexydayTime += 86400
for statRecord.Ts > time {
isFullDay := true
time2 := nexydayTime
if time2 > statRecord.Ts {
time2 = statRecord.Ts
isFullDay = false
} else if time == statRecord.Data.Atime {
isFullDay = false
}
downloaded := int64(0)
uploaded := int64(0)
if isFullDay {
downloaded = dailyDownloaded
uploaded = dailyUploaded
} else {
downloaded = (time2 - time) * aDownloadSpeed
uploaded = (time2 - time) * aUploadSpeed
}
// INSERT INTO torrent_traffics (client, day, site, downloaded, uploaded) VALUES (?,?,?,?,?)
// ON CONFLICT(client, day, site) DO UPDATE SET downloaded = downloaded + ?, uploaded = uploaded + ?;
db.sqldb.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "client"}, {Name: "day"}, {Name: "site"}},
DoUpdates: clause.Assignments(map[string]interface{}{
"downloaded": gorm.Expr("downloaded + ?", downloaded),
"uploaded": gorm.Expr("uploaded + ?", uploaded),
}),
}).Create(&TorrentTraffic{
Client: statRecord.Data.Client,
Day: day,
Site: statRecord.Data.Site,
Downloaded: downloaded,
Uploaded: uploaded,
})
time = nexydayTime
day = util.FormatDate(time)
nexydayTime += 86400
}
}
return db, nil
}
func init() {
}