mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 00:38:33 +02:00
api: enrich provider statistifcs
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
R "github.com/yusing/go-proxy/internal/route"
|
||||
"github.com/yusing/go-proxy/internal/route/provider/types"
|
||||
route "github.com/yusing/go-proxy/internal/route/types"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
W "github.com/yusing/go-proxy/internal/watcher"
|
||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||
@@ -33,11 +32,6 @@ type (
|
||||
NewWatcher() W.Watcher
|
||||
Logger() *zerolog.Logger
|
||||
}
|
||||
ProviderStats struct {
|
||||
NumRPs int `json:"num_reverse_proxies"`
|
||||
NumStreams int `json:"num_streams"`
|
||||
Type types.ProviderType `json:"type"`
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -154,21 +148,3 @@ func (p *Provider) LoadRoutes() E.Error {
|
||||
func (p *Provider) NumRoutes() int {
|
||||
return p.routes.Size()
|
||||
}
|
||||
|
||||
func (p *Provider) Statistics() ProviderStats {
|
||||
numRPs := 0
|
||||
numStreams := 0
|
||||
p.routes.RangeAll(func(_ string, r *R.Route) {
|
||||
switch r.Type {
|
||||
case route.RouteTypeReverseProxy:
|
||||
numRPs++
|
||||
case route.RouteTypeStream:
|
||||
numStreams++
|
||||
}
|
||||
})
|
||||
return ProviderStats{
|
||||
NumRPs: numRPs,
|
||||
NumStreams: numStreams,
|
||||
Type: p.t,
|
||||
}
|
||||
}
|
||||
|
||||
68
internal/route/provider/stats.go
Normal file
68
internal/route/provider/stats.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
R "github.com/yusing/go-proxy/internal/route"
|
||||
"github.com/yusing/go-proxy/internal/route/provider/types"
|
||||
route "github.com/yusing/go-proxy/internal/route/types"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
)
|
||||
|
||||
type (
|
||||
RouteStats struct {
|
||||
Total uint16 `json:"total"`
|
||||
NumHealthy uint16 `json:"healthy"`
|
||||
NumUnhealthy uint16 `json:"unhealthy"`
|
||||
NumNapping uint16 `json:"napping"`
|
||||
NumError uint16 `json:"error"`
|
||||
NumUnknown uint16 `json:"unknown"`
|
||||
}
|
||||
ProviderStats struct {
|
||||
Total uint16 `json:"total"`
|
||||
RPs RouteStats `json:"reverse_proxies"`
|
||||
Streams RouteStats `json:"streams"`
|
||||
Type types.ProviderType `json:"type"`
|
||||
}
|
||||
)
|
||||
|
||||
func (stats *RouteStats) Add(r *R.Route) {
|
||||
stats.Total++
|
||||
switch r.Health() {
|
||||
case health.StatusHealthy:
|
||||
stats.NumHealthy++
|
||||
case health.StatusUnhealthy:
|
||||
stats.NumUnhealthy++
|
||||
case health.StatusNapping:
|
||||
stats.NumNapping++
|
||||
case health.StatusError:
|
||||
stats.NumError++
|
||||
default:
|
||||
stats.NumUnknown++
|
||||
}
|
||||
}
|
||||
|
||||
func (stats *RouteStats) AddOther(other RouteStats) {
|
||||
stats.Total += other.Total
|
||||
stats.NumHealthy += other.NumHealthy
|
||||
stats.NumUnhealthy += other.NumUnhealthy
|
||||
stats.NumNapping += other.NumNapping
|
||||
stats.NumError += other.NumError
|
||||
stats.NumUnknown += other.NumUnknown
|
||||
}
|
||||
|
||||
func (p *Provider) Statistics() ProviderStats {
|
||||
var rps, streams RouteStats
|
||||
p.routes.RangeAll(func(_ string, r *R.Route) {
|
||||
switch r.Type {
|
||||
case route.RouteTypeReverseProxy:
|
||||
rps.Add(r)
|
||||
case route.RouteTypeStream:
|
||||
streams.Add(r)
|
||||
}
|
||||
})
|
||||
return ProviderStats{
|
||||
Total: rps.Total + streams.Total,
|
||||
RPs: rps,
|
||||
Streams: streams,
|
||||
Type: p.t,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user