forked from ava-labs/ortelius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
184 lines (154 loc) · 4.04 KB
/
params.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
// (c) 2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package params
import (
"errors"
"fmt"
"net/url"
"time"
"github.com/corpetty/avalanchego/ids"
"github.com/gocraft/dbr/v2"
)
const (
KeyID = "id"
KeyChainID = "chainID"
KeyAddress = "address"
KeyHash = "hash"
KeyAlias = "alias"
KeyAssetID = "assetID"
KeySearchQuery = "query"
KeySortBy = "sort"
KeyLimit = "limit"
KeyOffset = "offset"
KeySpent = "spent"
KeyStartTime = "startTime"
KeyEndTime = "endTime"
KeyIntervalSize = "intervalSize"
KeyDisableCount = "disableCount"
KeyDisableGenesis = "disableGenesis"
KeyVersion = "version"
KeyEnableAggregate = "enableAggregate"
KeyOutputOutputType = "outputOutputType"
KeyOutputGroupID = "outputGroupId"
PaginationMaxLimit = 5000
PaginationDefaultOffset = 0
VersionDefault = 0
)
var (
IntervalMinute = 1 * time.Minute
IntervalHour = 60 * time.Minute
IntervalDay = 1440 * time.Minute
IntervalWeek = 7 * IntervalDay
IntervalMonth = 30 * IntervalDay
IntervalYear = 365 * IntervalDay
IntervalAll = time.Duration(0)
IntervalNames = map[string]time.Duration{
"minute": IntervalMinute,
"hour": IntervalHour,
"day": IntervalDay,
"week": IntervalWeek,
"month": IntervalMonth,
"year": IntervalYear,
"all": IntervalAll,
}
ErrUndefinedSort = errors.New("undefined sort")
// Ensure params types satisfy the interface
_ Param = &ListParams{}
TransactionRoundDuration = time.Second
)
type Param interface {
ForValues(uint8, url.Values) error
CacheKey() []string
}
func CacheKey(name string, val interface{}) string {
return fmt.Sprintf("%s=%v", name, val)
}
//
// Global params
//
type ListParams struct {
Values url.Values
ID *ids.ID
Query string
StartTime time.Time
EndTime time.Time
Limit int
Offset int
DisableCounting bool
StartTimeProvided bool
EndTimeProvided bool
ObserveTimeProvided bool
}
func (p *ListParams) ForValues(version uint8, q url.Values) (err error) {
p.Values = q
p.Limit, err = GetQueryInt(q, KeyLimit, PaginationMaxLimit)
if err != nil {
return err
}
if p.Limit > PaginationMaxLimit {
p.Limit = PaginationMaxLimit
}
p.Offset, err = GetQueryInt(q, KeyOffset, PaginationDefaultOffset)
if err != nil {
return err
}
p.ID, err = GetQueryID(q, KeyID)
if err != nil {
return err
}
p.StartTimeProvided, p.StartTime, err = GetQueryTime(q, KeyStartTime)
if err != nil {
return err
}
p.StartTime = p.StartTime.Round(TransactionRoundDuration)
p.EndTimeProvided, p.EndTime, err = GetQueryTime(q, KeyEndTime)
if err != nil {
return err
}
if p.EndTime.IsZero() {
p.EndTime = time.Now().UTC()
}
p.EndTime = p.EndTime.Round(TransactionRoundDuration)
p.Query = GetQueryString(q, KeySearchQuery, "")
if p.DisableCounting, err = GetQueryBool(q, KeyDisableCount, false); err != nil {
return err
}
// Always disable counting for v2+
if version >= 2 {
p.DisableCounting = true
}
return nil
}
func (p *ListParams) CacheKey() []string {
var keys []string
if p.ID != nil {
keys = append(keys, CacheKey(KeyID, p.ID.String()))
}
return append(keys,
CacheKey(KeyLimit, p.Limit),
CacheKey(KeyOffset, p.Offset),
CacheKey(KeyStartTime, p.StartTime.Unix()),
CacheKey(KeyEndTime, p.EndTime.Unix()),
CacheKey(KeySearchQuery, p.Query),
CacheKey(KeyDisableCount, p.DisableCounting))
}
func (p ListParams) Apply(listTable string, b *dbr.SelectBuilder) *dbr.SelectBuilder {
return p.ApplyPk(listTable, b, "id", true)
}
func (p ListParams) ApplyPk(listTable string, b *dbr.SelectBuilder, primaryKey string, pk bool) *dbr.SelectBuilder {
if p.Limit != 0 {
b.Limit(uint64(p.Limit))
}
if p.Offset != 0 {
b.Offset(uint64(p.Offset))
}
if pk {
if p.ID != nil {
b.Where(listTable+"."+primaryKey+" = ?", p.ID.String()).Limit(1)
}
if p.Query != "" {
b.Where(dbr.Like(listTable+"."+primaryKey, p.Query+"%"))
}
}
return b
}