瀏覽代碼

Revert "Remove system, cleanup"

This reverts commit e7695a5aab4aabd7846fb632092e43844f8a209f.
Florian Hoss 2 年之前
父節點
當前提交
e75535cad7
共有 11 個文件被更改,包括 254 次插入3 次删除
  1. 1 1
      server/middlewares.go
  2. 5 1
      server/router.go
  3. 35 0
      server/routes.go
  4. 1 1
      static/js/app.js
  5. 29 0
      system/cpu.go
  6. 35 0
      system/disk.go
  7. 36 0
      system/ram.go
  8. 14 0
      system/storage.go
  9. 38 0
      system/system.go
  10. 46 0
      system/types.go
  11. 14 0
      system/uptime.go

+ 1 - 1
server/middlewares.go

@@ -17,7 +17,7 @@ func (server *Server) setupMiddlewares() {
 	server.Router.Use(middleware.CleanPath)
 	server.Router.Use(middleware.RedirectSlashes)
 	server.Router.Use(middleware.AllowContentEncoding("deflate", "gzip"))
-	server.Router.Use(middleware.Compress(5, "text/html", "text/js", "text/css"))
+	server.Router.Use(middleware.Compress(5, "text/html", "text/css"))
 	server.Router.Use(cors.Handler(cors.Options{
 		AllowedOrigins:   server.AllowedHosts,
 		AllowedMethods:   []string{"GET", "OPTIONS"},

+ 5 - 1
server/router.go

@@ -9,7 +9,11 @@ import (
 func (server *Server) setupRouter() {
 	server.Router.Get("/", launchpad)
 	server.Router.Route("/api", func(r chi.Router) {
-		r.Get("/ws", webSocket)
+		r.Route("/system", func(r chi.Router) {
+			r.Get("/static", routeStaticSystem)
+			r.Get("/live", routeLiveSystem)
+			r.Get("/ws", webSocket)
+		})
 		r.Get("/weather", getWeather)
 	})
 	server.Router.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {

+ 35 - 0
server/routes.go

@@ -7,6 +7,7 @@ import (
 	"godash/files"
 	"godash/hub"
 	"godash/message"
+	"godash/system"
 	"godash/weather"
 	"net/http"
 )
@@ -16,6 +17,7 @@ type launchpadInformation struct {
 	Host      string
 	Bookmarks []bookmark.Bookmark
 	Weather   weather.OpenWeatherApiResponse
+	System    system.LiveInformation
 }
 
 func launchpad(w http.ResponseWriter, r *http.Request) {
@@ -24,6 +26,7 @@ func launchpad(w http.ResponseWriter, r *http.Request) {
 		Title:     "Godash",
 		Bookmarks: bookmark.Bookmarks,
 		Weather:   weather.CurrentOpenWeather,
+		System:    system.Sys.Live,
 	})
 }
 
@@ -43,6 +46,38 @@ func getWeather(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
+// @Schemes
+// @Summary     live system information
+// @Description gets live information of the system
+// @Tags        system
+// @Produce     json
+// @Success     200 {object} system.LiveInformation
+// @Success     204 {object} message.Response
+// @Router      /system/live [get]
+func routeLiveSystem(w http.ResponseWriter, r *http.Request) {
+	if system.Config.LiveSystem {
+		jsonResponse(w, system.Sys.Live, http.StatusOK)
+	} else {
+		jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
+	}
+}
+
+// @Schemes
+// @Summary     static system information
+// @Description gets static information of the system
+// @Tags        system
+// @Produce     json
+// @Success     200 {object} system.StaticInformation
+// @Success     204 {object} message.Response
+// @Router      /system/static [get]
+func routeStaticSystem(w http.ResponseWriter, r *http.Request) {
+	if system.Config.LiveSystem {
+		jsonResponse(w, system.Sys.Static, http.StatusOK)
+	} else {
+		jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
+	}
+}
+
 func webSocket(w http.ResponseWriter, r *http.Request) {
 	conn, err := upgrader.Upgrade(w, r, nil)
 	if err != nil {

+ 1 - 1
static/js/app.js

@@ -1,6 +1,6 @@
 const WsType = { Weather: 0, System: 1 };
 const apiBase = window.location.origin + "/api";
-let socket = new WebSocket(apiBase.replace("http", "ws") + "/ws");
+let socket = new WebSocket(apiBase.replace("http", "ws") + "/system/ws");
 const weatherIcon = document.getElementById("weatherIcon");
 const weatherTemp = document.getElementById("weatherTemp");
 const weatherDescription = document.getElementById("weatherDescription");

+ 29 - 0
system/cpu.go

@@ -0,0 +1,29 @@
+package system
+
+import (
+	"github.com/shirou/gopsutil/v3/cpu"
+	"math"
+	"runtime"
+)
+
+func staticCpu() CPU {
+	var p CPU
+	p.Threads = runtime.NumCPU()
+	p.Architecture = runtime.GOARCH
+	c, err := cpu.Info()
+	if err == nil {
+		p.Name = c[0].ModelName
+	} else {
+		p.Name = "none detected"
+	}
+	return p
+}
+
+func (s *System) liveCpu() {
+	p, err := cpu.Percent(0, false)
+	if err != nil {
+		return
+	}
+	s.Live.CPU.Value = s.Static.CPU.Name
+	s.Live.CPU.Percentage = append(s.Live.CPU.Percentage[1:], math.RoundToEven(p[0]))
+}

+ 35 - 0
system/disk.go

@@ -0,0 +1,35 @@
+package system
+
+import (
+	"fmt"
+	"github.com/dariubs/percent"
+	"github.com/shirou/gopsutil/v3/disk"
+	"math"
+)
+
+func staticDisk() Storage {
+	var s Storage
+	d, err := disk.Usage("/")
+	if err != nil {
+		return s
+	}
+	total := d.Total
+	if total <= 0 {
+		return s
+	}
+	processStorage(&s, total)
+	return s
+}
+
+func (s *System) liveDisk() {
+	d, err := disk.Usage("/")
+	if err != nil {
+		return
+	}
+	usage := d.Used
+	if usage > 0 {
+		niceUsage := float64(usage) / s.Static.Disk.Unit
+		s.Live.Disk.Value = fmt.Sprintf("%.2f", niceUsage)
+		s.Live.Disk.Percentage = math.RoundToEven(percent.PercentOfFloat(niceUsage, s.Static.Disk.Value))
+	}
+}

+ 36 - 0
system/ram.go

@@ -0,0 +1,36 @@
+package system
+
+import (
+	"fmt"
+	"github.com/dariubs/percent"
+	"github.com/shirou/gopsutil/v3/mem"
+	"math"
+)
+
+func staticRam() Storage {
+	var s Storage
+	r, err := mem.VirtualMemory()
+	if err != nil {
+		return s
+	}
+	total := r.Total
+	if total <= 0 {
+		return s
+	}
+	processStorage(&s, total)
+	return s
+}
+
+func (s *System) liveRam() {
+	r, err := mem.VirtualMemory()
+	if err != nil {
+		return
+	}
+	var niceUsage float64 = 0
+	used := r.Used
+	if used > 0 {
+		niceUsage = float64(used) / s.Static.Ram.Unit
+		s.Live.Ram.Value = fmt.Sprintf("%.2f", niceUsage)
+		s.Live.Ram.Percentage = math.RoundToEven(percent.PercentOfFloat(niceUsage, s.Static.Ram.Value))
+	}
+}

+ 14 - 0
system/storage.go

@@ -0,0 +1,14 @@
+package system
+
+import (
+	"fmt"
+	"github.com/jaypipes/ghw/pkg/unitutil"
+)
+
+func processStorage(s *Storage, total uint64) {
+	unit, unitStr := unitutil.AmountString(int64(total))
+	s.Unit = float64(unit)
+	s.Value = float64(total) / s.Unit
+	s.UnitString = unitStr
+	s.Readable = fmt.Sprintf("%.2f %s", s.Value, s.UnitString)
+}

+ 38 - 0
system/system.go

@@ -0,0 +1,38 @@
+package system
+
+import (
+	"github.com/sirupsen/logrus"
+	"godash/config"
+	"godash/hub"
+	"time"
+)
+
+var Config = SystemConfig{}
+var Sys = System{}
+
+func init() {
+	config.ParseViperConfig(&Config, config.AddViperConfig("system"))
+	if Config.LiveSystem {
+		Sys.Initialize()
+	}
+}
+
+func (s *System) UpdateLiveInformation() {
+	for {
+		s.liveCpu()
+		s.liveRam()
+		s.liveDisk()
+		s.uptime()
+		hub.LiveInformationCh <- hub.Message{WsType: hub.System, Message: s.Live}
+		time.Sleep(1 * time.Second)
+	}
+}
+
+func (s *System) Initialize() {
+	s.Static.CPU = staticCpu()
+	s.Static.Ram = staticRam()
+	s.Static.Disk = staticDisk()
+	s.Live.CPU.Percentage = make([]float64, 120)
+	go s.UpdateLiveInformation()
+	logrus.WithFields(logrus.Fields{"cpu": s.Static.CPU.Name, "arch": s.Static.CPU.Architecture}).Debug("system updated")
+}

+ 46 - 0
system/types.go

@@ -0,0 +1,46 @@
+package system
+
+type SystemConfig struct {
+	LiveSystem bool `mapstructure:"LIVE_SYSTEM"`
+}
+
+type BasicSystemInformation struct {
+	Value      string  `json:"value" validate:"required"`
+	Percentage float64 `json:"percentage" validate:"required"`
+}
+
+type LiveInformation struct {
+	CPU          CpuSystemInformation   `json:"cpu" validate:"required"`
+	Ram          BasicSystemInformation `json:"ram" validate:"required"`
+	Disk         BasicSystemInformation `json:"disk" validate:"required"`
+	ServerUptime uint64                 `json:"server_uptime" validate:"required"`
+}
+
+type StaticInformation struct {
+	CPU  CPU     `json:"cpu" validate:"required"`
+	Ram  Storage `json:"ram" validate:"required"`
+	Disk Storage `json:"disk" validate:"required"`
+}
+
+type System struct {
+	Live   LiveInformation   `json:"live" validate:"required"`
+	Static StaticInformation `json:"static" validate:"required"`
+}
+
+type Storage struct {
+	Readable   string  `json:"readable" validate:"required"`
+	Value      float64 `json:"value" validate:"required"`
+	UnitString string  `json:"unit_string" validate:"required"`
+	Unit       float64 `json:"unit" validate:"required"`
+}
+
+type CPU struct {
+	Name         string `json:"name" validate:"required"`
+	Threads      int    `json:"threads" validate:"required"`
+	Architecture string `json:"architecture" validate:"required"`
+}
+
+type CpuSystemInformation struct {
+	Value      string    `json:"value" validate:"required"`
+	Percentage []float64 `json:"percentage" validate:"required"`
+}

+ 14 - 0
system/uptime.go

@@ -0,0 +1,14 @@
+package system
+
+import (
+	"github.com/shirou/gopsutil/v3/host"
+)
+
+func (s *System) uptime() {
+	i, err := host.Info()
+	if err != nil {
+		return
+	}
+	// returns uptime in milliseconds
+	s.Live.ServerUptime = i.Uptime * 1000
+}