niujiuru 1 settimana fa
parent
commit
3129a6064a

+ 23 - 0
web-ui.service/page1_handler.go

@@ -11,6 +11,7 @@ import (
 	"strconv"
 	"strings"
 	"sync"
+	"time"
 )
 
 var lastCPU struct {
@@ -251,16 +252,38 @@ func systemLoadAverageHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	boot, uptime := noValue, noValue
+	if b, err := os.ReadFile("/proc/uptime"); err == nil {
+		if fields := strings.Fields(string(b)); len(fields) > 0 {
+			if sec, err := strconv.ParseFloat(fields[0], 64); err == nil {
+				d := time.Duration(sec) * time.Second
+				boot = time.Now().Add(-d).Format("2006-01-02 15:04:05")
+				day := int(d.Hours() / 24)
+				if day > 0 {
+					uptime = fmt.Sprintf("%d天 %02d:%02d:%02d",
+						int(d.Hours()/24), int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
+				} else {
+					uptime = fmt.Sprintf("%02d:%02d:%02d",
+						int(d.Hours())%24, int(d.Minutes())%60, int(d.Seconds())%60)
+				}
+			}
+		}
+	}
+
 	resp := struct {
 		Load1m   string `json:"load1m"`
 		Load5m   string `json:"load5m"`
 		Load15m  string `json:"load15m"`
 		RunQueue string `json:"runQueue"`
+		BootTime string `json:"bootTime"`
+		UpTime   string `json:"uptime"`
 	}{
 		Load1m:   formatLoad(fields[0]),
 		Load5m:   formatLoad(fields[1]),
 		Load15m:  formatLoad(fields[2]),
 		RunQueue: fields[3],
+		BootTime: boot,
+		UpTime:   uptime,
 	}
 
 	w.Header().Set("Content-Type", "application/json")

+ 11 - 1
web-ui.service/web/pages/page1.html

@@ -29,7 +29,7 @@
   </div>
   <div class="page1-grid-2">
     <div class="page1-card">
-      <div class="page1-header">📈 负载</div>
+      <div class="page1-header">📈 统计</div>
       <div class="info-list">
         <div class="info-item">
           <span class="info-name">系统01分钟负载</span>
@@ -50,6 +50,16 @@
           <span class="info-name">系统待运行任务</span>
           <span class="info-value" id="runQueue">--</span>
         </div>
+
+        <div class="info-item">
+          <span class="info-name">本次开机的时间</span>
+          <span class="info-value" id="bootTime">--</span>
+        </div>
+
+        <div class="info-item">
+          <span class="info-name">持续开机的时长</span>
+          <span class="info-value" id="uptime">--</span>
+        </div>
       </div>
     </div>
 

+ 1 - 1
web-ui.service/web/static/css/page1.css

@@ -71,7 +71,7 @@
 }
 
 .info-value {
-    width:100px;
+    width:160px;
     margin-left:8px;
     text-align:center;
     color:#94a3b8;

+ 4 - 0
web-ui.service/web/static/js/page1.js

@@ -117,6 +117,8 @@ function getLoadInfo(){
       document.getElementById("load5m").textContent = data.load5m || "--";
       document.getElementById("load15m").textContent = data.load15m || "--";
       document.getElementById("runQueue").textContent = data.runQueue || "--";
+      document.getElementById("bootTime").textContent = data.bootTime || "--";
+      document.getElementById("uptime").textContent = data.uptime || "--";
     })
     .catch(e => {
       if(!page1Active){
@@ -128,6 +130,8 @@ function getLoadInfo(){
       document.getElementById("load5m").textContent = "--";
       document.getElementById("load15m").textContent = "--";
       document.getElementById("runQueue").textContent = "--";
+      document.getElementById("bootTime").textContent = "--";
+      document.getElementById("uptime").textContent = "--";
     });
 }