-
Notifications
You must be signed in to change notification settings - Fork 394
/
endpoint.go
215 lines (183 loc) · 5.84 KB
/
endpoint.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package snopayouts
import (
"context"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"storj.io/common/identity"
"storj.io/common/pb"
"storj.io/common/rpc/rpcstatus"
"storj.io/storj/private/date"
"storj.io/storj/satellite/accounting"
"storj.io/storj/satellite/overlay"
)
var (
mon = monkit.Package()
)
// Endpoint for querying node stats for the SNO.
//
// architecture: Endpoint
type Endpoint struct {
pb.DRPCHeldAmountUnimplementedServer
service *Service
log *zap.Logger
overlay overlay.DB
accounting accounting.StoragenodeAccounting
}
// NewEndpoint creates new endpoint.
func NewEndpoint(log *zap.Logger, accounting accounting.StoragenodeAccounting, overlay overlay.DB, service *Service) *Endpoint {
return &Endpoint{
log: log,
accounting: accounting,
overlay: overlay,
service: service,
}
}
// GetPayStub sends node paystub for client node.
func (e *Endpoint) GetPayStub(ctx context.Context, req *pb.GetHeldAmountRequest) (_ *pb.GetHeldAmountResponse, err error) {
defer mon.Task()(&ctx)(&err)
peer, err := identity.PeerIdentityFromContext(ctx)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
paystub, err := e.service.GetPaystub(ctx, node.Id, req.Period.Format("2006-01"))
if err != nil {
if ErrNoDataForPeriod.Has(err) {
return nil, rpcstatus.Wrap(rpcstatus.OutOfRange, err)
}
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
return convertPaystub(paystub)
}
// GetAllPaystubs sends all paystubs for client node.
func (e *Endpoint) GetAllPaystubs(ctx context.Context, req *pb.GetAllPaystubsRequest) (_ *pb.GetAllPaystubsResponse, err error) {
defer mon.Task()(&ctx)(&err)
peer, err := identity.PeerIdentityFromContext(ctx)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
if overlay.ErrNodeNotFound.Has(err) {
return &pb.GetAllPaystubsResponse{}, nil
}
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
paystubs, err := e.service.GetAllPaystubs(ctx, node.Id)
if err != nil {
if ErrNoDataForPeriod.Has(err) {
return nil, rpcstatus.Wrap(rpcstatus.OutOfRange, err)
}
return nil, Error.Wrap(err)
}
response := &pb.GetAllPaystubsResponse{}
for _, paystub := range paystubs {
pbPaystub, err := convertPaystub(paystub)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
response.Paystub = append(response.Paystub, pbPaystub)
}
return response, nil
}
func convertPaystub(paystub Paystub) (*pb.GetHeldAmountResponse, error) {
period, err := date.PeriodToTime(paystub.Period)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Internal, Error.Wrap(err))
}
return &pb.GetHeldAmountResponse{
Period: period,
NodeId: paystub.NodeID,
CreatedAt: paystub.Created,
Codes: paystub.Codes,
UsageAtRest: paystub.UsageAtRest,
UsageGet: paystub.UsageGet,
UsagePut: paystub.UsagePut,
UsageGetRepair: paystub.UsageGetRepair,
UsagePutRepair: paystub.UsagePutRepair,
UsageGetAudit: paystub.UsageGetAudit,
CompAtRest: paystub.CompAtRest,
CompGet: paystub.CompGet,
CompPut: paystub.CompPut,
CompGetRepair: paystub.CompGetRepair,
CompPutRepair: paystub.CompPutRepair,
CompGetAudit: paystub.CompGetAudit,
SurgePercent: paystub.SurgePercent,
Held: paystub.Held,
Owed: paystub.Owed,
Disposed: paystub.Disposed,
Paid: paystub.Paid,
Distributed: paystub.Distributed,
}, err
}
// GetPayment sends node payment data for client node.
func (e *Endpoint) GetPayment(ctx context.Context, req *pb.GetPaymentRequest) (_ *pb.GetPaymentResponse, err error) {
defer mon.Task()(&ctx)(&err)
peer, err := identity.PeerIdentityFromContext(ctx)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
payment, err := e.service.GetPayment(ctx, node.Id, req.Period.String())
if err != nil {
if ErrNoDataForPeriod.Has(err) {
return nil, rpcstatus.Wrap(rpcstatus.OutOfRange, err)
}
return nil, Error.Wrap(err)
}
return convertPayment(payment)
}
// GetAllPayments sends all payments to node.
func (e *Endpoint) GetAllPayments(ctx context.Context, req *pb.GetAllPaymentsRequest) (_ *pb.GetAllPaymentsResponse, err error) {
defer mon.Task()(&ctx)(&err)
peer, err := identity.PeerIdentityFromContext(ctx)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
if overlay.ErrNodeNotFound.Has(err) {
return &pb.GetAllPaymentsResponse{}, nil
}
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
payments, err := e.service.GetAllPayments(ctx, node.Id)
if err != nil {
if ErrNoDataForPeriod.Has(err) {
return nil, rpcstatus.Wrap(rpcstatus.OutOfRange, err)
}
return nil, Error.Wrap(err)
}
response := &pb.GetAllPaymentsResponse{}
for _, payment := range payments {
pbPayment, err := convertPayment(payment)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
}
response.Payment = append(response.Payment, pbPayment)
}
return response, nil
}
func convertPayment(payment Payment) (*pb.GetPaymentResponse, error) {
period, err := date.PeriodToTime(payment.Period)
if err != nil {
return nil, rpcstatus.Wrap(rpcstatus.Internal, Error.Wrap(err))
}
return &pb.GetPaymentResponse{
Id: payment.ID,
CreatedAt: payment.Created,
NodeId: payment.NodeID,
Period: period,
Amount: payment.Amount,
Receipt: payment.Receipt,
Notes: payment.Notes,
}, nil
}