storj/pkg/accounting/rollup/rollup.go
Bill Thorp f9845e7e92
changes to masterdb to support accountingDB (#846)
* added rollup to captplanet, moved accountingDB to masterdb
2018-12-14 09:27:21 -05:00

56 lines
1.0 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package rollup
import (
"context"
"time"
"go.uber.org/zap"
"storj.io/storj/pkg/accounting"
)
// Rollup is the service for totalling data on storage nodes for 1, 7, 30 day intervals
type Rollup interface {
Run(ctx context.Context) error
}
type rollup struct {
logger *zap.Logger
ticker *time.Ticker
db accounting.DB
}
func newRollup(logger *zap.Logger, db accounting.DB, interval time.Duration) *rollup {
return &rollup{
logger: logger,
ticker: time.NewTicker(interval),
db: db,
}
}
// Run the rollup loop
func (r *rollup) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
for {
err = r.Query(ctx)
if err != nil {
r.logger.Error("Query failed", zap.Error(err))
}
select {
case <-r.ticker.C: // wait for the next interval to happen
case <-ctx.Done(): // or the rollup is canceled via context
return ctx.Err()
}
}
}
func (r *rollup) Query(ctx context.Context) error {
//TODO
return nil
}