2020-03-13 15:56:25 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
package snopayout
|
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"
|
|
|
|
)
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// DB exposes all needed functionality to manage payout.
|
2020-03-13 15:56:25 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type DB interface {
|
|
|
|
// GetPaystub return payStub by nodeID and period.
|
|
|
|
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.
|
|
|
|
GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) ([]PayStub, error)
|
2020-03-17 10:06:20 +00:00
|
|
|
// CreatePaystub insert paystub into db.
|
|
|
|
CreatePaystub(ctx context.Context, stub PayStub) (err error)
|
2020-06-26 19:43:06 +01:00
|
|
|
// GetPayment return storagenode payment by nodeID and period.
|
|
|
|
GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (StoragenodePayment, error)
|
|
|
|
// CreatePayment insert payment into db.
|
|
|
|
CreatePayment(ctx context.Context, payment StoragenodePayment) (err error)
|
|
|
|
// GetAllPayments return all payments by nodeID.
|
|
|
|
GetAllPayments(ctx context.Context, nodeID storj.NodeID) ([]StoragenodePayment, error)
|
2020-03-13 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// ErrNoDataForPeriod represents errors from the payout database.
|
2020-03-24 12:39:13 +00:00
|
|
|
var ErrNoDataForPeriod = errs.Class("no payStub/payments for period error")
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// Error is the default error class for payout package.
|
|
|
|
var Error = errs.Class("payout db error")
|
2020-06-26 19:43:06 +01:00
|
|
|
|
2020-03-13 15:56:25 +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 {
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2020-06-26 19:43:06 +01:00
|
|
|
// StoragenodePayment is an entity that holds payment to storagenode operator parameters.
|
|
|
|
type StoragenodePayment struct {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if err != nil {
|
2020-06-26 19:43:06 +01:00
|
|
|
return PayStub{}, Error.Wrap(err)
|
2020-03-13 15:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return payStub, nil
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:35:38 +01:00
|
|
|
// GetAllPaystubs returns all paystubs by nodeID.
|
|
|
|
func (service *Service) GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) ([]PayStub, error) {
|
|
|
|
payStubs, err := service.db.GetAllPaystubs(ctx, nodeID)
|
|
|
|
if err != nil {
|
2020-06-26 19:43:06 +01:00
|
|
|
return []PayStub{}, Error.Wrap(err)
|
2020-04-19 16:35:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return payStubs, nil
|
|
|
|
}
|
2020-06-26 19:43:06 +01:00
|
|
|
|
|
|
|
// GetPayment returns storagenode payment data by nodeID and period.
|
|
|
|
func (service *Service) GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (StoragenodePayment, error) {
|
|
|
|
payment, err := service.db.GetPayment(ctx, nodeID, period)
|
|
|
|
if err != nil {
|
|
|
|
return StoragenodePayment{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllPayments returns all payments by nodeID.
|
|
|
|
func (service *Service) GetAllPayments(ctx context.Context, nodeID storj.NodeID) ([]StoragenodePayment, error) {
|
|
|
|
payments, err := service.db.GetAllPayments(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payments, nil
|
|
|
|
}
|