2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bandwidth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DB contains information about bandwidth usage.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2019-03-18 10:55:06 +00:00
|
|
|
type DB interface {
|
|
|
|
Add(ctx context.Context, satelliteID storj.NodeID, action pb.PieceAction, amount int64, created time.Time) error
|
2019-11-20 14:37:57 +00:00
|
|
|
// MonthSummary returns summary of the current months bandwidth usages.
|
2020-01-31 23:25:52 +00:00
|
|
|
MonthSummary(ctx context.Context, now time.Time) (int64, error)
|
2019-07-16 15:58:58 +01:00
|
|
|
Rollup(ctx context.Context) (err error)
|
2019-11-20 14:37:57 +00:00
|
|
|
// Summary returns summary of bandwidth usages.
|
2019-03-18 10:55:06 +00:00
|
|
|
Summary(ctx context.Context, from, to time.Time) (*Usage, error)
|
2019-11-20 14:37:57 +00:00
|
|
|
// EgressSummary returns summary of egress bandwidth usages.
|
|
|
|
EgressSummary(ctx context.Context, from, to time.Time) (*Usage, error)
|
|
|
|
// IngressSummary returns summary of ingress bandwidth usages.
|
|
|
|
IngressSummary(ctx context.Context, from, to time.Time) (*Usage, error)
|
2019-09-04 15:01:55 +01:00
|
|
|
// SatelliteSummary returns aggregated bandwidth usage for a particular satellite.
|
|
|
|
SatelliteSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (*Usage, error)
|
2019-11-20 14:37:57 +00:00
|
|
|
// SatelliteEgressSummary returns egress bandwidth usage for a particular satellite.
|
|
|
|
SatelliteEgressSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (*Usage, error)
|
|
|
|
// SatelliteIngressSummary returns ingress bandwidth usage for a particular satellite.
|
|
|
|
SatelliteIngressSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (*Usage, error)
|
2019-03-18 10:55:06 +00:00
|
|
|
SummaryBySatellite(ctx context.Context, from, to time.Time) (map[storj.NodeID]*Usage, error)
|
2019-09-04 15:01:55 +01:00
|
|
|
// GetDailyRollups returns slice of daily bandwidth usage rollups for provided time range,
|
|
|
|
// sorted in ascending order.
|
|
|
|
GetDailyRollups(ctx context.Context, from, to time.Time) ([]UsageRollup, error)
|
|
|
|
// GetDailySatelliteRollups returns slice of daily bandwidth usage for provided time range,
|
|
|
|
// sorted in ascending order for a particular satellite.
|
|
|
|
GetDailySatelliteRollups(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) ([]UsageRollup, error)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Usage contains bandwidth usage information based on the type.
|
2019-03-18 10:55:06 +00:00
|
|
|
type Usage struct {
|
|
|
|
Invalid int64
|
|
|
|
Unknown int64
|
|
|
|
|
|
|
|
Put int64
|
|
|
|
Get int64
|
|
|
|
GetAudit int64
|
|
|
|
GetRepair int64
|
|
|
|
PutRepair int64
|
|
|
|
Delete int64
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:01:55 +01:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// Include adds specified action to the appropriate field.
|
|
|
|
func (usage *Usage) Include(action pb.PieceAction, amount int64) {
|
|
|
|
switch action {
|
|
|
|
case pb.PieceAction_INVALID:
|
|
|
|
usage.Invalid += amount
|
|
|
|
case pb.PieceAction_PUT:
|
|
|
|
usage.Put += amount
|
|
|
|
case pb.PieceAction_GET:
|
|
|
|
usage.Get += amount
|
|
|
|
case pb.PieceAction_GET_AUDIT:
|
|
|
|
usage.GetAudit += amount
|
|
|
|
case pb.PieceAction_GET_REPAIR:
|
|
|
|
usage.GetRepair += amount
|
|
|
|
case pb.PieceAction_PUT_REPAIR:
|
|
|
|
usage.PutRepair += amount
|
|
|
|
case pb.PieceAction_DELETE:
|
|
|
|
usage.Delete += amount
|
|
|
|
default:
|
|
|
|
usage.Unknown += amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds another usage to this one.
|
|
|
|
func (usage *Usage) Add(b *Usage) {
|
|
|
|
usage.Invalid += b.Invalid
|
|
|
|
usage.Unknown += b.Unknown
|
|
|
|
usage.Put += b.Put
|
|
|
|
usage.Get += b.Get
|
|
|
|
usage.GetAudit += b.GetAudit
|
|
|
|
usage.GetRepair += b.GetRepair
|
|
|
|
usage.PutRepair += b.PutRepair
|
|
|
|
usage.Delete += b.Delete
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Total sums all type of bandwidths.
|
2019-03-18 10:55:06 +00:00
|
|
|
func (usage *Usage) Total() int64 {
|
|
|
|
return usage.Invalid +
|
|
|
|
usage.Unknown +
|
|
|
|
usage.Put +
|
|
|
|
usage.Get +
|
|
|
|
usage.GetAudit +
|
|
|
|
usage.GetRepair +
|
|
|
|
usage.PutRepair +
|
|
|
|
usage.Delete
|
|
|
|
}
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// TotalMonthlySummary returns total bandwidth usage for current month.
|
2019-04-15 11:12:22 +01:00
|
|
|
func TotalMonthlySummary(ctx context.Context, db DB) (*Usage, error) {
|
|
|
|
return db.Summary(ctx, getBeginningOfMonth(), time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBeginningOfMonth() time.Time {
|
|
|
|
t := time.Now()
|
|
|
|
y, m, _ := t.Date()
|
|
|
|
return time.Date(y, m, 1, 0, 0, 0, 0, time.Now().Location())
|
|
|
|
}
|