diff --git a/restapi/restapi.go b/restapi/restapi.go index 30f24c7..00917d4 100644 --- a/restapi/restapi.go +++ b/restapi/restapi.go @@ -14,10 +14,17 @@ import ( type InstanceStatsJson struct { Timestamp time.Time `json:"timestamp"` + Instance string `json:"timestamp"` ActivitiesCount int `json:"activitiescount"` ActorsCount int `json:"actorscount"` } +type ActorStatsJson struct { + Timestamp time.Time `json:"timestamp"` + Actor string `json:"actor"` + ActivitiesCount int `json:"activitiescount"` +} + var metricsText string func enableCors(w *http.ResponseWriter) { @@ -223,8 +230,6 @@ 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] @@ -232,6 +237,7 @@ func getInstanceStats(w http.ResponseWriter, r *http.Request) { instancestatsjson := &InstanceStatsJson{} instancestatsjson.Timestamp = time.Now() + instancestatsjson.Instance = instance if exists && instance != "" { var activitiescount int @@ -259,6 +265,36 @@ func getInstanceStats(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s", stringarray) } +func getActorStats(w http.ResponseWriter, r *http.Request) { + enableCors(&w) + actorKeys, exists := r.URL.Query()["a"] + + var actor string + if exists { + actor = actorKeys[0] + } + + actorstatsjson := &ActorStatsJson{} + actorstatsjson.Timestamp = time.Now() + actorstatsjson.Actor = actor + + if exists && actor != "" { + var actorscount int + + selectActivities := pool.QueryRow(context.Background(), "SELECT count(*) FROM activities WHERE document->>'attributedTo' = $1", actor) + err := selectActivities.Scan(&actorscount) + if err != nil { + fmt.Println("Error ", err) + return + } + actorstatsjson.ActivitiesCount = actorscount + } + + bytearray, _ := json.Marshal(actorstatsjson) + stringarray := string(bytearray) + fmt.Fprintf(w, "%s", stringarray) +} + func main() { pool = getDbPool() @@ -274,6 +310,7 @@ func main() { http.HandleFunc("/api/v1/search", getSearch) http.HandleFunc("/api/v1/trending", getTrending) http.HandleFunc("/api/v1/instance/stats", getInstanceStats) + http.HandleFunc("/api/v1/actor/stats", getActorStats) log.Print("Starting HTTP inbox on port http://0.0.0.0:6431") log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil)) }