2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2019-07-09 01:33:50 +01:00
|
|
|
"sync"
|
2019-03-18 10:55:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/date"
|
|
|
|
"storj.io/storj/private/dbutil"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/bandwidth"
|
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrBandwidth represents errors from the bandwidthdb database.
|
|
|
|
var ErrBandwidth = errs.Class("bandwidthdb error")
|
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
// BandwidthDBName represents the database name.
|
|
|
|
const BandwidthDBName = "bandwidth"
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
type bandwidthDB struct {
|
2019-07-10 18:47:22 +01:00
|
|
|
// Moved to top of struct to resolve alignment issue with atomic operations on ARM
|
2019-07-16 21:53:58 +01:00
|
|
|
usedSpace int64
|
|
|
|
usedMu sync.RWMutex
|
2019-07-09 01:33:50 +01:00
|
|
|
usedSince time.Time
|
2019-07-16 21:53:58 +01:00
|
|
|
|
2019-11-13 16:49:22 +00:00
|
|
|
dbContainerImpl
|
2019-08-21 15:32:25 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Add adds bandwidth usage to the table
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *bandwidthDB) Add(ctx context.Context, satelliteID storj.NodeID, action pb.PieceAction, amount int64, created time.Time) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.Exec(`
|
2019-06-04 13:31:39 +01:00
|
|
|
INSERT INTO
|
2019-03-18 10:55:06 +00:00
|
|
|
bandwidth_usage(satellite_id, action, amount, created_at)
|
2019-07-15 18:38:08 +01:00
|
|
|
VALUES(?, ?, ?, ?)`, satelliteID, action, amount, created.UTC())
|
2019-07-09 01:33:50 +01:00
|
|
|
if err == nil {
|
2019-07-16 21:53:58 +01:00
|
|
|
db.usedMu.Lock()
|
|
|
|
defer db.usedMu.Unlock()
|
2019-07-09 01:33:50 +01:00
|
|
|
|
|
|
|
beginningOfMonth := getBeginningOfMonth(created.UTC())
|
2019-07-16 21:53:58 +01:00
|
|
|
if beginningOfMonth.Equal(db.usedSince) {
|
|
|
|
db.usedSpace += amount
|
|
|
|
} else if beginningOfMonth.After(db.usedSince) {
|
2019-07-09 01:33:50 +01:00
|
|
|
usage, err := db.Summary(ctx, beginningOfMonth, time.Now().UTC())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-16 21:53:58 +01:00
|
|
|
db.usedSince = beginningOfMonth
|
|
|
|
db.usedSpace = usage.Total()
|
2019-07-09 01:33:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrBandwidth.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 21:05:14 +01:00
|
|
|
// MonthSummary returns summary of the current months bandwidth usages
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *bandwidthDB) MonthSummary(ctx context.Context) (_ int64, err error) {
|
2019-07-09 01:33:50 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-16 21:53:58 +01:00
|
|
|
db.usedMu.RLock()
|
2019-07-09 01:33:50 +01:00
|
|
|
beginningOfMonth := getBeginningOfMonth(time.Now().UTC())
|
2019-07-16 21:53:58 +01:00
|
|
|
if beginningOfMonth.Equal(db.usedSince) {
|
|
|
|
defer db.usedMu.RUnlock()
|
|
|
|
return db.usedSpace, nil
|
2019-07-09 01:33:50 +01:00
|
|
|
}
|
2019-07-16 21:53:58 +01:00
|
|
|
db.usedMu.RUnlock()
|
2019-07-09 01:33:50 +01:00
|
|
|
|
|
|
|
usage, err := db.Summary(ctx, beginningOfMonth, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// Just return the usage, don't update the cache. Let add handle updates
|
|
|
|
return usage.Total(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:37:57 +00:00
|
|
|
// actionFilter sums bandwidth depending on piece action type.
|
|
|
|
type actionFilter func(action pb.PieceAction, amount int64, usage *bandwidth.Usage)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ingressFilter sums put and put repair.
|
|
|
|
ingressFilter actionFilter = func(action pb.PieceAction, amount int64, usage *bandwidth.Usage) {
|
|
|
|
switch action {
|
|
|
|
case pb.PieceAction_PUT, pb.PieceAction_PUT_REPAIR:
|
|
|
|
usage.Include(action, amount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// egressFilter sums get, get audit and get repair.
|
|
|
|
egressFilter actionFilter = func(action pb.PieceAction, amount int64, usage *bandwidth.Usage) {
|
|
|
|
switch action {
|
|
|
|
case pb.PieceAction_GET, pb.PieceAction_GET_AUDIT, pb.PieceAction_GET_REPAIR:
|
|
|
|
usage.Include(action, amount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bandwidthFilter sums all bandwidth.
|
|
|
|
bandwidthFilter actionFilter = func(action pb.PieceAction, amount int64, usage *bandwidth.Usage) {
|
|
|
|
usage.Include(action, amount)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Summary returns summary of bandwidth usages for all satellites.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *bandwidthDB) Summary(ctx context.Context, from, to time.Time) (_ *bandwidth.Usage, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-11-20 14:37:57 +00:00
|
|
|
return db.getSummary(ctx, from, to, bandwidthFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EgressSummary returns summary of egress usages for all satellites.
|
|
|
|
func (db *bandwidthDB) EgressSummary(ctx context.Context, from, to time.Time) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return db.getSummary(ctx, from, to, egressFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IngressSummary returns summary of ingress usages for all satellites.
|
|
|
|
func (db *bandwidthDB) IngressSummary(ctx context.Context, from, to time.Time) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return db.getSummary(ctx, from, to, ingressFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSummary returns bandwidth data for all satellites.
|
|
|
|
func (db *bandwidthDB) getSummary(ctx context.Context, from, to time.Time, filter actionFilter) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
usage := &bandwidth.Usage{}
|
|
|
|
|
2019-07-16 15:58:58 +01:00
|
|
|
from = from.UTC()
|
|
|
|
to = to.UTC()
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.Query(`
|
2019-07-16 15:58:58 +01:00
|
|
|
SELECT action, sum(a) amount from(
|
|
|
|
SELECT action, sum(amount) a
|
|
|
|
FROM bandwidth_usage
|
|
|
|
WHERE datetime(?) <= datetime(created_at) AND datetime(created_at) <= datetime(?)
|
|
|
|
GROUP BY action
|
|
|
|
UNION ALL
|
|
|
|
SELECT action, sum(amount) a
|
|
|
|
FROM bandwidth_usage_rollups
|
|
|
|
WHERE datetime(?) <= datetime(interval_start) AND datetime(interval_start) <= datetime(?)
|
|
|
|
GROUP BY action
|
|
|
|
) GROUP BY action;
|
|
|
|
`, from, to, from, to)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return usage, nil
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrBandwidth.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var action pb.PieceAction
|
|
|
|
var amount int64
|
2019-11-20 14:37:57 +00:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
err := rows.Scan(&action, &amount)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrBandwidth.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-11-20 14:37:57 +00:00
|
|
|
|
|
|
|
filter(action, amount, usage)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return usage, ErrBandwidth.Wrap(rows.Err())
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 14:37:57 +00:00
|
|
|
// SatelliteSummary returns summary of bandwidth usages for a particular satellite.
|
2019-09-04 15:01:55 +01:00
|
|
|
func (db *bandwidthDB) SatelliteSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx, satelliteID, from, to)(&err)
|
|
|
|
|
2019-11-20 14:37:57 +00:00
|
|
|
return db.getSatelliteSummary(ctx, satelliteID, from, to, bandwidthFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SatelliteEgressSummary returns summary of egress usage for a particular satellite.
|
|
|
|
func (db *bandwidthDB) SatelliteEgressSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx, satelliteID, from, to)(&err)
|
|
|
|
|
|
|
|
return db.getSatelliteSummary(ctx, satelliteID, from, to, egressFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SatelliteIngressSummary returns summary of ingress usage for a particular satellite.
|
|
|
|
func (db *bandwidthDB) SatelliteIngressSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx, satelliteID, from, to)(&err)
|
|
|
|
|
|
|
|
return db.getSatelliteSummary(ctx, satelliteID, from, to, ingressFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSummary returns bandwidth data for a particular satellite.
|
|
|
|
func (db *bandwidthDB) getSatelliteSummary(ctx context.Context, satelliteID storj.NodeID, from, to time.Time, filter actionFilter) (_ *bandwidth.Usage, err error) {
|
|
|
|
defer mon.Task()(&ctx, satelliteID, from, to)(&err)
|
|
|
|
|
2019-09-04 15:01:55 +01:00
|
|
|
from, to = from.UTC(), to.UTC()
|
|
|
|
|
|
|
|
query := `SELECT action, sum(a) amount from(
|
|
|
|
SELECT action, sum(amount) a
|
|
|
|
FROM bandwidth_usage
|
|
|
|
WHERE datetime(?) <= datetime(created_at) AND datetime(created_at) <= datetime(?)
|
|
|
|
AND satellite_id = ?
|
|
|
|
GROUP BY action
|
|
|
|
UNION ALL
|
|
|
|
SELECT action, sum(amount) a
|
|
|
|
FROM bandwidth_usage_rollups
|
|
|
|
WHERE datetime(?) <= datetime(interval_start) AND datetime(interval_start) <= datetime(?)
|
|
|
|
AND satellite_id = ?
|
|
|
|
GROUP BY action
|
|
|
|
) GROUP BY action;`
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query, from, to, satelliteID, from, to, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrBandwidth.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = ErrBandwidth.Wrap(errs.Combine(err, rows.Close()))
|
|
|
|
}()
|
|
|
|
|
|
|
|
usage := new(bandwidth.Usage)
|
|
|
|
for rows.Next() {
|
|
|
|
var action pb.PieceAction
|
|
|
|
var amount int64
|
|
|
|
|
|
|
|
err := rows.Scan(&action, &amount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:37:57 +00:00
|
|
|
filter(action, amount, usage)
|
2019-09-04 15:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return usage, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// SummaryBySatellite returns summary of bandwidth usage grouping by satellite.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *bandwidthDB) SummaryBySatellite(ctx context.Context, from, to time.Time) (_ map[storj.NodeID]*bandwidth.Usage, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
entries := map[storj.NodeID]*bandwidth.Usage{}
|
|
|
|
|
2019-07-16 15:58:58 +01:00
|
|
|
from = from.UTC()
|
|
|
|
to = to.UTC()
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.Query(`
|
2019-07-16 15:58:58 +01:00
|
|
|
SELECT satellite_id, action, sum(a) amount from(
|
|
|
|
SELECT satellite_id, action, sum(amount) a
|
|
|
|
FROM bandwidth_usage
|
|
|
|
WHERE datetime(?) <= datetime(created_at) AND datetime(created_at) <= datetime(?)
|
|
|
|
GROUP BY satellite_id, action
|
|
|
|
UNION ALL
|
|
|
|
SELECT satellite_id, action, sum(amount) a
|
|
|
|
FROM bandwidth_usage_rollups
|
|
|
|
WHERE datetime(?) <= datetime(interval_start) AND datetime(interval_start) <= datetime(?)
|
|
|
|
GROUP BY satellite_id, action
|
|
|
|
) GROUP BY satellite_id, action;
|
|
|
|
`, from, to, from, to)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return entries, nil
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrBandwidth.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var satelliteID storj.NodeID
|
|
|
|
var action pb.PieceAction
|
|
|
|
var amount int64
|
|
|
|
|
|
|
|
err := rows.Scan(&satelliteID, &action, &amount)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrBandwidth.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry, ok := entries[satelliteID]
|
|
|
|
if !ok {
|
|
|
|
entry = &bandwidth.Usage{}
|
|
|
|
entries[satelliteID] = entry
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.Include(action, amount)
|
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return entries, ErrBandwidth.Wrap(rows.Err())
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-07-09 01:33:50 +01:00
|
|
|
|
2019-11-20 14:37:57 +00:00
|
|
|
// Rollup bandwidth_usage data earlier than the current hour, then delete the rolled up records.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *bandwidthDB) Rollup(ctx context.Context) (err error) {
|
2019-07-16 15:58:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
2019-07-29 15:07:52 +01:00
|
|
|
|
2019-07-16 15:58:58 +01:00
|
|
|
// Go back an hour to give us room for late persists
|
|
|
|
hour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()).Add(-time.Hour)
|
|
|
|
|
2019-07-29 15:07:52 +01:00
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrBandwidth.Wrap(err)
|
2019-07-29 15:07:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
} else {
|
|
|
|
err = errs.Combine(err, tx.Rollback())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
result, err := tx.Exec(`
|
2019-07-16 15:58:58 +01:00
|
|
|
INSERT INTO bandwidth_usage_rollups (interval_start, satellite_id, action, amount)
|
|
|
|
SELECT datetime(strftime('%Y-%m-%dT%H:00:00', created_at)) created_hr, satellite_id, action, SUM(amount)
|
|
|
|
FROM bandwidth_usage
|
|
|
|
WHERE datetime(created_at) < datetime(?)
|
2019-07-29 15:07:52 +01:00
|
|
|
GROUP BY created_hr, satellite_id, action
|
|
|
|
ON CONFLICT(interval_start, satellite_id, action)
|
|
|
|
DO UPDATE SET amount = bandwidth_usage_rollups.amount + excluded.amount;
|
2019-07-16 15:58:58 +01:00
|
|
|
|
|
|
|
DELETE FROM bandwidth_usage WHERE datetime(created_at) < datetime(?);
|
|
|
|
`, hour, hour)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrBandwidth.Wrap(err)
|
2019-07-16 15:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = result.RowsAffected()
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrBandwidth.Wrap(err)
|
2019-07-16 15:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:01:55 +01:00
|
|
|
// GetDailyRollups returns slice of daily bandwidth usage rollups for provided time range,
|
|
|
|
// sorted in ascending order.
|
|
|
|
func (db *bandwidthDB) GetDailyRollups(ctx context.Context, from, to time.Time) (_ []bandwidth.UsageRollup, err error) {
|
|
|
|
defer mon.Task()(&ctx, from, to)(&err)
|
|
|
|
|
|
|
|
since, _ := date.DayBoundary(from.UTC())
|
|
|
|
_, before := date.DayBoundary(to.UTC())
|
|
|
|
|
|
|
|
return db.getDailyUsageRollups(ctx,
|
|
|
|
"WHERE DATETIME(?) <= DATETIME(interval_start) AND DATETIME(interval_start) <= DATETIME(?)",
|
|
|
|
since, before)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDailySatelliteRollups returns slice of daily bandwidth usage for provided time range,
|
|
|
|
// sorted in ascending order for a particular satellite.
|
|
|
|
func (db *bandwidthDB) GetDailySatelliteRollups(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (_ []bandwidth.UsageRollup, err error) {
|
|
|
|
defer mon.Task()(&ctx, satelliteID, from, to)(&err)
|
|
|
|
|
|
|
|
since, _ := date.DayBoundary(from.UTC())
|
|
|
|
_, before := date.DayBoundary(to.UTC())
|
|
|
|
|
|
|
|
return db.getDailyUsageRollups(ctx,
|
|
|
|
"WHERE satellite_id = ? AND DATETIME(?) <= DATETIME(interval_start) AND DATETIME(interval_start) <= DATETIME(?)",
|
|
|
|
satelliteID, since, before)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDailyUsageRollups returns slice of grouped by date bandwidth usage rollups
|
|
|
|
// sorted in ascending order and applied condition if any.
|
|
|
|
func (db *bandwidthDB) getDailyUsageRollups(ctx context.Context, cond string, args ...interface{}) (_ []bandwidth.UsageRollup, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
query := `SELECT action, sum(a) as amount, DATETIME(DATE(interval_start)) as date FROM (
|
|
|
|
SELECT action, sum(amount) as a, created_at AS interval_start
|
|
|
|
FROM bandwidth_usage
|
|
|
|
` + cond + `
|
|
|
|
GROUP BY interval_start, action
|
|
|
|
UNION ALL
|
|
|
|
SELECT action, sum(amount) as a, interval_start
|
|
|
|
FROM bandwidth_usage_rollups
|
|
|
|
` + cond + `
|
|
|
|
GROUP BY interval_start, action
|
|
|
|
) GROUP BY date, action
|
|
|
|
ORDER BY interval_start`
|
|
|
|
|
|
|
|
// duplicate args as they are used twice
|
|
|
|
args = append(args, args...)
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrBandwidth.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = ErrBandwidth.Wrap(errs.Combine(err, rows.Close()))
|
|
|
|
}()
|
|
|
|
|
|
|
|
var dates []time.Time
|
|
|
|
usageRollupsByDate := make(map[time.Time]*bandwidth.UsageRollup)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var action int32
|
|
|
|
var amount int64
|
|
|
|
var intervalStartN dbutil.NullTime
|
|
|
|
|
|
|
|
err = rows.Scan(&action, &amount, &intervalStartN)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
intervalStart := intervalStartN.Time
|
|
|
|
|
|
|
|
rollup, ok := usageRollupsByDate[intervalStart]
|
|
|
|
if !ok {
|
|
|
|
rollup = &bandwidth.UsageRollup{
|
|
|
|
IntervalStart: intervalStart,
|
|
|
|
}
|
|
|
|
|
|
|
|
dates = append(dates, intervalStart)
|
|
|
|
usageRollupsByDate[intervalStart] = rollup
|
|
|
|
}
|
|
|
|
|
|
|
|
switch pb.PieceAction(action) {
|
|
|
|
case pb.PieceAction_GET:
|
|
|
|
rollup.Egress.Usage = amount
|
|
|
|
case pb.PieceAction_GET_AUDIT:
|
|
|
|
rollup.Egress.Audit = amount
|
|
|
|
case pb.PieceAction_GET_REPAIR:
|
|
|
|
rollup.Egress.Repair = amount
|
|
|
|
case pb.PieceAction_PUT:
|
|
|
|
rollup.Ingress.Usage = amount
|
|
|
|
case pb.PieceAction_PUT_REPAIR:
|
|
|
|
rollup.Ingress.Repair = amount
|
|
|
|
case pb.PieceAction_DELETE:
|
|
|
|
rollup.Delete = amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var usageRollups []bandwidth.UsageRollup
|
|
|
|
for _, d := range dates {
|
|
|
|
usageRollups = append(usageRollups, *usageRollupsByDate[d])
|
|
|
|
}
|
|
|
|
|
|
|
|
return usageRollups, nil
|
|
|
|
}
|
|
|
|
|
2019-07-09 01:33:50 +01:00
|
|
|
func getBeginningOfMonth(now time.Time) time.Time {
|
|
|
|
y, m, _ := now.Date()
|
|
|
|
return time.Date(y, m, 1, 0, 0, 0, 0, time.Now().UTC().Location())
|
|
|
|
}
|