mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-21 07:51:28 +02:00
refactor: refactor to adapt new custom json marshaler
This commit is contained in:
22
internal/metrics/uptime/status.go
Normal file
22
internal/metrics/uptime/status.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package uptime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Status health.Status
|
||||
Latency int64
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
type RouteStatuses map[string][]*Status
|
||||
|
||||
func (s *Status) MarshalJSONTo(buf []byte) []byte {
|
||||
return fmt.Appendf(buf,
|
||||
`{"status":"%s","latency":"%d","timestamp":"%d"}`,
|
||||
s.Status, s.Latency, s.Timestamp,
|
||||
)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package uptime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"sort"
|
||||
"time"
|
||||
@@ -13,20 +12,15 @@ import (
|
||||
"github.com/yusing/go-proxy/internal/route/routes"
|
||||
"github.com/yusing/go-proxy/internal/route/routes/routequery"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
"github.com/yusing/go-proxy/pkg/json"
|
||||
)
|
||||
|
||||
type (
|
||||
StatusByAlias struct {
|
||||
Map map[string]*routequery.HealthInfoRaw `json:"statuses"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Map json.Map[*routequery.HealthInfoRaw] `json:"statuses"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
Status struct {
|
||||
Status health.Status `json:"status"`
|
||||
Latency int64 `json:"latency"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
RouteStatuses map[string][]*Status
|
||||
Aggregated []map[string]any
|
||||
Aggregated = json.MapSlice[any]
|
||||
)
|
||||
|
||||
var Poller = period.NewPoller("uptime", getStatuses, aggregateStatuses)
|
||||
@@ -124,7 +118,3 @@ func (rs RouteStatuses) aggregate(limit int, offset int) Aggregated {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (result Aggregated) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal([]map[string]any(result))
|
||||
}
|
||||
|
||||
10
internal/metrics/uptime/uptime_test.go
Normal file
10
internal/metrics/uptime/uptime_test.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package uptime
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPoller(t *testing.T) {
|
||||
Poller.Test(t, url.Values{"limit": []string{"1"}})
|
||||
}
|
||||
Reference in New Issue
Block a user