storagenode/console/consoleapi: using cached data in heldamount api

Change-Id: I0efca320eaf722ade1146100bbb0e70d75a5dca3
This commit is contained in:
crawter 2020-03-16 01:24:04 +02:00
parent 9f84261c36
commit 89374e260d
3 changed files with 33 additions and 13 deletions

View File

@ -49,7 +49,7 @@ func (heldamount *HeldAmount) GetMonthlyHeldAmount(w http.ResponseWriter, r *htt
return
}
paystubData, err := heldamount.service.GetPaystubStats(ctx, satelliteID, period)
paystubData, err := heldamount.service.GetPaystubStatsCached(ctx, satelliteID, period)
if err != nil {
heldamount.serveJSONError(w, http.StatusInternalServerError, ErrHeldAmountPI.Wrap(err))
return
@ -78,7 +78,7 @@ func (heldamount *HeldAmount) GetMonthlyPayment(w http.ResponseWriter, r *http.R
return
}
paymentData, err := heldamount.service.GetPayment(ctx, satelliteID, period)
paymentData, err := heldamount.service.GetPaymentCached(ctx, satelliteID, period)
if err != nil {
heldamount.serveJSONError(w, http.StatusInternalServerError, ErrHeldAmountPI.Wrap(err))
return

View File

@ -18,8 +18,8 @@ import (
)
var (
// HeldAmountServiceErr defines held amount service error
HeldAmountServiceErr = errs.Class("heldamount service error")
// ErrHeldAmountService defines held amount service error
ErrHeldAmountService = errs.Class("heldamount service error")
mon = monkit.Package()
)
@ -37,45 +37,50 @@ func (c *Client) Close() error {
return c.conn.Close()
}
// TODO: separate service on service and endpoint.
// Service retrieves info from satellites using an rpc client
//
// architecture: Service
type Service struct {
log *zap.Logger
db DB
dialer rpc.Dialer
trust *trust.Pool
}
// NewService creates new instance of service
func NewService(log *zap.Logger, dialer rpc.Dialer, trust *trust.Pool) *Service {
func NewService(log *zap.Logger, db DB, dialer rpc.Dialer, trust *trust.Pool) *Service {
return &Service{
log: log,
db: db,
dialer: dialer,
trust: trust,
}
}
// GetPaystubStats retrieves held amount for particular satellite
// GetPaystubStats retrieves held amount for particular satellite from satellite using grpc.
func (service *Service) GetPaystubStats(ctx context.Context, satelliteID storj.NodeID, period string) (_ *PayStub, err error) {
defer mon.Task()(&ctx)(&err)
client, err := service.dial(ctx, satelliteID)
if err != nil {
return nil, HeldAmountServiceErr.Wrap(err)
return nil, ErrHeldAmountService.Wrap(err)
}
defer func() { err = errs.Combine(err, client.Close()) }()
requestedPeriod, err := stringToTime(period)
if err != nil {
service.log.Error("stringToTime", zap.Error(err))
return nil, HeldAmountServiceErr.Wrap(err)
return nil, ErrHeldAmountService.Wrap(err)
}
resp, err := client.GetPayStub(ctx, &pb.GetHeldAmountRequest{Period: requestedPeriod})
if err != nil {
service.log.Error("GetPayStub", zap.Error(err))
return nil, HeldAmountServiceErr.Wrap(err)
return nil, ErrHeldAmountService.Wrap(err)
}
service.log.Error("paystub = = = =", zap.Any("", resp))
return &PayStub{
@ -103,24 +108,24 @@ func (service *Service) GetPaystubStats(ctx context.Context, satelliteID storj.N
}, nil
}
// GetPayment retrieves payment data from particular satellite
// GetPayment retrieves payment data from particular satellite using grpc.
func (service *Service) GetPayment(ctx context.Context, satelliteID storj.NodeID, period string) (_ *Payment, err error) {
defer mon.Task()(&ctx)(&err)
client, err := service.dial(ctx, satelliteID)
if err != nil {
return nil, HeldAmountServiceErr.Wrap(err)
return nil, ErrHeldAmountService.Wrap(err)
}
defer func() { err = errs.Combine(err, client.Close()) }()
requestedPeriod, err := stringToTime(period)
if err != nil {
return nil, HeldAmountServiceErr.Wrap(err)
return nil, ErrHeldAmountService.Wrap(err)
}
resp, err := client.GetPayment(ctx, &pb.GetPaymentRequest{Period: requestedPeriod})
if err != nil {
return nil, HeldAmountServiceErr.Wrap(err)
return nil, ErrHeldAmountService.Wrap(err)
}
return &Payment{
@ -134,6 +139,20 @@ func (service *Service) GetPayment(ctx context.Context, satelliteID storj.NodeID
}, nil
}
// GetPaystubStatsCached retrieves held amount for particular satellite from storagenode database.
func (service *Service) GetPaystubStatsCached(ctx context.Context, satelliteID storj.NodeID, period string) (_ *PayStub, err error) {
defer mon.Task()(&ctx, &satelliteID, &period)(&err)
return service.db.GetPayStub(ctx, satelliteID, period)
}
// GetPaymentCached retrieves payment data from particular satellite from storagenode database.
func (service *Service) GetPaymentCached(ctx context.Context, satelliteID storj.NodeID, period string) (_ *Payment, err error) {
defer mon.Task()(&ctx, &satelliteID, &period)(&err)
return service.db.GetPayment(ctx, satelliteID, period)
}
// dial dials the HeldAmount client for the satellite by id
func (service *Service) dial(ctx context.Context, satelliteID storj.NodeID) (_ *Client, err error) {
defer mon.Task()(&ctx)(&err)

View File

@ -491,6 +491,7 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, revocationDB exten
{ // setup heldamount service.
peer.Heldamount.Service = heldamount.NewService(
peer.Log.Named("heldamount:service"),
peer.DB.HeldAmount(),
peer.Dialer,
peer.Storage2.Trust,
)