/
pagination.go
284 lines (242 loc) · 7.81 KB
/
pagination.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package gui
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/decred/dcrpool/pool"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
type paginationPayload struct {
Data interface{} `json:"data"`
Count int `json:"count"`
}
// getPaginationParams parses the request parameters to find pageNumber and
// pageSize which are required for all paginated data requests. Returns first
// and last, the indices of the first and last items to return.
func getPaginationParams(r *http.Request) (first, last int, err error) {
pageNumber, err := strconv.Atoi(r.FormValue("pageNumber"))
if err != nil {
return 0, 0, err
}
pageSize, err := strconv.Atoi(r.FormValue("pageSize"))
if err != nil {
return 0, 0, err
}
if pageNumber < 1 || pageSize < 1 {
return 0, 0, errors.New("invalid number given for pagenumber or pagesize")
}
first = (pageNumber - 1) * pageSize
last = first + pageSize
return first, last, nil
}
// sendJSONResponse JSON encodes the provided payload and writes it to the
// ResponseWriter. Will send a "500 Internal Server Error" if JSON encoding
// fails.
func sendJSONResponse(w http.ResponseWriter, payload interface{}) {
js, err := json.Marshal(payload)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// paginatedBlocks is the handler for "GET /blocks". It uses parameters
// pageNumber and pageSize to prepare a json payload describing confirmed blocks
// mined by the pool, as well as the total count of all confirmed blocks.
func (ui *GUI) paginatedBlocks(w http.ResponseWriter, r *http.Request) {
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
count, confirmedWork, err := ui.cache.getConfirmedMinedWork(first, last)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: confirmedWork,
})
}
// paginatedRewardQuotas is the handler for "GET /rewardquotas". It uses
// parameters pageNumber and pageSize to prepare a json payload describing
// pending reward payment quotas, as well as the total count of all reward
// quotas.
func (ui *GUI) paginatedRewardQuotas(w http.ResponseWriter, r *http.Request) {
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
count, quotas, err := ui.cache.getRewardQuotas(first, last)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: quotas,
})
}
// paginatedBlocksByAccount is the handler for "GET /account/{accountID}/blocks".
// It uses parameters pageNumber, pageSize and accountID to prepare a json
// payload describing blocks mined by the account, as well as the total count of
// all blocks mined by the account.
func (ui *GUI) paginatedBlocksByAccount(w http.ResponseWriter, r *http.Request) {
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
accountID := mux.Vars(r)["accountID"]
count, work, err := ui.cache.getMinedWorkByAccount(first, last, accountID)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: work,
})
}
// paginatedClientsByAccount is the handler for "GET /account/{accountID}/clients".
// It uses parameters pageNumber, pageSize and accountID to prepare a json
// payload describing connected mining clients belonging to the account, as well
// as the total count of all connected clients.
func (ui *GUI) paginatedClientsByAccount(w http.ResponseWriter, r *http.Request) {
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
accountID := mux.Vars(r)["accountID"]
count, clients, err := ui.cache.getClientsForAccount(first, last, accountID)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: clients,
})
}
// paginatedPendingPaymentsByAccount is the handler for "GET
// /account/{accountID}/payments/pending". It uses parameters pageNumber,
// pageSize and accountID to prepare a json payload describing unpaid payments
// due to the account, as well as the total count of all unpaid payments.
func (ui *GUI) paginatedPendingPaymentsByAccount(w http.ResponseWriter, r *http.Request) {
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
accountID := mux.Vars(r)["accountID"]
count, payments, err := ui.cache.getPendingPayments(first, last, accountID)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: payments,
})
}
// paginatedArchivedPaymentsByAccount is the handler for "GET
// /account/{accountID}/payments/archived". It uses parameters pageNumber,
// pageSize and accountID to prepare a json payload describing payments made to
// the account, as well as the total count of all paid payments.
func (ui *GUI) paginatedArchivedPaymentsByAccount(w http.ResponseWriter, r *http.Request) {
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
accountID := mux.Vars(r)["accountID"]
count, payments, err := ui.cache.getArchivedPayments(first, last, accountID)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: payments,
})
}
// paginatedArchivedPoolPayments is the handler for "GET
// /admin/payments/archived". It uses parameters pageNumber, pageSize and
// accountID to prepare a json payload describing payments made to the pool, as
// well as the total count of all paid payments.
// Returns an error if the current session is not authenticated as an admin.
func (ui *GUI) paginatedArchivedPoolPayments(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(sessionKey).(*sessions.Session)
if session.Values["IsAdmin"] != true {
log.Warn("Unauthorized access")
w.WriteHeader(http.StatusUnauthorized)
return
}
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
count, payments, err := ui.cache.getArchivedPayments(first, last, pool.PoolFeesK)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: payments,
})
}
// paginatedPendingPoolPayments is the handler for "GET
// /admin/payments/pending". It uses parameters pageNumber, pageSize and
// accountID to prepare a json payload describing unpaid payments due to the
// pool, as well as the total count of all unpaid payments.
// Returns an error if the current session is not authenticated as an admin.
func (ui *GUI) paginatedPendingPoolPayments(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(sessionKey).(*sessions.Session)
if session.Values["IsAdmin"] != true {
log.Warn("Unauthorized access")
w.WriteHeader(http.StatusUnauthorized)
return
}
first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
count, payments, err := ui.cache.getPendingPayments(first, last, pool.PoolFeesK)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}
sendJSONResponse(w, paginationPayload{
Count: count,
Data: payments,
})
}