2021-02-05 11:37:59 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2021-05-05 18:10:58 +01:00
|
|
|
"github.com/gorilla/mux"
|
2021-02-05 11:37:59 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-05-05 18:10:58 +01:00
|
|
|
"storj.io/common/storj"
|
2021-02-05 11:37:59 +00:00
|
|
|
"storj.io/storj/multinode/payouts"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrPayouts is an internal error type for payouts web api controller.
|
2021-04-28 09:06:17 +01:00
|
|
|
ErrPayouts = errs.Class("payouts web api controller")
|
2021-02-05 11:37:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Payouts is a web api controller.
|
|
|
|
type Payouts struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *payouts.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPayouts is a constructor for Payouts.
|
|
|
|
func NewPayouts(log *zap.Logger, service *payouts.Service) *Payouts {
|
|
|
|
return &Payouts{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllNodesTotalEarned handles retrieval total earned amount .
|
|
|
|
func (controller *Payouts) GetAllNodesTotalEarned(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
earned, err := controller.service.GetAllNodesAllTimeEarned(ctx)
|
|
|
|
if err != nil {
|
|
|
|
controller.log.Error("all node total earned internal error", zap.Error(err))
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(earned); err != nil {
|
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
// NodeExpectations handles node's estimated and undistributed.
|
|
|
|
func (controller *Payouts) NodeExpectations(w http.ResponseWriter, r *http.Request) {
|
2021-05-05 18:10:58 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-05-05 21:01:16 +01:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2021-05-05 18:10:58 +01:00
|
|
|
segmentParams := mux.Vars(r)
|
2021-05-05 21:01:16 +01:00
|
|
|
|
2021-05-24 16:10:30 +01:00
|
|
|
id, ok := segmentParams["nodeID"]
|
2021-05-05 18:10:58 +01:00
|
|
|
if !ok {
|
2021-05-24 16:10:30 +01:00
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.New("couldn't receive route variable nodeID"))
|
2021-05-05 18:10:58 +01:00
|
|
|
return
|
|
|
|
}
|
2021-05-05 21:01:16 +01:00
|
|
|
|
2021-05-24 16:10:30 +01:00
|
|
|
nodeID, err := storj.NodeIDFromString(id)
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 21:01:16 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
expectations, err := controller.service.NodeExpectations(ctx, nodeID)
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 21:01:16 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
if err = json.NewEncoder(w).Encode(expectations); err != nil {
|
2021-05-05 18:10:58 +01:00
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
// Expectations handles nodes estimated and undistributed earnings.
|
|
|
|
func (controller *Payouts) Expectations(w http.ResponseWriter, r *http.Request) {
|
2021-05-05 18:10:58 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-05-05 21:01:16 +01:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
expectations, err := controller.service.Expectations(ctx)
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 21:01:16 +01:00
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
if err = json.NewEncoder(w).Encode(expectations); err != nil {
|
2021-05-05 18:10:58 +01:00
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 18:34:10 +01:00
|
|
|
// PeriodSummary handles retrieval from nodes for specific period.
|
|
|
|
func (controller *Payouts) PeriodSummary(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2021-05-05 21:01:16 +01:00
|
|
|
segmentParams := mux.Vars(r)
|
2021-05-05 18:34:10 +01:00
|
|
|
|
|
|
|
period, ok := segmentParams["period"]
|
|
|
|
if !ok {
|
2021-05-05 21:01:16 +01:00
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.New("couldn't receive route variable period"))
|
2021-05-05 18:34:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := controller.service.NodesPeriodSummary(ctx, period)
|
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(summary); err != nil {
|
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summary handles retrieval from nodes.
|
|
|
|
func (controller *Payouts) Summary(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
summary, err := controller.service.NodesSummary(ctx)
|
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(summary); err != nil {
|
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:01:16 +01:00
|
|
|
// SatellitePeriodSummary handles retrieval from nodes from specific satellite for specific period.
|
|
|
|
func (controller *Payouts) SatellitePeriodSummary(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
segmentParams := mux.Vars(r)
|
|
|
|
|
|
|
|
period, ok := segmentParams["period"]
|
|
|
|
if !ok {
|
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.New("couldn't receive route variable period"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, ok := segmentParams["id"]
|
|
|
|
if !ok {
|
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.New("couldn't receive route variable satelliteID"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
satelliteID, err := storj.NodeIDFromString(id)
|
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := controller.service.NodesSatellitePeriodSummary(ctx, satelliteID, period)
|
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(summary); err != nil {
|
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SatelliteSummary handles retrieval from nodes from specific satellite.
|
|
|
|
func (controller *Payouts) SatelliteSummary(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
segmentParams := mux.Vars(r)
|
|
|
|
|
|
|
|
id, ok := segmentParams["id"]
|
|
|
|
if !ok {
|
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.New("couldn't receive route variable satelliteID"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
satelliteID, err := storj.NodeIDFromString(id)
|
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusBadRequest, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := controller.service.NodesSatelliteSummary(ctx, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
controller.serveError(w, http.StatusInternalServerError, ErrPayouts.Wrap(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(summary); err != nil {
|
|
|
|
controller.log.Error("failed to write json response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:37:59 +00:00
|
|
|
// serveError set http statuses and send json error.
|
|
|
|
func (controller *Payouts) serveError(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 {
|
|
|
|
controller.log.Error("failed to write json error response", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|