2020-09-08 16:15:08 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
package estimatedpayouts
|
2020-09-08 16:15:08 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2021-01-22 13:06:59 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"storj.io/storj/private/date"
|
2020-09-08 16:15:08 +01:00
|
|
|
)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// EstimatedPayout contains usage and estimated payouts data for current and previous months.
|
2020-09-08 16:15:08 +01:00
|
|
|
type EstimatedPayout struct {
|
2021-01-14 16:41:36 +00:00
|
|
|
CurrentMonth PayoutMonthly `json:"currentMonth"`
|
|
|
|
PreviousMonth PayoutMonthly `json:"previousMonth"`
|
2021-05-05 18:10:58 +01:00
|
|
|
CurrentMonthExpectations int64 `json:"currentMonthExpectations"`
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// PayoutMonthly contains usage and estimated payouts date.
|
2020-09-08 16:15:08 +01:00
|
|
|
type PayoutMonthly struct {
|
|
|
|
EgressBandwidth int64 `json:"egressBandwidth"`
|
2020-09-30 17:02:29 +01:00
|
|
|
EgressBandwidthPayout float64 `json:"egressBandwidthPayout"`
|
2020-09-08 16:15:08 +01:00
|
|
|
EgressRepairAudit int64 `json:"egressRepairAudit"`
|
2020-09-30 17:02:29 +01:00
|
|
|
EgressRepairAuditPayout float64 `json:"egressRepairAuditPayout"`
|
2020-09-08 16:15:08 +01:00
|
|
|
DiskSpace float64 `json:"diskSpace"`
|
2020-09-30 17:02:29 +01:00
|
|
|
DiskSpacePayout float64 `json:"diskSpacePayout"`
|
2020-09-04 13:05:13 +01:00
|
|
|
HeldRate float64 `json:"heldRate"`
|
2020-09-30 17:02:29 +01:00
|
|
|
Payout float64 `json:"payout"`
|
|
|
|
Held float64 `json:"held"`
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// SetEgressBandwidthPayout counts egress bandwidth payouts for PayoutMonthly object.
|
2020-09-08 16:15:08 +01:00
|
|
|
func (pm *PayoutMonthly) SetEgressBandwidthPayout(egressPrice int64) {
|
2020-09-30 17:02:29 +01:00
|
|
|
amount := float64(pm.EgressBandwidth*egressPrice) / math.Pow10(12)
|
|
|
|
pm.EgressBandwidthPayout += RoundFloat(amount)
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// SetEgressRepairAuditPayout counts audit and repair payouts for PayoutMonthly object.
|
2020-09-08 16:15:08 +01:00
|
|
|
func (pm *PayoutMonthly) SetEgressRepairAuditPayout(auditRepairPrice int64) {
|
2020-09-30 17:02:29 +01:00
|
|
|
amount := float64(pm.EgressRepairAudit*auditRepairPrice) / math.Pow10(12)
|
|
|
|
pm.EgressRepairAuditPayout += RoundFloat(amount)
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// SetDiskSpacePayout counts disk space payouts for PayoutMonthly object.
|
2020-09-08 16:15:08 +01:00
|
|
|
func (pm *PayoutMonthly) SetDiskSpacePayout(diskSpacePrice int64) {
|
2020-09-30 17:02:29 +01:00
|
|
|
amount := pm.DiskSpace * float64(diskSpacePrice) / math.Pow10(12)
|
|
|
|
pm.DiskSpacePayout += RoundFloat(amount)
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeldAmount counts held amount for PayoutMonthly object.
|
|
|
|
func (pm *PayoutMonthly) SetHeldAmount() {
|
2020-09-04 13:05:13 +01:00
|
|
|
amount := (pm.DiskSpacePayout + pm.EgressBandwidthPayout + pm.EgressRepairAuditPayout) * pm.HeldRate / 100
|
2020-10-22 13:05:37 +01:00
|
|
|
pm.Held = amount
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// SetPayout counts payouts amount for PayoutMonthly object.
|
2020-09-08 16:15:08 +01:00
|
|
|
func (pm *PayoutMonthly) SetPayout() {
|
2020-09-30 17:02:29 +01:00
|
|
|
amount := pm.DiskSpacePayout + pm.EgressBandwidthPayout + pm.EgressRepairAuditPayout - pm.Held
|
|
|
|
pm.Payout = RoundFloat(amount)
|
|
|
|
}
|
|
|
|
|
2021-02-21 15:22:44 +00:00
|
|
|
// Add sums payout monthly data.
|
|
|
|
func (pm *PayoutMonthly) Add(monthly PayoutMonthly) {
|
|
|
|
pm.Payout += monthly.Payout
|
|
|
|
pm.EgressRepairAuditPayout += monthly.EgressRepairAuditPayout
|
|
|
|
pm.DiskSpacePayout += monthly.DiskSpacePayout
|
|
|
|
pm.DiskSpace += monthly.DiskSpace
|
|
|
|
pm.EgressBandwidth += monthly.EgressBandwidth
|
|
|
|
pm.EgressBandwidthPayout += monthly.EgressBandwidthPayout
|
|
|
|
pm.EgressRepairAudit += monthly.EgressRepairAudit
|
|
|
|
pm.Held += monthly.Held
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:02:29 +01:00
|
|
|
// RoundFloat rounds float value till 2 signs after dot.
|
|
|
|
func RoundFloat(value float64) float64 {
|
|
|
|
return math.Round(value*100) / 100
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
2021-01-22 13:06:59 +00:00
|
|
|
|
2021-02-21 15:22:44 +00:00
|
|
|
// Set set's estimated payout with current/previous PayoutMonthly's data and current month expectations.
|
|
|
|
func (estimatedPayout *EstimatedPayout) Set(current, previous PayoutMonthly, now, joinedAt time.Time) {
|
|
|
|
estimatedPayout.CurrentMonth = current
|
|
|
|
estimatedPayout.PreviousMonth = previous
|
2021-02-01 11:18:47 +00:00
|
|
|
|
2021-11-17 23:26:21 +00:00
|
|
|
beginOfMonth := date.UTCBeginOfMonth(now)
|
|
|
|
endOfMonth := date.UTCEndOfMonth(now)
|
2021-02-01 11:18:47 +00:00
|
|
|
|
2021-11-17 23:26:21 +00:00
|
|
|
// Joined before the first day of this month
|
|
|
|
if joinedAt.Before(beginOfMonth) {
|
|
|
|
minutesPast := now.Sub(beginOfMonth).Minutes()
|
|
|
|
estimatedMinutesJoinedCurrentMonth := endOfMonth.Sub(beginOfMonth).Minutes()
|
2021-02-21 15:22:44 +00:00
|
|
|
|
2021-11-17 23:26:21 +00:00
|
|
|
payoutPerMin := estimatedPayout.CurrentMonth.Payout / minutesPast
|
|
|
|
estimatedPayout.CurrentMonthExpectations += int64(payoutPerMin * estimatedMinutesJoinedCurrentMonth)
|
2021-02-21 15:22:44 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-17 23:26:21 +00:00
|
|
|
// Joined this month
|
|
|
|
minutesSinceJoined := now.Sub(joinedAt).Minutes()
|
|
|
|
estimatedMinutesJoinedCurrentMonth := endOfMonth.Sub(joinedAt).Minutes()
|
|
|
|
estimatedPayout.CurrentMonthExpectations += int64(estimatedPayout.CurrentMonth.Payout / minutesSinceJoined * estimatedMinutesJoinedCurrentMonth)
|
2021-02-21 15:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds estimate into the receiver.
|
|
|
|
func (estimatedPayout *EstimatedPayout) Add(other EstimatedPayout) {
|
|
|
|
estimatedPayout.CurrentMonth.Add(other.CurrentMonth)
|
|
|
|
estimatedPayout.PreviousMonth.Add(other.PreviousMonth)
|
|
|
|
estimatedPayout.CurrentMonthExpectations += other.CurrentMonthExpectations
|
2021-01-22 13:06:59 +00:00
|
|
|
}
|