-
Notifications
You must be signed in to change notification settings - Fork 16
/
stats.go
124 lines (117 loc) · 4.23 KB
/
stats.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
package nomad
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/hashicorp/nomad/api"
"github.com/robinovitch61/wander/internal/tui/components/page"
"github.com/robinovitch61/wander/internal/tui/formatter"
"github.com/robinovitch61/wander/internal/tui/message"
"github.com/robinovitch61/wander/internal/tui/style"
)
func FetchStats(client api.Client, allocID, allocName string) tea.Cmd {
return func() tea.Msg {
alloc, _, err := client.Allocations().Info(allocID, nil)
if err != nil {
return message.ErrMsg{Err: err}
}
if alloc == nil {
return message.ErrMsg{Err: fmt.Errorf("allocation %s (%s) not found", allocName, allocID)}
}
allocGivenResources := alloc.Resources
if allocGivenResources == nil {
return message.ErrMsg{Err: fmt.Errorf("allocation %s (%s) has no allocGivenResources", allocName, allocID)}
}
allocatedCpuMhz := allocGivenResources.CPU
if allocatedCpuMhz == nil {
return message.ErrMsg{Err: fmt.Errorf("allocation %s (%s) has no CPU resources", allocName, allocID)}
}
allocatedMemoryMB := allocGivenResources.MemoryMB
if allocatedMemoryMB == nil {
return message.ErrMsg{Err: fmt.Errorf("allocation %s (%s) has no memory resources", allocName, allocID)}
}
stats, err := client.Allocations().Stats(alloc, nil)
if err != nil {
return message.ErrMsg{Err: err}
}
var tableRows [][]string
if stats != nil {
allocUsage := stats.ResourceUsage
var allocCpuTableVal, allocMemoryTableVal string
if allocUsage != nil {
allocMemory := allocUsage.MemoryStats
if allocMemory != nil {
memMiB := float64(allocMemory.Usage) / 1024 / 1024
givenMemMiB := *allocatedMemoryMB
perc := memMiB / float64(givenMemMiB) * 100
stylePercent := style.Regular
if perc > 100 {
stylePercent = style.StatBad
}
percStr := stylePercent.Render(fmt.Sprintf("%.1f%%", perc))
allocMemoryTableVal = fmt.Sprintf("%.0f/%d MiB (%s)", memMiB, givenMemMiB, percStr)
}
allocCpu := allocUsage.CpuStats
if allocCpu != nil {
cpuMhz := int(allocCpu.TotalTicks)
givenCpuMhz := *allocatedCpuMhz
perc := float64(cpuMhz) / float64(givenCpuMhz) * 100
stylePercent := style.Regular
if perc > 100 {
stylePercent = style.StatBad
}
percStr := stylePercent.Render(fmt.Sprintf("%.1f%%", perc))
allocCpuTableVal = fmt.Sprintf("%d/%d MHz (%s)", cpuMhz, givenCpuMhz, percStr)
}
tableRows = append(tableRows, []string{
fmt.Sprintf("Allocation %s", allocName), allocMemoryTableVal, allocCpuTableVal,
})
}
for taskName, taskResources := range stats.Tasks {
if taskResources == nil {
continue
}
taskGivenResources := alloc.TaskResources[taskName]
if taskGivenResources == nil {
continue
}
taskUsage := stats.ResourceUsage
var taskCpuTableVal, taskMemoryTableVal string
if taskUsage != nil {
taskMemory := taskUsage.MemoryStats
if taskMemory != nil {
memMiB := float64(taskMemory.Usage) / 1024 / 1024
givenMemMiB := *taskGivenResources.MemoryMB
perc := memMiB / float64(givenMemMiB) * 100
stylePercent := style.Regular
if perc > 100 {
stylePercent = style.StatBad
}
percStr := stylePercent.Render(fmt.Sprintf("%.1f%%", perc))
taskMemoryTableVal = fmt.Sprintf("%.0f/%d MiB (%s)", memMiB, givenMemMiB, percStr)
}
taskCpu := taskUsage.CpuStats
if taskCpu != nil {
cpuMhz := int(taskCpu.TotalTicks)
givenCpuMhz := *taskGivenResources.CPU
perc := float64(cpuMhz) / float64(givenCpuMhz) * 100
stylePercent := style.Regular
if perc > 100 {
stylePercent = style.StatBad
}
percStr := stylePercent.Render(fmt.Sprintf("%.1f%%", perc))
taskCpuTableVal = fmt.Sprintf("%d/%d MHz (%s)", cpuMhz, givenCpuMhz, percStr)
}
tableRows = append(tableRows, []string{
fmt.Sprintf("Task %s", taskName), taskMemoryTableVal, taskCpuTableVal,
})
}
}
}
table := formatter.GetRenderedTableAsString([]string{"Entity", "Memory", "CPU"}, tableRows)
var pageRows []page.Row
for _, row := range table.ContentRows {
pageRows = append(pageRows, page.Row{Key: "", Row: row})
}
return PageLoadedMsg{Page: StatsPage, TableHeader: table.HeaderRows, AllPageRows: pageRows}
}
}