@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
homelabv1 "pwa-homelab-mon/gen/homelab/v1"
|
||||
)
|
||||
|
||||
// --- mock mode (MOCK=1): fixture cards for frontend dev on machines without systemd/docker ---
|
||||
|
||||
func mockServices() []*homelabv1.Service {
|
||||
return []*homelabv1.Service{
|
||||
{Kind: homelabv1.ServiceKind_SYSTEMD, Name: "hub-api", State: "active", Substate: "running", LoadState: "loaded", UnitFileState: "enabled"},
|
||||
{Kind: homelabv1.ServiceKind_SYSTEMD, Name: "rathole", State: "active", Substate: "running", LoadState: "loaded", UnitFileState: "enabled"},
|
||||
{Kind: homelabv1.ServiceKind_SYSTEMD, Name: "hermes-dashboard", State: "inactive", Substate: "dead", LoadState: "loaded", UnitFileState: "disabled"},
|
||||
{Kind: homelabv1.ServiceKind_SYSTEMD, Name: "olivetin", State: "failed", Substate: "exit-code", LoadState: "loaded", UnitFileState: "enabled"},
|
||||
{Kind: homelabv1.ServiceKind_SYSTEMD, Name: "ntfy", LoadState: "not-found"},
|
||||
{Kind: homelabv1.ServiceKind_DOCKER, Name: "affine", State: "active", Substate: "3/3",
|
||||
Containers: []string{"affine-self-hosted-1", "affine-migration-1", "affine-redis-1"}},
|
||||
{Kind: homelabv1.ServiceKind_DOCKER, Name: "itsaplan", State: "partial", Substate: "1/2",
|
||||
Containers: []string{"itsaplan-web-1", "itsaplan-worker-1"}},
|
||||
{Kind: homelabv1.ServiceKind_DOCKER, Name: "gitea", State: "inactive", Substate: "0/1", Containers: []string{"gitea"}},
|
||||
}
|
||||
}
|
||||
|
||||
// mockAct flips fixture state so the UI reacts to the buttons in mock mode.
|
||||
func (s *server) mockAct(name string, kind homelabv1.ServiceKind, op string) string {
|
||||
for _, svc := range s.mockSvcs {
|
||||
if svc.Name != name || svc.Kind != kind {
|
||||
continue
|
||||
}
|
||||
switch op {
|
||||
case "start", "restart":
|
||||
svc.State, svc.Substate = "active", "running"
|
||||
if kind == homelabv1.ServiceKind_DOCKER {
|
||||
n := len(svc.Containers)
|
||||
svc.Substate = fmt.Sprintf("%d/%d", n, n)
|
||||
}
|
||||
case "stop":
|
||||
svc.State, svc.Substate = "inactive", "dead"
|
||||
if kind == homelabv1.ServiceKind_DOCKER {
|
||||
svc.Substate = fmt.Sprintf("0/%d", len(svc.Containers))
|
||||
}
|
||||
}
|
||||
return "mock: " + op + " " + name
|
||||
}
|
||||
return "mock: no card " + name
|
||||
}
|
||||
|
||||
func (s *server) mockInfo(name string, kind homelabv1.ServiceKind) string {
|
||||
if kind == homelabv1.ServiceKind_DOCKER {
|
||||
return fmt.Sprintf("NAMES STATUS\n%s-1 Up 2 days\n%s-2 Exited (0) 5 hours ago", name, name)
|
||||
}
|
||||
return fmt.Sprintf("● %s.service - hub API\n Loaded: loaded (~/srv/%s.service; enabled)\n Active: %s (%s) since Mon 2026-08-31 10:00:00 MSK; 2 days ago\n Main PID: 4242 (hub-api)\n Memory: 12.3M",
|
||||
name, name, mockField(s.mockSvcs, name, "state"), mockField(s.mockSvcs, name, "substate"))
|
||||
}
|
||||
|
||||
// mockField reads a fixture field by name for the info template ("" falls back inside mockField callers).
|
||||
func mockField(svcs []*homelabv1.Service, name, field string) string {
|
||||
for _, svc := range svcs {
|
||||
if svc.Name != name {
|
||||
continue
|
||||
}
|
||||
switch field {
|
||||
case "state":
|
||||
if svc.State != "" {
|
||||
return svc.State
|
||||
}
|
||||
case "substate":
|
||||
if svc.Substate != "" {
|
||||
return svc.Substate
|
||||
}
|
||||
}
|
||||
}
|
||||
if field == "state" {
|
||||
return "active"
|
||||
}
|
||||
return "running"
|
||||
}
|
||||
Reference in New Issue
Block a user