From 0b38f818d3a4aa4464c8a21689f29dff25940b90 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Thu, 13 Jul 2023 16:29:18 +0000 Subject: [PATCH] Adding instance statistics API call --- restapi/restapi.go | 55 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/restapi/restapi.go b/restapi/restapi.go index 6a9f126..30f24c7 100644 --- a/restapi/restapi.go +++ b/restapi/restapi.go @@ -12,6 +12,12 @@ import ( "github.com/jackc/pgx/v4" ) +type InstanceStatsJson struct { + Timestamp time.Time `json:"timestamp"` + ActivitiesCount int `json:"activitiescount"` + ActorsCount int `json:"actorscount"` +} + var metricsText string func enableCors(w *http.ResponseWriter) { @@ -48,7 +54,7 @@ GROUP BY hashtags ORDER BY count DESC LIMIT 20;` panic(err) } - hashtagitems := make([]interface{}, 0); + hashtagitems := make([]interface{}, 0) hashcount := 0 for rows.Next() { var hashtag string @@ -67,7 +73,7 @@ GROUP BY hashtags ORDER BY count DESC LIMIT 20;` } rows.Close() - hashtagtotal := make(map[string]interface{}); + hashtagtotal := make(map[string]interface{}) hashtagtotal["count"] = hashcount hashtagtotal["items"] = hashtagitems @@ -102,7 +108,7 @@ ORDER BY 2 DESC LIMIT 20;` panic(err) } - trendingitems := make([]interface{}, 0); + trendingitems := make([]interface{}, 0) trendingcount := 0 for rows.Next() { var trendingword string @@ -121,7 +127,7 @@ ORDER BY 2 DESC LIMIT 20;` } rows.Close() - trendingwordtotal := make(map[string]interface{}); + trendingwordtotal := make(map[string]interface{}) trendingwordtotal["count"] = trendingcount trendingwordtotal["items"] = trendingitems @@ -213,6 +219,46 @@ func getSearch(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s", data) } +func getInstanceStats(w http.ResponseWriter, r *http.Request) { + enableCors(&w) + instanceKeys, exists := r.URL.Query()["i"] + + // var err error + // var rows pgx.Rows + var instance string + if exists { + instance = instanceKeys[0] + } + + instancestatsjson := &InstanceStatsJson{} + instancestatsjson.Timestamp = time.Now() + + if exists && instance != "" { + var activitiescount int + + selectActivities := pool.QueryRow(context.Background(), "SELECT count(*) FROM activities WHERE instance = $1", instance) + err := selectActivities.Scan(&activitiescount) + if err != nil { + fmt.Println("Error ", err) + return + } + instancestatsjson.ActivitiesCount = activitiescount + + var actorscount int + selectActors := pool.QueryRow(context.Background(), "SELECT count(*) FROM actors WHERE instance = $1", instance) + err = selectActors.Scan(&actorscount) + if err != nil { + fmt.Println("Error ", err) + return + } + instancestatsjson.ActorsCount = actorscount + } + + bytearray, _ := json.Marshal(instancestatsjson) + stringarray := string(bytearray) + fmt.Fprintf(w, "%s", stringarray) +} + func main() { pool = getDbPool() @@ -227,6 +273,7 @@ func main() { http.HandleFunc("/api/v1/search", getSearch) http.HandleFunc("/api/v1/trending", getTrending) + http.HandleFunc("/api/v1/instance/stats", getInstanceStats) log.Print("Starting HTTP inbox on port http://0.0.0.0:6431") log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil)) }