-
Notifications
You must be signed in to change notification settings - Fork 17
/
clients.go
83 lines (74 loc) · 2.52 KB
/
clients.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
package handlers
import (
"net/http"
"time"
"github.com/pk910/dora/services"
"github.com/pk910/dora/templates"
"github.com/pk910/dora/types/models"
"github.com/pk910/dora/utils"
"github.com/sirupsen/logrus"
)
// Clients will return the main "clients" page using a go template
func Clients(w http.ResponseWriter, r *http.Request) {
var clientsTemplateFiles = append(layoutTemplateFiles,
"clients/clients.html",
)
var pageTemplate = templates.GetTemplate(clientsTemplateFiles...)
data := InitPageData(w, r, "clients", "/clients", "Clients", clientsTemplateFiles)
var pageError error
pageError = services.GlobalCallRateLimiter.CheckCallLimit(r, 1)
if pageError == nil {
data.Data, pageError = getClientsPageData()
}
if pageError != nil {
handlePageError(w, r, pageError)
return
}
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "clients.go", "Clients", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func getClientsPageData() (*models.ClientsPageData, error) {
pageData := &models.ClientsPageData{}
pageCacheKey := "clients"
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} {
pageData, cacheTimeout := buildClientsPageData()
pageCall.CacheTimeout = cacheTimeout
return pageData
})
if pageErr == nil && pageRes != nil {
resData, resOk := pageRes.(*models.ClientsPageData)
if !resOk {
return nil, ErrInvalidPageModel
}
pageData = resData
}
return pageData, pageErr
}
func buildClientsPageData() (*models.ClientsPageData, time.Duration) {
logrus.Debugf("clients page called")
pageData := &models.ClientsPageData{
Clients: []*models.ClientsPageDataClient{},
}
cacheTime := time.Duration(utils.Config.Chain.Config.SecondsPerSlot) * time.Second
for _, client := range services.GlobalBeaconService.GetClients() {
lastHeadSlot, lastHeadRoot, clientRefresh := client.GetLastHead()
if lastHeadSlot < 0 {
lastHeadSlot = 0
}
resClient := &models.ClientsPageDataClient{
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Version: client.GetVersion(),
HeadSlot: uint64(lastHeadSlot),
HeadRoot: lastHeadRoot,
Status: client.GetStatus(),
LastRefresh: clientRefresh,
LastError: client.GetLastClientError(),
}
pageData.Clients = append(pageData.Clients, resClient)
}
pageData.ClientCount = uint64(len(pageData.Clients))
return pageData, cacheTime
}