fix: uptime metrics

This commit is contained in:
yusing
2025-04-25 11:26:24 +08:00
parent af8d2c74f6
commit 3947152336
3 changed files with 47 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package routes
import (
"encoding/json"
"time"
"github.com/yusing/go-proxy/internal/homepage"
@@ -29,6 +30,27 @@ type HealthInfoRaw struct {
Latency time.Duration `json:"latency"`
}
func (info *HealthInfoRaw) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"status": info.Status.String(),
"latency": info.Latency.Milliseconds(),
})
}
func (info *HealthInfoRaw) UnmarshalJSON(data []byte) error {
var v map[string]any
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if status, ok := v["status"].(string); ok {
info.Status = health.NewStatus(status)
}
if latency, ok := v["latency"].(float64); ok {
info.Latency = time.Duration(latency)
}
return nil
}
func getHealthInfoRaw(r Route) *HealthInfoRaw {
mon := r.HealthMonitor()
if mon == nil {