Fix monthly earning estimation (#4282)

This commit is contained in:
TungHoang 2021-11-18 00:26:21 +01:00 committed by GitHub
parent 336500c04d
commit f9b630b0f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 20 deletions

View File

@ -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{})
}

View File

@ -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.

View File

@ -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,