godash/internal/system/system.go

40 lines
830 B
Go
Raw Normal View History

2023-06-14 21:53:27 +02:00
package system
import (
"encoding/json"
"time"
"github.com/r3labs/sse/v2"
"go.uber.org/zap"
)
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-06-21 17:19:21 +02:00
zap.L().Debug("system updated", zap.String("cpu", c.System.Static.CPU.Name), zap.String("arch", c.System.Static.Host.Architecture))
2023-06-14 21:53:27 +02:00
}