Skip to content

Commit 54b9d6c

Browse files
committed
πŸ“Š Cleanup metric endpoint
1 parent 4055f01 commit 54b9d6c

File tree

4 files changed

+170
-190
lines changed

4 files changed

+170
-190
lines changed

β€Žcmd/info/metrics.goβ€Ž

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package info
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
internalIO "github.com/kloudkit/ws-cli/internals/io"
9+
"github.com/kloudkit/ws-cli/internals/styles"
10+
)
11+
12+
type metrics struct {
13+
cpuUsage float64
14+
cpuSeconds float64
15+
memoryTotal uint64
16+
memoryUsed uint64
17+
memoryRSS uint64
18+
diskTotal uint64
19+
diskUsed uint64
20+
fdOpen uint64
21+
fdLimit uint64
22+
gpu *internalIO.GPUStats
23+
}
24+
25+
func formatCPUTime(seconds float64) string {
26+
if seconds < 60 {
27+
return fmt.Sprintf("%.1fs", seconds)
28+
}
29+
30+
if seconds < 3600 {
31+
return fmt.Sprintf("%.1fm", seconds/60)
32+
}
33+
34+
return fmt.Sprintf("%.1fh", seconds/3600)
35+
}
36+
37+
func getMetrics(includeGPU bool) (*metrics, error) {
38+
cpuUsage, _ := internalIO.GetCPUUsagePercent()
39+
40+
cpuStats, err := internalIO.GetCPUStats()
41+
if err != nil {
42+
return nil, fmt.Errorf("failed to get CPU stats: %w", err)
43+
}
44+
45+
memStats, err := internalIO.GetMemoryStats()
46+
if err != nil {
47+
return nil, fmt.Errorf("failed to get memory stats: %w", err)
48+
}
49+
50+
diskStats, err := internalIO.GetDiskStats()
51+
if err != nil {
52+
return nil, fmt.Errorf("failed to get disk stats: %w", err)
53+
}
54+
55+
fdStats, err := internalIO.GetFileDescriptorStats()
56+
if err != nil {
57+
return nil, fmt.Errorf("failed to get file descriptor stats: %w", err)
58+
}
59+
60+
m := &metrics{
61+
cpuUsage: cpuUsage,
62+
cpuSeconds: cpuStats.UsageSeconds,
63+
memoryTotal: memStats.LimitBytes,
64+
memoryUsed: memStats.UsageBytes,
65+
memoryRSS: memStats.RSSBytes,
66+
diskTotal: diskStats.LimitBytes,
67+
diskUsed: diskStats.UsageBytes,
68+
fdOpen: fdStats.Open,
69+
fdLimit: fdStats.Limit,
70+
}
71+
72+
if includeGPU {
73+
gpuStats, err := internalIO.GetGPUStats()
74+
if err == nil {
75+
m.gpu = gpuStats
76+
}
77+
}
78+
79+
return m, nil
80+
}
81+
82+
var metricsCmd = &cobra.Command{
83+
Use: "metrics",
84+
Short: "Display workspace metrics",
85+
RunE: func(cmd *cobra.Command, args []string) error {
86+
includeGPU, _ := cmd.Flags().GetBool("gpu")
87+
88+
m, err := getMetrics(includeGPU)
89+
if err != nil {
90+
styles.PrintWarning(cmd.OutOrStdout(), "Could not read workspace metrics")
91+
return nil
92+
}
93+
94+
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", styles.Title().Render("Metrics"))
95+
96+
rows := [][]string{
97+
{"CPU", fmt.Sprintf("%.1f%% (%s)", m.cpuUsage, formatCPUTime(m.cpuSeconds))},
98+
{"Memory", fmt.Sprintf("%s / %s (%s)",
99+
styles.FormatBytes(m.memoryUsed),
100+
styles.FormatBytes(m.memoryTotal),
101+
styles.FormatPercent(m.memoryUsed, m.memoryTotal))},
102+
{"Memory RSS", styles.FormatBytes(m.memoryRSS)},
103+
{"Disk", fmt.Sprintf("%s / %s (%s)",
104+
styles.FormatBytes(m.diskUsed),
105+
styles.FormatBytes(m.diskTotal),
106+
styles.FormatPercent(m.diskUsed, m.diskTotal))},
107+
{"File Descriptors", fmt.Sprintf("%d / %d", m.fdOpen, m.fdLimit)},
108+
}
109+
110+
if m.gpu != nil && m.gpu.Available {
111+
rows = append(rows,
112+
[]string{"GPU Utilization", fmt.Sprintf("%.0f%%", m.gpu.UtilizationRatio*100)},
113+
[]string{"GPU Memory", fmt.Sprintf("%s / %s",
114+
styles.FormatBytes(m.gpu.MemoryUsedBytes),
115+
styles.FormatBytes(m.gpu.MemoryTotalBytes))},
116+
[]string{"GPU Temperature", fmt.Sprintf("%.0fC", m.gpu.TemperatureCelsius)},
117+
[]string{"GPU Power", fmt.Sprintf("%.1fW", m.gpu.PowerWatts)},
118+
)
119+
}
120+
121+
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", styles.Table().Rows(rows...).Render())
122+
123+
return nil
124+
},
125+
}
126+
127+
func init() {
128+
metricsCmd.Flags().Bool("gpu", false, "Include GPU metrics")
129+
InfoCmd.AddCommand(metricsCmd)
130+
}

β€Žcmd/info/resources.goβ€Ž

Lines changed: 0 additions & 93 deletions
This file was deleted.

β€Žcmd/info/version.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package info
22

3-
var Version = "0.0.45"
3+
var Version = "0.0.46"

0 commit comments

Comments
Β (0)