/
stats.go
69 lines (60 loc) · 1.56 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
package fs
// Used to calculate how many log lines matched the regular expression
// and how many log files could be transmitted from the server to the client.
// Hit and transmit percentage takes only the last 100 log lines into calculation.
type stats struct {
pos int
lineCount uint64
matched [100]bool
matchCount uint64
transmitted [100]bool
transmitCount int
}
// Return the total line count.
func (f *stats) totalLineCount() uint64 {
return f.lineCount
}
// Calculate the percentage of log lines transmitted to the client.
func (f *stats) transmittedPerc() int {
return int(percentOf(float64(f.matchCount), float64(f.transmitCount)))
}
// Update bucket position. We only take into consideration the last 100
// lines for stats.
func (f *stats) updatePosition() {
f.pos = (f.pos + 1) % 100
f.lineCount++
}
// Increment match counter.
func (f *stats) updateLineMatched() {
if !f.matched[f.pos] {
f.matchCount++
f.matched[f.pos] = true
}
}
// Increment transmitted counter.
func (f *stats) updateLineTransmitted() {
if !f.transmitted[f.pos] {
f.transmitCount++
f.transmitted[f.pos] = true
}
}
// Decrement match counter.
func (f *stats) updateLineNotMatched() {
if f.matched[f.pos] {
f.matchCount--
f.matched[f.pos] = false
}
}
// Decrement transmitted counter.
func (f *stats) updateLineNotTransmitted() {
if f.transmitted[f.pos] {
f.transmitCount--
f.transmitted[f.pos] = false
}
}
func percentOf(total float64, value float64) float64 {
if total == 0 || total == value {
return 100
}
return value / (total / 100.0)
}