2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-17 18:40:15 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package psdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2018-10-10 15:04:42 +01:00
|
|
|
"errors"
|
2018-08-17 18:40:15 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-09-08 16:34:55 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3" // register sqlite to sql
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-08-17 18:40:15 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-17 18:40:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-07 14:46:42 +00:00
|
|
|
mon = monkit.Package()
|
|
|
|
// Error is the default psdb errs class
|
|
|
|
Error = errs.Class("psdb")
|
2018-08-17 18:40:15 +01:00
|
|
|
)
|
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
// AgreementStatus keep tracks of the agreement payout status
|
|
|
|
type AgreementStatus int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AgreementStatusUnsent sets the agreement status to UNSENT
|
|
|
|
AgreementStatusUnsent = iota
|
|
|
|
// AgreementStatusSent sets the agreement status to SENT
|
|
|
|
AgreementStatusSent
|
|
|
|
// AgreementStatusReject sets the agreement status to REJECT
|
|
|
|
AgreementStatusReject
|
|
|
|
// add new status here ...
|
|
|
|
)
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// DB is a piece store database
|
|
|
|
type DB struct {
|
2019-01-29 15:41:01 +00:00
|
|
|
mu sync.Mutex
|
2019-03-01 05:46:16 +00:00
|
|
|
db *sql.DB
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:47:25 +00:00
|
|
|
// Agreement is a struct that contains a bandwidth agreement and the associated signature
|
|
|
|
type Agreement struct {
|
2019-02-22 21:17:35 +00:00
|
|
|
Agreement pb.Order
|
2018-10-31 18:47:25 +00:00
|
|
|
Signature []byte
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// Open opens DB at DBPath
|
2019-01-29 15:41:01 +00:00
|
|
|
func Open(DBPath string) (db *DB, err error) {
|
2018-08-17 18:40:15 +01:00
|
|
|
if err = os.MkdirAll(filepath.Dir(DBPath), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
sqlite, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?%s", DBPath, "_journal=WAL"))
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
2018-12-07 14:46:42 +00:00
|
|
|
return nil, Error.Wrap(err)
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-10-30 16:43:09 +00:00
|
|
|
db = &DB{
|
2019-03-01 05:46:16 +00:00
|
|
|
db: sqlite,
|
2018-10-30 16:43:09 +00:00
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-10-30 16:43:09 +00:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenInMemory opens sqlite DB inmemory
|
2019-01-29 15:41:01 +00:00
|
|
|
func OpenInMemory() (db *DB, err error) {
|
2018-10-30 16:43:09 +00:00
|
|
|
sqlite, err := sql.Open("sqlite3", ":memory:")
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-30 16:43:09 +00:00
|
|
|
|
|
|
|
db = &DB{
|
2019-03-01 05:46:16 +00:00
|
|
|
db: sqlite,
|
2018-10-30 16:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
// Migration define piecestore DB migration
|
|
|
|
func (db *DB) Migration() *migrate.Migration {
|
|
|
|
migration := &migrate.Migration{
|
|
|
|
Table: "versions",
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
|
|
|
Description: "Initial setup",
|
|
|
|
Version: 0,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE TABLE IF NOT EXISTS ttl (
|
|
|
|
id BLOB UNIQUE,
|
|
|
|
created INT(10),
|
|
|
|
expires INT(10),
|
|
|
|
size INT(10)
|
|
|
|
)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS bandwidth_agreements (
|
|
|
|
satellite BLOB,
|
|
|
|
agreement BLOB,
|
|
|
|
signature BLOB
|
|
|
|
)`,
|
|
|
|
`CREATE INDEX IF NOT EXISTS idx_ttl_expires ON ttl (
|
|
|
|
expires
|
|
|
|
)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS bwusagetbl (
|
|
|
|
size INT(10),
|
|
|
|
daystartdate INT(10),
|
|
|
|
dayenddate INT(10)
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
},
|
2019-02-22 15:51:39 +00:00
|
|
|
{
|
|
|
|
Description: "Extending bandwidth_agreements table and drop bwusagetbl",
|
|
|
|
Version: 1,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, db migrate.DB, tx *sql.Tx) error {
|
|
|
|
v1sql := migrate.SQL{
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN uplink BLOB`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN serial_num BLOB`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN total INT(10)`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN max_size INT(10)`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN created_utc_sec INT(10)`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN expiration_utc_sec INT(10)`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN action INT(10)`,
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN daystart_utc_sec INT(10)`,
|
|
|
|
}
|
|
|
|
err := v1sql.Run(log, db, tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through the table and fill
|
|
|
|
err = func() error {
|
|
|
|
rows, err := tx.Query(`SELECT agreement, signature FROM bandwidth_agreements`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var rbaBytes, signature []byte
|
|
|
|
rba := &pb.RenterBandwidthAllocation{}
|
|
|
|
err := rows.Scan(&rbaBytes, &signature)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// unmarshal the rbaBytes
|
|
|
|
err = proto.Unmarshal(rbaBytes, rba)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// update the new columns data
|
|
|
|
t := time.Unix(rba.PayerAllocation.CreatedUnixSec, 0)
|
|
|
|
startofthedayUnixSec := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC).Unix()
|
|
|
|
|
|
|
|
// update the row by signature as it is unique
|
|
|
|
_, err = tx.Exec(`UPDATE bandwidth_agreements SET
|
|
|
|
uplink = ?,
|
|
|
|
serial_num = ?,
|
|
|
|
total = ?,
|
|
|
|
max_size = ?,
|
|
|
|
created_utc_sec = ?,
|
|
|
|
expiration_utc_sec = ?,
|
|
|
|
action = ?,
|
|
|
|
daystart_utc_sec = ?
|
|
|
|
WHERE signature = ?
|
|
|
|
`,
|
|
|
|
rba.PayerAllocation.UplinkId.Bytes(), rba.PayerAllocation.SerialNumber,
|
|
|
|
rba.Total, rba.PayerAllocation.MaxSize, rba.PayerAllocation.CreatedUnixSec,
|
|
|
|
rba.PayerAllocation.ExpirationUnixSec, rba.PayerAllocation.GetAction(),
|
|
|
|
startofthedayUnixSec, signature)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.Err()
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec(`DROP TABLE bwusagetbl;`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
},
|
2019-03-05 22:20:46 +00:00
|
|
|
{
|
|
|
|
Description: "Add status column for bandwidth_agreements",
|
|
|
|
Version: 2,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`ALTER TABLE bandwidth_agreements ADD COLUMN status INT(10) DEFAULT 0`,
|
|
|
|
},
|
|
|
|
},
|
2019-03-08 09:53:04 +00:00
|
|
|
{
|
|
|
|
Description: "Add index on serial number for bandwidth_agreements",
|
|
|
|
Version: 3,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE INDEX IF NOT EXISTS idx_bwa_serial ON bandwidth_agreements (serial_num)`,
|
|
|
|
},
|
|
|
|
},
|
2019-04-03 18:17:29 +01:00
|
|
|
{
|
|
|
|
Description: "Initiate Network reset",
|
|
|
|
Version: 4,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`UPDATE ttl SET expires = 1553727600 WHERE created <= 1553727600 `,
|
|
|
|
},
|
|
|
|
},
|
2019-02-19 09:39:04 +00:00
|
|
|
},
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2019-02-19 09:39:04 +00:00
|
|
|
return migration
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the database
|
2018-09-08 16:34:55 +01:00
|
|
|
func (db *DB) Close() error {
|
2019-03-01 05:46:16 +00:00
|
|
|
return db.db.Close()
|
2018-09-08 16:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) locked() func() {
|
|
|
|
db.mu.Lock()
|
|
|
|
return db.mu.Unlock
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 15:41:01 +00:00
|
|
|
// DeleteExpired deletes expired pieces
|
|
|
|
func (db *DB) DeleteExpired(ctx context.Context) (expired []string, err error) {
|
2018-08-17 18:40:15 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-29 15:41:01 +00:00
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-01-29 15:41:01 +00:00
|
|
|
// TODO: add limit
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
tx, err := db.db.BeginTx(ctx, nil)
|
2019-01-29 15:41:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-01-29 15:41:01 +00:00
|
|
|
now := time.Now().Unix()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-02-28 17:51:24 +00:00
|
|
|
rows, err := tx.Query("SELECT id FROM ttl WHERE expires > 0 AND expires < ?", now)
|
2019-01-29 15:41:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-01-29 15:41:01 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var id string
|
|
|
|
if err := rows.Scan(&id); err != nil {
|
|
|
|
return nil, err
|
2019-01-11 11:26:39 +00:00
|
|
|
}
|
2019-01-29 15:41:01 +00:00
|
|
|
expired = append(expired, id)
|
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 17:51:24 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM ttl WHERE expires > 0 AND expires < ?`, now)
|
2019-01-29 15:41:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2019-01-29 15:41:01 +00:00
|
|
|
|
|
|
|
return expired, tx.Commit()
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 15:41:01 +00:00
|
|
|
// WriteBandwidthAllocToDB inserts bandwidth agreement into DB
|
2019-02-22 21:17:35 +00:00
|
|
|
func (db *DB) WriteBandwidthAllocToDB(rba *pb.Order) error {
|
2019-01-28 19:45:25 +00:00
|
|
|
rbaBytes, err := proto.Marshal(rba)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-10-31 18:47:25 +00:00
|
|
|
// We begin extracting the satellite_id
|
|
|
|
// The satellite id can be used to sort the bandwidth agreements
|
|
|
|
// If the agreements are sorted we can send them in bulk streams to the satellite
|
2019-02-22 15:51:39 +00:00
|
|
|
t := time.Now()
|
|
|
|
startofthedayunixsec := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
|
2019-03-05 22:20:46 +00:00
|
|
|
_, err = db.db.Exec(`INSERT INTO bandwidth_agreements (satellite, agreement, signature, uplink, serial_num, total, max_size, created_utc_sec, status, expiration_utc_sec, action, daystart_utc_sec) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
2019-02-22 15:51:39 +00:00
|
|
|
rba.PayerAllocation.SatelliteId.Bytes(), rbaBytes, rba.GetSignature(),
|
|
|
|
rba.PayerAllocation.UplinkId.Bytes(), rba.PayerAllocation.SerialNumber,
|
2019-03-05 22:20:46 +00:00
|
|
|
rba.Total, rba.PayerAllocation.MaxSize, rba.PayerAllocation.CreatedUnixSec, AgreementStatusUnsent,
|
2019-02-22 15:51:39 +00:00
|
|
|
rba.PayerAllocation.ExpirationUnixSec, rba.PayerAllocation.GetAction().String(),
|
|
|
|
startofthedayunixsec)
|
2018-10-31 18:47:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
// DeleteBandwidthAllocationPayouts delete paid and/or old payout enteries based on days old
|
|
|
|
func (db *DB) DeleteBandwidthAllocationPayouts() error {
|
|
|
|
defer db.locked()()
|
|
|
|
|
|
|
|
//@TODO make a config value for older days
|
|
|
|
t := time.Now().Add(time.Hour * 24 * -90).Unix()
|
|
|
|
_, err := db.db.Exec(`DELETE FROM bandwidth_agreements WHERE created_utc_sec < ?`, t)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateBandwidthAllocationStatus update the bwa payout status
|
|
|
|
func (db *DB) UpdateBandwidthAllocationStatus(serialnum string, status AgreementStatus) (err error) {
|
|
|
|
defer db.locked()()
|
|
|
|
_, err = db.db.Exec(`UPDATE bandwidth_agreements SET status = ? WHERE serial_num = ?`, status, serialnum)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-27 16:29:23 +00:00
|
|
|
// DeleteBandwidthAllocationBySerialnum finds an allocation by signature and deletes it
|
|
|
|
func (db *DB) DeleteBandwidthAllocationBySerialnum(serialnum string) error {
|
2018-10-31 18:47:25 +00:00
|
|
|
defer db.locked()()
|
2019-03-01 05:46:16 +00:00
|
|
|
_, err := db.db.Exec(`DELETE FROM bandwidth_agreements WHERE serial_num=?`, serialnum)
|
2018-10-31 18:47:25 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// GetBandwidthAllocationBySignature finds allocation info by signature
|
2019-02-22 21:17:35 +00:00
|
|
|
func (db *DB) GetBandwidthAllocationBySignature(signature []byte) ([]*pb.Order, error) {
|
2018-09-08 16:34:55 +01:00
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
rows, err := db.db.Query(`SELECT agreement FROM bandwidth_agreements WHERE signature = ?`, signature)
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
2018-09-08 16:34:55 +01:00
|
|
|
return nil, err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
defer func() {
|
2018-10-31 18:47:25 +00:00
|
|
|
if closeErr := rows.Close(); closeErr != nil {
|
|
|
|
zap.S().Errorf("failed to close rows when selecting from bandwidth_agreements: %+v", closeErr)
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
}()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-02-22 21:17:35 +00:00
|
|
|
agreements := []*pb.Order{}
|
2018-09-08 16:34:55 +01:00
|
|
|
for rows.Next() {
|
2019-01-28 19:45:25 +00:00
|
|
|
var rbaBytes []byte
|
|
|
|
err := rows.Scan(&rbaBytes)
|
|
|
|
if err != nil {
|
|
|
|
return agreements, err
|
|
|
|
}
|
2019-02-22 21:17:35 +00:00
|
|
|
rba := &pb.Order{}
|
2019-01-28 19:45:25 +00:00
|
|
|
err = proto.Unmarshal(rbaBytes, rba)
|
2018-09-08 16:34:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return agreements, err
|
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
agreements = append(agreements, rba)
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
return agreements, nil
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
// GetBandwidthAllocations all bandwidth agreements
|
2018-11-29 18:39:27 +00:00
|
|
|
func (db *DB) GetBandwidthAllocations() (map[storj.NodeID][]*Agreement, error) {
|
2018-10-31 18:47:25 +00:00
|
|
|
defer db.locked()()
|
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
rows, err := db.db.Query(`SELECT satellite, agreement FROM bandwidth_agreements WHERE status = ?`, AgreementStatusUnsent)
|
2018-10-31 18:47:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if closeErr := rows.Close(); closeErr != nil {
|
|
|
|
zap.S().Errorf("failed to close rows when selecting from bandwidth_agreements: %+v", closeErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
agreements := make(map[storj.NodeID][]*Agreement)
|
2018-10-31 18:47:25 +00:00
|
|
|
for rows.Next() {
|
2019-01-28 19:45:25 +00:00
|
|
|
rbaBytes := []byte{}
|
2018-11-01 16:40:26 +00:00
|
|
|
agreement := &Agreement{}
|
2018-11-29 18:39:27 +00:00
|
|
|
var satellite []byte
|
2019-02-22 15:51:39 +00:00
|
|
|
err := rows.Scan(&satellite, &rbaBytes)
|
2019-01-28 19:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return agreements, err
|
|
|
|
}
|
|
|
|
err = proto.Unmarshal(rbaBytes, &agreement.Agreement)
|
2018-10-31 18:47:25 +00:00
|
|
|
if err != nil {
|
2018-11-13 15:37:49 +00:00
|
|
|
return agreements, err
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
satelliteID, err := storj.NodeIDFromBytes(satellite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
agreements[satelliteID] = append(agreements[satelliteID], agreement)
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|
|
|
|
return agreements, nil
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
// GetBwaStatusBySerialNum get BWA status by serial num
|
|
|
|
func (db *DB) GetBwaStatusBySerialNum(serialnum string) (status AgreementStatus, err error) {
|
|
|
|
defer db.locked()()
|
|
|
|
err = db.db.QueryRow(`SELECT status FROM bandwidth_agreements WHERE serial_num=?`, serialnum).Scan(&status)
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:30:45 +01:00
|
|
|
// AddTTL adds TTL into database by id
|
|
|
|
func (db *DB) AddTTL(id string, expiration, size int64) error {
|
2018-09-08 16:34:55 +01:00
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
created := time.Now().Unix()
|
2019-03-01 05:46:16 +00:00
|
|
|
_, err := db.db.Exec("INSERT OR REPLACE INTO ttl (id, created, expires, size) VALUES (?, ?, ?, ?)", id, created, expiration, size)
|
2018-09-11 13:40:45 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// GetTTLByID finds the TTL in the database by id and return it
|
|
|
|
func (db *DB) GetTTLByID(id string) (expiration int64, err error) {
|
|
|
|
defer db.locked()()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
err = db.db.QueryRow(`SELECT expires FROM ttl WHERE id=?`, id).Scan(&expiration)
|
2018-09-08 16:34:55 +01:00
|
|
|
return expiration, err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:40:45 +01:00
|
|
|
// SumTTLSizes sums the size column on the ttl table
|
2019-03-01 05:46:16 +00:00
|
|
|
func (db *DB) SumTTLSizes() (int64, error) {
|
2018-09-11 13:40:45 +01:00
|
|
|
defer db.locked()()
|
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
var sum *int64
|
|
|
|
err := db.db.QueryRow(`SELECT SUM(size) FROM ttl;`).Scan(&sum)
|
|
|
|
if err == sql.ErrNoRows || sum == nil {
|
2018-09-25 17:49:55 +01:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-03-01 05:46:16 +00:00
|
|
|
return *sum, err
|
2018-09-11 13:40:45 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
// DeleteTTLByID finds the TTL in the database by id and delete it
|
|
|
|
func (db *DB) DeleteTTLByID(id string) error {
|
|
|
|
defer db.locked()()
|
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
_, err := db.db.Exec(`DELETE FROM ttl WHERE id=?`, id)
|
2018-09-08 16:34:55 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
return err
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-10-10 15:04:42 +01:00
|
|
|
|
|
|
|
// GetBandwidthUsedByDay finds the so far bw used by day and return it
|
|
|
|
func (db *DB) GetBandwidthUsedByDay(t time.Time) (size int64, err error) {
|
2019-02-22 15:51:39 +00:00
|
|
|
return db.GetTotalBandwidthBetween(t, t)
|
2018-10-10 15:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalBandwidthBetween each row in the bwusagetbl contains the total bw used per day
|
2019-03-01 05:46:16 +00:00
|
|
|
func (db *DB) GetTotalBandwidthBetween(startdate time.Time, enddate time.Time) (int64, error) {
|
2018-10-10 15:04:42 +01:00
|
|
|
defer db.locked()()
|
|
|
|
|
|
|
|
startTimeUnix := time.Date(startdate.Year(), startdate.Month(), startdate.Day(), 0, 0, 0, 0, startdate.Location()).Unix()
|
2019-02-22 15:51:39 +00:00
|
|
|
endTimeUnix := time.Date(enddate.Year(), enddate.Month(), enddate.Day(), 24, 0, 0, 0, enddate.Location()).Unix()
|
2018-10-10 15:04:42 +01:00
|
|
|
defaultunixtime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Now().Location()).Unix()
|
|
|
|
|
|
|
|
if (endTimeUnix < startTimeUnix) && (startTimeUnix > defaultunixtime || endTimeUnix > defaultunixtime) {
|
2019-03-01 05:46:16 +00:00
|
|
|
return 0, errors.New("Invalid date range")
|
2018-10-10 15:04:42 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
var totalUsage *int64
|
|
|
|
err := db.db.QueryRow(`SELECT SUM(total) FROM bandwidth_agreements WHERE daystart_utc_sec BETWEEN ? AND ?`, startTimeUnix, endTimeUnix).Scan(&totalUsage)
|
|
|
|
if err == sql.ErrNoRows || totalUsage == nil {
|
2018-10-15 20:59:05 +01:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-03-01 05:46:16 +00:00
|
|
|
return *totalUsage, err
|
2018-10-10 15:04:42 +01:00
|
|
|
}
|
2019-02-19 09:39:04 +00:00
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
// RawDB returns access to the raw database, only for migration tests.
|
|
|
|
func (db *DB) RawDB() *sql.DB { return db.db }
|
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
// Begin begins transaction
|
2019-03-01 05:46:16 +00:00
|
|
|
func (db *DB) Begin() (*sql.Tx, error) { return db.db.Begin() }
|
2019-02-19 09:39:04 +00:00
|
|
|
|
|
|
|
// Rebind rebind parameters
|
|
|
|
func (db *DB) Rebind(s string) string { return s }
|
|
|
|
|
|
|
|
// Schema returns schema
|
|
|
|
func (db *DB) Schema() string { return "" }
|