2020-03-12 14:42:01 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
package payouts
|
2020-03-12 14:42:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-03-16 04:28:03 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-03-12 14:42:01 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// DB works with payouts database.
|
2020-03-12 14:42:01 +00:00
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type DB interface {
|
2020-09-08 16:15:08 +01:00
|
|
|
// StorePayStub inserts or updates paystub into the DB.
|
2020-03-12 14:42:01 +00:00
|
|
|
StorePayStub(ctx context.Context, paystub PayStub) error
|
2020-09-08 16:15:08 +01:00
|
|
|
// GetPayStub retrieves paystub for specific satellite and period.
|
2020-03-12 14:42:01 +00:00
|
|
|
GetPayStub(ctx context.Context, satelliteID storj.NodeID, period string) (*PayStub, error)
|
2020-09-08 16:15:08 +01:00
|
|
|
// AllPayStubs retrieves paystubs from all satellites in specific period from DB.
|
2020-03-12 14:42:01 +00:00
|
|
|
AllPayStubs(ctx context.Context, period string) ([]PayStub, error)
|
2020-04-28 14:07:50 +01:00
|
|
|
// SatellitesHeldbackHistory retrieves heldback history for specific satellite from DB.
|
2021-04-22 19:50:42 +01:00
|
|
|
SatellitesHeldbackHistory(ctx context.Context, satelliteID storj.NodeID) ([]HeldForPeriod, error)
|
2020-06-24 16:57:22 +01:00
|
|
|
// SatellitesDisposedHistory returns all disposed amount for specific satellite from DB.
|
|
|
|
SatellitesDisposedHistory(ctx context.Context, satelliteID storj.NodeID) (int64, error)
|
2021-01-14 16:41:36 +00:00
|
|
|
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payouts data.
|
2020-05-18 19:37:16 +01:00
|
|
|
SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) ([]string, error)
|
2021-01-14 16:41:36 +00:00
|
|
|
// AllPeriods retrieves all periods in which we have some payouts data.
|
2020-05-18 19:37:16 +01:00
|
|
|
AllPeriods(ctx context.Context) ([]string, error)
|
2020-06-24 18:05:43 +01:00
|
|
|
// StorePayment inserts or updates payment into the DB
|
|
|
|
StorePayment(ctx context.Context, payment Payment) error
|
2020-09-08 16:15:08 +01:00
|
|
|
// GetReceipt retrieves receipt for specific satellite and period.
|
|
|
|
GetReceipt(ctx context.Context, satelliteID storj.NodeID, period string) (string, error)
|
2021-01-07 19:17:46 +00:00
|
|
|
// GetTotalEarned returns total earned amount of node from all paystubs.
|
|
|
|
GetTotalEarned(ctx context.Context) (_ int64, err error)
|
2021-02-02 17:37:24 +00:00
|
|
|
// 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)
|
2021-05-05 18:34:10 +01:00
|
|
|
// GetSatelliteSummary returns satellite all time paid and held amounts.
|
|
|
|
GetSatelliteSummary(ctx context.Context, satelliteID storj.NodeID) (paid, held int64, err error)
|
|
|
|
// GetSatellitePeriodSummary returns satellite paid and held amounts for specific period.
|
|
|
|
GetSatellitePeriodSummary(ctx context.Context, satelliteID storj.NodeID, period string) (paid, held int64, err error)
|
2021-05-14 23:03:38 +01:00
|
|
|
// GetUndistributed returns total undistributed amount.
|
|
|
|
GetUndistributed(ctx context.Context) (int64, error)
|
2021-05-25 13:02:20 +01:00
|
|
|
// GetSatellitePaystubs returns summed paystubs for specific satellite.
|
|
|
|
GetSatellitePaystubs(ctx context.Context, satelliteID storj.NodeID) (*PayStub, error)
|
|
|
|
// GetPaystubs returns summed all paystubs.
|
|
|
|
GetPaystubs(ctx context.Context) (*PayStub, error)
|
2021-05-24 21:40:31 +01:00
|
|
|
// GetSatellitesPeriodPaystubs returns summed all satellites paystubs for specific period.
|
|
|
|
GetPeriodPaystubs(ctx context.Context, period string) (*PayStub, error)
|
|
|
|
// GetSatellitePeriodPaystubs returns summed satellite paystubs for specific period.
|
|
|
|
GetSatellitePeriodPaystubs(ctx context.Context, period string, satelliteID storj.NodeID) (*PayStub, error)
|
2021-04-22 19:50:42 +01:00
|
|
|
// HeldAmountHistory retrieves held amount history for all satellites.
|
|
|
|
HeldAmountHistory(ctx context.Context) ([]HeldAmountHistory, error)
|
2020-03-12 14:42:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// ErrNoPayStubForPeriod represents errors from the payouts database.
|
2021-04-28 09:06:17 +01:00
|
|
|
var ErrNoPayStubForPeriod = errs.Class("no payStub for period")
|
2020-03-16 04:28:03 +00:00
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// PayStub is node payouts data for satellite by specific period.
|
2020-03-12 14:42:01 +00:00
|
|
|
type PayStub struct {
|
|
|
|
SatelliteID storj.NodeID `json:"satelliteId"`
|
|
|
|
Period string `json:"period"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Codes string `json:"codes"`
|
|
|
|
UsageAtRest float64 `json:"usageAtRest"`
|
|
|
|
UsageGet int64 `json:"usageGet"`
|
|
|
|
UsagePut int64 `json:"usagePut"`
|
|
|
|
UsageGetRepair int64 `json:"usageGetRepair"`
|
|
|
|
UsagePutRepair int64 `json:"usagePutRepair"`
|
|
|
|
UsageGetAudit int64 `json:"usageGetAudit"`
|
|
|
|
CompAtRest int64 `json:"compAtRest"`
|
|
|
|
CompGet int64 `json:"compGet"`
|
|
|
|
CompPut int64 `json:"compPut"`
|
|
|
|
CompGetRepair int64 `json:"compGetRepair"`
|
|
|
|
CompPutRepair int64 `json:"compPutRepair"`
|
|
|
|
CompGetAudit int64 `json:"compGetAudit"`
|
|
|
|
SurgePercent int64 `json:"surgePercent"`
|
|
|
|
Held int64 `json:"held"`
|
|
|
|
Owed int64 `json:"owed"`
|
|
|
|
Disposed int64 `json:"disposed"`
|
|
|
|
Paid int64 `json:"paid"`
|
2021-02-02 10:51:02 +00:00
|
|
|
Distributed int64 `json:"distributed"`
|
2020-03-12 14:42:01 +00:00
|
|
|
}
|
2020-04-28 14:07:50 +01:00
|
|
|
|
2021-04-22 19:50:42 +01:00
|
|
|
// GetEarnedWithSurge returns paystub's total earned and surge.
|
|
|
|
func (paystub *PayStub) GetEarnedWithSurge() (earned int64, surge int64) {
|
|
|
|
earned = paystub.CompGetAudit + paystub.CompGet + paystub.CompGetRepair + paystub.CompAtRest
|
|
|
|
surge = earned * paystub.SurgePercent / 100
|
|
|
|
|
|
|
|
return earned, surge
|
|
|
|
}
|
|
|
|
|
|
|
|
// UsageAtRestTbM converts paystub's usage_at_rest from tbh to tbm.
|
|
|
|
func (paystub *PayStub) UsageAtRestTbM() {
|
|
|
|
paystub.UsageAtRest /= 720
|
2020-04-28 14:07:50 +01:00
|
|
|
}
|
2020-05-10 12:23:13 +01:00
|
|
|
|
2020-06-24 18:05:43 +01:00
|
|
|
// Payment is node payment data for specific period.
|
|
|
|
type Payment struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
SatelliteID storj.NodeID `json:"satelliteId"`
|
|
|
|
Period string `json:"period"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
Receipt string `json:"receipt"`
|
|
|
|
Notes string `json:"notes"`
|
|
|
|
}
|
2020-09-14 15:40:52 +01:00
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// SatelliteHeldHistory amount of held for specific satellite for all time since join.
|
|
|
|
type SatelliteHeldHistory struct {
|
|
|
|
SatelliteID storj.NodeID `json:"satelliteID"`
|
|
|
|
SatelliteName string `json:"satelliteName"`
|
|
|
|
HoldForFirstPeriod int64 `json:"holdForFirstPeriod"`
|
|
|
|
HoldForSecondPeriod int64 `json:"holdForSecondPeriod"`
|
|
|
|
HoldForThirdPeriod int64 `json:"holdForThirdPeriod"`
|
|
|
|
TotalHeld int64 `json:"totalHeld"`
|
|
|
|
TotalDisposed int64 `json:"totalDisposed"`
|
|
|
|
JoinedAt time.Time `json:"joinedAt"`
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// SatellitePayoutForPeriod contains payouts information for specific period for specific satellite.
|
2020-09-10 15:08:25 +01:00
|
|
|
type SatellitePayoutForPeriod struct {
|
2020-09-04 13:05:13 +01:00
|
|
|
SatelliteID string `json:"satelliteID"`
|
|
|
|
SatelliteURL string `json:"satelliteURL"`
|
|
|
|
Age int64 `json:"age"`
|
|
|
|
Earned int64 `json:"earned"`
|
|
|
|
Surge int64 `json:"surge"`
|
|
|
|
SurgePercent int64 `json:"surgePercent"`
|
|
|
|
Held int64 `json:"held"`
|
|
|
|
HeldPercent float64 `json:"heldPercent"`
|
|
|
|
AfterHeld int64 `json:"afterHeld"`
|
|
|
|
Disposed int64 `json:"disposed"`
|
|
|
|
Paid int64 `json:"paid"`
|
|
|
|
Receipt string `json:"receipt"`
|
|
|
|
IsExitComplete bool `json:"isExitComplete"`
|
2021-02-18 19:59:39 +00:00
|
|
|
Distributed int64 `json:"distributed"`
|
2020-09-10 15:08:25 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:50:42 +01:00
|
|
|
// HeldAmountHistory contains held amount history for satellite.
|
|
|
|
type HeldAmountHistory struct {
|
|
|
|
SatelliteID storj.NodeID `json:"satelliteId"`
|
|
|
|
HeldAmounts []HeldForPeriod `json:"heldAmounts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeldForPeriod is node's held amount for period.
|
|
|
|
type HeldForPeriod struct {
|
|
|
|
Period string `json:"period"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// Period is a string that represents paystub period type in format yyyy-mm.
|
|
|
|
type Period string
|
|
|
|
|
|
|
|
// Time returns period in time.Time type from string.
|
|
|
|
func (p Period) Time() (time.Time, error) {
|
|
|
|
return time.Parse("2006-01", string(p))
|
|
|
|
}
|