-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_entry.go
218 lines (196 loc) · 7.74 KB
/
stats_entry.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
package varroa
import (
"errors"
"fmt"
"strconv"
"time"
"gitlab.com/catastrophic/assistance/fs"
"gitlab.com/catastrophic/assistance/logthis"
"gitlab.com/passelecasque/obstruction/tracker"
)
const (
progress = "Buffer: %s (%s) | Ratio: %.3f (%.3f) | Up: %s (%s) | Down: %s (%s) | Warning Buffer: %s (%s)"
firstProgress = "Buffer: %s | Ratio: %.3f | Up: %s | Down: %s | Warning Buffer: %s"
regexpProgress = `(.*): (.*): (.*) \((.*)\) \| (.*): (.*) \((.*)\) \| (.*): (.*) \((.*)\) \| (.*): (.*) \((.*)\) \| (.*): (.*) \((.*)\)`
currentStatsDBSchemaVersion = 1
)
type StatsEntry struct {
ID uint32 `storm:"id,increment"`
Tracker string `storm:"index"`
Up uint64
Down uint64
Ratio float64
Timestamp time.Time
TimestampUnix int64 `storm:"index"`
Collected bool `storm:"index"`
StartOfDay bool `storm:"index"`
StartOfWeek bool `storm:"index"`
StartOfMonth bool `storm:"index"`
SchemaVersion int
}
func NewStatsEntry(gazelleTracker *tracker.Gazelle, gzStats *tracker.GazelleUserStats) (*StatsEntry, error) {
// return StatsEntry
stats := &StatsEntry{
Tracker: gazelleTracker.Name,
Up: gzStats.Stats.Uploaded,
Down: gzStats.Stats.Downloaded,
Ratio: gzStats.Stats.Ratio,
Timestamp: time.Now(),
TimestampUnix: time.Now().Unix(),
Collected: true,
SchemaVersion: currentStatsDBSchemaVersion,
}
return stats, nil
}
func (se *StatsEntry) String() string {
buffer, warningBuffer := se.getBufferValues()
return fmt.Sprintf(firstProgress, fs.FileSizeDelta(buffer), se.Ratio, fs.FileSize(se.Up), fs.FileSize(se.Down), fs.FileSizeDelta(warningBuffer))
}
func (se *StatsEntry) getBufferValues() (int64, int64) {
conf, err := NewConfig(DefaultConfigurationFile)
if err != nil {
logthis.Error(err, logthis.VERBOSEST)
return 0, 0
}
statsConfig, err := conf.GetStats(se.Tracker)
if err != nil {
logthis.Error(err, logthis.VERBOSEST)
return 0, 0
}
return int64(float64(se.Up)/statsConfig.TargetRatio) - int64(se.Down), int64(float64(se.Up)/warningRatio) - int64(se.Down)
}
// TODO REPLACE BY A DELTA
func (se *StatsEntry) Diff(previous *StatsEntry) (int64, int64, int64, int64, float64) {
buffer, warningBuffer := se.getBufferValues()
prevBuffer, prevWarningBuffer := previous.getBufferValues()
return int64(se.Up - previous.Up), int64(se.Down - previous.Down), buffer - prevBuffer,
warningBuffer - prevWarningBuffer, se.Ratio - previous.Ratio
}
func (se *StatsEntry) Progress(previous *StatsEntry) string {
if previous.Ratio == 0 {
return se.String()
}
buffer, warningBuffer := se.getBufferValues()
dup, ddown, dbuff, dwbuff, dratio := se.Diff(previous)
return fmt.Sprintf(progress, fs.FileSizeDelta(buffer), fs.FileSizeDelta(dbuff), se.Ratio, dratio, fs.FileSize(se.Up),
fs.FileSizeDelta(dup), fs.FileSize(se.Down), fs.FileSizeDelta(ddown), fs.FileSizeDelta(warningBuffer),
fs.FileSizeDelta(dwbuff))
}
// TODO do something about this awful thing
func (se *StatsEntry) ProgressParts(previous *StatsEntry) []string {
buffer, warningBuffer := se.getBufferValues()
if previous.Ratio == 0 {
return []string{"+", se.Timestamp.Format("2006-01-02 15:04"), fs.FileSize(se.Up), fs.FileSize(se.Down), fs.FileSizeDelta(buffer), fs.FileSizeDelta(warningBuffer), fmt.Sprintf("%.3f", se.Ratio)}
}
dup, ddown, dbuff, dwbuff, dratio := se.Diff(previous)
return []string{
fs.Sign(dbuff),
se.Timestamp.Format("2006-01-02 15:04"),
fmt.Sprintf("%s (%s)", fs.FileSize(se.Up), fs.FileSizeDelta(dup)),
fmt.Sprintf("%s (%s)", fs.FileSize(se.Down), fs.FileSizeDelta(ddown)),
fmt.Sprintf("%s (%s)", fs.FileSizeDelta(buffer), fs.FileSizeDelta(dbuff)),
fmt.Sprintf("%s (%s)", fs.FileSizeDelta(warningBuffer), fs.FileSizeDelta(dwbuff)),
fmt.Sprintf("%.3f (%+.3f)", se.Ratio, dratio),
}
}
func (se *StatsEntry) IsProgressAcceptable(previous *StatsEntry, maxDecrease int, minimumRatio float64) bool {
if se.Ratio <= minimumRatio {
logthis.Info("Ratio has dropped below minimum authorized, unacceptable.", logthis.NORMAL)
return false
}
if previous.Ratio == 0 {
// first pass
return true
}
_, _, bufferChange, _, _ := se.Diff(previous)
// if maxDecrease is unset (=0), always return true
if maxDecrease == 0 || bufferChange >= 0 || -bufferChange <= int64(maxDecrease*1024*1024) {
return true
}
logthis.Info(fmt.Sprintf("Decrease: %d bytes, only %d allowed. Unacceptable.", bufferChange, maxDecrease*1024*1024), logthis.VERBOSE)
return false
}
// TODO reimplement export to CSV
func (se *StatsEntry) ToSlice() []string {
// timestamp;up;down;ratio
return []string{fmt.Sprintf("%d", se.Timestamp.Unix()), strconv.FormatUint(se.Up, 10), strconv.FormatUint(se.Down, 10), strconv.FormatFloat(se.Ratio, 'f', -1, 64)}
}
func InterpolateStats(previous, next StatsEntry, targetTime time.Time) (*StatsEntry, error) {
// check targetTime is between se.Timest
if targetTime.Before(previous.Timestamp) || targetTime.After(next.Timestamp) {
return nil, errors.New("incorrect timestamp")
}
// create a virtual StatsEntry using simple linear interpolation
virtualStats := &StatsEntry{}
upSlope := (float64(next.Up) - float64(previous.Up)) / float64(next.Timestamp.Unix()-previous.Timestamp.Unix())
upOffset := float64(previous.Up) - upSlope*float64(previous.Timestamp.Unix())
virtualStats.Up = uint64(upSlope*float64(targetTime.Unix()) + upOffset)
downSlope := (float64(next.Down) - float64(previous.Down)) / float64(next.Timestamp.Unix()-previous.Timestamp.Unix())
downOffset := float64(previous.Down) - downSlope*float64(previous.Timestamp.Unix())
virtualStats.Down = uint64(downSlope*float64(targetTime.Unix()) + downOffset)
ratioSlope := (next.Ratio - previous.Ratio) / float64(next.Timestamp.Unix()-previous.Timestamp.Unix())
ratioOffset := previous.Ratio - ratioSlope*float64(previous.Timestamp.Unix())
virtualStats.Ratio = ratioSlope*float64(targetTime.Unix()) + ratioOffset
virtualStats.Timestamp = targetTime
virtualStats.TimestampUnix = targetTime.Unix()
virtualStats.Tracker = previous.Tracker
virtualStats.SchemaVersion = currentStatsDBSchemaVersion
return virtualStats, nil
}
// ------------------------
type StatsDelta struct {
Tracker string
Timestamp time.Time
Up int64
Down int64
Ratio float64
Buffer int64
WarningBuffer int64
}
func CalculateDelta(first, second StatsEntry) (*StatsDelta, error) {
// check second after first
if !second.Timestamp.After(first.Timestamp) {
return nil, errors.New("cannot calculate delta for out of order entries")
}
firstBuffer, firstWarningBuffer := first.getBufferValues()
secondBuffer, secondWarningBuffer := second.getBufferValues()
d := &StatsDelta{
Tracker: second.Tracker,
Timestamp: second.Timestamp,
Up: int64(second.Up - first.Up),
Down: int64(second.Down - first.Down),
Ratio: second.Ratio - first.Ratio,
Buffer: secondBuffer - firstBuffer,
WarningBuffer: secondWarningBuffer - firstWarningBuffer,
}
return d, nil
}
func CalculateDeltas(entries []StatsEntry) []StatsDelta {
var deltas []StatsDelta
for i, e := range entries {
if i == 0 {
deltas = append(deltas, StatsDelta{Timestamp: e.Timestamp})
} else {
delta, err := CalculateDelta(entries[i-1], e)
if err != nil {
logthis.Error(err, logthis.VERBOSEST)
deltas = append(deltas, StatsDelta{Timestamp: e.Timestamp})
} else {
deltas = append(deltas, *delta)
}
}
}
return deltas
}
// ------------------------
type SnatchStatsEntry struct {
ID uint32 `storm:"id,increment"`
Tracker string `storm:"index"`
Size uint64
Number int
Timestamp time.Time `storm:"index"`
Collected bool `storm:"index"`
StartOfDay bool `storm:"index"`
StartOfWeek bool `storm:"index"`
StartOfMonth bool `storm:"index"`
}