more abstraction and simple dashboard for processes
All checks were successful
Build Process Supervisor / build (amd64, .exe, windows) (push) Successful in 2m24s
Build Process Supervisor / build (amd64, , linux) (push) Successful in 2m36s
Build Process Supervisor / build (arm, 6, , linux) (push) Successful in 2m19s
Build Process Supervisor / build (arm64, , linux) (push) Successful in 2m14s
All checks were successful
Build Process Supervisor / build (amd64, .exe, windows) (push) Successful in 2m24s
Build Process Supervisor / build (amd64, , linux) (push) Successful in 2m36s
Build Process Supervisor / build (arm, 6, , linux) (push) Successful in 2m19s
Build Process Supervisor / build (arm64, , linux) (push) Successful in 2m14s
This commit is contained in:
107
htop/htopHandler.go
Normal file
107
htop/htopHandler.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package htop
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//go:embed templates
|
||||
var templatesFS embed.FS
|
||||
|
||||
type HTopHandler struct {
|
||||
Table *HtopTable
|
||||
}
|
||||
|
||||
func NewHTopHandler() (*HTopHandler, error) {
|
||||
table, err := NewTable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &HTopHandler{
|
||||
Table: table,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *HTopHandler) UpdateHTop(w http.ResponseWriter, r *http.Request) {
|
||||
err := h.Table.UpdateTable()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get processes", 500)
|
||||
return
|
||||
}
|
||||
h.Table.Sort(r)
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"div": divide,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
// Detect HTMX request via the HX-Request header
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
tmpl := template.Must(
|
||||
template.New("table.html").Funcs(funcMap).ParseFS(
|
||||
templatesFS,
|
||||
"templates/table.html",
|
||||
),
|
||||
)
|
||||
tmpl.Execute(w, h.Table)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(
|
||||
template.New("htop.html").Funcs(funcMap).ParseFS(
|
||||
templatesFS,
|
||||
"templates/htop.html",
|
||||
"templates/table.html",
|
||||
),
|
||||
)
|
||||
tmpl.Execute(w, h.Table)
|
||||
}
|
||||
|
||||
func divide(a any, b any) float64 {
|
||||
af := toFloat64(a)
|
||||
bf := toFloat64(b)
|
||||
if bf == 0 {
|
||||
return 0
|
||||
}
|
||||
return af / bf
|
||||
}
|
||||
|
||||
func toFloat64(v any) float64 {
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return float64(val)
|
||||
case int32:
|
||||
return float64(val)
|
||||
case int64:
|
||||
return float64(val)
|
||||
case uint64:
|
||||
return float64(val)
|
||||
case float32:
|
||||
return float64(val)
|
||||
case float64:
|
||||
return val
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// GET /taskmanager/usage
|
||||
func UsageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
cpu, mem, err := GetSystemUsage()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get usage", 500)
|
||||
return
|
||||
}
|
||||
|
||||
usage := struct {
|
||||
CPU float64 `json:"cpu"`
|
||||
Mem float64 `json:"mem"`
|
||||
}{
|
||||
CPU: cpu,
|
||||
Mem: mem,
|
||||
}
|
||||
json.NewEncoder(w).Encode(usage)
|
||||
}
|
Reference in New Issue
Block a user