-
Notifications
You must be signed in to change notification settings - Fork 402
/
bandwidth.go
93 lines (82 loc) · 2.62 KB
/
bandwidth.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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package bandwidth
import (
"sort"
"time"
)
// Egress stores info about storage node egress usage.
type Egress struct {
Repair int64 `json:"repair"`
Audit int64 `json:"audit"`
Usage int64 `json:"usage"`
}
// Ingress stores info about storage node ingress usage.
type Ingress struct {
Repair int64 `json:"repair"`
Usage int64 `json:"usage"`
}
// UsageRollup contains rolluped bandwidth usage.
type UsageRollup struct {
Egress Egress `json:"egress"`
Ingress Ingress `json:"ingress"`
Delete int64 `json:"delete"`
IntervalStart time.Time `json:"intervalStart"`
}
// Monthly contains all bandwidth, ingress, egress monthly data.
type Monthly struct {
BandwidthDaily []UsageRollup `json:"bandwidthDaily"`
BandwidthSummary int64 `json:"bandwidthSummary"`
EgressSummary int64 `json:"egressSummary"`
IngressSummary int64 `json:"ingressSummary"`
}
// UsageRollupDailyCache caches storage usage stamps by interval date.
type UsageRollupDailyCache map[time.Time]UsageRollup
// Sorted returns usage rollup slice sorted by interval start.
func (cache *UsageRollupDailyCache) Sorted() []UsageRollup {
var usageRollup []UsageRollup
for _, stamp := range *cache {
usageRollup = append(usageRollup, stamp)
}
sort.Slice(usageRollup, func(i, j int) bool {
return usageRollup[i].IntervalStart.Before(usageRollup[j].IntervalStart)
})
return usageRollup
}
// Add adds usage rollup to cache aggregating bandwidth data by date.
func (cache *UsageRollupDailyCache) Add(rollup UsageRollup) {
year, month, day := rollup.IntervalStart.UTC().Date()
intervalStart := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
cached := *cache
cacheStamp, ok := cached[intervalStart]
if ok {
cached[intervalStart] = UsageRollup{
Egress: Egress{
Repair: cacheStamp.Egress.Repair + rollup.Egress.Repair,
Audit: cacheStamp.Egress.Audit + rollup.Egress.Audit,
Usage: cacheStamp.Egress.Usage + rollup.Egress.Usage,
},
Ingress: Ingress{
Repair: cacheStamp.Ingress.Repair + rollup.Ingress.Repair,
Usage: cacheStamp.Ingress.Usage + rollup.Ingress.Usage,
},
Delete: cacheStamp.Delete + rollup.Delete,
IntervalStart: intervalStart,
}
} else {
cached[intervalStart] = UsageRollup{
Egress: Egress{
Repair: rollup.Egress.Repair,
Audit: rollup.Egress.Audit,
Usage: rollup.Egress.Usage,
},
Ingress: Ingress{
Repair: rollup.Ingress.Repair,
Usage: rollup.Ingress.Usage,
},
Delete: rollup.Delete,
IntervalStart: intervalStart,
}
}
*cache = cached
}