From dee4cbd48c54253984fbf5d0a373753a8499d438 Mon Sep 17 00:00:00 2001 From: fatedier Date: Mon, 18 Jul 2016 00:18:40 +0800 Subject: [PATCH] models/metric: sort for metric result --- src/frp/models/metric/server.go | 21 ++++++++++++++++----- src/frp/models/server/dashboard_api.go | 6 +----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/frp/models/metric/server.go b/src/frp/models/metric/server.go index 51d0e522..9f6585df 100644 --- a/src/frp/models/metric/server.go +++ b/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 } diff --git a/src/frp/models/server/dashboard_api.go b/src/frp/models/server/dashboard_api.go index 7a2f70bc..6a07a558 100644 --- a/src/frp/models/server/dashboard_api.go +++ b/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) }