CLI Loading server stats and traces

This commit is contained in:
2024-10-01 23:33:18 +08:00
parent 632d37caf5
commit f66f144f2e
9 changed files with 208 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/pelletier/go-toml/v2"
)
@ -19,6 +20,7 @@ func ReadInConfig(root string) error {
Traffic: make(map[string]int64),
TrafficFrom: make(map[string]int64),
TotalTraffic: 0,
StartupAt: time.Now(),
},
}

View File

@ -1,6 +1,9 @@
package navi
import "github.com/spf13/viper"
import (
"github.com/spf13/viper"
"time"
)
type RoadMetrics struct {
Traces []RoadTrace `json:"-"`
@ -8,9 +11,11 @@ type RoadMetrics struct {
Traffic map[string]int64 `json:"traffic"`
TrafficFrom map[string]int64 `json:"traffic_from"`
TotalTraffic int64 `json:"total_traffic"`
StartupAt time.Time `json:"startup_at"`
}
type RoadTrace struct {
Timestamp time.Time `json:"timestamp"`
Region string `json:"region"`
Location string `json:"location"`
Destination string `json:"destination"`
@ -27,6 +32,7 @@ type RoadTraceError struct {
func (v *RoadMetrics) AddTrace(trace RoadTrace) {
v.TotalTraffic++
trace.Timestamp = time.Now()
if _, ok := v.Traffic[trace.Region]; !ok {
v.Traffic[trace.Region] = 0
} else {

View File

@ -5,6 +5,7 @@ import (
"git.solsynth.dev/goatworks/roadsign/pkg/warden"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"time"
)
func getStats(c *fiber.Ctx) error {
@ -23,5 +24,10 @@ func getStats(c *fiber.Ctx) error {
"locations": len(locations),
"destinations": len(destinations),
"applications": len(applications),
"uptime": time.Since(navi.R.Metrics.StartupAt).Milliseconds(),
"traffic": fiber.Map{
"total": navi.R.Metrics.TotalTraffic,
"unique_client": len(navi.R.Metrics.TrafficFrom),
},
})
}