-
Notifications
You must be signed in to change notification settings - Fork 390
/
payouts.go
133 lines (112 loc) · 4.3 KB
/
payouts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package snopayouts
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/storj"
)
// DB exposes all needed functionality to manage payouts.
//
// 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) (Payment, error)
// GetAllPayments return all payments by nodeID.
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)
}
// ErrNoDataForPeriod represents errors from the payouts database.
var ErrNoDataForPeriod = errs.Class("no payStub/payments for period")
// Error is the default error class for payouts package.
var Error = errs.Class("payoutsdb")
// 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"`
Distributed int64 `json:"distributed"`
}
// Payment is an entity that holds payment to storagenode operator parameters.
type Payment 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{}, Error.Wrap(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{}, Error.Wrap(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) (Payment, error) {
payment, err := service.db.GetPayment(ctx, nodeID, period)
if err != nil {
return Payment{}, Error.Wrap(err)
}
return payment, nil
}
// GetAllPayments returns all payments by nodeID.
func (service *Service) GetAllPayments(ctx context.Context, nodeID storj.NodeID) ([]Payment, error) {
payments, err := service.db.GetAllPayments(ctx, nodeID)
if err != nil {
return nil, Error.Wrap(err)
}
return payments, nil
}