godash/internal/system/system.go

40 lines
796 B
Go
Raw Normal View History

2023-06-14 21:53:27 +02:00
package system
import (
"encoding/json"
2023-10-26 11:42:14 +02:00
"log/slog"
2023-06-14 21:53:27 +02:00
"time"
"github.com/r3labs/sse/v2"
)
2023-06-21 17:19:21 +02:00
func NewSystemService(enabled bool, sse *sse.Server) *System {
2023-06-14 21:53:27 +02:00
var s Config
if enabled {
2023-06-21 17:19:21 +02:00
s = Config{sse: sse}
2023-06-14 21:53:27 +02:00
s.Initialize()
}
return &s.System
}
func (c *Config) UpdateLiveInformation() {
for {
c.liveCpu()
c.liveRam()
c.liveDisk()
c.uptime()
json, _ := json.Marshal(c.System.Live)
c.sse.Publish("system", &sse.Event{Data: json})
time.Sleep(1 * time.Second)
}
}
func (c *Config) Initialize() {
c.System.Static.Host = staticHost()
c.System.Static.CPU = staticCpu()
c.System.Static.Ram = staticRam()
c.System.Static.Disk = staticDisk()
go c.UpdateLiveInformation()
2023-10-26 11:42:14 +02:00
slog.Debug("system updated", "cpu", c.System.Static.CPU.Name, "arch", c.System.Static.Host.Architecture)
2023-06-14 21:53:27 +02:00
}