Skip to content

Commit

Permalink
multinode/payouts: estimated payouts added
Browse files Browse the repository at this point in the history
estimated payouts for specific/all satellites added.

Change-Id: I2530c9f1775593588e2a8f6c087ce6b4f9e354c4
  • Loading branch information
Qweder93 committed May 11, 2021
1 parent 0858c37 commit a11698f
Show file tree
Hide file tree
Showing 15 changed files with 651 additions and 89 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
google.golang.org/api v0.20.0 // indirect
gopkg.in/segmentio/analytics-go.v3 v3.1.0
storj.io/common v0.0.0-20210429174118-60091ebbbdaf
storj.io/common v0.0.0-20210504141454-bcb03a80052f
storj.io/drpc v0.0.20
storj.io/monkit-jaeger v0.0.0-20210426161729-debb1cbcbbd7
storj.io/private v0.0.0-20210511083637-239fca6e9894
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,9 @@ sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=
storj.io/common v0.0.0-20200424175742-65ac59022f4f/go.mod h1:pZyXiIE7bGETIRXtfs0nICqMwp7PM8HqnDuyUeldNA0=
storj.io/common v0.0.0-20201026135900-1aaeec90670b/go.mod h1:GqdmNf3fLm2UZX/7Zr0BLFCJ4gFjgm6eHrk/fnmr5jQ=
storj.io/common v0.0.0-20210429174118-60091ebbbdaf h1:OEKQlWDSvswTPPGOJZxSoIqyyyPGQr/rrjbN4Z5/7WA=
storj.io/common v0.0.0-20210429174118-60091ebbbdaf/go.mod h1:PdP3eTld9RqSV3E4K44JSlw7Z/zNsymj9rnKuHFKhJE=
storj.io/common v0.0.0-20210504141454-bcb03a80052f h1:TwWEzxjvhnkCKUGds4HQKtAgFBjzf/C0hcA1luiNuKI=
storj.io/common v0.0.0-20210504141454-bcb03a80052f/go.mod h1:PdP3eTld9RqSV3E4K44JSlw7Z/zNsymj9rnKuHFKhJE=
storj.io/drpc v0.0.11/go.mod h1:TiFc2obNjL9/3isMW1Rpxjy8V9uE0B2HMeMFGiiI7Iw=
storj.io/drpc v0.0.14/go.mod h1:82nfl+6YwRwF6UG31cEWWUqv/FaKvP5SGqUvoqTxCMA=
storj.io/drpc v0.0.20 h1:nzOxsetLi0fJ8xCL92LPlYL0B6iYdDDk1Cpdbn0/r9Y=
Expand Down
45 changes: 45 additions & 0 deletions multinode/console/controllers/payouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"encoding/json"
"net/http"

"github.com/gorilla/mux"
"github.com/zeebo/errs"
"go.uber.org/zap"

"storj.io/common/storj"
"storj.io/storj/multinode/payouts"
)

Expand Down Expand Up @@ -51,6 +53,49 @@ func (controller *Payouts) GetAllNodesTotalEarned(w http.ResponseWriter, r *http
}
}

// SatelliteEstimations handles nodes estimated earnings from satellite.
func (controller *Payouts) SatelliteEstimations(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
segmentParams := mux.Vars(r)
id, ok := segmentParams["satelliteID"]
if !ok {
controller.serveError(w, http.StatusBadRequest, ErrPayouts.Wrap(err))
return
}
satelliteID, err := storj.NodeIDFromString(id)
if err != nil {
controller.serveError(w, http.StatusBadRequest, ErrPayouts.Wrap(err))
return
}
estimatedEarnings, err := controller.service.AllNodesSatelliteEstimations(ctx, satelliteID)
if err != nil {
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
return
}
if err = json.NewEncoder(w).Encode(estimatedEarnings); err != nil {
controller.log.Error("failed to write json response", zap.Error(err))
return
}
}

// Estimations handles nodes estimated earnings.
func (controller *Payouts) Estimations(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
estimatedEarnings, err := controller.service.AllNodesEstimations(ctx)
if err != nil {
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
return
}
if err = json.NewEncoder(w).Encode(estimatedEarnings); err != nil {
controller.log.Error("failed to write json response", zap.Error(err))
return
}
}

// serveError set http statuses and send json error.
func (controller *Payouts) serveError(w http.ResponseWriter, status int, err error) {
w.WriteHeader(status)
Expand Down
2 changes: 2 additions & 0 deletions multinode/console/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func NewServer(log *zap.Logger, config Config, nodes *nodes.Service, payouts *pa
payoutsController := controllers.NewPayouts(server.log, server.payouts)
payoutsRouter := apiRouter.PathPrefix("/payouts").Subrouter()
payoutsRouter.HandleFunc("/total-earned", payoutsController.GetAllNodesTotalEarned).Methods(http.MethodGet)
payoutsRouter.HandleFunc("/estimations/{satelliteID}", payoutsController.SatelliteEstimations).Methods(http.MethodGet)
payoutsRouter.HandleFunc("/estimations", payoutsController.Estimations).Methods(http.MethodGet)

if server.config.StaticDir != "" {
router.PathPrefix("/static/").Handler(http.StripPrefix("/static", fs))
Expand Down
97 changes: 97 additions & 0 deletions multinode/payouts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,103 @@ func (service *Service) GetAllNodesEarnedOnSatellite(ctx context.Context) (earne
return earned, nil
}

// AllNodesSatelliteEstimations returns specific satellite all time estimated earnings.
func (service *Service) AllNodesSatelliteEstimations(ctx context.Context, satelliteID storj.NodeID) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)

var estimatedEarnings int64

list, err := service.nodes.List(ctx)
if err != nil {
return 0, Error.Wrap(err)
}

for _, node := range list {
estimation, err := service.nodeSatelliteEstimations(ctx, node, satelliteID)
if err != nil {
return 0, Error.Wrap(err)
}

estimatedEarnings += estimation
}

return estimatedEarnings, nil
}

// AllNodesEstimations returns all satellites all time estimated earnings.
func (service *Service) AllNodesEstimations(ctx context.Context) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)

var estimatedEarnings int64

list, err := service.nodes.List(ctx)
if err != nil {
return 0, Error.Wrap(err)
}

for _, node := range list {
estimation, err := service.nodeEstimations(ctx, node)
if err != nil {
return 0, Error.Wrap(err)
}

estimatedEarnings += estimation
}

return estimatedEarnings, nil
}

// nodeEstimations retrieves data from a single node.
func (service *Service) nodeEstimations(ctx context.Context, node nodes.Node) (estimation int64, err error) {
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Address: node.PublicAddress,
})
if err != nil {
return 0, Error.Wrap(err)
}

defer func() {
err = errs.Combine(err, conn.Close())
}()

payoutClient := multinodepb.NewDRPCPayoutClient(conn)
header := &multinodepb.RequestHeader{
ApiKey: node.APISecret,
}

response, err := payoutClient.EstimatedPayoutTotal(ctx, &multinodepb.EstimatedPayoutTotalRequest{Header: header})
if err != nil {
return 0, Error.Wrap(err)
}

return response.EstimatedEarnings, nil
}

// nodeSatelliteEstimations retrieves data from a single node.
func (service *Service) nodeSatelliteEstimations(ctx context.Context, node nodes.Node, satelliteID storj.NodeID) (estimation int64, err error) {
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Address: node.PublicAddress,
})
if err != nil {
return 0, Error.Wrap(err)
}

defer func() {
err = errs.Combine(err, conn.Close())
}()
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
header := &multinodepb.RequestHeader{
ApiKey: node.APISecret,
}
response, err := payoutClient.EstimatedPayoutSatellite(ctx, &multinodepb.EstimatedPayoutSatelliteRequest{Header: header, SatelliteId: satelliteID})
if err != nil {
return 0, Error.Wrap(err)
}
return response.EstimatedEarnings, nil
}

func (service *Service) getAmount(ctx context.Context, node nodes.Node) (_ int64, err error) {
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: node.ID,
Expand Down

0 comments on commit a11698f

Please sign in to comment.