-
Notifications
You must be signed in to change notification settings - Fork 789
/
datahistory_manager_types.go
199 lines (183 loc) · 6.76 KB
/
datahistory_manager_types.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package engine
import (
"errors"
"time"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/repository/datahistoryjob"
"github.com/thrasher-corp/gocryptotrader/database/repository/datahistoryjobresult"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
)
const dataHistoryManagerName = "data_history_manager"
type dataHistoryStatus int64
type dataHistoryDataType int64
// Data type descriptors
const (
dataHistoryCandleDataType dataHistoryDataType = iota
dataHistoryTradeDataType
dataHistoryConvertTradesDataType
dataHistoryConvertCandlesDataType
dataHistoryCandleValidationDataType
dataHistoryCandleValidationSecondarySourceType
)
// DataHistoryJob status descriptors
const (
dataHistoryStatusActive dataHistoryStatus = iota
dataHistoryStatusFailed
dataHistoryStatusComplete
dataHistoryStatusRemoved
dataHistoryIntervalIssuesFound
dataHistoryStatusPaused
)
// String stringifies iotas to readable
func (d dataHistoryStatus) String() string {
switch {
case int64(d) == 0:
return "active"
case int64(d) == 1:
return "failed"
case int64(d) == 2:
return "complete"
case int64(d) == 3:
return "removed"
case int64(d) == 4:
return "issues found"
case int64(d) == 5:
return "paused"
}
return ""
}
// Valid ensures the value set is legitimate
func (d dataHistoryStatus) Valid() bool {
return int64(d) >= 0 && int64(d) <= 5
}
// String stringifies iotas to readable
func (d dataHistoryDataType) String() string {
n := int64(d)
switch n {
case 0:
return "candles"
case 1:
return "trades"
case 2:
return "trade conversion"
case 3:
return "candle conversion"
case 4:
return "conversion validation"
case 5:
return "conversion validation secondary source"
}
return ""
}
// Valid ensures the value set is legitimate
func (d dataHistoryDataType) Valid() bool {
return int64(d) >= 0 && int64(d) <= 5
}
var (
errJobNotFound = errors.New("job not found")
errUnknownDataType = errors.New("job has invalid datatype set and cannot be processed")
errNilJob = errors.New("nil job received")
errNicknameIDUnset = errors.New("must set 'id' OR 'nickname'")
errEmptyID = errors.New("id not set")
errOnlyNicknameOrID = errors.New("can only set 'id' OR 'nickname'")
errBadStatus = errors.New("cannot set job status")
errNicknameInUse = errors.New("cannot continue as nickname already in use")
errNicknameUnset = errors.New("cannot continue as nickname unset")
errJobInvalid = errors.New("job has not been setup properly and cannot be processed")
errInvalidDataHistoryStatus = errors.New("unsupported data history status received")
errInvalidDataHistoryDataType = errors.New("unsupported data history data type received")
errNilResult = errors.New("received nil job result")
errJobMustBeActiveOrPaused = errors.New("job must be active or paused to be set as a prerequisite")
errNilCandles = errors.New("received nil candles")
// defaultDataHistoryTradeInterval is the default interval size used to verify whether there is any database data
// for a trade job
defaultDataHistoryTradeInterval = kline.FifteenMin
defaultDataHistoryMaxJobsPerCycle int64 = 5
defaultMaxResultInsertions int64 = 10000
defaultDataHistoryBatchLimit int64 = 3
defaultDataHistoryRetryAttempts int64 = 3
defaultDataHistoryRequestSizeLimit int64 = 500
defaultDataHistoryTicker = time.Minute
defaultDataHistoryTradeRequestSize int64 = 10
defaultDecimalPlaceComparison int64 = 3
)
// DataHistoryManager is responsible for synchronising,
// retrieving and saving candle and trade data from loaded jobs
type DataHistoryManager struct {
exchangeManager iExchangeManager
databaseConnectionInstance database.IDatabase
started int32
processing int32
shutdown chan struct{}
interval *time.Ticker
jobDB datahistoryjob.IDBService
jobResultDB datahistoryjobresult.IDBService
maxJobsPerCycle int64
maxResultInsertions int64
verbose bool
candleLoader func(string, currency.Pair, asset.Item, kline.Interval, time.Time, time.Time) (*kline.Item, error)
tradeLoader func(string, string, string, string, time.Time, time.Time) ([]trade.Data, error)
tradeSaver func(...trade.Data) error
candleSaver func(*kline.Item, bool) (uint64, error)
}
// DataHistoryJob used to gather candle/trade history and save
// to the database
type DataHistoryJob struct {
ID uuid.UUID
Nickname string
Exchange string
Asset asset.Item
Pair currency.Pair
StartDate time.Time
EndDate time.Time
Interval kline.Interval
RunBatchLimit int64
RequestSizeLimit int64
DataType dataHistoryDataType
MaxRetryAttempts int64
Status dataHistoryStatus
CreatedDate time.Time
Results map[int64][]DataHistoryJobResult
rangeHolder *kline.IntervalRangeHolder
OverwriteExistingData bool
ConversionInterval kline.Interval
DecimalPlaceComparison int64
SecondaryExchangeSource string
IssueTolerancePercentage float64
ReplaceOnIssue bool
// Prerequisites mean this job is paused until the prerequisite job is completed
PrerequisiteJobID uuid.UUID
PrerequisiteJobNickname string
}
// DataHistoryJobResult contains details on
// the result of a history request
type DataHistoryJobResult struct {
ID uuid.UUID
JobID uuid.UUID
IntervalStartDate time.Time
IntervalEndDate time.Time
Status dataHistoryStatus
Result string
Date time.Time
}
// DataHistoryJobSummary is a human readable summary of the job
// for quickly understanding the status of a given job
type DataHistoryJobSummary struct {
Nickname string
Exchange string
Asset asset.Item
Pair currency.Pair
StartDate time.Time
EndDate time.Time
Interval kline.Interval
Status dataHistoryStatus
DataType dataHistoryDataType
ResultRanges []string
OverwriteExistingData bool
ConversionInterval kline.Interval
PrerequisiteJobNickname string
}