Stats endpoint

This commit is contained in:
JimZAH 2023-03-20 20:44:31 +00:00
parent d526429a7c
commit 4de688f294

22
main.go
View File

@ -17,6 +17,8 @@ import (
var ( var (
calls []Call calls []Call
mu sync.Mutex mu sync.Mutex
uptime time.Time
stats Stats
users Users users Users
user_name map[string]string user_name map[string]string
user_update time.Time user_update time.Time
@ -33,6 +35,13 @@ type Call struct {
Talkgroup string `json:"talkgroup"` Talkgroup string `json:"talkgroup"`
} }
/* Store API Stats */
type Stats struct {
Hits uint64 `json:"hits"`
Refresh uint64 `json:"refresh"`
Uptime uint64 `json:"uptime"`
}
/* /*
User data from radioid.net database User data from radioid.net database
we can save memory by ignoring unused fields if required we can save memory by ignoring unused fields if required
@ -103,15 +112,26 @@ func req(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
mu.Lock() mu.Lock()
stats.Hits++
json.NewEncoder(w).Encode(calls) json.NewEncoder(w).Encode(calls)
mu.Unlock() mu.Unlock()
} }
func getStats(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
mu.Lock()
stats.Uptime = uint64(time.Since(uptime)) / 100_000_000_0
json.NewEncoder(w).Encode(stats)
mu.Unlock()
}
func serv() { func serv() {
srv := &http.Server{ srv := &http.Server{
Addr: ":8181", Addr: ":8181",
} }
http.HandleFunc("/monitor", req) http.HandleFunc("/monitor", req)
http.HandleFunc("/monitor/stats", getStats)
srv.ListenAndServe() srv.ListenAndServe()
} }
@ -148,6 +168,7 @@ func main() {
callback := make(chan []Call) callback := make(chan []Call)
last_update := time.Now() last_update := time.Now()
uptime = time.Now()
user_update = time.Now() user_update = time.Now()
user_name = make(map[string]string) user_name = make(map[string]string)
/* Serve the API service */ /* Serve the API service */
@ -158,6 +179,7 @@ func main() {
last_update = time.Now() last_update = time.Now()
go scrape(&config, callback) go scrape(&config, callback)
mu.Lock() mu.Lock()
stats.Refresh++
calls = <-callback calls = <-callback
mu.Unlock() mu.Unlock()
} }