Преглед изворни кода

models/metric: sort for metric result

fatedier пре 8 година
родитељ
комит
dee4cbd48c
2 измењених фајлова са 17 додато и 10 уклоњено
  1. 16 5
      src/frp/models/metric/server.go
  2. 1 5
      src/frp/models/server/dashboard_api.go

+ 16 - 5
src/frp/models/metric/server.go

@@ -16,6 +16,7 @@ package metric
 
 import (
 	"encoding/json"
+	"sort"
 	"sync"
 	"time"
 
@@ -52,22 +53,32 @@ type DailyServerStats struct {
 	TotalAcceptConns int64  `json:"total_accept_conns"`
 }
 
+// for sort
+type ServerMetricList []*ServerMetric
+
+func (l ServerMetricList) Len() int           { return len(l) }
+func (l ServerMetricList) Less(i, j int) bool { return l[i].Name < l[j].Name }
+func (l ServerMetricList) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }
+
 func init() {
 	ServerMetricInfoMap = make(map[string]*ServerMetric)
 }
 
-func GetAllProxyMetrics() map[string]*ServerMetric {
-	result := make(map[string]*ServerMetric)
+func GetAllProxyMetrics() []*ServerMetric {
+	result := make(ServerMetricList, 0)
 	smMutex.RLock()
-	defer smMutex.RUnlock()
-	for proxyName, metric := range ServerMetricInfoMap {
+	for _, metric := range ServerMetricInfoMap {
 		metric.mutex.RLock()
 		byteBuf, _ := json.Marshal(metric)
 		metric.mutex.RUnlock()
 		tmpMetric := &ServerMetric{}
 		json.Unmarshal(byteBuf, &tmpMetric)
-		result[proxyName] = tmpMetric
+		result = append(result, tmpMetric)
 	}
+	smMutex.RUnlock()
+
+	// sort for result by proxy name
+	sort.Sort(result)
 	return result
 }
 

+ 1 - 5
src/frp/models/server/dashboard_api.go

@@ -54,15 +54,11 @@ type ProxiesResponse struct {
 
 func apiProxies(c *gin.Context) {
 	res := &ProxiesResponse{}
-	res.Proxies = make([]*metric.ServerMetric, 0)
 	defer func() {
 		log.Info("Http response [/api/proxies]: code [%d]", res.Code)
 	}()
 
 	log.Info("Http request: [/api/proxies]")
-	serverMetricMap := metric.GetAllProxyMetrics()
-	for _, metric := range serverMetricMap {
-		res.Proxies = append(res.Proxies, metric)
-	}
+	res.Proxies = metric.GetAllProxyMetrics()
 	c.JSON(200, res)
 }