From 443fb992699818305e3d7f6885ca7d1d36fbe7cd Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 01 十一月 2023 15:47:57 +0800
Subject: [PATCH] 增加系统运行时间,系统状态,集群状态,集群节点数

---
 utils/cmd.go             |   14 +++
 conf/config.go           |   34 ++++++++
 serf/sync.go             |   34 +++++++-
 model/response/common.go |   17 +++
 docs/swagger.yaml        |   24 ++++++
 api/v1/device.go         |   10 +
 crontask/cron_task.go    |   10 ++
 docs/docs.go             |   35 ++++++++
 docs/swagger.json        |   35 ++++++++
 9 files changed, 202 insertions(+), 11 deletions(-)

diff --git a/api/v1/device.go b/api/v1/device.go
index ee652be..5c08540 100644
--- a/api/v1/device.go
+++ b/api/v1/device.go
@@ -73,9 +73,13 @@
 		return
 	}
 	resp := response.DeviceListResponse{
-		SystemDeviceID:  conf.Conf.System.DeviceId,
-		CurrentDeviceID: conf.Conf.CurrentDeviceID,
-		DeviceIDList:    list,
+		SystemDeviceID:       conf.Conf.System.DeviceId,
+		CurrentDeviceID:      conf.Conf.CurrentDeviceID,
+		DeviceIDList:         list,
+		SystemDeviceStatus:   response.SystemDeviceStatusNormal,
+		ClusterStatus:        conf.Conf.SerfClusterStatus,
+		ClusterNodeQuantity:  conf.Conf.ClusterNodeQuantity,
+		SystemDeviceRunSince: conf.Conf.SystemDeviceRunSince,
 	}
 	ctx.OkWithDetailed(resp)
 }
diff --git a/conf/config.go b/conf/config.go
index ad8f0cc..53632ae 100644
--- a/conf/config.go
+++ b/conf/config.go
@@ -4,8 +4,11 @@
 	"apsClient/pkg/convertx"
 	"apsClient/pkg/logx"
 	"apsClient/pkg/sqlitex"
+	"apsClient/pkg/timex"
+	"apsClient/utils"
 	"github.com/spf13/viper"
 	"log"
+	"time"
 )
 
 var (
@@ -104,7 +107,11 @@
 
 		CurrentDeviceID string //璁剧疆褰撳墠闈㈡澘鎺у埗鐨勮澶�
 
-		SerfClusterStatus string
+		SerfClusterStatus string //闆嗙兢鐘舵��
+
+		ClusterNodeQuantity int //闆嗙兢鑺傜偣鏁伴噺
+
+		SystemDeviceRunSince int64 //绯荤粺寮�濮嬭繍琛屾椂闂存埑
 	}
 )
 
@@ -130,6 +137,19 @@
 	if Conf.PLC.StandbyTime == 0 {
 		Conf.PLC.StandbyTime = 300 //5鍒嗛挓
 	}
+
+	uptimeStr, err := utils.Exec("uptime -s")
+	if err == nil {
+		t, err := timex.StringToTime(uptimeStr)
+		if err == nil {
+			Conf.SystemDeviceRunSince = t.Unix()
+		}
+	} else {
+		Conf.SystemDeviceRunSince = time.Now().Unix()
+	}
+
+	SetUpTime()
+
 	ShowConfig()
 }
 
@@ -150,3 +170,15 @@
 	log.Printf("   services :                  %+v", Conf.Services)
 	log.Println("......................................................")
 }
+
+func SetUpTime() {
+	uptimeStr, err := utils.Exec("uptime -s")
+	if err == nil {
+		t, err := timex.StringToTime(uptimeStr)
+		if err == nil {
+			Conf.SystemDeviceRunSince = t.Unix()
+		}
+	} else {
+		Conf.SystemDeviceRunSince = time.Now().Unix()
+	}
+}
diff --git a/crontask/cron_task.go b/crontask/cron_task.go
index 230d38e..66ddf9b 100644
--- a/crontask/cron_task.go
+++ b/crontask/cron_task.go
@@ -7,6 +7,7 @@
 	"apsClient/nsq"
 	"apsClient/pkg/ecode"
 	"apsClient/pkg/logx"
+	"apsClient/serf"
 	"apsClient/service"
 	"fmt"
 	"github.com/go-co-op/gocron"
@@ -79,6 +80,9 @@
 		s.Every(60).Seconds().Do(SyncProductionProgress) //鍚屾鐢熶骇鏁版嵁
 		s.Every(30).Seconds().Do(SyncTaskStatus)         //鍚屾浠诲姟鐘舵��
 	}
+
+	s.Every(10).Seconds().Do(QueryClusterStatus) //鏌ヨ闆嗙兢鑺傜偣鏁伴噺
+
 	s.StartAsync()
 	return nil
 }
@@ -152,3 +156,9 @@
 		logx.Errorf("send pull data msg error:%v, msg:%+v", err.Error(), msg)
 	}
 }
+
+func QueryClusterStatus() {
+	clusterStatus, nodeQuantity := serf.QueryClusterStatusAndNodeQuantity()
+	conf.Conf.SerfClusterStatus = clusterStatus
+	conf.Conf.ClusterNodeQuantity = nodeQuantity
+}
diff --git a/docs/docs.go b/docs/docs.go
index c4341c0..27a9079 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -1482,6 +1482,14 @@
         "response.DeviceListResponse": {
             "type": "object",
             "properties": {
+                "clusterNodeQuantity": {
+                    "description": "闆嗙兢鑺傜偣鏁伴噺",
+                    "type": "integer"
+                },
+                "clusterStatus": {
+                    "description": "闆嗙兢鐘舵��",
+                    "type": "string"
+                },
                 "currentDeviceID": {
                     "description": "褰撳墠閫夊畾鐨勭敓浜ц澶�",
                     "type": "string"
@@ -1496,6 +1504,18 @@
                 "systemDeviceID": {
                     "description": "宸ユ帶鏈鸿澶嘔D",
                     "type": "string"
+                },
+                "systemDeviceRunSince": {
+                    "description": "绯荤粺杩愯寮�濮嬫椂闂存埑",
+                    "type": "integer"
+                },
+                "systemDeviceStatus": {
+                    "description": "璁惧鐘舵��",
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/response.SystemDeviceStatus"
+                        }
+                    ]
                 }
             }
         },
@@ -1551,6 +1571,21 @@
                 }
             }
         },
+        "response.SystemDeviceStatus": {
+            "type": "integer",
+            "enum": [
+                1,
+                2
+            ],
+            "x-enum-comments": {
+                "SystemDeviceStatusNormal": "姝e父",
+                "SystemDeviceStatusUnNormal": "寮傚父"
+            },
+            "x-enum-varnames": [
+                "SystemDeviceStatusNormal",
+                "SystemDeviceStatusUnNormal"
+            ]
+        },
         "response.TaskCountdown": {
             "type": "object",
             "properties": {
diff --git a/docs/swagger.json b/docs/swagger.json
index ca08c1d..45f97c4 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -1470,6 +1470,14 @@
         "response.DeviceListResponse": {
             "type": "object",
             "properties": {
+                "clusterNodeQuantity": {
+                    "description": "闆嗙兢鑺傜偣鏁伴噺",
+                    "type": "integer"
+                },
+                "clusterStatus": {
+                    "description": "闆嗙兢鐘舵��",
+                    "type": "string"
+                },
                 "currentDeviceID": {
                     "description": "褰撳墠閫夊畾鐨勭敓浜ц澶�",
                     "type": "string"
@@ -1484,6 +1492,18 @@
                 "systemDeviceID": {
                     "description": "宸ユ帶鏈鸿澶嘔D",
                     "type": "string"
+                },
+                "systemDeviceRunSince": {
+                    "description": "绯荤粺杩愯寮�濮嬫椂闂存埑",
+                    "type": "integer"
+                },
+                "systemDeviceStatus": {
+                    "description": "璁惧鐘舵��",
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/response.SystemDeviceStatus"
+                        }
+                    ]
                 }
             }
         },
@@ -1539,6 +1559,21 @@
                 }
             }
         },
+        "response.SystemDeviceStatus": {
+            "type": "integer",
+            "enum": [
+                1,
+                2
+            ],
+            "x-enum-comments": {
+                "SystemDeviceStatusNormal": "姝e父",
+                "SystemDeviceStatusUnNormal": "寮傚父"
+            },
+            "x-enum-varnames": [
+                "SystemDeviceStatusNormal",
+                "SystemDeviceStatusUnNormal"
+            ]
+        },
         "response.TaskCountdown": {
             "type": "object",
             "properties": {
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index a990e3a..5e38b70 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -491,6 +491,12 @@
     type: object
   response.DeviceListResponse:
     properties:
+      clusterNodeQuantity:
+        description: 闆嗙兢鑺傜偣鏁伴噺
+        type: integer
+      clusterStatus:
+        description: 闆嗙兢鐘舵��
+        type: string
       currentDeviceID:
         description: 褰撳墠閫夊畾鐨勭敓浜ц澶�
         type: string
@@ -502,6 +508,13 @@
       systemDeviceID:
         description: 宸ユ帶鏈鸿澶嘔D
         type: string
+      systemDeviceRunSince:
+        description: 绯荤粺杩愯寮�濮嬫椂闂存埑
+        type: integer
+      systemDeviceStatus:
+        allOf:
+        - $ref: '#/definitions/response.SystemDeviceStatus'
+        description: 璁惧鐘舵��
     type: object
   response.ListResponse:
     properties:
@@ -537,6 +550,17 @@
       totalNumber:
         type: integer
     type: object
+  response.SystemDeviceStatus:
+    enum:
+    - 1
+    - 2
+    type: integer
+    x-enum-comments:
+      SystemDeviceStatusNormal: 姝e父
+      SystemDeviceStatusUnNormal: 寮傚父
+    x-enum-varnames:
+    - SystemDeviceStatusNormal
+    - SystemDeviceStatusUnNormal
   response.TaskCountdown:
     properties:
       countDownHour:
diff --git a/model/response/common.go b/model/response/common.go
index 78341ce..9e36958 100644
--- a/model/response/common.go
+++ b/model/response/common.go
@@ -70,8 +70,19 @@
 	Data  interface{}
 }
 
+type SystemDeviceStatus int
+
+const (
+	SystemDeviceStatusNormal   SystemDeviceStatus = 1 //姝e父
+	SystemDeviceStatusUnNormal SystemDeviceStatus = 2 //寮傚父
+)
+
 type DeviceListResponse struct {
-	SystemDeviceID  string   `json:"systemDeviceID,omitempty"`  //宸ユ帶鏈鸿澶嘔D
-	CurrentDeviceID string   `json:"currentDeviceID,omitempty"` //褰撳墠閫夊畾鐨勭敓浜ц澶�
-	DeviceIDList    []string `json:"deviceIDList,omitempty"`    //鐢熶骇璁惧id鍒楄〃
+	SystemDeviceID       string             `json:"systemDeviceID,omitempty"`  //宸ユ帶鏈鸿澶嘔D
+	CurrentDeviceID      string             `json:"currentDeviceID,omitempty"` //褰撳墠閫夊畾鐨勭敓浜ц澶�
+	DeviceIDList         []string           `json:"deviceIDList,omitempty"`    //鐢熶骇璁惧id鍒楄〃
+	SystemDeviceStatus   SystemDeviceStatus `json:"systemDeviceStatus"`        //璁惧鐘舵��
+	ClusterStatus        string             `json:"clusterStatus"`             //闆嗙兢鐘舵��
+	ClusterNodeQuantity  int                `json:"clusterNodeQuantity"`       //闆嗙兢鑺傜偣鏁伴噺
+	SystemDeviceRunSince int64              `json:"systemDeviceRunSince"`      //绯荤粺杩愯寮�濮嬫椂闂存埑
 }
diff --git a/serf/sync.go b/serf/sync.go
index d8ef4c7..16d19ac 100644
--- a/serf/sync.go
+++ b/serf/sync.go
@@ -5,6 +5,7 @@
 	"context"
 	"encoding/json"
 	"fmt"
+	"github.com/mitchellh/mapstructure"
 	"os"
 	"os/signal"
 	"regexp"
@@ -311,7 +312,7 @@
 }
 
 // 鏌ヨ闆嗙兢鐘舵��, 杩斿洖 master, slave, leave
-func (ss *SyncServer) QueryClusterStat() string {
+func (ss *SyncServer) QueryClusterStat() *bhomeclient.Reply {
 	clusterStatTopic := "/data/api-v/cluster/status"
 	req := bhomeclient.Request{
 		Path:   clusterStatTopic,
@@ -322,14 +323,14 @@
 	if err != nil {
 		fmt.Println("RequestTopic error", err.Error())
 
-		return ""
+		return reply
 	}
 
 	ss.ClusterStatus = reply.Msg
 
 	logx.Debugf("褰撳墠闆嗙兢鐘舵��: %s", ss.ClusterStatus)
 
-	return reply.Msg
+	return reply
 }
 
 func (ss *SyncServer) handleDbLoggerPrint() {
@@ -439,7 +440,7 @@
 	return err
 }
 
-func (ss *SyncServer)Print(values ...interface{}) {
+func (ss *SyncServer) Print(values ...interface{}) {
 	var (
 		level = values[0]
 	)
@@ -519,3 +520,28 @@
 
 	return ""
 }
+
+type NodeInfo struct {
+	NodeID     string `json:"node_id,omitempty"`
+	NodeIp     string `json:"node_ip,omitempty"`
+	NodeName   string `json:"node_name,omitempty"`
+	ClusterID  string `json:"cluster_id"`
+	CreateTime string `json:"create_time"`
+	DeviceType string `json:"device_type"`
+	DriftState string `json:"drift_state"`
+	Online     string `json:"online"`
+}
+
+func QueryClusterStatusAndNodeQuantity() (string, int) {
+	reply := agent.QueryClusterStat()
+	if reply == nil {
+		return "", 0
+	}
+	var nodes []NodeInfo
+	err := mapstructure.Decode(reply.Data, &nodes)
+	if err != nil {
+		logx.Errorf("mapstructure.Decode QueryClusterStat data err:%v", err)
+		return reply.Msg, 0
+	}
+	return reply.Msg, len(nodes)
+}
diff --git a/utils/cmd.go b/utils/cmd.go
index b7392b1..cbce3c6 100644
--- a/utils/cmd.go
+++ b/utils/cmd.go
@@ -26,3 +26,17 @@
 	}
 	return nil
 }
+
+func Exec(command string) (outputStr string, err error) {
+	cmd := exec.Command(command)
+
+	// 鎹曡幏鍛戒护鐨勮緭鍑�
+	output, err := cmd.Output()
+	if err != nil {
+		return "", fmt.Errorf("鍛戒护鎵ц澶辫触: %v", err)
+	}
+
+	// 灏嗚緭鍑鸿浆鎹负瀛楃涓�
+	outputStr = string(output)
+	return
+}

--
Gitblit v1.8.0