This commit is contained in:
yusing 2025-02-14 22:04:45 +08:00
parent 9cd5237bb8
commit 205726b045
2 changed files with 17 additions and 15 deletions

View file

@ -11,13 +11,12 @@ import (
"github.com/yusing/go-proxy/internal/metrics/period" "github.com/yusing/go-proxy/internal/metrics/period"
metricsutils "github.com/yusing/go-proxy/internal/metrics/utils" metricsutils "github.com/yusing/go-proxy/internal/metrics/utils"
"github.com/yusing/go-proxy/internal/route/routes/routequery" "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" "github.com/yusing/go-proxy/internal/watcher/health"
) )
type ( type (
StatusByAlias struct { StatusByAlias struct {
Map map[string]map[string]any Map map[string]*routequery.HealthInfoRaw
Timestamp time.Time Timestamp time.Time
} }
Status struct { Status struct {
@ -51,8 +50,8 @@ func aggregateStatuses(entries []*StatusByAlias, query url.Values) (int, Aggrega
for _, entry := range entries { for _, entry := range entries {
for alias, status := range entry.Map { for alias, status := range entry.Map {
statuses[alias] = append(statuses[alias], &Status{ statuses[alias] = append(statuses[alias], &Status{
Status: status["status"].(health.Status), Status: status.Status,
Latency: status["latency"].(time.Duration), Latency: status.Latency,
Timestamp: entry.Timestamp, Timestamp: entry.Timestamp,
}) })
} }
@ -129,7 +128,6 @@ func (s *Status) MarshalJSON() ([]byte, error) {
"status": s.Status.String(), "status": s.Status.String(),
"latency": s.Latency.Milliseconds(), "latency": s.Latency.Milliseconds(),
"timestamp": s.Timestamp.Unix(), "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{}{ return json.Marshal(map[string]interface{}{
"statuses": s.Map, "statuses": s.Map,
"timestamp": s.Timestamp.Unix(), "timestamp": s.Timestamp.Unix(),
"time": strutils.FormatTime(s.Timestamp),
}) })
} }

View file

@ -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() mon := r.HealthMonitor()
if mon == nil { if mon == nil {
return map[string]any{ return &HealthInfoRaw{
"status": health.StatusUnknown, Status: health.StatusUnknown,
"latency": time.Duration(0), Latency: time.Duration(0),
} }
} }
return map[string]any{ return &HealthInfoRaw{
"status": mon.Status(), Status: mon.Status(),
"latency": mon.Latency(), Latency: mon.Latency(),
} }
} }
@ -51,8 +56,8 @@ func HealthMap() map[string]map[string]string {
return healthMap return healthMap
} }
func HealthInfo() map[string]map[string]any { func HealthInfo() map[string]*HealthInfoRaw {
healthMap := make(map[string]map[string]any, routes.NumRoutes()) healthMap := make(map[string]*HealthInfoRaw, routes.NumRoutes())
routes.RangeRoutes(func(alias string, r route.Route) { routes.RangeRoutes(func(alias string, r route.Route) {
healthMap[alias] = getHealthInfoRaw(r) healthMap[alias] = getHealthInfoRaw(r)
}) })