From f9b630b0f4dd894347635d8483bccd73b907fbbc Mon Sep 17 00:00:00 2001 From: TungHoang Date: Thu, 18 Nov 2021 00:26:21 +0100 Subject: [PATCH] Fix monthly earning estimation (#4282) --- private/date/utils.go | 7 +++++ .../estimatedpayouts/estimatedpayouts.go | 26 +++++++++---------- .../estimatedpayouts/estimatedpayouts_test.go | 12 ++++----- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/private/date/utils.go b/private/date/utils.go index a33693868..ecbc53daa 100644 --- a/private/date/utils.go +++ b/private/date/utils.go @@ -58,3 +58,10 @@ func UTCEndOfMonth(now time.Time) time.Time { y, m, _ := now.Date() return time.Date(y, m+1, 1, 0, 0, 0, 0, &time.Location{}).Add(-time.Nanosecond) } + +// UTCBeginOfMonth returns utc begin of month (f.e. to get first day in month at 00h00m). +func UTCBeginOfMonth(now time.Time) time.Time { + now = now.UTC() + y, m, _ := now.Date() + return time.Date(y, m, 1, 0, 0, 0, 0, &time.Location{}) +} diff --git a/storagenode/payouts/estimatedpayouts/estimatedpayouts.go b/storagenode/payouts/estimatedpayouts/estimatedpayouts.go index 616798e1a..a46d24b85 100644 --- a/storagenode/payouts/estimatedpayouts/estimatedpayouts.go +++ b/storagenode/payouts/estimatedpayouts/estimatedpayouts.go @@ -82,24 +82,22 @@ func (estimatedPayout *EstimatedPayout) Set(current, previous PayoutMonthly, now estimatedPayout.CurrentMonth = current estimatedPayout.PreviousMonth = previous - daysPerMonth := float64(date.UTCEndOfMonth(now).Day()) - daysSinceJoined := now.Sub(joinedAt).Hours() / 24 - if daysSinceJoined < 1 { - daysSinceJoined = 1 - } + beginOfMonth := date.UTCBeginOfMonth(now) + endOfMonth := date.UTCEndOfMonth(now) - if daysSinceJoined >= float64(now.Day()) { - daysPast := float64(now.Day()) - 1 - if daysPast < 1 { - daysPast = 1 - } + // Joined before the first day of this month + if joinedAt.Before(beginOfMonth) { + minutesPast := now.Sub(beginOfMonth).Minutes() + estimatedMinutesJoinedCurrentMonth := endOfMonth.Sub(beginOfMonth).Minutes() - payoutPerDay := estimatedPayout.CurrentMonth.Payout / daysPast - estimatedPayout.CurrentMonthExpectations += int64(payoutPerDay * daysPerMonth) + payoutPerMin := estimatedPayout.CurrentMonth.Payout / minutesPast + estimatedPayout.CurrentMonthExpectations += int64(payoutPerMin * estimatedMinutesJoinedCurrentMonth) return } - - estimatedPayout.CurrentMonthExpectations += int64(estimatedPayout.CurrentMonth.Payout / math.Round(daysSinceJoined) * daysPerMonth) + // Joined this month + minutesSinceJoined := now.Sub(joinedAt).Minutes() + estimatedMinutesJoinedCurrentMonth := endOfMonth.Sub(joinedAt).Minutes() + estimatedPayout.CurrentMonthExpectations += int64(estimatedPayout.CurrentMonth.Payout / minutesSinceJoined * estimatedMinutesJoinedCurrentMonth) } // Add adds estimate into the receiver. diff --git a/storagenode/payouts/estimatedpayouts/estimatedpayouts_test.go b/storagenode/payouts/estimatedpayouts/estimatedpayouts_test.go index c0c34c28e..307262627 100644 --- a/storagenode/payouts/estimatedpayouts/estimatedpayouts_test.go +++ b/storagenode/payouts/estimatedpayouts/estimatedpayouts_test.go @@ -24,7 +24,7 @@ func TestCurrentMonthExpectations(t *testing.T) { } tests := []test{ // 28 days in month - {time.Date(2021, 2, 1, 16, 0, 0, 0, time.UTC), 2800.00, time.Date(2021, 1, 1, 12, 0, 0, 0, time.UTC), + {time.Date(2021, 2, 1, 16, 0, 0, 0, time.UTC), 4199.00, time.Date(2021, 1, 1, 12, 0, 0, 0, time.UTC), estimatedpayouts.EstimatedPayout{}, estimatedpayouts.PayoutMonthly{ EgressBandwidth: 123, @@ -48,7 +48,7 @@ func TestCurrentMonthExpectations(t *testing.T) { Payout: payout, Held: 901, }}, - {time.Date(2021, 2, 28, 10, 0, 0, 0, time.UTC), 103, time.Date(2021, 1, 26, 10, 0, 0, 0, time.UTC), + {time.Date(2021, 2, 28, 10, 0, 0, 0, time.UTC), 102, time.Date(2021, 1, 26, 10, 0, 0, 0, time.UTC), estimatedpayouts.EstimatedPayout{}, estimatedpayouts.PayoutMonthly{ EgressBandwidth: 123, @@ -72,7 +72,7 @@ func TestCurrentMonthExpectations(t *testing.T) { Payout: payout, Held: 901, }}, - {time.Date(2021, 2, 28, 10, 0, 0, 0, time.UTC), 215, time.Date(2021, 2, 15, 10, 0, 0, 0, time.UTC), + {time.Date(2021, 2, 28, 10, 0, 0, 0, time.UTC), 104, time.Date(2021, 2, 15, 10, 0, 0, 0, time.UTC), estimatedpayouts.EstimatedPayout{}, estimatedpayouts.PayoutMonthly{ EgressBandwidth: 123, @@ -97,7 +97,7 @@ func TestCurrentMonthExpectations(t *testing.T) { Held: 901, }}, // 31 days in month - {time.Date(2021, 3, 1, 19, 0, 0, 0, time.UTC), 3100.0, time.Date(2021, 1, 1, 19, 0, 0, 0, time.UTC), + {time.Date(2021, 3, 1, 19, 0, 0, 0, time.UTC), 3915.0, time.Date(2021, 1, 1, 19, 0, 0, 0, time.UTC), estimatedpayouts.EstimatedPayout{}, estimatedpayouts.PayoutMonthly{ EgressBandwidth: 123, @@ -121,7 +121,7 @@ func TestCurrentMonthExpectations(t *testing.T) { Payout: payout, Held: 901, }}, - {time.Date(2021, 3, 31, 21, 0, 0, 0, time.UTC), 103, time.Date(2021, 1, 31, 21, 0, 0, 0, time.UTC), + {time.Date(2021, 3, 31, 21, 0, 0, 0, time.UTC), 100, time.Date(2021, 1, 31, 21, 0, 0, 0, time.UTC), estimatedpayouts.EstimatedPayout{}, estimatedpayouts.PayoutMonthly{ EgressBandwidth: 123, @@ -145,7 +145,7 @@ func TestCurrentMonthExpectations(t *testing.T) { Payout: payout, Held: 901, }}, - {time.Date(2021, 3, 31, 21, 0, 0, 0, time.UTC), 193, time.Date(2021, 3, 15, 21, 0, 0, 0, time.UTC), + {time.Date(2021, 3, 31, 21, 0, 0, 0, time.UTC), 100, time.Date(2021, 3, 15, 21, 0, 0, 0, time.UTC), estimatedpayouts.EstimatedPayout{}, estimatedpayouts.PayoutMonthly{ EgressBandwidth: 123,