storagenode/{payouts,console}: use same time for all calculations
When using calling time.Now() multiple times, they can cross month boundary causing errors in calculations. Change-Id: I66b5be7598f3bf475b4b5fe0dcce82eee55b3134
This commit is contained in:
parent
ee1f67bb18
commit
5d895fb404
@ -6,6 +6,7 @@ package consoleapi
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/zeebo/errs"
|
||||
@ -119,10 +120,12 @@ func (dashboard *StorageNode) EstimatedPayout(w http.ResponseWriter, r *http.Req
|
||||
|
||||
w.Header().Set(contentType, applicationJSON)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
queryParams := r.URL.Query()
|
||||
id := queryParams.Get("id")
|
||||
if id == "" {
|
||||
data, err := dashboard.service.GetAllSatellitesEstimatedPayout(ctx)
|
||||
data, err := dashboard.service.GetAllSatellitesEstimatedPayout(ctx, now)
|
||||
if err != nil {
|
||||
dashboard.serveJSONError(w, http.StatusInternalServerError, ErrStorageNodeAPI.Wrap(err))
|
||||
return
|
||||
@ -139,7 +142,7 @@ func (dashboard *StorageNode) EstimatedPayout(w http.ResponseWriter, r *http.Req
|
||||
return
|
||||
}
|
||||
|
||||
data, err := dashboard.service.GetSatelliteEstimatedPayout(ctx, satelliteID)
|
||||
data, err := dashboard.service.GetSatelliteEstimatedPayout(ctx, satelliteID, now)
|
||||
if err != nil {
|
||||
dashboard.serveJSONError(w, http.StatusInternalServerError, ErrStorageNodeAPI.Wrap(err))
|
||||
return
|
||||
|
@ -115,7 +115,7 @@ func TestStorageNodeApi(t *testing.T) {
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
estimation, err := sno.Console.Service.GetAllSatellitesEstimatedPayout(ctx)
|
||||
estimation, err := sno.Console.Service.GetAllSatellitesEstimatedPayout(ctx, time.Now())
|
||||
require.NoError(t, err)
|
||||
expected, err := json.Marshal(estimatedpayouts.EstimatedPayout{
|
||||
CurrentMonth: estimation.CurrentMonth,
|
||||
|
@ -429,8 +429,8 @@ func (s *Service) GetAllSatellitesData(ctx context.Context) (_ *Satellites, err
|
||||
}
|
||||
|
||||
// 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)
|
||||
func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID storj.NodeID, now time.Time) (estimatedPayout estimatedpayouts.EstimatedPayout, err error) {
|
||||
estimatedPayout, err = s.estimation.GetSatelliteEstimatedPayout(ctx, satelliteID, now)
|
||||
if err != nil {
|
||||
return estimatedpayouts.EstimatedPayout{}, SNOServiceErr.Wrap(err)
|
||||
}
|
||||
@ -439,8 +439,8 @@ func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID s
|
||||
}
|
||||
|
||||
// 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)
|
||||
func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context, now time.Time) (estimatedPayout estimatedpayouts.EstimatedPayout, err error) {
|
||||
estimatedPayout, err = s.estimation.GetAllSatellitesEstimatedPayout(ctx, now)
|
||||
if err != nil {
|
||||
return estimatedpayouts.EstimatedPayout{}, SNOServiceErr.Wrap(err)
|
||||
}
|
||||
|
@ -53,11 +53,10 @@ func NewService(bandwidthDB bandwidth.DB, reputationDB reputation.DB, storageUsa
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID storj.NodeID, now time.Time) (payout EstimatedPayout, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
now := time.Now()
|
||||
currentMonthPayout, previousMonthPayout, err := s.estimatedPayout(ctx, satelliteID)
|
||||
currentMonthPayout, previousMonthPayout, err := s.estimatedPayout(ctx, satelliteID, now)
|
||||
if err != nil {
|
||||
return EstimatedPayout{}, EstimationServiceErr.Wrap(err)
|
||||
}
|
||||
@ -70,7 +69,7 @@ func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID s
|
||||
return EstimatedPayout{}, EstimationServiceErr.Wrap(err)
|
||||
}
|
||||
|
||||
daysSinceJoined := time.Since(stats.JoinedAt).Hours() / 24
|
||||
daysSinceJoined := stats.JoinedAt.Sub(now).Hours() / 24
|
||||
if daysSinceJoined >= float64(now.Day()) {
|
||||
payout.SetExpectedMonth(now)
|
||||
|
||||
@ -82,13 +81,12 @@ func (s *Service) GetSatelliteEstimatedPayout(ctx context.Context, satelliteID s
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context, now time.Time) (payout EstimatedPayout, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
now := time.Now()
|
||||
|
||||
satelliteIDs := s.trust.GetSatellites(ctx)
|
||||
for i := 0; i < len(satelliteIDs); i++ {
|
||||
current, previous, err := s.estimatedPayout(ctx, satelliteIDs[i])
|
||||
current, previous, err := s.estimatedPayout(ctx, satelliteIDs[i], now)
|
||||
if err != nil {
|
||||
return EstimatedPayout{}, EstimationServiceErr.Wrap(err)
|
||||
}
|
||||
@ -117,7 +115,7 @@ func (s *Service) GetAllSatellitesEstimatedPayout(ctx context.Context) (payout E
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func (s *Service) estimatedPayout(ctx context.Context, satelliteID storj.NodeID, now time.Time) (currentMonthPayout PayoutMonthly, previousMonthPayout PayoutMonthly, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
priceModel, err := s.pricingDB.Get(ctx, satelliteID)
|
||||
@ -130,8 +128,8 @@ func (s *Service) estimatedPayout(ctx context.Context, satelliteID storj.NodeID)
|
||||
return PayoutMonthly{}, PayoutMonthly{}, EstimationServiceErr.Wrap(err)
|
||||
}
|
||||
|
||||
currentMonthPayout, err = s.estimationUsagePeriod(ctx, time.Now().UTC(), stats.JoinedAt, priceModel)
|
||||
previousMonthPayout, err = s.estimationUsagePeriod(ctx, time.Now().UTC().AddDate(0, -1, 0), stats.JoinedAt, priceModel)
|
||||
currentMonthPayout, err = s.estimationUsagePeriod(ctx, now.UTC(), stats.JoinedAt, priceModel)
|
||||
previousMonthPayout, err = s.estimationUsagePeriod(ctx, now.UTC().AddDate(0, -1, 0), stats.JoinedAt, priceModel)
|
||||
|
||||
return currentMonthPayout, previousMonthPayout, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user