From 205726b04539f665188ea5de53c49845e477e491 Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 14 Feb 2025 22:04:45 +0800 Subject: [PATCH] refactor --- internal/metrics/uptime/uptime.go | 9 +++------ internal/route/routes/routequery/query.go | 23 ++++++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/internal/metrics/uptime/uptime.go b/internal/metrics/uptime/uptime.go index cf5c083..cf8e807 100644 --- a/internal/metrics/uptime/uptime.go +++ b/internal/metrics/uptime/uptime.go @@ -11,13 +11,12 @@ import ( "github.com/yusing/go-proxy/internal/metrics/period" metricsutils "github.com/yusing/go-proxy/internal/metrics/utils" "github.com/yusing/go-proxy/internal/route/routes/routequery" - "github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/watcher/health" ) type ( StatusByAlias struct { - Map map[string]map[string]any + Map map[string]*routequery.HealthInfoRaw Timestamp time.Time } Status struct { @@ -51,8 +50,8 @@ func aggregateStatuses(entries []*StatusByAlias, query url.Values) (int, Aggrega for _, entry := range entries { for alias, status := range entry.Map { statuses[alias] = append(statuses[alias], &Status{ - Status: status["status"].(health.Status), - Latency: status["latency"].(time.Duration), + Status: status.Status, + Latency: status.Latency, Timestamp: entry.Timestamp, }) } @@ -129,7 +128,6 @@ func (s *Status) MarshalJSON() ([]byte, error) { "status": s.Status.String(), "latency": s.Latency.Milliseconds(), "timestamp": s.Timestamp.Unix(), - "time": strutils.FormatTime(s.Timestamp), }) } @@ -137,6 +135,5 @@ func (s *StatusByAlias) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]interface{}{ "statuses": s.Map, "timestamp": s.Timestamp.Unix(), - "time": strutils.FormatTime(s.Timestamp), }) } diff --git a/internal/route/routes/routequery/query.go b/internal/route/routes/routequery/query.go index ff4114c..85066f7 100644 --- a/internal/route/routes/routequery/query.go +++ b/internal/route/routes/routequery/query.go @@ -29,17 +29,22 @@ func getHealthInfo(r route.Route) map[string]string { } } -func getHealthInfoRaw(r route.Route) map[string]any { +type HealthInfoRaw struct { + Status health.Status + Latency time.Duration +} + +func getHealthInfoRaw(r route.Route) *HealthInfoRaw { mon := r.HealthMonitor() if mon == nil { - return map[string]any{ - "status": health.StatusUnknown, - "latency": time.Duration(0), + return &HealthInfoRaw{ + Status: health.StatusUnknown, + Latency: time.Duration(0), } } - return map[string]any{ - "status": mon.Status(), - "latency": mon.Latency(), + return &HealthInfoRaw{ + Status: mon.Status(), + Latency: mon.Latency(), } } @@ -51,8 +56,8 @@ func HealthMap() map[string]map[string]string { return healthMap } -func HealthInfo() map[string]map[string]any { - healthMap := make(map[string]map[string]any, routes.NumRoutes()) +func HealthInfo() map[string]*HealthInfoRaw { + healthMap := make(map[string]*HealthInfoRaw, routes.NumRoutes()) routes.RangeRoutes(func(alias string, r route.Route) { healthMap[alias] = getHealthInfoRaw(r) })