-
Notifications
You must be signed in to change notification settings - Fork 402
/
payout.go
132 lines (116 loc) · 5.23 KB
/
payout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package payout
import (
"context"
"time"
"github.com/zeebo/errs"
"storj.io/common/storj"
)
// DB works with payout database.
//
// architecture: Database
type DB interface {
// StorePayStub inserts or updates paystub into the DB.
StorePayStub(ctx context.Context, paystub PayStub) error
// GetPayStub retrieves paystub for specific satellite and period.
GetPayStub(ctx context.Context, satelliteID storj.NodeID, period string) (*PayStub, error)
// AllPayStubs retrieves paystubs from all satellites in specific period from DB.
AllPayStubs(ctx context.Context, period string) ([]PayStub, error)
// SatellitesHeldbackHistory retrieves heldback history for specific satellite from DB.
SatellitesHeldbackHistory(ctx context.Context, satelliteID storj.NodeID) ([]HoldForPeriod, error)
// SatellitesDisposedHistory returns all disposed amount for specific satellite from DB.
SatellitesDisposedHistory(ctx context.Context, satelliteID storj.NodeID) (int64, error)
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payout data.
SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) ([]string, error)
// AllPeriods retrieves all periods in which we have some payout data.
AllPeriods(ctx context.Context) ([]string, error)
// StorePayment inserts or updates payment into the DB
StorePayment(ctx context.Context, payment Payment) error
// GetReceipt retrieves receipt for specific satellite and period.
GetReceipt(ctx context.Context, satelliteID storj.NodeID, period string) (string, error)
}
// ErrNoPayStubForPeriod represents errors from the payout database.
var ErrNoPayStubForPeriod = errs.Class("no payStub for period error")
// PayStub is node payout data for satellite by specific period.
type PayStub struct {
SatelliteID storj.NodeID `json:"satelliteId"`
Period string `json:"period"`
Created time.Time `json:"created"`
Codes string `json:"codes"`
UsageAtRest float64 `json:"usageAtRest"`
UsageGet int64 `json:"usageGet"`
UsagePut int64 `json:"usagePut"`
UsageGetRepair int64 `json:"usageGetRepair"`
UsagePutRepair int64 `json:"usagePutRepair"`
UsageGetAudit int64 `json:"usageGetAudit"`
CompAtRest int64 `json:"compAtRest"`
CompGet int64 `json:"compGet"`
CompPut int64 `json:"compPut"`
CompGetRepair int64 `json:"compGetRepair"`
CompPutRepair int64 `json:"compPutRepair"`
CompGetAudit int64 `json:"compGetAudit"`
SurgePercent int64 `json:"surgePercent"`
Held int64 `json:"held"`
Owed int64 `json:"owed"`
Disposed int64 `json:"disposed"`
Paid int64 `json:"paid"`
}
// HoldForPeriod is node's held amount for period.
type HoldForPeriod struct {
Period string `json:"period"`
Amount int64 `json:"amount"`
}
// Payment is node payment data for specific period.
type Payment struct {
ID int64 `json:"id"`
Created time.Time `json:"created"`
SatelliteID storj.NodeID `json:"satelliteId"`
Period string `json:"period"`
Amount int64 `json:"amount"`
Receipt string `json:"receipt"`
Notes string `json:"notes"`
}
// SatelliteHeldHistory amount of held for specific satellite for all time since join.
type SatelliteHeldHistory struct {
SatelliteID storj.NodeID `json:"satelliteID"`
SatelliteName string `json:"satelliteName"`
HoldForFirstPeriod int64 `json:"holdForFirstPeriod"`
HoldForSecondPeriod int64 `json:"holdForSecondPeriod"`
HoldForThirdPeriod int64 `json:"holdForThirdPeriod"`
TotalHeld int64 `json:"totalHeld"`
TotalDisposed int64 `json:"totalDisposed"`
JoinedAt time.Time `json:"joinedAt"`
}
// SatellitePayoutForPeriod contains payout information for specific period for specific satellite.
type SatellitePayoutForPeriod struct {
SatelliteID string `json:"satelliteID"`
SatelliteURL string `json:"satelliteURL"`
Age int64 `json:"age"`
Earned int64 `json:"earned"`
Surge int64 `json:"surge"`
SurgePercent int64 `json:"surgePercent"`
Held int64 `json:"held"`
HeldPercent float64 `json:"heldPercent"`
AfterHeld int64 `json:"afterHeld"`
Disposed int64 `json:"disposed"`
Paid int64 `json:"paid"`
Receipt string `json:"receipt"`
IsExitComplete bool `json:"isExitComplete"`
}
// Period is a string that represents paystub period type in format yyyy-mm.
type Period string
// Time returns period in time.Time type from string.
func (p Period) Time() (time.Time, error) {
return time.Parse("2006-01", string(p))
}
// GetEarnedWithSurge returns paystub's total earned and surge.
func (paystub *PayStub) GetEarnedWithSurge() (earned int64, surge int64) {
earned = paystub.CompGetAudit + paystub.CompGet + paystub.CompGetRepair + paystub.CompAtRest
surge = earned * paystub.SurgePercent / 100
return earned, surge
}
// UsageAtRestTbM converts paystub's usage_at_rest from tbh to tbm.
func (paystub *PayStub) UsageAtRestTbM() {
paystub.UsageAtRest /= 720
}