From 4dde79f6e22d8c030432c60a85aed71073ad9cbc Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Thu, 13 Jul 2023 17:57:03 +0000 Subject: [PATCH] Adding global statistics API call --- restapi/restapi.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/restapi/restapi.go b/restapi/restapi.go index 00917d4..48d5a59 100644 --- a/restapi/restapi.go +++ b/restapi/restapi.go @@ -25,6 +25,12 @@ type ActorStatsJson struct { ActivitiesCount int `json:"activitiescount"` } +type GlobalStatsJson struct { + Timestamp time.Time `json:"timestamp"` + ActivitiesCount int `json:"activitiescount"` + ActorsCount int `json:"actorscount"` +} + var metricsText string func enableCors(w *http.ResponseWriter) { @@ -295,6 +301,35 @@ func getActorStats(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s", stringarray) } +func getGlobalStats(w http.ResponseWriter, r *http.Request) { + enableCors(&w) + + globalstatsjson := &GlobalStatsJson{} + globalstatsjson.Timestamp = time.Now() + + var activitiescount int + selectActivities := pool.QueryRow(context.Background(), "SELECT count(*) FROM activities") + err := selectActivities.Scan(&activitiescount) + if err != nil { + fmt.Println("Error ", err) + return + } + globalstatsjson.ActivitiesCount = activitiescount + + var actorscount int + selectActors := pool.QueryRow(context.Background(), "SELECT count(*) FROM actors") + err = selectActors.Scan(&actorscount) + if err != nil { + fmt.Println("Error ", err) + return + } + globalstatsjson.ActorsCount = actorscount + + bytearray, _ := json.Marshal(globalstatsjson) + stringarray := string(bytearray) + fmt.Fprintf(w, "%s", stringarray) +} + func main() { pool = getDbPool() @@ -311,6 +346,7 @@ func main() { http.HandleFunc("/api/v1/trending", getTrending) http.HandleFunc("/api/v1/instance/stats", getInstanceStats) http.HandleFunc("/api/v1/actor/stats", getActorStats) + http.HandleFunc("/api/v1/global/stats", getGlobalStats) log.Print("Starting HTTP inbox on port http://0.0.0.0:6431") log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil)) }