storagenode/payouts: earned amount at satellite and list of paying satellites IDs added to DB

Change-Id: I66e34d6d80e37481d6eaae4c7bd017a7e096a751
This commit is contained in:
Qweder93 2021-02-02 19:37:24 +02:00 committed by Nikolai Siedov
parent 21dfd99883
commit b92a86e524
3 changed files with 146 additions and 0 deletions

View File

@ -314,3 +314,97 @@ func TestAllPayStubPeriodCached(t *testing.T) {
require.Equal(t, 0, len(payStubs))
})
}
func TestPayouts(t *testing.T) {
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
payout := db.Payout()
t.Run("Test SatelliteIDs", func(t *testing.T) {
id1 := storj.NodeID{1, 2, 3}
id2 := storj.NodeID{2, 3, 4}
id3 := storj.NodeID{3, 3, 3}
err := payout.StorePayStub(ctx, payouts.PayStub{
SatelliteID: id1,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
SatelliteID: id1,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
SatelliteID: id2,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
SatelliteID: id3,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
SatelliteID: id3,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
SatelliteID: id2,
})
require.NoError(t, err)
listIDs, err := payout.GetPayingSatellitesIDs(ctx)
require.Equal(t, len(listIDs), 3)
require.NoError(t, err)
})
t.Run("Test GetSatelliteEarned", func(t *testing.T) {
id1 := storj.NodeID{1, 2, 3}
id2 := storj.NodeID{2, 3, 4}
id3 := storj.NodeID{3, 3, 3}
err := payout.StorePayStub(ctx, payouts.PayStub{
Period: "2020-11",
SatelliteID: id1,
CompGet: 11,
CompAtRest: 11,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
Period: "2020-12",
SatelliteID: id1,
CompGet: 22,
CompAtRest: 22,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
Period: "2020-11",
SatelliteID: id2,
CompGet: 33,
CompAtRest: 33,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
Period: "2020-10",
SatelliteID: id3,
CompGet: 44,
CompAtRest: 44,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
Period: "2020-11",
SatelliteID: id3,
CompGet: 55,
CompAtRest: 55,
})
require.NoError(t, err)
err = payout.StorePayStub(ctx, payouts.PayStub{
Period: "2020-10",
SatelliteID: id2,
CompGet: 66,
CompAtRest: 66,
})
require.NoError(t, err)
satellite1Earned, err := payout.GetEarnedAtSatellite(ctx, id1)
require.Equal(t, int(satellite1Earned), 66)
require.NoError(t, err)
satellite2Earned, err := payout.GetEarnedAtSatellite(ctx, id2)
require.Equal(t, int(satellite2Earned), 198)
require.NoError(t, err)
satellite3Earned, err := payout.GetEarnedAtSatellite(ctx, id3)
require.Equal(t, int(satellite3Earned), 198)
require.NoError(t, err)
})
})
}

View File

@ -36,6 +36,10 @@ type DB interface {
GetReceipt(ctx context.Context, satelliteID storj.NodeID, period string) (string, error)
// GetTotalEarned returns total earned amount of node from all paystubs.
GetTotalEarned(ctx context.Context) (_ int64, err error)
// GetEarnedAtSatellite returns total earned value for node from specific satellite.
GetEarnedAtSatellite(ctx context.Context, id storj.NodeID) (int64, error)
// GetPayingSatellitesIDs returns list of satellite ID's that ever paid to storagenode.
GetPayingSatellitesIDs(ctx context.Context) ([]storj.NodeID, error)
}
// ErrNoPayStubForPeriod represents errors from the payouts database.

View File

@ -430,3 +430,51 @@ func (db *payoutDB) GetTotalEarned(ctx context.Context) (_ int64, err error) {
return totalEarned, nil
}
// GetEarnedAtSatellite returns total earned value for node from specific satellite.
func (db *payoutDB) GetEarnedAtSatellite(ctx context.Context, id storj.NodeID) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)
query := `SELECT comp_at_rest, comp_get, comp_get_repair, comp_get_audit FROM paystubs WHERE satellite_id = ?`
rows, err := db.QueryContext(ctx, query, id)
if err != nil {
return 0, err
}
defer func() { err = errs.Combine(err, rows.Close()) }()
var totalEarned int64
for rows.Next() {
var compAtRest, compGet, compGetRepair, compGetAudit int64
err := rows.Scan(&compAtRest, &compGet, &compGetRepair, &compGetAudit)
if err != nil {
return 0, ErrPayout.Wrap(err)
}
totalEarned += compGetAudit + compGet + compGetRepair + compAtRest
}
if err = rows.Err(); err != nil {
return 0, ErrPayout.Wrap(err)
}
return totalEarned, nil
}
// GetPayingSatellitesIDs returns list of satellite ID's that ever paid to storagenode.
func (db *payoutDB) GetPayingSatellitesIDs(ctx context.Context) (_ []storj.NodeID, err error) {
defer mon.Task()(&ctx)(&err)
query := `SELECT DISTINCT (satellite_id) FROM paystubs`
rows, err := db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer func() { err = errs.Combine(err, rows.Close()) }()
var satelliteIDs []storj.NodeID
for rows.Next() {
var satelliteID storj.NodeID
err := rows.Scan(&satelliteID)
if err != nil {
return nil, ErrPayout.Wrap(err)
}
satelliteIDs = append(satelliteIDs, satelliteID)
}
if err = rows.Err(); err != nil {
return nil, ErrPayout.Wrap(err)
}
return satelliteIDs, nil
}