/
metrics.go
206 lines (166 loc) · 6.46 KB
/
metrics.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
package api
import (
"fmt"
"net/http"
"strconv"
"github.com/celestiaorg/leaderboard-backend/receiver"
"github.com/celestiaorg/nodelogger/database/models"
"github.com/gorilla/mux"
)
// GetBridgeNodes implements GET /metrics/nodes/bridge
func (a *RESTApiV1) GetBridgeNodes(resp http.ResponseWriter, req *http.Request) {
limitOffset := a.getLimitOffsetFromHttpReq(req)
nodesList, totalRows, err := a.metrics.GetNodesByType(receiver.BridgeNodeType, int(limitOffset.Offset), int(limitOffset.Limit))
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetBridgeNodes`: %v", err))
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return
}
err = sendJSON(resp,
map[string]interface{}{
"pagination": a.getPagination(uint64(totalRows), limitOffset.Page),
"rows": nodesList,
},
)
a.logger.Info(fmt.Sprintf("api call `GetBridgeNodes` %v ", req.URL.Path))
a.logger.Debug(fmt.Sprintf("api call `GetBridgeNodes` limitOffset: %#v totalRows: %v", limitOffset, totalRows))
if err != nil {
a.logger.Error(fmt.Sprintf("sendJSON `GetBridgeNodes`: %v", err))
}
}
// GetFullNodes implements GET /metrics/nodes/full
func (a *RESTApiV1) GetFullNodes(resp http.ResponseWriter, req *http.Request) {
limitOffset := a.getLimitOffsetFromHttpReq(req)
nodesList, totalRows, err := a.metrics.GetNodesByType(receiver.FullNodeType, int(limitOffset.Offset), int(limitOffset.Limit))
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetFullNodes`: %v", err))
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return
}
err = sendJSON(resp,
map[string]interface{}{
"pagination": a.getPagination(uint64(totalRows), limitOffset.Page),
"rows": nodesList,
},
)
a.logger.Info(fmt.Sprintf("api call `GetFullNodes` %v ", req.URL.Path))
a.logger.Debug(fmt.Sprintf("api call `GetFullNodes` limitOffset: %#v totalRows: %v", limitOffset, totalRows))
if err != nil {
a.logger.Error(fmt.Sprintf("sendJSON `GetFullNodes`: %v", err))
}
}
// GetLightNodes implements GET /metrics/nodes/light
func (a *RESTApiV1) GetLightNodes(resp http.ResponseWriter, req *http.Request) {
limitOffset := a.getLimitOffsetFromHttpReq(req)
nodesList, totalRows, err := a.metrics.GetNodesByType(receiver.LightNodeType, int(limitOffset.Offset), int(limitOffset.Limit))
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetLightNodes`: %v", err))
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return
}
err = sendJSON(resp,
map[string]interface{}{
"pagination": a.getPagination(uint64(totalRows), limitOffset.Page),
"rows": nodesList,
},
)
a.logger.Info(fmt.Sprintf("api call `GetLightNodes` %v ", req.URL.Path))
a.logger.Debug(fmt.Sprintf("api call `GetLightNodes` limitOffset: %#v totalRows: %v", limitOffset, totalRows))
if err != nil {
a.logger.Error(fmt.Sprintf("sendJSON `GetLightNodes`: %v", err))
}
}
// GetNodeById implements GET /metrics/nodes/{id}
func (a *RESTApiV1) GetNodeById(resp http.ResponseWriter, req *http.Request) {
id := mux.Vars(req)["id"]
limitOffset := a.getLimitOffsetFromHttpReq(req)
rows, totalRows, err := a.metrics.FindByNodeId(id, int(limitOffset.Offset), int(limitOffset.Limit))
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetNodeById`: %v", err))
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return
}
if totalRows == 0 {
http.Error(resp, fmt.Sprintf("no data found for node id `%s`", id), http.StatusNotFound)
return
}
err = sendJSON(resp,
map[string]interface{}{
"pagination": a.getPagination(uint64(totalRows), limitOffset.Page),
"rows": rows,
},
)
a.logger.Info(fmt.Sprintf("api call `GetNodeById` %v id: %v", req.URL.Path, id))
a.logger.Debug(fmt.Sprintf("api call `GetNodeById` limitOffset: %#v totalRows: %v", limitOffset, totalRows))
if err != nil {
a.logger.Error(fmt.Sprintf("sendJSON `GetNodeById`: %v", err))
}
}
// GetNodeByIdAtNetworkHeight implements GET /metrics/nodes/{id}/height/{height}
// GetNodeByIdAtNetworkHeight implements GET /metrics/nodes/{id}/height/{height}/{height_end} Search into a range of heights
func (a *RESTApiV1) GetNodeByIdAtNetworkHeight(resp http.ResponseWriter, req *http.Request) {
id := mux.Vars(req)["id"]
heightStr := mux.Vars(req)["height"]
height, err := strconv.ParseUint(heightStr, 10, 64)
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetNodeByIdAtNetworkHeight`: %v", err))
http.Error(resp, "malformed height value", http.StatusBadRequest)
return
}
heightEnd := uint64(0)
heightEndStr := mux.Vars(req)["height_end"]
if heightEndStr != "" {
heightEnd, err = strconv.ParseUint(heightEndStr, 10, 64)
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetNodeByIdAtNetworkHeight`: %v", err))
http.Error(resp, "malformed height_end value", http.StatusBadRequest)
return
}
}
var rows []models.CelestiaNode
if heightEnd != 0 {
rows, err = a.metrics.FindByNodeIdAtNetworkHeightRange(id, height, heightEnd)
} else {
rows, err = a.metrics.FindByNodeIdAtNetworkHeight(id, height)
}
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetNodeByIdAtNetworkHeight`: %v", err))
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return
}
// cut the rows if there are too many of them
if len(rows) > 100 {
rows = rows[:100]
}
err = sendJSON(resp,
map[string]interface{}{
"rows": rows,
},
)
a.logger.Info(fmt.Sprintf("api call `GetNodeByIdAtNetworkHeight` %v id: %v", req.URL.Path, id))
a.logger.Debug(fmt.Sprintf("api call `GetNodeByIdAtNetworkHeight` totalRows: %v", len(rows)))
if err != nil {
a.logger.Error(fmt.Sprintf("sendJSON `GetNodeByIdAtNetworkHeight`: %v", err))
}
}
// GetAllNodes implements GET /metrics/nodes
func (a *RESTApiV1) GetAllNodes(resp http.ResponseWriter, req *http.Request) {
limitOffset := a.getLimitOffsetFromHttpReq(req)
nodesList, totalRows, err := a.metrics.GetAllNodes(int(limitOffset.Offset), int(limitOffset.Limit))
if err != nil {
a.logger.Error(fmt.Sprintf("api `GetAllNodes`: %v", err))
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return
}
err = sendJSON(resp,
map[string]interface{}{
"pagination": a.getPagination(uint64(totalRows), limitOffset.Page),
"rows": nodesList,
},
)
a.logger.Info(fmt.Sprintf("api call `GetAllNodes` %v ", req.URL.Path))
a.logger.Debug(fmt.Sprintf("api call `GetAllNodes` limitOffset: %#v totalRows: %v", limitOffset, totalRows))
if err != nil {
a.logger.Error(fmt.Sprintf("sendJSON `GetAllNodes`: %v", err))
}
}