satellite/heldamount/endpoint : GetAllPaystubs added

Change-Id: Ic8cdd9db8b2a68796f9579c7fed2d49d9054bd64
This commit is contained in:
Qweder93 2020-04-19 18:35:38 +03:00
parent 31c9cc322f
commit 6e3585e394
6 changed files with 258 additions and 1 deletions

2
go.mod
View File

@ -41,7 +41,7 @@ require (
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
google.golang.org/grpc v1.28.0
storj.io/common v0.0.0-20200416175331-40469cc6b6d5
storj.io/common v0.0.0-20200419143955-e54806ee8c56
storj.io/drpc v0.0.12-0.20200413163255-debb458a7474
storj.io/monkit-jaeger v0.0.0-20200403204040-f5a746eeacca
storj.io/private v0.0.0-20200403212157-26f222c154f0

2
go.sum
View File

@ -625,6 +625,8 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
storj.io/common v0.0.0-20200402141523-7780ee0cca0d/go.mod h1:pZyXiIE7bGETIRXtfs0nICqMwp7PM8HqnDuyUeldNA0=
storj.io/common v0.0.0-20200416175331-40469cc6b6d5 h1:ApFVw3uGVbUqPPkJbTVq8wcqJFmJRU24uhjdoedsj/M=
storj.io/common v0.0.0-20200416175331-40469cc6b6d5/go.mod h1:pZyXiIE7bGETIRXtfs0nICqMwp7PM8HqnDuyUeldNA0=
storj.io/common v0.0.0-20200419143955-e54806ee8c56 h1:SACDryIjBnW4sqkHbzEpAmB0PhJYv5/mmJCHsuStsKY=
storj.io/common v0.0.0-20200419143955-e54806ee8c56/go.mod h1:pZyXiIE7bGETIRXtfs0nICqMwp7PM8HqnDuyUeldNA0=
storj.io/drpc v0.0.11 h1:6vLxfpSbwCLtqzAoXzXx/SxBqBtbzbmquXPqfcWKqfw=
storj.io/drpc v0.0.11 h1:6vLxfpSbwCLtqzAoXzXx/SxBqBtbzbmquXPqfcWKqfw=
storj.io/drpc v0.0.11/go.mod h1:TiFc2obNjL9/3isMW1Rpxjy8V9uE0B2HMeMFGiiI7Iw=

View File

@ -96,6 +96,73 @@ func (e *Endpoint) GetPayStub(ctx context.Context, req *pb.GetHeldAmountRequest)
}, nil
}
// 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.Error(rpcstatus.Unauthenticated, err.Error())
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
if overlay.ErrNodeNotFound.Has(err) {
return nil, rpcstatus.Error(rpcstatus.PermissionDenied, err.Error())
}
e.log.Error("overlay.Get failed", zap.Error(err))
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
stubs, err := e.service.GetAllPaystubs(ctx, node.Id)
if err != nil {
if ErrNoDataForPeriod.Has(err) {
return nil, rpcstatus.Error(rpcstatus.OutOfRange, err.Error())
}
return nil, err
}
var paystubs []*pb.GetHeldAmountResponse
response := pb.GetAllPaystubsResponse{
Paystub: paystubs,
}
for i := 0; i < len(stubs); i++ {
period, err := date.PeriodToTime(stubs[i].Period)
if err != nil {
return nil, err
}
heldAmountResponse := pb.GetHeldAmountResponse{
Period: period,
NodeId: stubs[i].NodeID,
CreatedAt: stubs[i].Created,
Codes: stubs[i].Codes,
UsageAtRest: float32(stubs[i].UsageAtRest),
UsageGet: stubs[i].UsageGet,
UsagePut: stubs[i].UsagePut,
UsageGetRepair: stubs[i].UsageGetRepair,
UsagePutRepair: stubs[i].UsagePutRepair,
UsageGetAudit: stubs[i].UsageGetAudit,
CompAtRest: stubs[i].CompAtRest,
CompGet: stubs[i].CompGet,
CompPut: stubs[i].CompPut,
CompGetRepair: stubs[i].CompGetRepair,
CompPutRepair: stubs[i].CompPutRepair,
CompGetAudit: stubs[i].CompGetAudit,
SurgePercent: stubs[i].SurgePercent,
Held: stubs[i].Held,
Owed: stubs[i].Owed,
Disposed: stubs[i].Disposed,
Paid: stubs[i].Paid,
}
response.Paystub = append(response.Paystub, &heldAmountResponse)
}
return &response, nil
}
// 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)

View File

@ -19,6 +19,8 @@ import (
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.
@ -92,6 +94,16 @@ func (service *Service) GetPayStub(ctx context.Context, nodeID storj.NodeID, per
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)

View File

@ -45,9 +45,61 @@ func TestHeldAmountDB(t *testing.T) {
Paid: 17,
}
paystub2 := heldamount.PayStub{
Period: "2020-02",
NodeID: NodeID,
Created: time.Now().UTC(),
Codes: "2",
UsageAtRest: 4,
UsageGet: 5,
UsagePut: 6,
UsageGetRepair: 7,
UsagePutRepair: 8,
UsageGetAudit: 9,
CompAtRest: 10,
CompGet: 11,
CompPut: 12,
CompGetRepair: 13,
CompPutRepair: 14,
CompGetAudit: 15,
SurgePercent: 16,
Held: 17,
Owed: 18,
Disposed: 19,
Paid: 20,
}
paystub3 := heldamount.PayStub{
Period: "2020-03",
NodeID: NodeID,
Created: time.Now().UTC(),
Codes: "33",
UsageAtRest: 10,
UsageGet: 11,
UsagePut: 12,
UsageGetRepair: 13,
UsagePutRepair: 14,
UsageGetAudit: 15,
CompAtRest: 16,
CompGet: 17,
CompPut: 18,
CompGetRepair: 19,
CompPutRepair: 20,
CompGetAudit: 21,
SurgePercent: 22,
Held: 23,
Owed: 24,
Disposed: 25,
Paid: 26,
}
t.Run("Test StorePayStub", func(t *testing.T) {
err := heldAmount.CreatePaystub(ctx, paystub)
assert.NoError(t, err)
err = heldAmount.CreatePaystub(ctx, paystub2)
assert.NoError(t, err)
err = heldAmount.CreatePaystub(ctx, paystub3)
assert.NoError(t, err)
})
t.Run("Test GetPayStub", func(t *testing.T) {
@ -81,6 +133,79 @@ func TestHeldAmountDB(t *testing.T) {
assert.Error(t, err)
})
t.Run("Test GetAllPaystubs", func(t *testing.T) {
stubs, err := heldAmount.GetAllPaystubs(ctx, NodeID)
assert.NoError(t, err)
for i := 0; i < len(stubs); i++ {
if stubs[i].Period == "2020-01" {
assert.Equal(t, stubs[i].Period, paystub.Period)
assert.Equal(t, stubs[i].Codes, paystub.Codes)
assert.Equal(t, stubs[i].CompAtRest, paystub.CompAtRest)
assert.Equal(t, stubs[i].CompGet, paystub.CompGet)
assert.Equal(t, stubs[i].CompGetAudit, paystub.CompGetAudit)
assert.Equal(t, stubs[i].CompGetRepair, paystub.CompGetRepair)
assert.Equal(t, stubs[i].CompPut, paystub.CompPut)
assert.Equal(t, stubs[i].CompPutRepair, paystub.CompPutRepair)
assert.Equal(t, stubs[i].Disposed, paystub.Disposed)
assert.Equal(t, stubs[i].Held, paystub.Held)
assert.Equal(t, stubs[i].Owed, paystub.Owed)
assert.Equal(t, stubs[i].Paid, paystub.Paid)
assert.Equal(t, stubs[i].NodeID, paystub.NodeID)
assert.Equal(t, stubs[i].SurgePercent, paystub.SurgePercent)
assert.Equal(t, stubs[i].UsageAtRest, paystub.UsageAtRest)
assert.Equal(t, stubs[i].UsageGet, paystub.UsageGet)
assert.Equal(t, stubs[i].UsageGetAudit, paystub.UsageGetAudit)
assert.Equal(t, stubs[i].UsageGetRepair, paystub.UsageGetRepair)
assert.Equal(t, stubs[i].UsagePut, paystub.UsagePut)
assert.Equal(t, stubs[i].UsagePutRepair, paystub.UsagePutRepair)
}
if stubs[i].Period == "2020-02" {
assert.Equal(t, stubs[i].Period, paystub2.Period)
assert.Equal(t, stubs[i].Codes, paystub2.Codes)
assert.Equal(t, stubs[i].CompAtRest, paystub2.CompAtRest)
assert.Equal(t, stubs[i].CompGet, paystub2.CompGet)
assert.Equal(t, stubs[i].CompGetAudit, paystub2.CompGetAudit)
assert.Equal(t, stubs[i].CompGetRepair, paystub2.CompGetRepair)
assert.Equal(t, stubs[i].CompPut, paystub2.CompPut)
assert.Equal(t, stubs[i].CompPutRepair, paystub2.CompPutRepair)
assert.Equal(t, stubs[i].Disposed, paystub2.Disposed)
assert.Equal(t, stubs[i].Held, paystub2.Held)
assert.Equal(t, stubs[i].Owed, paystub2.Owed)
assert.Equal(t, stubs[i].Paid, paystub2.Paid)
assert.Equal(t, stubs[i].NodeID, paystub2.NodeID)
assert.Equal(t, stubs[i].SurgePercent, paystub2.SurgePercent)
assert.Equal(t, stubs[i].UsageAtRest, paystub2.UsageAtRest)
assert.Equal(t, stubs[i].UsageGet, paystub2.UsageGet)
assert.Equal(t, stubs[i].UsageGetAudit, paystub2.UsageGetAudit)
assert.Equal(t, stubs[i].UsageGetRepair, paystub2.UsageGetRepair)
assert.Equal(t, stubs[i].UsagePut, paystub2.UsagePut)
assert.Equal(t, stubs[i].UsagePutRepair, paystub2.UsagePutRepair)
}
if stubs[i].Period == "2020-03" {
assert.Equal(t, stubs[i].Period, paystub3.Period)
assert.Equal(t, stubs[i].Codes, paystub3.Codes)
assert.Equal(t, stubs[i].CompAtRest, paystub3.CompAtRest)
assert.Equal(t, stubs[i].CompGet, paystub3.CompGet)
assert.Equal(t, stubs[i].CompGetAudit, paystub3.CompGetAudit)
assert.Equal(t, stubs[i].CompGetRepair, paystub3.CompGetRepair)
assert.Equal(t, stubs[i].CompPut, paystub3.CompPut)
assert.Equal(t, stubs[i].CompPutRepair, paystub3.CompPutRepair)
assert.Equal(t, stubs[i].Disposed, paystub3.Disposed)
assert.Equal(t, stubs[i].Held, paystub3.Held)
assert.Equal(t, stubs[i].Owed, paystub3.Owed)
assert.Equal(t, stubs[i].Paid, paystub3.Paid)
assert.Equal(t, stubs[i].NodeID, paystub3.NodeID)
assert.Equal(t, stubs[i].SurgePercent, paystub3.SurgePercent)
assert.Equal(t, stubs[i].UsageAtRest, paystub3.UsageAtRest)
assert.Equal(t, stubs[i].UsageGet, paystub3.UsageGet)
assert.Equal(t, stubs[i].UsageGetAudit, paystub3.UsageGetAudit)
assert.Equal(t, stubs[i].UsageGetRepair, paystub3.UsageGetRepair)
assert.Equal(t, stubs[i].UsagePut, paystub3.UsagePut)
assert.Equal(t, stubs[i].UsagePutRepair, paystub3.UsagePutRepair)
}
}
})
payment := heldamount.StoragenodePayment{
ID: 1,
Created: time.Now().UTC(),

View File

@ -7,6 +7,8 @@ import (
"context"
"database/sql"
"github.com/zeebo/errs"
"storj.io/storj/pkg/storj"
"storj.io/storj/satellite/heldamount"
"storj.io/storj/satellite/satellitedb/dbx"
@ -58,6 +60,55 @@ func (paystubs *paymentStubs) GetPaystub(ctx context.Context, nodeID storj.NodeI
return payStub, nil
}
// GetAllPaystubs return all payStubs by nodeID.
func (paystubs *paymentStubs) GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) (payStubs []heldamount.PayStub, err error) {
query := `SELECT * FROM storagenode_paystubs WHERE node_id = $1;`
rows, err := paystubs.db.QueryContext(ctx, query, nodeID)
if err != nil {
return []heldamount.PayStub{}, Error.Wrap(err)
}
defer func() {
err = errs.Combine(err, Error.Wrap(rows.Close()))
}()
for rows.Next() {
paystub := heldamount.PayStub{}
err = rows.Scan(
&paystub.Period,
&paystub.NodeID,
&paystub.Created,
&paystub.Codes,
&paystub.UsageAtRest,
&paystub.UsageGet,
&paystub.UsagePut,
&paystub.UsageGetRepair,
&paystub.UsagePutRepair,
&paystub.UsageGetAudit,
&paystub.CompAtRest,
&paystub.CompGet,
&paystub.CompPut,
&paystub.CompGetRepair,
&paystub.CompPutRepair,
&paystub.CompGetAudit,
&paystub.SurgePercent,
&paystub.Held,
&paystub.Owed,
&paystub.Disposed,
&paystub.Paid,
)
if err = rows.Err(); err != nil {
return []heldamount.PayStub{}, Error.Wrap(err)
}
payStubs = append(payStubs, paystub)
}
return payStubs, Error.Wrap(rows.Err())
}
// GetPayment returns payment by nodeID and period.
func (paystubs *paymentStubs) GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (payment heldamount.StoragenodePayment, err error) {
query := `SELECT * FROM storagenode_payments WHERE node_id = $1 AND period = $2;`