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"
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),
})
}

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()
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)
})