-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
58 lines (49 loc) · 1.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
package clipper
import (
"encoding/json"
"fmt"
"sync/atomic"
)
type Stats struct {
NumOfCircuits int `json:"num_of_circuits"`
IsOpen bool `json:"is_open"`
TotalRuns int64 `json:"total_runs"`
TotalFails int64 `json:"total_fails"`
AvgSuccess float64 `json:"avg_success"`
NumOfOpenings int `json:"num_of_openings"`
}
type circuitStats struct {
numOfRuns int64
avgTime float64
lowestLatency int64
highestLatency int64
numOfOpenings int
}
func (c *circuitStats) updateRuns(delta int) {
atomic.AddInt64(&c.numOfRuns, int64(delta))
}
func FillStats(name string, print bool) Stats {
cb := getClipperWithName(name)
total := cb.statistics.numOfRuns
fails := cb.Failures
var avg float64 = 100
if fails != 0 {
avg = float64(100 - (fails * 100 / total))
}
s := Stats{
NumOfCircuits: len(clippers),
IsOpen: cb.open,
TotalRuns: total,
TotalFails: fails,
AvgSuccess: avg,
NumOfOpenings: cb.statistics.numOfOpenings,
}
if print {
printStats(s)
}
return s
}
func printStats(s Stats) {
b, _ := json.MarshalIndent(s, "", "\t")
fmt.Println(string(b))
}