-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
46 lines (40 loc) · 1012 Bytes
/
utils.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
package main
import (
"net/http"
"regexp"
"strings"
"time"
"github.com/guptarohit/asciigraph"
)
func createDevice(id string) DeviceStatsModel {
devicesMap[id] = DeviceStatsModel{
Hostname: id,
Timestamp: time.Now().Unix(),
Graph: make([]float64, 30),
}
return devicesMap[id]
}
func renderTemplate(w http.ResponseWriter, name string, data any) {
err := templates.ExecuteTemplate(w, name, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func updateGraph(diff int64, graph []float64) []float64 {
if diff < 30 {
graph = append(graph, make([]float64, diff)...)
graph = graph[(len(graph) - 30):]
} else {
graph = make([]float64, 30)
}
return graph
}
func plotGraph(graph []float64) string {
var g string
g = asciigraph.Plot(graph, asciigraph.Precision(0), asciigraph.Height(2))
reg := regexp.MustCompile("[0-9]")
g = reg.ReplaceAllString(g, "")
g = strings.ReplaceAll(g, " ┼", "")
g = strings.ReplaceAll(g, " ┤", "")
return g
}