storj/satellite/heldamount/heldamount.go
Qweder93 6e3585e394 satellite/heldamount/endpoint : GetAllPaystubs added
Change-Id: Ic8cdd9db8b2a68796f9579c7fed2d49d9054bd64
2020-04-19 19:21:54 +03:00

116 lines
3.7 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package heldamount
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/storj"
)
// DB exposes all needed functionality to manage heldAmount.
//
// architecture: Service
type DB interface {
// GetPaystub return payStub by nodeID and period.
GetPaystub(ctx context.Context, nodeID storj.NodeID, period string) (PayStub, error)
// GetAllPaystubs return all payStubs by nodeID.
GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) ([]PayStub, error)
// GetPayment return storagenode payment by nodeID and period.
GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (StoragenodePayment, error)
// CreatePaystub insert paystub into db.
CreatePaystub(ctx context.Context, stub PayStub) (err error)
// CreatePayment insert payment into db.
CreatePayment(ctx context.Context, payment StoragenodePayment) (err error)
}
// ErrNoDataForPeriod represents errors from the heldamount database.
var ErrNoDataForPeriod = errs.Class("no payStub/payments for period error")
// 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"`
}
// 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"`
}
// Service is used to store and handle node paystub information
//
// architecture: Service
type Service struct {
log *zap.Logger
db DB
}
// NewService returns a new Service
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 {
return PayStub{}, err
}
return payStub, nil
}
// 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 {
return []PayStub{}, err
}
return payStubs, nil
}
// 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{}, err
}
return payment, nil
}