storanode, satellite: payout renamed to payouts, expected estimation payouts added, console api for audits reworked
Change-Id: I4aa5e99bffaa87d0a800a429a4c83aa498ad4b7b
This commit is contained in:
parent
1ed7227521
commit
6ba8f6c8a9
1
go.mod
1
go.mod
@ -20,6 +20,7 @@ require (
|
||||
github.com/jackc/pgconn v1.7.0
|
||||
github.com/jackc/pgtype v1.5.0
|
||||
github.com/jackc/pgx/v4 v4.9.0
|
||||
github.com/jinzhu/now v1.1.1
|
||||
github.com/jtolds/monkit-hw/v2 v2.0.0-20191108235325-141a0da276b3
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
||||
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce
|
||||
|
2
go.sum
2
go.sum
@ -304,6 +304,8 @@ github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
|
||||
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.1.2/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
|
||||
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
||||
|
@ -51,7 +51,7 @@ import (
|
||||
"storj.io/storj/satellite/referrals"
|
||||
"storj.io/storj/satellite/repair/irreparable"
|
||||
"storj.io/storj/satellite/rewards"
|
||||
"storj.io/storj/satellite/snopayout"
|
||||
"storj.io/storj/satellite/snopayouts"
|
||||
)
|
||||
|
||||
// API is the satellite API process.
|
||||
@ -157,9 +157,9 @@ type API struct {
|
||||
}
|
||||
|
||||
SnoPayout struct {
|
||||
Endpoint *snopayout.Endpoint
|
||||
Service *snopayout.Service
|
||||
DB snopayout.DB
|
||||
Endpoint *snopayouts.Endpoint
|
||||
Service *snopayouts.Service
|
||||
DB snopayouts.DB
|
||||
}
|
||||
|
||||
GracefulExit struct {
|
||||
@ -655,11 +655,11 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
|
||||
|
||||
{ // setup SnoPayout endpoint
|
||||
peer.SnoPayout.DB = peer.DB.SnoPayout()
|
||||
peer.SnoPayout.Service = snopayout.NewService(
|
||||
peer.Log.Named("payout:service"),
|
||||
peer.SnoPayout.Service = snopayouts.NewService(
|
||||
peer.Log.Named("payouts:service"),
|
||||
peer.SnoPayout.DB)
|
||||
peer.SnoPayout.Endpoint = snopayout.NewEndpoint(
|
||||
peer.Log.Named("payout:endpoint"),
|
||||
peer.SnoPayout.Endpoint = snopayouts.NewEndpoint(
|
||||
peer.Log.Named("payouts:endpoint"),
|
||||
peer.DB.StoragenodeAccounting(),
|
||||
peer.Overlay.DB,
|
||||
peer.SnoPayout.Service)
|
||||
|
@ -45,7 +45,7 @@ import (
|
||||
"storj.io/storj/satellite/repair/repairer"
|
||||
"storj.io/storj/satellite/revocation"
|
||||
"storj.io/storj/satellite/rewards"
|
||||
"storj.io/storj/satellite/snopayout"
|
||||
"storj.io/storj/satellite/snopayouts"
|
||||
)
|
||||
|
||||
var mon = monkit.Package()
|
||||
@ -96,8 +96,8 @@ type DB interface {
|
||||
GracefulExit() gracefulexit.DB
|
||||
// StripeCoinPayments returns stripecoinpayments database.
|
||||
StripeCoinPayments() stripecoinpayments.DB
|
||||
// SnoPayout returns database for payout.
|
||||
SnoPayout() snopayout.DB
|
||||
// SnoPayout returns database for payouts.
|
||||
SnoPayout() snopayouts.DB
|
||||
// Compoensation tracks storage node compensation
|
||||
Compensation() compensation.DB
|
||||
// Revocation tracks revoked macaroons
|
||||
|
@ -33,7 +33,7 @@ import (
|
||||
"storj.io/storj/satellite/revocation"
|
||||
"storj.io/storj/satellite/rewards"
|
||||
"storj.io/storj/satellite/satellitedb/dbx"
|
||||
"storj.io/storj/satellite/snopayout"
|
||||
"storj.io/storj/satellite/snopayouts"
|
||||
)
|
||||
|
||||
// Error is the default satellitedb errs class.
|
||||
@ -278,8 +278,8 @@ func (dbc *satelliteDBCollection) StripeCoinPayments() stripecoinpayments.DB {
|
||||
}
|
||||
|
||||
// SnoPayout returns database for storagenode payStubs and payments info.
|
||||
func (dbc *satelliteDBCollection) SnoPayout() snopayout.DB {
|
||||
return &paymentStubs{db: dbc.getByName("snopayout")}
|
||||
func (dbc *satelliteDBCollection) SnoPayout() snopayouts.DB {
|
||||
return &paymentStubs{db: dbc.getByName("snopayouts")}
|
||||
}
|
||||
|
||||
// Compenstation returns database for storage node compensation.
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
|
||||
"storj.io/common/storj"
|
||||
"storj.io/storj/satellite/satellitedb/dbx"
|
||||
"storj.io/storj/satellite/snopayout"
|
||||
"storj.io/storj/satellite/snopayouts"
|
||||
)
|
||||
|
||||
// paymentStubs is payment data for specific storagenode for some specific period by working with satellite.
|
||||
@ -23,7 +23,7 @@ type paymentStubs struct {
|
||||
}
|
||||
|
||||
// GetPaystub returns payStub by nodeID and period.
|
||||
func (paystubs *paymentStubs) GetPaystub(ctx context.Context, nodeID storj.NodeID, period string) (payStub snopayout.PayStub, err error) {
|
||||
func (paystubs *paymentStubs) GetPaystub(ctx context.Context, nodeID storj.NodeID, period string) (payStub snopayouts.PayStub, err error) {
|
||||
query := `SELECT * FROM storagenode_paystubs WHERE node_id = $1 AND period = $2;`
|
||||
|
||||
row := paystubs.db.QueryRowContext(ctx, query, nodeID, period)
|
||||
@ -52,22 +52,22 @@ func (paystubs *paymentStubs) GetPaystub(ctx context.Context, nodeID storj.NodeI
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return snopayout.PayStub{}, snopayout.ErrNoDataForPeriod.Wrap(err)
|
||||
return snopayouts.PayStub{}, snopayouts.ErrNoDataForPeriod.Wrap(err)
|
||||
}
|
||||
|
||||
return snopayout.PayStub{}, Error.Wrap(err)
|
||||
return snopayouts.PayStub{}, Error.Wrap(err)
|
||||
}
|
||||
|
||||
return payStub, nil
|
||||
}
|
||||
|
||||
// GetAllPaystubs return all payStubs by nodeID.
|
||||
func (paystubs *paymentStubs) GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) (payStubs []snopayout.PayStub, err error) {
|
||||
func (paystubs *paymentStubs) GetAllPaystubs(ctx context.Context, nodeID storj.NodeID) (payStubs []snopayouts.PayStub, err error) {
|
||||
query := `SELECT * FROM storagenode_paystubs WHERE node_id = $1;`
|
||||
|
||||
rows, err := paystubs.db.QueryContext(ctx, query, nodeID)
|
||||
if err != nil {
|
||||
return []snopayout.PayStub{}, Error.Wrap(err)
|
||||
return []snopayouts.PayStub{}, Error.Wrap(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@ -75,7 +75,7 @@ func (paystubs *paymentStubs) GetAllPaystubs(ctx context.Context, nodeID storj.N
|
||||
}()
|
||||
|
||||
for rows.Next() {
|
||||
paystub := snopayout.PayStub{}
|
||||
paystub := snopayouts.PayStub{}
|
||||
|
||||
err = rows.Scan(
|
||||
&paystub.Period,
|
||||
@ -101,7 +101,7 @@ func (paystubs *paymentStubs) GetAllPaystubs(ctx context.Context, nodeID storj.N
|
||||
&paystub.Paid,
|
||||
)
|
||||
if err = rows.Err(); err != nil {
|
||||
return []snopayout.PayStub{}, Error.Wrap(err)
|
||||
return []snopayouts.PayStub{}, Error.Wrap(err)
|
||||
}
|
||||
|
||||
payStubs = append(payStubs, paystub)
|
||||
@ -111,7 +111,7 @@ func (paystubs *paymentStubs) GetAllPaystubs(ctx context.Context, nodeID storj.N
|
||||
}
|
||||
|
||||
// CreatePaystub inserts storagenode_paystub into database.
|
||||
func (paystubs *paymentStubs) CreatePaystub(ctx context.Context, stub snopayout.PayStub) (err error) {
|
||||
func (paystubs *paymentStubs) CreatePaystub(ctx context.Context, stub snopayouts.PayStub) (err error) {
|
||||
return paystubs.db.CreateNoReturn_StoragenodePaystub(
|
||||
ctx,
|
||||
dbx.StoragenodePaystub_Period(stub.Period),
|
||||
@ -138,7 +138,7 @@ func (paystubs *paymentStubs) CreatePaystub(ctx context.Context, stub snopayout.
|
||||
}
|
||||
|
||||
// GetPayment returns payment by nodeID and period.
|
||||
func (paystubs *paymentStubs) GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (payment snopayout.StoragenodePayment, err error) {
|
||||
func (paystubs *paymentStubs) GetPayment(ctx context.Context, nodeID storj.NodeID, period string) (payment snopayouts.StoragenodePayment, err error) {
|
||||
query := `SELECT * FROM storagenode_payments WHERE node_id = $1 AND period = $2;`
|
||||
|
||||
row := paystubs.db.QueryRowContext(ctx, query, nodeID, period)
|
||||
@ -153,17 +153,17 @@ func (paystubs *paymentStubs) GetPayment(ctx context.Context, nodeID storj.NodeI
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return snopayout.StoragenodePayment{}, snopayout.ErrNoDataForPeriod.Wrap(err)
|
||||
return snopayouts.StoragenodePayment{}, snopayouts.ErrNoDataForPeriod.Wrap(err)
|
||||
}
|
||||
|
||||
return snopayout.StoragenodePayment{}, Error.Wrap(err)
|
||||
return snopayouts.StoragenodePayment{}, Error.Wrap(err)
|
||||
}
|
||||
|
||||
return payment, nil
|
||||
}
|
||||
|
||||
// CreatePayment inserts storagenode_payment into database.
|
||||
func (paystubs *paymentStubs) CreatePayment(ctx context.Context, payment snopayout.StoragenodePayment) (err error) {
|
||||
func (paystubs *paymentStubs) CreatePayment(ctx context.Context, payment snopayouts.StoragenodePayment) (err error) {
|
||||
return paystubs.db.CreateNoReturn_StoragenodePayment(
|
||||
ctx,
|
||||
dbx.StoragenodePayment_NodeId(payment.NodeID[:]),
|
||||
@ -177,7 +177,7 @@ func (paystubs *paymentStubs) CreatePayment(ctx context.Context, payment snopayo
|
||||
}
|
||||
|
||||
// GetAllPayments return all payments by nodeID.
|
||||
func (paystubs *paymentStubs) GetAllPayments(ctx context.Context, nodeID storj.NodeID) (payments []snopayout.StoragenodePayment, err error) {
|
||||
func (paystubs *paymentStubs) GetAllPayments(ctx context.Context, nodeID storj.NodeID) (payments []snopayouts.StoragenodePayment, err error) {
|
||||
query := `SELECT * FROM storagenode_payments WHERE node_id = $1;`
|
||||
|
||||
rows, err := paystubs.db.QueryContext(ctx, query, nodeID)
|
||||
@ -190,7 +190,7 @@ func (paystubs *paymentStubs) GetAllPayments(ctx context.Context, nodeID storj.N
|
||||
}()
|
||||
|
||||
for rows.Next() {
|
||||
payment := snopayout.StoragenodePayment{}
|
||||
payment := snopayouts.StoragenodePayment{}
|
||||
|
||||
err = rows.Scan(
|
||||
&payment.ID,
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package snopayout
|
||||
package snopayouts
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package snopayout
|
||||
package snopayouts
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -13,7 +13,7 @@ import (
|
||||
"storj.io/common/storj"
|
||||
)
|
||||
|
||||
// DB exposes all needed functionality to manage payout.
|
||||
// DB exposes all needed functionality to manage payouts.
|
||||
//
|
||||
// architecture: Service
|
||||
type DB interface {
|
||||
@ -31,11 +31,11 @@ type DB interface {
|
||||
GetAllPayments(ctx context.Context, nodeID storj.NodeID) ([]StoragenodePayment, error)
|
||||
}
|
||||
|
||||
// ErrNoDataForPeriod represents errors from the payout database.
|
||||
// ErrNoDataForPeriod represents errors from the payouts database.
|
||||
var ErrNoDataForPeriod = errs.Class("no payStub/payments for period error")
|
||||
|
||||
// Error is the default error class for payout package.
|
||||
var Error = errs.Class("payout db error")
|
||||
// Error is the default error class for payouts package.
|
||||
var Error = errs.Class("payouts db error")
|
||||
|
||||
// PayStub is an entity that holds held amount of cash that will be paid to storagenode operator after some period.
|
||||
type PayStub struct {
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package snopayout_test
|
||||
package snopayouts_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -14,7 +14,7 @@ import (
|
||||
"storj.io/common/testrand"
|
||||
"storj.io/storj/satellite"
|
||||
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
||||
"storj.io/storj/satellite/snopayout"
|
||||
"storj.io/storj/satellite/snopayouts"
|
||||
)
|
||||
|
||||
func TestPayoutDB(t *testing.T) {
|
||||
@ -22,7 +22,7 @@ func TestPayoutDB(t *testing.T) {
|
||||
snoPayout := db.SnoPayout()
|
||||
NodeID := storj.NodeID{}
|
||||
period := "2020-01"
|
||||
paystub := snopayout.PayStub{
|
||||
paystub := snopayouts.PayStub{
|
||||
Period: "2020-01",
|
||||
NodeID: NodeID,
|
||||
Created: time.Now().UTC(),
|
||||
@ -46,7 +46,7 @@ func TestPayoutDB(t *testing.T) {
|
||||
Paid: 17,
|
||||
}
|
||||
|
||||
paystub2 := snopayout.PayStub{
|
||||
paystub2 := snopayouts.PayStub{
|
||||
Period: "2020-02",
|
||||
NodeID: NodeID,
|
||||
Created: time.Now().UTC(),
|
||||
@ -70,7 +70,7 @@ func TestPayoutDB(t *testing.T) {
|
||||
Paid: 20,
|
||||
}
|
||||
|
||||
paystub3 := snopayout.PayStub{
|
||||
paystub3 := snopayouts.PayStub{
|
||||
Period: "2020-03",
|
||||
NodeID: NodeID,
|
||||
Created: time.Now().UTC(),
|
||||
@ -207,7 +207,7 @@ func TestPayoutDB(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
payment := snopayout.StoragenodePayment{
|
||||
payment := snopayouts.StoragenodePayment{
|
||||
ID: 1,
|
||||
Created: time.Now().UTC(),
|
||||
NodeID: NodeID,
|
@ -1,262 +0,0 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package consoleapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/zeebo/errs"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"storj.io/common/storj"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
)
|
||||
|
||||
// ErrPayoutAPI - console payout api error type.
|
||||
var ErrPayoutAPI = errs.Class("payout console web error")
|
||||
|
||||
// Payout is an api controller that exposes all payout related api.
|
||||
type Payout struct {
|
||||
service *payout.Service
|
||||
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
// NewPayout is a constructor for payout controller.
|
||||
func NewPayout(log *zap.Logger, service *payout.Service) *Payout {
|
||||
return &Payout{
|
||||
log: log,
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// PayStubMonthly returns payout, storage holding and prices data for specific month from all satellites or specified satellite by query parameter id.
|
||||
func (payouts *Payout) PayStubMonthly(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
segmentParams := mux.Vars(r)
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
period, ok := segmentParams["period"]
|
||||
if !ok {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := payouts.service.AllPayStubsMonthly(ctx, period)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStub, err := payouts.service.SatellitePayStubMonthly(ctx, satelliteID, period)
|
||||
if err != nil {
|
||||
if payout.ErrNoPayStubForPeriod.Has(err) {
|
||||
payouts.serveJSONError(w, http.StatusNotFound, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode([]*payout.PayStub{payStub}); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PayStubPeriod retrieves paystubs for selected range of months from storagenode database for all satellites or specified satellite by query parameter id.
|
||||
func (payouts *Payout) PayStubPeriod(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
segmentParams := mux.Vars(r)
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
start, ok := segmentParams["start"]
|
||||
if !ok {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
end, ok := segmentParams["end"]
|
||||
if !ok {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := payouts.service.AllPayStubsPeriod(ctx, start, end)
|
||||
if err != nil {
|
||||
if payout.ErrBadPeriod.Has(err) {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStubs, err := payouts.service.SatellitePayStubPeriod(ctx, satelliteID, start, end)
|
||||
if err != nil {
|
||||
if payout.ErrBadPeriod.Has(err) {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HeldHistory returns held amount for each % period for all satellites.
|
||||
func (payouts *Payout) HeldHistory(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
heldbackHistory, err := payouts.service.AllHeldbackHistory(ctx)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(heldbackHistory); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// PayoutHistory retrieves paystubs for specific period from all satellites and transaction receipts if exists.
|
||||
func (payouts *Payout) PayoutHistory(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
segmentParams := mux.Vars(r)
|
||||
|
||||
period, ok := segmentParams["period"]
|
||||
if !ok {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payoutHistory, err := payouts.service.AllSatellitesPayoutPeriod(ctx, period)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payoutHistory); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// HeldAmountPeriods retrieves all periods in which we have some payout data.
|
||||
// Have optional parameter - satelliteID.
|
||||
// If satelliteID specified - will retrieve periods only for concrete satellite.
|
||||
func (payouts *Payout) HeldAmountPeriods(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := payouts.service.AllPeriods(ctx)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStubs, err := payouts.service.SatellitePeriods(ctx, satelliteID)
|
||||
if err != nil {
|
||||
payouts.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payouts.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (payouts *Payout) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
w.WriteHeader(status)
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
payouts.log.Error("failed to write json error response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
262
storagenode/console/consoleapi/payouts.go
Normal file
262
storagenode/console/consoleapi/payouts.go
Normal file
@ -0,0 +1,262 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package consoleapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/zeebo/errs"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"storj.io/common/storj"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
)
|
||||
|
||||
// ErrPayoutAPI - console payouts api error type.
|
||||
var ErrPayoutAPI = errs.Class("payouts console web error")
|
||||
|
||||
// Payout is an api controller that exposes all payouts related api.
|
||||
type Payout struct {
|
||||
service *payouts.Service
|
||||
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
// NewPayout is a constructor for payouts controller.
|
||||
func NewPayout(log *zap.Logger, service *payouts.Service) *Payout {
|
||||
return &Payout{
|
||||
log: log,
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// PayStubMonthly returns payouts, storage holding and prices data for specific month from all satellites or specified satellite by query parameter id.
|
||||
func (payout *Payout) PayStubMonthly(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
segmentParams := mux.Vars(r)
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
period, ok := segmentParams["period"]
|
||||
if !ok {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := payout.service.AllPayStubsMonthly(ctx, period)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStub, err := payout.service.SatellitePayStubMonthly(ctx, satelliteID, period)
|
||||
if err != nil {
|
||||
if payouts.ErrNoPayStubForPeriod.Has(err) {
|
||||
payout.serveJSONError(w, http.StatusNotFound, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode([]*payouts.PayStub{payStub}); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PayStubPeriod retrieves paystubs for selected range of months from storagenode database for all satellites or specified satellite by query parameter id.
|
||||
func (payout *Payout) PayStubPeriod(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
segmentParams := mux.Vars(r)
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
start, ok := segmentParams["start"]
|
||||
if !ok {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
end, ok := segmentParams["end"]
|
||||
if !ok {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := payout.service.AllPayStubsPeriod(ctx, start, end)
|
||||
if err != nil {
|
||||
if payouts.ErrBadPeriod.Has(err) {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStubs, err := payout.service.SatellitePayStubPeriod(ctx, satelliteID, start, end)
|
||||
if err != nil {
|
||||
if payouts.ErrBadPeriod.Has(err) {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HeldHistory returns held amount for each % period for all satellites.
|
||||
func (payout *Payout) HeldHistory(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
heldbackHistory, err := payout.service.AllHeldbackHistory(ctx)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(heldbackHistory); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// PayoutHistory retrieves paystubs for specific period from all satellites and transaction receipts if exists.
|
||||
func (payout *Payout) PayoutHistory(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
segmentParams := mux.Vars(r)
|
||||
|
||||
period, ok := segmentParams["period"]
|
||||
if !ok {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payoutHistory, err := payout.service.AllSatellitesPayoutPeriod(ctx, period)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payoutHistory); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// HeldAmountPeriods retrieves all periods in which we have some payouts data.
|
||||
// Have optional parameter - satelliteID.
|
||||
// If satelliteID specified - will retrieve periods only for concrete satellite.
|
||||
func (payout *Payout) HeldAmountPeriods(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
payStubs, err := payout.service.AllPeriods(ctx)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
satelliteID, err := storj.NodeIDFromString(id)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusBadRequest, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
payStubs, err := payout.service.SatellitePeriods(ctx, satelliteID)
|
||||
if err != nil {
|
||||
payout.serveJSONError(w, http.StatusInternalServerError, ErrPayoutAPI.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payStubs); err != nil {
|
||||
payout.log.Error("failed to encode json response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (payout *Payout) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
w.WriteHeader(status)
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
payout.log.Error("failed to write json error response", zap.Error(ErrPayoutAPI.Wrap(err)))
|
||||
return
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ import (
|
||||
"storj.io/common/storj"
|
||||
"storj.io/common/testcontext"
|
||||
"storj.io/storj/private/testplanet"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
"storj.io/storj/storagenode/reputation"
|
||||
)
|
||||
|
||||
@ -36,7 +36,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
baseURL := fmt.Sprintf("http://%s/api/heldamount", console.Listener.Addr())
|
||||
|
||||
period := "2020-03"
|
||||
paystub := payout.PayStub{
|
||||
paystub := payouts.PayStub{
|
||||
SatelliteID: satellite.ID(),
|
||||
Period: period,
|
||||
Created: time.Now().UTC(),
|
||||
@ -72,7 +72,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
|
||||
paystub.UsageAtRest /= 720
|
||||
|
||||
expected, err := json.Marshal([]payout.PayStub{paystub})
|
||||
expected, err := json.Marshal([]payouts.PayStub{paystub})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -84,7 +84,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
|
||||
require.Equal(t, string(expected)+"\n", string(body))
|
||||
|
||||
// should return 404 cause no payout for the period.
|
||||
// should return 404 cause no payouts for the period.
|
||||
url = fmt.Sprintf("%s/paystubs/%s?id=%s", baseURL, "2020-01", satellite.ID().String())
|
||||
res2, err := http.Get(url)
|
||||
require.NoError(t, err)
|
||||
@ -98,7 +98,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
body2, err := ioutil.ReadAll(res2.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
expected = []byte("{\"error\":\"payout console web error: payout service error: no payStub for period error: sql: no rows in result set\"}\n")
|
||||
expected = []byte("{\"error\":\"payouts console web error: payouts service error: no payStub for period error: sql: no rows in result set\"}\n")
|
||||
require.Equal(t, expected, body2)
|
||||
|
||||
// should return 400 cause of wrong satellite id.
|
||||
@ -114,7 +114,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
}()
|
||||
})
|
||||
|
||||
paystub2 := payout.PayStub{
|
||||
paystub2 := payouts.PayStub{
|
||||
SatelliteID: storj.NodeID{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 0},
|
||||
Period: period,
|
||||
Created: time.Now().UTC(),
|
||||
@ -150,7 +150,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
|
||||
paystub2.UsageAtRest /= 720
|
||||
|
||||
expected, err := json.Marshal([]payout.PayStub{paystub2, paystub})
|
||||
expected, err := json.Marshal([]payouts.PayStub{paystub2, paystub})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -180,7 +180,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
})
|
||||
|
||||
period2 := "2020-02"
|
||||
paystub3 := payout.PayStub{
|
||||
paystub3 := payouts.PayStub{
|
||||
SatelliteID: satellite.ID(),
|
||||
Period: period2,
|
||||
Created: time.Now().UTC(),
|
||||
@ -216,7 +216,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
|
||||
paystub3.UsageAtRest /= 720
|
||||
|
||||
expected, err := json.Marshal([]payout.PayStub{paystub3, paystub})
|
||||
expected, err := json.Marshal([]payouts.PayStub{paystub3, paystub})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -234,7 +234,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
require.NotNil(t, res2)
|
||||
require.Equal(t, http.StatusOK, res2.StatusCode)
|
||||
|
||||
expected, err = json.Marshal([]payout.PayStub{paystub})
|
||||
expected, err = json.Marshal([]payouts.PayStub{paystub})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -252,7 +252,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
require.NotNil(t, res3)
|
||||
require.Equal(t, http.StatusOK, res3.StatusCode)
|
||||
|
||||
expected, err = json.Marshal([]payout.PayStub{paystub2})
|
||||
expected, err = json.Marshal([]payouts.PayStub{paystub2})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -291,7 +291,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
body5, err := ioutil.ReadAll(res5.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "{\"error\":\"payout console web error: wrong period format: period has wrong format\"}\n", string(body5))
|
||||
require.Equal(t, "{\"error\":\"payouts console web error: wrong period format: period has wrong format\"}\n", string(body5))
|
||||
})
|
||||
|
||||
t.Run("test AllPayStubsPeriod", func(t *testing.T) {
|
||||
@ -302,7 +302,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
require.NotNil(t, res)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
|
||||
expected, err := json.Marshal([]payout.PayStub{paystub3, paystub2, paystub})
|
||||
expected, err := json.Marshal([]payouts.PayStub{paystub3, paystub2, paystub})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -320,7 +320,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
require.NotNil(t, res2)
|
||||
require.Equal(t, http.StatusOK, res2.StatusCode)
|
||||
|
||||
expected, err = json.Marshal([]payout.PayStub{paystub2, paystub})
|
||||
expected, err = json.Marshal([]payouts.PayStub{paystub2, paystub})
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
@ -347,7 +347,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
body5, err := ioutil.ReadAll(res5.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "{\"error\":\"payout console web error: wrong period format: period has wrong format\"}\n", string(body5))
|
||||
require.Equal(t, "{\"error\":\"payouts console web error: wrong period format: period has wrong format\"}\n", string(body5))
|
||||
})
|
||||
|
||||
t.Run("test HeldbackHistory", func(t *testing.T) {
|
||||
@ -365,7 +365,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
require.NotNil(t, res)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
|
||||
held := payout.SatelliteHeldHistory{
|
||||
held := payouts.SatelliteHeldHistory{
|
||||
SatelliteID: satellite.ID(),
|
||||
SatelliteName: satellite.Addr(),
|
||||
HoldForFirstPeriod: 28,
|
||||
@ -376,7 +376,7 @@ func TestHeldAmountApi(t *testing.T) {
|
||||
JoinedAt: date.Round(time.Minute),
|
||||
}
|
||||
|
||||
var periods []payout.SatelliteHeldHistory
|
||||
var periods []payouts.SatelliteHeldHistory
|
||||
periods = append(periods, held)
|
||||
|
||||
expected, err := json.Marshal(periods)
|
@ -111,7 +111,7 @@ func (dashboard *StorageNode) Satellite(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
// EstimatedPayout returns estimated payout from specific satellite or all satellites if current traffic level remains same.
|
||||
// EstimatedPayout returns estimated payouts from specific satellite or all satellites if current traffic level remains same.
|
||||
func (dashboard *StorageNode) EstimatedPayout(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
"storj.io/common/testcontext"
|
||||
"storj.io/storj/private/testplanet"
|
||||
"storj.io/storj/satellite"
|
||||
"storj.io/storj/storagenode/payout/estimatedpayout"
|
||||
"storj.io/storj/storagenode/payouts/estimatedpayouts"
|
||||
"storj.io/storj/storagenode/pricing"
|
||||
"storj.io/storj/storagenode/reputation"
|
||||
"storj.io/storj/storagenode/storageusage"
|
||||
@ -117,9 +117,10 @@ func TestStorageNodeApi(t *testing.T) {
|
||||
|
||||
estimation, err := sno.Console.Service.GetAllSatellitesEstimatedPayout(ctx)
|
||||
require.NoError(t, err)
|
||||
expected, err := json.Marshal(estimatedpayout.EstimatedPayout{
|
||||
CurrentMonth: estimation.CurrentMonth,
|
||||
PreviousMonth: estimation.PreviousMonth,
|
||||
expected, err := json.Marshal(estimatedpayouts.EstimatedPayout{
|
||||
CurrentMonth: estimation.CurrentMonth,
|
||||
PreviousMonth: estimation.PreviousMonth,
|
||||
CurrentMonthExpectations: estimation.CurrentMonthExpectations,
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
@ -20,7 +20,7 @@ import (
|
||||
"storj.io/storj/storagenode/console"
|
||||
"storj.io/storj/storagenode/console/consoleapi"
|
||||
"storj.io/storj/storagenode/notifications"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -43,14 +43,14 @@ type Server struct {
|
||||
|
||||
service *console.Service
|
||||
notifications *notifications.Service
|
||||
payout *payout.Service
|
||||
payout *payouts.Service
|
||||
listener net.Listener
|
||||
|
||||
server http.Server
|
||||
}
|
||||
|
||||
// NewServer creates new instance of storagenode console web server.
|
||||
func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifications.Service, service *console.Service, payout *payout.Service, listener net.Listener) *Server {
|
||||
func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifications.Service, service *console.Service, payout *payouts.Service, listener net.Listener) *Server {
|
||||
server := Server{
|
||||
log: logger,
|
||||
service: service,
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
"storj.io/storj/private/version/checker"
|
||||
"storj.io/storj/storagenode/bandwidth"
|
||||
"storj.io/storj/storagenode/contact"
|
||||
"storj.io/storj/storagenode/payout/estimatedpayout"
|
||||
"storj.io/storj/storagenode/payouts/estimatedpayouts"
|
||||
"storj.io/storj/storagenode/pieces"
|
||||
"storj.io/storj/storagenode/pricing"
|
||||
"storj.io/storj/storagenode/reputation"
|
||||
@ -50,7 +50,7 @@ type Service struct {
|
||||
pieceStore *pieces.Store
|
||||
contact *contact.Service
|
||||
|
||||
estimation *estimatedpayout.Service
|
||||
estimation *estimatedpayouts.Service
|
||||
version *checker.Service
|
||||
pingStats *contact.PingStats
|
||||
|
||||
@ -65,7 +65,7 @@ type Service struct {
|
||||
func NewService(log *zap.Logger, bandwidth bandwidth.DB, pieceStore *pieces.Store, version *checker.Service,
|
||||
allocatedDiskSpace memory.Size, walletAddress string, versionInfo version.Info, trust *trust.Pool,
|
||||
reputationDB reputation.DB, storageUsageDB storageusage.DB, pricingDB pricing.DB, satelliteDB satellites.DB,
|
||||
pingStats *contact.PingStats, contact *contact.Service, estimation *estimatedpayout.Service, usageCache *pieces.BlobsUsageCache) (*Service, error) {
|
||||
pingStats *contact.PingStats, contact *contact.Service, estimation *estimatedpayouts.Service, usageCache *pieces.BlobsUsageCache) (*Service, error) {
|
||||
if log == nil {
|
||||
return nil, errs.New("log can't be nil")
|
||||
}
|
||||
@ -241,9 +241,7 @@ type Satellite struct {
|
||||
EgressSummary int64 `json:"egressSummary"`
|
||||
IngressSummary int64 `json:"ingressSummary"`
|
||||
CurrentStorageUsed int64 `json:"currentStorageUsed"`
|
||||
Audit reputation.Metric `json:"audit"`
|
||||
Uptime reputation.Metric `json:"uptime"`
|
||||
OnlineScore float64 `json:"onlineScore"`
|
||||
Audits Audits `json:"audits"`
|
||||
AuditHistory reputation.AuditHistory `json:"auditHistory"`
|
||||
PriceModel PriceModel `json:"priceModel"`
|
||||
NodeJoinedAt time.Time `json:"nodeJoinedAt"`
|
||||
@ -306,6 +304,12 @@ func (s *Service) GetSatelliteData(ctx context.Context, satelliteID storj.NodeID
|
||||
DiskSpace: pricingModel.DiskSpace,
|
||||
}
|
||||
|
||||
url, err := s.trust.GetNodeURL(ctx, satelliteID)
|
||||
if err != nil {
|
||||
s.log.Warn("unable to get Satellite URL", zap.String("Satellite ID", satelliteID.String()),
|
||||
zap.Error(SNOServiceErr.Wrap(err)))
|
||||
}
|
||||
|
||||
return &Satellite{
|
||||
ID: satelliteID,
|
||||
StorageDaily: storageDaily,
|
||||
@ -315,12 +319,15 @@ func (s *Service) GetSatelliteData(ctx context.Context, satelliteID storj.NodeID
|
||||
CurrentStorageUsed: currentStorageUsed,
|
||||
EgressSummary: egressSummary.Total(),
|
||||
IngressSummary: ingressSummary.Total(),
|
||||
Audit: rep.Audit,
|
||||
Uptime: rep.Uptime,
|
||||
OnlineScore: rep.OnlineScore,
|
||||
AuditHistory: reputation.GetAuditHistoryFromPB(rep.AuditHistory),
|
||||
PriceModel: satellitePricing,
|
||||
NodeJoinedAt: rep.JoinedAt,
|
||||
Audits: Audits{
|
||||
AuditScore: rep.Audit.Score,
|
||||
SuspensionScore: rep.Audit.UnknownScore,
|
||||
OnlineScore: rep.OnlineScore,
|
||||
SatelliteName: url.Address,
|
||||
},
|
||||
AuditHistory: reputation.GetAuditHistoryFromPB(rep.AuditHistory),
|
||||
PriceModel: satellitePricing,
|
||||
NodeJoinedAt: rep.JoinedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -421,21 +428,21 @@ func (s *Service) GetAllSatellitesData(ctx context.Context) (_ *Satellites, err
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetSatelliteEstimatedPayout returns estimated payout for current and previous months for selected satellite.
|
||||
func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID storj.NodeID) (estimatedPayout estimatedpayout.EstimatedPayout, err error) {
|
||||
// GetSatelliteEstimatedPayout returns estimated payouts for current and previous months for selected satellite.
|
||||
func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID storj.NodeID) (estimatedPayout estimatedpayouts.EstimatedPayout, err error) {
|
||||
estimatedPayout, err = s.estimation.GetSatelliteEstimatedPayout(ctx, satelliteID)
|
||||
if err != nil {
|
||||
return estimatedpayout.EstimatedPayout{}, SNOServiceErr.Wrap(err)
|
||||
return estimatedpayouts.EstimatedPayout{}, SNOServiceErr.Wrap(err)
|
||||
}
|
||||
|
||||
return estimatedPayout, nil
|
||||
}
|
||||
|
||||
// GetAllSatellitesEstimatedPayout returns estimated payout for current and previous months for all satellites.
|
||||
func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context) (estimatedPayout estimatedpayout.EstimatedPayout, err error) {
|
||||
// GetAllSatellitesEstimatedPayout returns estimated payouts for current and previous months for all satellites.
|
||||
func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context) (estimatedPayout estimatedpayouts.EstimatedPayout, err error) {
|
||||
estimatedPayout, err = s.estimation.GetAllSatellitesEstimatedPayout(ctx)
|
||||
if err != nil {
|
||||
return estimatedpayout.EstimatedPayout{}, SNOServiceErr.Wrap(err)
|
||||
return estimatedpayouts.EstimatedPayout{}, SNOServiceErr.Wrap(err)
|
||||
}
|
||||
|
||||
return estimatedPayout, nil
|
||||
|
@ -11,22 +11,22 @@ import (
|
||||
"storj.io/common/rpc/rpcstatus"
|
||||
"storj.io/storj/private/multinodepb"
|
||||
"storj.io/storj/storagenode/apikeys"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
)
|
||||
|
||||
var _ multinodepb.DRPCPayoutServer = (*PayoutEndpoint)(nil)
|
||||
|
||||
// PayoutEndpoint implements multinode payout endpoint.
|
||||
// PayoutEndpoint implements multinode payouts endpoint.
|
||||
//
|
||||
// architecture: Endpoint
|
||||
type PayoutEndpoint struct {
|
||||
log *zap.Logger
|
||||
apiKeys *apikeys.Service
|
||||
db payout.DB
|
||||
db payouts.DB
|
||||
}
|
||||
|
||||
// NewPayoutEndpoint creates new multinode payout endpoint.
|
||||
func NewPayoutEndpoint(log *zap.Logger, apiKeys *apikeys.Service, db payout.DB) *PayoutEndpoint {
|
||||
// NewPayoutEndpoint creates new multinode payouts endpoint.
|
||||
func NewPayoutEndpoint(log *zap.Logger, apiKeys *apikeys.Service, db payouts.DB) *PayoutEndpoint {
|
||||
return &PayoutEndpoint{
|
||||
log: log,
|
||||
apiKeys: apiKeys,
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
"storj.io/common/storj"
|
||||
"storj.io/common/sync2"
|
||||
"storj.io/storj/private/date"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
"storj.io/storj/storagenode/pricing"
|
||||
"storj.io/storj/storagenode/reputation"
|
||||
"storj.io/storj/storagenode/satellites"
|
||||
@ -34,7 +34,7 @@ type Config struct {
|
||||
type CacheStorage struct {
|
||||
Reputation reputation.DB
|
||||
StorageUsage storageusage.DB
|
||||
Payout payout.DB
|
||||
Payout payouts.DB
|
||||
Pricing pricing.DB
|
||||
Satellites satellites.DB
|
||||
}
|
||||
@ -47,7 +47,7 @@ type Cache struct {
|
||||
|
||||
db CacheStorage
|
||||
service *Service
|
||||
payoutEndpoint *payout.Endpoint
|
||||
payoutEndpoint *payouts.Endpoint
|
||||
reputationService *reputation.Service
|
||||
trust *trust.Pool
|
||||
|
||||
@ -58,7 +58,7 @@ type Cache struct {
|
||||
|
||||
// NewCache creates new caching service instance.
|
||||
func NewCache(log *zap.Logger, config Config, db CacheStorage, service *Service,
|
||||
payoutEndpoint *payout.Endpoint, reputationService *reputation.Service, trust *trust.Pool) *Cache {
|
||||
payoutEndpoint *payouts.Endpoint, reputationService *reputation.Service, trust *trust.Pool) *Cache {
|
||||
|
||||
return &Cache{
|
||||
log: log,
|
||||
@ -203,11 +203,11 @@ func (cache *Cache) CacheHeldAmount(ctx context.Context) (err error) {
|
||||
previousMonth := yearAndMonth.AddDate(0, -1, 0).String()
|
||||
payStub, err := cache.payoutEndpoint.GetPaystub(ctx, satellite, previousMonth)
|
||||
if err != nil {
|
||||
if payout.ErrNoPayStubForPeriod.Has(err) {
|
||||
if payouts.ErrNoPayStubForPeriod.Has(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
cache.log.Error("payout err", zap.String("satellite", satellite.String()))
|
||||
cache.log.Error("payouts err", zap.String("satellite", satellite.String()))
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package payout_test
|
||||
package payouts_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -14,16 +14,16 @@ import (
|
||||
"storj.io/common/storj"
|
||||
"storj.io/common/testcontext"
|
||||
"storj.io/storj/storagenode"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
||||
)
|
||||
|
||||
func TestHeldAmountDB(t *testing.T) {
|
||||
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
||||
payouts := db.Payout()
|
||||
payout := db.Payout()
|
||||
satelliteID := storj.NodeID{}
|
||||
period := "2020-01"
|
||||
paystub := payout.PayStub{
|
||||
paystub := payouts.PayStub{
|
||||
SatelliteID: satelliteID,
|
||||
Period: "2020-01",
|
||||
Created: time.Now().UTC(),
|
||||
@ -50,23 +50,23 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
paystub2.Created = paystub.Created.Add(time.Hour * 24 * 30)
|
||||
|
||||
t.Run("Test StorePayStub", func(t *testing.T) {
|
||||
err := payouts.StorePayStub(ctx, paystub)
|
||||
err := payout.StorePayStub(ctx, paystub)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
payment := payout.Payment{
|
||||
payment := payouts.Payment{
|
||||
SatelliteID: satelliteID,
|
||||
Period: period,
|
||||
Receipt: "test",
|
||||
}
|
||||
|
||||
t.Run("Test GetPayStub", func(t *testing.T) {
|
||||
err := payouts.StorePayment(ctx, payment)
|
||||
err := payout.StorePayment(ctx, payment)
|
||||
assert.NoError(t, err)
|
||||
|
||||
stub, err := payouts.GetPayStub(ctx, satelliteID, period)
|
||||
stub, err := payout.GetPayStub(ctx, satelliteID, period)
|
||||
assert.NoError(t, err)
|
||||
receipt, err := payouts.GetReceipt(ctx, satelliteID, period)
|
||||
receipt, err := payout.GetReceipt(ctx, satelliteID, period)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, stub.Period, paystub.Period)
|
||||
assert.Equal(t, stub.Created, paystub.Created)
|
||||
@ -91,24 +91,24 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
assert.Equal(t, stub.UsagePutRepair, paystub.UsagePutRepair)
|
||||
assert.Equal(t, receipt, payment.Receipt)
|
||||
|
||||
stub, err = payouts.GetPayStub(ctx, satelliteID, "")
|
||||
stub, err = payout.GetPayStub(ctx, satelliteID, "")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, true, payout.ErrNoPayStubForPeriod.Has(err))
|
||||
assert.Equal(t, true, payouts.ErrNoPayStubForPeriod.Has(err))
|
||||
assert.Nil(t, stub)
|
||||
assert.NotNil(t, receipt)
|
||||
receipt, err = payouts.GetReceipt(ctx, satelliteID, "")
|
||||
receipt, err = payout.GetReceipt(ctx, satelliteID, "")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, true, payout.ErrNoPayStubForPeriod.Has(err))
|
||||
assert.Equal(t, true, payouts.ErrNoPayStubForPeriod.Has(err))
|
||||
|
||||
stub, err = payouts.GetPayStub(ctx, storj.NodeID{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, period)
|
||||
stub, err = payout.GetPayStub(ctx, storj.NodeID{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, period)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, true, payout.ErrNoPayStubForPeriod.Has(err))
|
||||
assert.Equal(t, true, payouts.ErrNoPayStubForPeriod.Has(err))
|
||||
assert.Nil(t, stub)
|
||||
assert.NotNil(t, receipt)
|
||||
})
|
||||
|
||||
t.Run("Test AllPayStubs", func(t *testing.T) {
|
||||
stubs, err := payouts.AllPayStubs(ctx, period)
|
||||
stubs, err := payout.AllPayStubs(ctx, period)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, stubs)
|
||||
assert.Equal(t, 1, len(stubs))
|
||||
@ -134,12 +134,12 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
assert.Equal(t, stubs[0].UsagePut, paystub.UsagePut)
|
||||
assert.Equal(t, stubs[0].UsagePutRepair, paystub.UsagePutRepair)
|
||||
|
||||
stubs, err = payouts.AllPayStubs(ctx, "")
|
||||
stubs, err = payout.AllPayStubs(ctx, "")
|
||||
assert.Equal(t, len(stubs), 0)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
payment = payout.Payment{
|
||||
payment = payouts.Payment{
|
||||
ID: 1,
|
||||
Created: time.Now().UTC(),
|
||||
SatelliteID: satelliteID,
|
||||
@ -150,28 +150,28 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("Test StorePayment", func(t *testing.T) {
|
||||
err := payouts.StorePayment(ctx, payment)
|
||||
err := payout.StorePayment(ctx, payment)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Test SatellitesHeldbackHistory", func(t *testing.T) {
|
||||
heldback, err := payouts.SatellitesHeldbackHistory(ctx, satelliteID)
|
||||
heldback, err := payout.SatellitesHeldbackHistory(ctx, satelliteID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, heldback[0].Amount, paystub.Held)
|
||||
assert.Equal(t, heldback[0].Period, paystub.Period)
|
||||
})
|
||||
|
||||
t.Run("Test SatellitePeriods", func(t *testing.T) {
|
||||
periods, err := payouts.SatellitePeriods(ctx, paystub.SatelliteID)
|
||||
periods, err := payout.SatellitePeriods(ctx, paystub.SatelliteID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 1, len(periods))
|
||||
assert.Equal(t, paystub.Period, periods[0])
|
||||
|
||||
err = payouts.StorePayStub(ctx, paystub2)
|
||||
err = payout.StorePayStub(ctx, paystub2)
|
||||
require.NoError(t, err)
|
||||
|
||||
periods, err = payouts.SatellitePeriods(ctx, paystub.SatelliteID)
|
||||
periods, err = payout.SatellitePeriods(ctx, paystub.SatelliteID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 2, len(periods))
|
||||
@ -180,7 +180,7 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Test AllPeriods", func(t *testing.T) {
|
||||
periods, err := payouts.AllPeriods(ctx)
|
||||
periods, err := payout.AllPeriods(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 2, len(periods))
|
||||
@ -192,10 +192,10 @@ func TestHeldAmountDB(t *testing.T) {
|
||||
paystub3.Period = "2020-03"
|
||||
paystub3.Created = paystub2.Created.Add(time.Hour * 24 * 30)
|
||||
|
||||
err = payouts.StorePayStub(ctx, paystub3)
|
||||
err = payout.StorePayStub(ctx, paystub3)
|
||||
require.NoError(t, err)
|
||||
|
||||
periods, err = payouts.AllPeriods(ctx)
|
||||
periods, err = payout.AllPeriods(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, periods)
|
||||
assert.Equal(t, 3, len(periods))
|
||||
@ -211,10 +211,10 @@ func TestSatellitePayStubPeriodCached(t *testing.T) {
|
||||
heldAmountDB := db.Payout()
|
||||
reputationDB := db.Reputation()
|
||||
satellitesDB := db.Satellites()
|
||||
service, err := payout.NewService(nil, heldAmountDB, reputationDB, satellitesDB, nil)
|
||||
service, err := payouts.NewService(nil, heldAmountDB, reputationDB, satellitesDB, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
payStub := payout.PayStub{
|
||||
payStub := payouts.PayStub{
|
||||
SatelliteID: storj.NodeID{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
||||
Created: time.Now().UTC(),
|
||||
Codes: "code",
|
||||
@ -262,10 +262,10 @@ func TestAllPayStubPeriodCached(t *testing.T) {
|
||||
heldAmountDB := db.Payout()
|
||||
reputationDB := db.Reputation()
|
||||
satellitesDB := db.Satellites()
|
||||
service, err := payout.NewService(nil, heldAmountDB, reputationDB, satellitesDB, nil)
|
||||
service, err := payouts.NewService(nil, heldAmountDB, reputationDB, satellitesDB, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
payStub := payout.PayStub{
|
||||
payStub := payouts.PayStub{
|
||||
SatelliteID: storj.NodeID{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
||||
Created: time.Now().UTC(),
|
||||
Codes: "code",
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package payout
|
||||
package payouts
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,19 +1,20 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package estimatedpayout
|
||||
package estimatedpayouts
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// EstimatedPayout contains usage and estimated payout data for current and previous months.
|
||||
// EstimatedPayout contains usage and estimated payouts data for current and previous months.
|
||||
type EstimatedPayout struct {
|
||||
CurrentMonth PayoutMonthly `json:"currentMonth"`
|
||||
PreviousMonth PayoutMonthly `json:"previousMonth"`
|
||||
CurrentMonth PayoutMonthly `json:"currentMonth"`
|
||||
PreviousMonth PayoutMonthly `json:"previousMonth"`
|
||||
CurrentMonthExpectations float64 `json:"currentMonthExpectations"`
|
||||
}
|
||||
|
||||
// PayoutMonthly contains usage and estimated payout date.
|
||||
// PayoutMonthly contains usage and estimated payouts date.
|
||||
type PayoutMonthly struct {
|
||||
EgressBandwidth int64 `json:"egressBandwidth"`
|
||||
EgressBandwidthPayout float64 `json:"egressBandwidthPayout"`
|
||||
@ -26,19 +27,19 @@ type PayoutMonthly struct {
|
||||
Held float64 `json:"held"`
|
||||
}
|
||||
|
||||
// SetEgressBandwidthPayout counts egress bandwidth payout for PayoutMonthly object.
|
||||
// SetEgressBandwidthPayout counts egress bandwidth payouts for PayoutMonthly object.
|
||||
func (pm *PayoutMonthly) SetEgressBandwidthPayout(egressPrice int64) {
|
||||
amount := float64(pm.EgressBandwidth*egressPrice) / math.Pow10(12)
|
||||
pm.EgressBandwidthPayout += RoundFloat(amount)
|
||||
}
|
||||
|
||||
// SetEgressRepairAuditPayout counts audit and repair payout for PayoutMonthly object.
|
||||
// SetEgressRepairAuditPayout counts audit and repair payouts for PayoutMonthly object.
|
||||
func (pm *PayoutMonthly) SetEgressRepairAuditPayout(auditRepairPrice int64) {
|
||||
amount := float64(pm.EgressRepairAudit*auditRepairPrice) / math.Pow10(12)
|
||||
pm.EgressRepairAuditPayout += RoundFloat(amount)
|
||||
}
|
||||
|
||||
// SetDiskSpacePayout counts disk space payout for PayoutMonthly object.
|
||||
// SetDiskSpacePayout counts disk space payouts for PayoutMonthly object.
|
||||
func (pm *PayoutMonthly) SetDiskSpacePayout(diskSpacePrice int64) {
|
||||
amount := pm.DiskSpace * float64(diskSpacePrice) / math.Pow10(12)
|
||||
pm.DiskSpacePayout += RoundFloat(amount)
|
||||
@ -50,7 +51,7 @@ func (pm *PayoutMonthly) SetHeldAmount() {
|
||||
pm.Held = amount
|
||||
}
|
||||
|
||||
// SetPayout counts payout amount for PayoutMonthly object.
|
||||
// SetPayout counts payouts amount for PayoutMonthly object.
|
||||
func (pm *PayoutMonthly) SetPayout() {
|
||||
amount := pm.DiskSpacePayout + pm.EgressBandwidthPayout + pm.EgressRepairAuditPayout - pm.Held
|
||||
pm.Payout = RoundFloat(amount)
|
@ -1,19 +1,20 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package estimatedpayout
|
||||
package estimatedpayouts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/now"
|
||||
"github.com/spacemonkeygo/monkit/v3"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/common/storj"
|
||||
"storj.io/storj/private/date"
|
||||
"storj.io/storj/storagenode/bandwidth"
|
||||
payout2 "storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
"storj.io/storj/storagenode/pricing"
|
||||
"storj.io/storj/storagenode/reputation"
|
||||
"storj.io/storj/storagenode/satellites"
|
||||
@ -23,12 +24,12 @@ import (
|
||||
|
||||
var (
|
||||
// EstimationServiceErr defines sno service error.
|
||||
EstimationServiceErr = errs.Class("storage node estimation payout service error")
|
||||
EstimationServiceErr = errs.Class("storage node estimation payouts service error")
|
||||
|
||||
mon = monkit.Package()
|
||||
)
|
||||
|
||||
// Service is handling storage node estimation payout logic.
|
||||
// Service is handling storage node estimation payouts logic.
|
||||
//
|
||||
// architecture: Service
|
||||
type Service struct {
|
||||
@ -52,7 +53,7 @@ func NewService(bandwidthDB bandwidth.DB, reputationDB reputation.DB, storageUsa
|
||||
}
|
||||
}
|
||||
|
||||
// GetSatelliteEstimatedPayout returns estimated payout for current and previous months from specific satellite with current level of load.
|
||||
// GetSatelliteEstimatedPayout returns estimated payouts for current and previous months from specific satellite with current level of load.
|
||||
func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID storj.NodeID) (payout EstimatedPayout, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
@ -63,11 +64,12 @@ func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID s
|
||||
|
||||
payout.CurrentMonth = currentMonthPayout
|
||||
payout.PreviousMonth = previousMonthPayout
|
||||
payout.setExpectations(ctx)
|
||||
|
||||
return payout, nil
|
||||
}
|
||||
|
||||
// GetAllSatellitesEstimatedPayout returns estimated payout for current and previous months from all satellites with current level of load.
|
||||
// GetAllSatellitesEstimatedPayout returns estimated payouts for current and previous months from all satellites with current level of load.
|
||||
func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context) (payout EstimatedPayout, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
@ -95,11 +97,19 @@ func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context) (payout E
|
||||
payout.PreviousMonth.EgressRepairAudit += previous.EgressRepairAudit
|
||||
payout.PreviousMonth.Held += previous.Held
|
||||
}
|
||||
payout.setExpectations(ctx)
|
||||
|
||||
return payout, nil
|
||||
}
|
||||
|
||||
// estimatedPayout returns estimated payout data for current and previous months from specific satellite.
|
||||
// setExpectations set current month expectations.
|
||||
func (estimatedPayout *EstimatedPayout) setExpectations(ctx context.Context) {
|
||||
daysPaste := float64(time.Now().Day() - 1)
|
||||
DaysInMonth := float64(now.EndOfMonth().Day())
|
||||
estimatedPayout.CurrentMonthExpectations = (estimatedPayout.CurrentMonth.Payout / daysPaste) * DaysInMonth
|
||||
}
|
||||
|
||||
// estimatedPayout returns estimated payouts data for current and previous months from specific satellite.
|
||||
func (s *Service) estimatedPayout(ctx context.Context, satelliteID storj.NodeID) (currentMonthPayout PayoutMonthly, previousMonthPayout PayoutMonthly, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
@ -123,7 +133,7 @@ func (s *Service) estimatedPayout(ctx context.Context, satelliteID storj.NodeID)
|
||||
func (s *Service) estimationUsagePeriod(ctx context.Context, period time.Time, joinedAt time.Time, priceModel *pricing.Pricing) (payout PayoutMonthly, err error) {
|
||||
var from, to time.Time
|
||||
|
||||
heldRate := payout2.GetHeldRate(joinedAt, period)
|
||||
heldRate := payouts.GetHeldRate(joinedAt, period)
|
||||
payout.HeldRate = heldRate
|
||||
|
||||
from, to = date.MonthBoundary(period)
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package payout
|
||||
package payouts
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -12,7 +12,7 @@ import (
|
||||
"storj.io/common/storj"
|
||||
)
|
||||
|
||||
// DB works with payout database.
|
||||
// DB works with payouts database.
|
||||
//
|
||||
// architecture: Database
|
||||
type DB interface {
|
||||
@ -26,9 +26,9 @@ type DB interface {
|
||||
SatellitesHeldbackHistory(ctx context.Context, satelliteID storj.NodeID) ([]HoldForPeriod, error)
|
||||
// SatellitesDisposedHistory returns all disposed amount for specific satellite from DB.
|
||||
SatellitesDisposedHistory(ctx context.Context, satelliteID storj.NodeID) (int64, error)
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payout data.
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payouts data.
|
||||
SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) ([]string, error)
|
||||
// AllPeriods retrieves all periods in which we have some payout data.
|
||||
// AllPeriods retrieves all periods in which we have some payouts data.
|
||||
AllPeriods(ctx context.Context) ([]string, error)
|
||||
// StorePayment inserts or updates payment into the DB
|
||||
StorePayment(ctx context.Context, payment Payment) error
|
||||
@ -38,10 +38,10 @@ type DB interface {
|
||||
GetTotalEarned(ctx context.Context) (_ int64, err error)
|
||||
}
|
||||
|
||||
// ErrNoPayStubForPeriod represents errors from the payout database.
|
||||
// ErrNoPayStubForPeriod represents errors from the payouts database.
|
||||
var ErrNoPayStubForPeriod = errs.Class("no payStub for period error")
|
||||
|
||||
// PayStub is node payout data for satellite by specific period.
|
||||
// PayStub is node payouts data for satellite by specific period.
|
||||
type PayStub struct {
|
||||
SatelliteID storj.NodeID `json:"satelliteId"`
|
||||
Period string `json:"period"`
|
||||
@ -95,7 +95,7 @@ type SatelliteHeldHistory struct {
|
||||
JoinedAt time.Time `json:"joinedAt"`
|
||||
}
|
||||
|
||||
// SatellitePayoutForPeriod contains payout information for specific period for specific satellite.
|
||||
// SatellitePayoutForPeriod contains payouts information for specific period for specific satellite.
|
||||
type SatellitePayoutForPeriod struct {
|
||||
SatelliteID string `json:"satelliteID"`
|
||||
SatelliteURL string `json:"satelliteURL"`
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package payout
|
||||
package payouts
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -25,7 +25,7 @@ import (
|
||||
|
||||
var (
|
||||
// ErrPayoutService defines payout service error.
|
||||
ErrPayoutService = errs.Class("payout service error")
|
||||
ErrPayoutService = errs.Class("payouts service error")
|
||||
|
||||
// ErrBadPeriod defines that period has wrong format.
|
||||
ErrBadPeriod = errs.Class("wrong period format")
|
||||
@ -152,14 +152,14 @@ func (service *Service) AllPayStubsPeriod(ctx context.Context, periodStart, peri
|
||||
return payStubs, nil
|
||||
}
|
||||
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payout data.
|
||||
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payouts data.
|
||||
func (service *Service) SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) (_ []string, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
return service.db.SatellitePeriods(ctx, satelliteID)
|
||||
}
|
||||
|
||||
// AllPeriods retrieves all periods in which we have some payout data.
|
||||
// AllPeriods retrieves all periods in which we have some payouts data.
|
||||
func (service *Service) AllPeriods(ctx context.Context) (_ []string, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package payout
|
||||
package payouts
|
||||
|
||||
import (
|
||||
"testing"
|
@ -47,8 +47,8 @@ import (
|
||||
"storj.io/storj/storagenode/nodestats"
|
||||
"storj.io/storj/storagenode/notifications"
|
||||
"storj.io/storj/storagenode/orders"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payout/estimatedpayout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
"storj.io/storj/storagenode/payouts/estimatedpayouts"
|
||||
"storj.io/storj/storagenode/pieces"
|
||||
"storj.io/storj/storagenode/piecestore"
|
||||
"storj.io/storj/storagenode/piecestore/usedserials"
|
||||
@ -88,7 +88,7 @@ type DB interface {
|
||||
StorageUsage() storageusage.DB
|
||||
Satellites() satellites.DB
|
||||
Notifications() notifications.DB
|
||||
Payout() payout.DB
|
||||
Payout() payouts.DB
|
||||
Pricing() pricing.DB
|
||||
APIKeys() apikeys.DB
|
||||
|
||||
@ -226,7 +226,7 @@ type Peer struct {
|
||||
}
|
||||
|
||||
Estimation struct {
|
||||
Service *estimatedpayout.Service
|
||||
Service *estimatedpayouts.Service
|
||||
}
|
||||
|
||||
Storage2 struct {
|
||||
@ -274,8 +274,8 @@ type Peer struct {
|
||||
}
|
||||
|
||||
Payout struct {
|
||||
Service *payout.Service
|
||||
Endpoint *payout.Endpoint
|
||||
Service *payouts.Service
|
||||
Endpoint *payouts.Endpoint
|
||||
}
|
||||
|
||||
Bandwidth *bandwidth.Service
|
||||
@ -562,9 +562,9 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, revocationDB exten
|
||||
debug.Cycle("Orders Cleanup", peer.Storage2.Orders.Cleanup))
|
||||
}
|
||||
|
||||
{ // setup payout service.
|
||||
service, err := payout.NewService(
|
||||
peer.Log.Named("payout:service"),
|
||||
{ // setup payouts service.
|
||||
service, err := payouts.NewService(
|
||||
peer.Log.Named("payouts:service"),
|
||||
peer.DB.Payout(),
|
||||
peer.DB.Reputation(),
|
||||
peer.DB.Satellites(),
|
||||
@ -574,8 +574,8 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, revocationDB exten
|
||||
return nil, errs.Combine(err, peer.Close())
|
||||
}
|
||||
peer.Payout.Service = service
|
||||
peer.Payout.Endpoint = payout.NewEndpoint(
|
||||
peer.Log.Named("payout:endpoint"),
|
||||
peer.Payout.Endpoint = payouts.NewEndpoint(
|
||||
peer.Log.Named("payouts:endpoint"),
|
||||
peer.Dialer,
|
||||
peer.Storage2.Trust,
|
||||
)
|
||||
@ -624,7 +624,7 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, revocationDB exten
|
||||
}
|
||||
|
||||
{ // setup estimation service
|
||||
peer.Estimation.Service = estimatedpayout.NewService(
|
||||
peer.Estimation.Service = estimatedpayouts.NewService(
|
||||
peer.DB.Bandwidth(),
|
||||
peer.DB.Reputation(),
|
||||
peer.DB.StorageUsage(),
|
||||
|
@ -29,7 +29,7 @@ import (
|
||||
"storj.io/storj/storagenode/bandwidth"
|
||||
"storj.io/storj/storagenode/notifications"
|
||||
"storj.io/storj/storagenode/orders"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
"storj.io/storj/storagenode/pieces"
|
||||
"storj.io/storj/storagenode/pricing"
|
||||
"storj.io/storj/storagenode/reputation"
|
||||
@ -534,7 +534,7 @@ func (db *DB) Notifications() notifications.DB {
|
||||
}
|
||||
|
||||
// Payout returns instance of the SnoPayout database.
|
||||
func (db *DB) Payout() payout.DB {
|
||||
func (db *DB) Payout() payouts.DB {
|
||||
return db.payoutDB
|
||||
}
|
||||
|
||||
|
@ -11,14 +11,14 @@ import (
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/common/storj"
|
||||
"storj.io/storj/storagenode/payout"
|
||||
"storj.io/storj/storagenode/payouts"
|
||||
)
|
||||
|
||||
// ensures that payoutDB implements payout.DB interface.
|
||||
var _ payout.DB = (*payoutDB)(nil)
|
||||
// ensures that payoutDB implements payouts.DB interface.
|
||||
var _ payouts.DB = (*payoutDB)(nil)
|
||||
|
||||
// ErrPayout represents errors from the payouts database.
|
||||
var ErrPayout = errs.Class("payout error")
|
||||
var ErrPayout = errs.Class("payouts error")
|
||||
|
||||
// HeldAmountDBName represents the database name.
|
||||
const HeldAmountDBName = "heldamount"
|
||||
@ -29,7 +29,7 @@ type payoutDB struct {
|
||||
}
|
||||
|
||||
// StorePayStub inserts or updates paystub data into the db.
|
||||
func (db *payoutDB) StorePayStub(ctx context.Context, paystub payout.PayStub) (err error) {
|
||||
func (db *payoutDB) StorePayStub(ctx context.Context, paystub payouts.PayStub) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
query := `INSERT OR REPLACE INTO paystubs (
|
||||
@ -84,10 +84,10 @@ func (db *payoutDB) StorePayStub(ctx context.Context, paystub payout.PayStub) (e
|
||||
}
|
||||
|
||||
// GetPayStub retrieves paystub data for a specific satellite and period.
|
||||
func (db *payoutDB) GetPayStub(ctx context.Context, satelliteID storj.NodeID, period string) (_ *payout.PayStub, err error) {
|
||||
func (db *payoutDB) GetPayStub(ctx context.Context, satelliteID storj.NodeID, period string) (_ *payouts.PayStub, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
result := payout.PayStub{
|
||||
result := payouts.PayStub{
|
||||
SatelliteID: satelliteID,
|
||||
Period: period,
|
||||
}
|
||||
@ -139,7 +139,7 @@ func (db *payoutDB) GetPayStub(ctx context.Context, satelliteID storj.NodeID, pe
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, payout.ErrNoPayStubForPeriod.Wrap(err)
|
||||
return nil, payouts.ErrNoPayStubForPeriod.Wrap(err)
|
||||
}
|
||||
return nil, ErrPayout.Wrap(err)
|
||||
}
|
||||
@ -148,7 +148,7 @@ func (db *payoutDB) GetPayStub(ctx context.Context, satelliteID storj.NodeID, pe
|
||||
}
|
||||
|
||||
// AllPayStubs retrieves all paystub stats from DB for specific period.
|
||||
func (db *payoutDB) AllPayStubs(ctx context.Context, period string) (_ []payout.PayStub, err error) {
|
||||
func (db *payoutDB) AllPayStubs(ctx context.Context, period string) (_ []payouts.PayStub, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
query := `SELECT
|
||||
@ -181,9 +181,9 @@ func (db *payoutDB) AllPayStubs(ctx context.Context, period string) (_ []payout.
|
||||
|
||||
defer func() { err = errs.Combine(err, rows.Close()) }()
|
||||
|
||||
var paystubList []payout.PayStub
|
||||
var paystubList []payouts.PayStub
|
||||
for rows.Next() {
|
||||
var paystub payout.PayStub
|
||||
var paystub payouts.PayStub
|
||||
paystub.Period = period
|
||||
|
||||
err := rows.Scan(&paystub.SatelliteID,
|
||||
@ -221,7 +221,7 @@ func (db *payoutDB) AllPayStubs(ctx context.Context, period string) (_ []payout.
|
||||
}
|
||||
|
||||
// SatellitesHeldbackHistory retrieves heldback history for specific satellite.
|
||||
func (db *payoutDB) SatellitesHeldbackHistory(ctx context.Context, id storj.NodeID) (_ []payout.HoldForPeriod, err error) {
|
||||
func (db *payoutDB) SatellitesHeldbackHistory(ctx context.Context, id storj.NodeID) (_ []payouts.HoldForPeriod, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
query := `SELECT
|
||||
@ -236,9 +236,9 @@ func (db *payoutDB) SatellitesHeldbackHistory(ctx context.Context, id storj.Node
|
||||
|
||||
defer func() { err = errs.Combine(err, rows.Close()) }()
|
||||
|
||||
var heldback []payout.HoldForPeriod
|
||||
var heldback []payouts.HoldForPeriod
|
||||
for rows.Next() {
|
||||
var held payout.HoldForPeriod
|
||||
var held payouts.HoldForPeriod
|
||||
|
||||
err := rows.Scan(&held.Period, &held.Amount)
|
||||
if err != nil {
|
||||
@ -315,7 +315,7 @@ func (db *payoutDB) AllPeriods(ctx context.Context) (_ []string, err error) {
|
||||
}
|
||||
|
||||
// StorePayment inserts or updates payment data into the db.
|
||||
func (db *payoutDB) StorePayment(ctx context.Context, payment payout.Payment) (err error) {
|
||||
func (db *payoutDB) StorePayment(ctx context.Context, payment payouts.Payment) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
query := `INSERT OR REPLACE INTO payments (
|
||||
@ -386,7 +386,7 @@ func (db *payoutDB) GetReceipt(ctx context.Context, satelliteID storj.NodeID, pe
|
||||
err = rowPayment.Scan(&receipt)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", payout.ErrNoPayStubForPeriod.Wrap(err)
|
||||
return "", payouts.ErrNoPayStubForPeriod.Wrap(err)
|
||||
}
|
||||
return "", ErrPayout.Wrap(err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user