2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-14 01:22:18 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
package rollup_test
|
2019-01-18 16:53:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-02-07 04:16:24 +00:00
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-01-18 16:53:23 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
2019-03-12 13:08:23 +00:00
|
|
|
func TestRollupRaws(t *testing.T) {
|
2019-02-07 04:16:24 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 10, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
days := 5
|
|
|
|
atRest := float64(5000)
|
|
|
|
bw := []int64{1000, 2000, 3000, 4000}
|
|
|
|
|
|
|
|
nodeData, bwTotals := createData(planet, atRest, bw)
|
|
|
|
|
|
|
|
// Set timestamp back by the number of days we want to save
|
|
|
|
timestamp := time.Now().UTC().AddDate(0, 0, -1*days)
|
|
|
|
start := timestamp
|
|
|
|
|
2019-02-07 20:26:55 +00:00
|
|
|
for i := 0; i <= days; i++ {
|
2019-02-07 04:16:24 +00:00
|
|
|
err := planet.Satellites[0].DB.Accounting().SaveAtRestRaw(ctx, timestamp, timestamp, nodeData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Accounting().SaveBWRaw(ctx, timestamp, timestamp, bwTotals)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-03-12 13:08:23 +00:00
|
|
|
err = planet.Satellites[0].Accounting.Rollup.RollupRaws(ctx)
|
2019-02-07 04:16:24 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Advance time by 24 hours
|
|
|
|
timestamp = timestamp.Add(time.Hour * 24)
|
|
|
|
end := timestamp
|
|
|
|
|
2019-03-12 13:08:23 +00:00
|
|
|
// rollup.RollupRaws cuts off the hr/min/sec before saving, we need to do the same when querying
|
2019-02-07 04:16:24 +00:00
|
|
|
start = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
|
|
|
|
end = time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, end.Location())
|
|
|
|
|
|
|
|
rows, err := planet.Satellites[0].DB.Accounting().QueryPaymentInfo(ctx, start, end)
|
|
|
|
assert.NoError(t, err)
|
2019-02-07 20:26:55 +00:00
|
|
|
if i == 0 { // we need at least two days for rollup to work
|
2019-02-07 04:16:24 +00:00
|
|
|
assert.Equal(t, 0, len(rows))
|
2019-02-07 20:26:55 +00:00
|
|
|
continue
|
2019-02-07 04:16:24 +00:00
|
|
|
}
|
2019-02-07 20:26:55 +00:00
|
|
|
// the number of rows should be number of nodes
|
|
|
|
assert.Equal(t, len(planet.StorageNodes), len(rows))
|
2019-02-07 04:16:24 +00:00
|
|
|
|
|
|
|
// verify data is correct
|
|
|
|
for _, r := range rows {
|
2019-02-07 20:26:55 +00:00
|
|
|
i := int64(i)
|
|
|
|
assert.Equal(t, i*bw[0], r.PutTotal)
|
|
|
|
assert.Equal(t, i*bw[1], r.GetTotal)
|
|
|
|
assert.Equal(t, i*bw[2], r.GetAuditTotal)
|
|
|
|
assert.Equal(t, i*bw[3], r.GetRepairTotal)
|
|
|
|
assert.Equal(t, float64(i)*atRest, r.AtRestTotal)
|
2019-02-07 04:16:24 +00:00
|
|
|
assert.NotNil(t, nodeData[r.NodeID])
|
2019-02-07 20:26:55 +00:00
|
|
|
assert.NotEmpty(t, r.Wallet)
|
2019-02-07 04:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-01-18 16:53:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 04:16:24 +00:00
|
|
|
func createData(planet *testplanet.Planet, atRest float64, bw []int64) (nodeData map[storj.NodeID]float64, bwTotals map[storj.NodeID][]int64) {
|
|
|
|
nodeData = make(map[storj.NodeID]float64)
|
|
|
|
bwTotals = make(map[storj.NodeID][]int64)
|
|
|
|
for _, n := range planet.StorageNodes {
|
|
|
|
id := n.Identity.ID
|
|
|
|
nodeData[id] = atRest
|
|
|
|
bwTotals[id] = bw
|
2019-01-18 16:53:23 +00:00
|
|
|
}
|
2019-02-07 04:16:24 +00:00
|
|
|
return nodeData, bwTotals
|
2019-01-18 16:53:23 +00:00
|
|
|
}
|