-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatus.go
More file actions
48 lines (37 loc) · 1.12 KB
/
status.go
File metadata and controls
48 lines (37 loc) · 1.12 KB
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
package main
import (
"sync/atomic"
"encoding/json"
"log"
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/xlab/tablewriter"
)
func JsonBackends() []string {
var backendsServerList []string
for _, server := range backends {
jsrv, err := json.Marshal(server)
if err != nil {
log.Print("Error marchal backendsServerList for status ", err.Error())
}
backendsServerList = append(backendsServerList, string(jsrv))
}
return backendsServerList
}
func getStatus(c *gin.Context) {
backendsServerList := JsonBackends()
c.JSON(200, gin.H{
"ServerList": backendsServerList,
"LastBlockAverage": atomic.LoadInt64(&LastBlock.Dig),
})
}
func GetStatusTable() string {
str := fmt.Sprintln("\nAverage LastBlock ", atomic.LoadInt64(&LastBlock.Dig), "\n")
table := tablewriter.CreateTable()
table.AddHeaders("Status", "Hostname", "LastBlock", "Weight", "LastUpdate", "ResponseTime")
for _, server := range backends {
table.AddRow(server.FSM.Current(), server.Hostname, server.LastBlock, server.Weight, time.Unix(server.TimeUpdate, 0).Format(time.RFC3339), server.ResponseTime)
}
return str + table.Render()
}