2021-06-03 12:52:28 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-07-01 13:07:40 +01:00
|
|
|
// Usage holds storage usage stamps and summary for a particular period.
|
|
|
|
type Usage struct {
|
2023-05-03 17:56:43 +01:00
|
|
|
Stamps []UsageStamp `json:"stamps"`
|
|
|
|
Summary float64 `json:"summary"`
|
|
|
|
SummaryBytes float64 `json:"summaryBytes"`
|
2021-07-01 13:07:40 +01:00
|
|
|
}
|
|
|
|
|
2021-06-03 12:52:28 +01:00
|
|
|
// UsageStamp holds data at rest total for an interval beginning at interval start.
|
|
|
|
type UsageStamp struct {
|
2023-05-03 17:56:43 +01:00
|
|
|
AtRestTotal float64 `json:"atRestTotal"`
|
|
|
|
AtRestTotalBytes float64 `json:"atRestTotalBytes"`
|
|
|
|
IntervalStart time.Time `json:"intervalStart"`
|
2021-06-03 12:52:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UsageStampDailyCache caches storage usage stamps by interval date.
|
|
|
|
type UsageStampDailyCache map[time.Time]UsageStamp
|
|
|
|
|
|
|
|
// Add adds usage stamp to cache aggregating at rest data by date.
|
|
|
|
func (cache *UsageStampDailyCache) Add(stamp UsageStamp) {
|
|
|
|
year, month, day := stamp.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] = UsageStamp{
|
2023-05-03 17:56:43 +01:00
|
|
|
AtRestTotal: cacheStamp.AtRestTotal + stamp.AtRestTotal,
|
|
|
|
AtRestTotalBytes: cacheStamp.AtRestTotalBytes + stamp.AtRestTotalBytes,
|
|
|
|
IntervalStart: intervalStart,
|
2021-06-03 12:52:28 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cached[intervalStart] = UsageStamp{
|
2023-05-03 17:56:43 +01:00
|
|
|
AtRestTotal: stamp.AtRestTotal,
|
|
|
|
AtRestTotalBytes: stamp.AtRestTotalBytes,
|
|
|
|
IntervalStart: intervalStart,
|
2021-06-03 12:52:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*cache = cached
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorted returns usage stamp slice sorted by interval start.
|
|
|
|
func (cache *UsageStampDailyCache) Sorted() []UsageStamp {
|
|
|
|
var usage []UsageStamp
|
|
|
|
|
|
|
|
for _, stamp := range *cache {
|
|
|
|
usage = append(usage, stamp)
|
|
|
|
}
|
|
|
|
sort.Slice(usage, func(i, j int) bool {
|
|
|
|
return usage[i].IntervalStart.Before(usage[j].IntervalStart)
|
|
|
|
})
|
|
|
|
|
|
|
|
return usage
|
|
|
|
}
|
2021-06-14 13:03:21 +01:00
|
|
|
|
|
|
|
// DiskSpace stores all info about storagenode disk space usage.
|
|
|
|
type DiskSpace struct {
|
|
|
|
Allocated int64 `json:"allocated"`
|
|
|
|
Used int64 `json:"usedPieces"`
|
|
|
|
Trash int64 `json:"usedTrash"`
|
|
|
|
Free int64 `json:"free"`
|
|
|
|
Available int64 `json:"available"`
|
|
|
|
Overused int64 `json:"overused"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add combines disk space with another one.
|
|
|
|
func (diskSpace *DiskSpace) Add(space DiskSpace) {
|
|
|
|
diskSpace.Allocated += space.Allocated
|
|
|
|
diskSpace.Used += space.Used
|
|
|
|
diskSpace.Trash += space.Trash
|
|
|
|
diskSpace.Free += space.Free
|
|
|
|
diskSpace.Available += space.Available
|
|
|
|
diskSpace.Overused += space.Overused
|
|
|
|
}
|