2020-03-13 15:56:25 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
package snopayouts
|
2020-03-13 15:56:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-03-24 12:39:13 +00:00
|
|
|
"github.com/zeebo/errs"
|
2020-03-13 15:56:25 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// DB exposes all needed functionality to manage payouts.
|
2020-03-13 15:56:25 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type DB interface {
|
|
|
|
// GetPaystub return payStub by nodeID and period.
|
2021-01-15 16:23:19 +00:00
|
|
|
GetPaystub(ctx context.Context, nodeID storj.NodeID, period string) (Paystub, error)
|
2020-04-19 16:35:38 +01:00
|
|
|
// GetAllPaystubs return all payStubs by nodeID.
|
2021-01-15 16:23:19 +00:00
|
|
|
GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) ([]Paystub, error)
|
|
|
|
|
2020-06-26 19:43:06 +01:00
|
|
|
// GetPayment return storagenode payment by nodeID and period.
|
2021-01-15 16:23:19 +00:00
|
|
|
GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (Payment, error)
|
2020-06-26 19:43:06 +01:00
|
|
|
// GetAllPayments return all payments by nodeID.
|
2021-01-15 16:23:19 +00:00
|
|
|
GetAllPayments(ctx context.Context, nodeID storj.NodeID) ([]Payment, error)
|
|
|
|
|
|
|
|
// TestCreatePaystub insert paystub into db. Only used for tests.
|
|
|
|
TestCreatePaystub(ctx context.Context, stub Paystub) (err error)
|
|
|
|
// TestCreatePayment insert payment into db. Only used for tests.
|
|
|
|
TestCreatePayment(ctx context.Context, payment Payment) (err error)
|
2020-03-13 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// ErrNoDataForPeriod represents errors from the payouts database.
|
2021-04-28 09:06:17 +01:00
|
|
|
var ErrNoDataForPeriod = errs.Class("no payStub/payments for period")
|
2020-03-24 12:39:13 +00:00
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// Error is the default error class for payouts package.
|
2021-04-28 09:06:17 +01:00
|
|
|
var Error = errs.Class("payoutsdb")
|
2020-06-26 19:43:06 +01:00
|
|
|
|
2021-01-15 16:23:19 +00:00
|
|
|
// Paystub is an entity that holds held amount of cash that will be paid to storagenode operator after some period.
|
|
|
|
type Paystub struct {
|
2020-03-13 15:56:25 +00:00
|
|
|
Period string `json:"period"`
|
|
|
|
NodeID storj.NodeID `json:"nodeId"`
|
|
|
|
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-01-19 23:30:50 +00:00
|
|
|
Distributed int64 `json:"distributed"`
|
2020-03-13 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:23:19 +00:00
|
|
|
// Payment is an entity that holds payment to storagenode operator parameters.
|
|
|
|
type Payment struct {
|
2020-06-26 19:43:06 +01:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
NodeID storj.NodeID `json:"nodeId"`
|
|
|
|
Period string `json:"period"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
Receipt string `json:"receipt"`
|
|
|
|
Notes string `json:"notes"`
|
|
|
|
}
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Service is used to store and handle node paystub information.
|
2020-03-13 15:56:25 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
db DB
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:42:21 +01:00
|
|
|
// NewService returns a new Service.
|
2020-03-13 15:56:25 +00:00
|
|
|
func NewService(log *zap.Logger, db DB) *Service {
|
|
|
|
return &Service{
|
|
|
|
log: log,
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:23:19 +00:00
|
|
|
// GetPaystub returns Paystub by nodeID and period.
|
|
|
|
func (service *Service) GetPaystub(ctx context.Context, nodeID storj.NodeID, period string) (Paystub, error) {
|
|
|
|
paystub, err := service.db.GetPaystub(ctx, nodeID, period)
|
2020-03-13 15:56:25 +00:00
|
|
|
if err != nil {
|
2021-01-15 16:23:19 +00:00
|
|
|
return Paystub{}, Error.Wrap(err)
|
2020-03-13 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:23:19 +00:00
|
|
|
return paystub, nil
|
2020-03-13 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 16:35:38 +01:00
|
|
|
// GetAllPaystubs returns all paystubs by nodeID.
|
2021-01-15 16:23:19 +00:00
|
|
|
func (service *Service) GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) ([]Paystub, error) {
|
|
|
|
paystubs, err := service.db.GetAllPaystubs(ctx, nodeID)
|
2020-04-19 16:35:38 +01:00
|
|
|
if err != nil {
|
2021-01-15 16:23:19 +00:00
|
|
|
return []Paystub{}, Error.Wrap(err)
|
2020-04-19 16:35:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:23:19 +00:00
|
|
|
return paystubs, nil
|
2020-04-19 16:35:38 +01:00
|
|
|
}
|
2020-06-26 19:43:06 +01:00
|
|
|
|
|
|
|
// GetPayment returns storagenode payment data by nodeID and period.
|
2021-01-15 16:23:19 +00:00
|
|
|
func (service *Service) GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (Payment, error) {
|
2020-06-26 19:43:06 +01:00
|
|
|
payment, err := service.db.GetPayment(ctx, nodeID, period)
|
|
|
|
if err != nil {
|
2021-01-15 16:23:19 +00:00
|
|
|
return Payment{}, Error.Wrap(err)
|
2020-06-26 19:43:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return payment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllPayments returns all payments by nodeID.
|
2021-01-15 16:23:19 +00:00
|
|
|
func (service *Service) GetAllPayments(ctx context.Context, nodeID storj.NodeID) ([]Payment, error) {
|
2020-06-26 19:43:06 +01:00
|
|
|
payments, err := service.db.GetAllPayments(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payments, nil
|
|
|
|
}
|