-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashboardController.go
314 lines (274 loc) · 9.73 KB
/
dashboardController.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package controllers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/quiknode-labs/marketplace-starter-go/initializers"
"github.com/quiknode-labs/marketplace-starter-go/models"
"github.com/quiknode-labs/marketplace-starter-go/types"
"gorm.io/gorm"
)
func Dashboard(c *gin.Context) {
session := sessions.Default(c)
tokenString := c.Request.URL.Query().Get("jwt")
jwtSecret := os.Getenv("QN_SSO_SECRET")
if session.Get("quicknodeId") != nil {
fmt.Printf("Dashboard: found existing session for user: %s\n", session.Get("userName"))
} else {
// Parse the token string
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
// Check the signing method
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
// Define the secret key used to sign the token
return []byte(jwtSecret), nil
})
// Handle any parsing errors
if err != nil {
fmt.Println("[1] Error parsing token:", err)
c.String(http.StatusUnauthorized, "Unauthorized - Error parsing token: %v", err)
return
}
// Verify the token
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
fmt.Printf("JWT Claims: %v", claims)
quicknodeId := claims["quicknode_id"].(string)
userName := claims["name"].(string)
organizationName := claims["organization_name"].(string)
email := claims["email"].(string)
session.Set("quicknodeId", quicknodeId)
session.Set("email", email)
session.Set("userName", userName)
session.Set("organizationName", organizationName)
session.Save()
fmt.Printf("Token is valid and verified for user: %s\n", userName)
} else {
c.String(http.StatusUnauthorized, "Unauthorized - Failed JWT verification")
return
}
}
// find the account
var account models.Account
findAccountResult := initializers.DB.Where("quicknode_id = ?", session.Get("quicknodeId")).Last(&account)
if findAccountResult.Error != nil {
c.String(http.StatusNotFound, "Could not find account with quicknode-id = : %v", session.Get("quicknodeId"))
return
}
log.Println("Found account with ID =", account.ID)
// Find the RPC requests
// Pagination parameters from the client
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
var count int64
initializers.DB.Table("rpc_requests").Where("account_id = ?", account.ID).Count(&count)
offset := (page - 1) * limit
var rpcRequests []models.RpcRequest
findRpcRequestsResult := initializers.DB.Where("account_id = ?", account.ID).Order("created_at desc").Offset(offset).Limit(limit).Find(&rpcRequests)
if findRpcRequestsResult.Error != nil {
c.String(http.StatusNotFound, "Error fetching RPC requests for account with ID = : %v", account.ID)
return
}
log.Println("Found", len(rpcRequests), "RPC requests")
options := GetChartOptions()
chartOptionsJson, err := json.Marshal(options)
if err != nil {
fmt.Println("Error marshaling chart options to JSON:", err)
return
}
chartData, err := RpcRequestLogsMetricsForChart(initializers.DB, int(account.ID))
log.Print("chartData", chartData)
if err != nil {
fmt.Println("Error fetch data for chart", err)
return
}
chartDataJSON, err := json.Marshal(chartData)
if err != nil {
fmt.Println("Error marshaling chart data to JSON:", err)
return
}
c.HTML(http.StatusOK, "dash.gohtml", gin.H{
"AccountId": c.Param("id"),
"ActiveTab": "dashboard",
"UserName": session.Get("userName"),
"OrganizationName": session.Get("organizationName"),
"Email": session.Get("email"),
"RpcRequests": rpcRequests,
"Paginate": false,
"ChartDataJSON": string(chartDataJSON),
"ChartOptionsJSON": string(chartOptionsJson),
"Count": count,
"Page": page,
"PreviousPage": page - 1,
"NextPage": page + 1,
"Pages": createPageRange(1, int(count/int64(limit))+1),
"Limit": limit,
"FirstPage": count == 0 || page == 1,
"LastPage": count == 0 || page == int(count/int64(limit))+1,
})
}
func RequestsIndex(c *gin.Context) {
session := sessions.Default(c)
if session.Get("quicknodeId") == nil {
c.String(http.StatusUnauthorized, "Unauthorized - No session found")
return
}
// find the account
var account models.Account
findAccountResult := initializers.DB.Where("quicknode_id = ?", session.Get("quicknodeId")).Last(&account)
if findAccountResult.Error != nil {
c.String(http.StatusNotFound, "Could not find account with quicknode-id = : %v", session.Get("quicknodeId"))
return
}
// Find the RPC requests
// Pagination parameters from the client
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
var count int64
initializers.DB.Table("rpc_requests").Where("account_id = ?", account.ID).Count(&count)
offset := (page - 1) * limit
var rpcRequests []models.RpcRequest
findRpcRequestsResult := initializers.DB.Where("account_id = ?", account.ID).Order("created_at desc").Offset(offset).Limit(limit).Find(&rpcRequests)
if findRpcRequestsResult.Error != nil {
c.String(http.StatusNotFound, "Error fetching RPC requests for account with ID = : %v", account.ID)
return
}
log.Println("Found", count, "RPC requests", "page", page, "limit", limit)
c.HTML(http.StatusOK, "requests.gohtml", gin.H{
"AccountId": c.Param("id"),
"ActiveTab": "requests",
"UserName": session.Get("userName"),
"OrganizationName": session.Get("organizationName"),
"Email": session.Get("email"),
"RpcRequests": rpcRequests,
"Paginate": (count > int64(limit)),
"Count": count,
"Page": page,
"PreviousPage": page - 1,
"NextPage": page + 1,
"Pages": createPageRange(1, int(count/int64(limit))+1),
"Limit": limit,
"FirstPage": count == 0 || page == 1,
"LastPage": count == 0 || page == int(count/int64(limit))+1,
})
}
// Helper methods
func createPageRange(firstPage, lastPage int) []int {
var pages []int
for i := firstPage; i <= lastPage; i++ {
pages = append(pages, i)
}
return pages
}
func GetChartOptions() types.ChartOptions {
options := types.ChartOptions{}
options.Plugins.Legend.Display = true
options.Plugins.Legend.Position = "bottom"
options.Plugins.Title.Display = true
options.Plugins.Title.Text = "Requests in the last 14 days"
options.Plugins.Title.Align = "start"
options.Plugins.Title.Weight = "normal"
options.Plugins.Title.Padding.Top = 10
options.Plugins.Title.Padding.Bottom = 30
options.Plugins.Tooltip.Mode = "nearest"
options.Plugins.Tooltip.Intersect = true
options.Scales.X.Stacked = true
options.Scales.X.Ticks.Major.Enabled = true
options.Scales.X.Ticks.Major.FontStyle = "bold"
options.Scales.X.Ticks.Source = "data"
options.Scales.X.Ticks.AutoSkip = true
options.Scales.X.Ticks.MaxRotation = 0
options.Scales.X.Ticks.SampleSize = 100
options.Scales.X.Ticks.BackdropPadding = 10
options.Scales.Y.Stacked = true
options.Layout.Padding = 30
return options
}
// The GetRpcRequestsByMethod is not being used now but it's a good example of how to use GORM to query the database
// so i'm leaving it here. Also, it could be helpful to build an API that returns metrics to the customer in JSON.
func GetRpcRequestsByMethod(db *gorm.DB, accountID int) ([]types.RpcMethodCallsByDay, error) {
var results []types.RpcMethodCallsByDay
query := `
SELECT
method_name,
date_trunc('day', created_at) AS truncated_date,
COUNT(id)
FROM
rpc_requests
WHERE
account_id = ?
AND created_at >= ?
GROUP BY
method_name, truncated_date
ORDER BY
truncated_date, method_name
`
if err := db.Raw(query, accountID, time.Now().AddDate(0, 0, -14)).Scan(&results).Error; err != nil {
return nil, err
}
return results, nil
}
// FILLME - Update colors below based on your method names
var MethodNameColors = map[string]string{
"qn_test": "#0ad193",
"qn_family": "#009fd1",
"qn_gettx": "#924fe7",
}
func RpcRequestLogsMetricsForChart(db *gorm.DB, accountID int) (map[string]interface{}, error) {
var requestLogs []models.RpcRequest
now := time.Now()
sevenDaysAgo := now.AddDate(0, 0, -14)
// Fetch the logs
if err := db.Where("account_id = ? AND created_at >= ?", accountID, sevenDaysAgo).Order("created_at").Find(&requestLogs).Error; err != nil {
return nil, err
}
// Group logs by method name
requestLogsGroupedByMethod := make(map[string][]models.RpcRequest)
for _, log := range requestLogs {
methodName := strings.ToLower(log.MethodName)
requestLogsGroupedByMethod[methodName] = append(requestLogsGroupedByMethod[methodName], log)
}
log.Print("requestLogsGroupedByMethod", requestLogsGroupedByMethod)
// Create labels and datasets
labels := []string{}
for i := 0; i <= 13; i++ {
labels = append(labels, now.AddDate(0, 0, i-13).Format("2006-01-02"))
}
datasets := []map[string]interface{}{}
for methodName, color := range MethodNameColors {
methodName = strings.ToLower(methodName)
log.Print("methodName", methodName)
if logs, exists := requestLogsGroupedByMethod[methodName]; exists {
data := []int{}
for _, label := range labels {
count := 0
for _, log := range logs {
if log.CreatedAt.Format("2006-01-02") == label {
count++
}
}
data = append(data, count)
}
dataset := map[string]interface{}{
"label": methodName,
"data": data,
"backgroundColor": color,
"maxBarThickness": 10,
}
datasets = append(datasets, dataset)
}
}
result := map[string]interface{}{
"labels": labels,
"datasets": datasets,
}
return result, nil
}