Adding global statistics API call

This commit is contained in:
Farhan Khan 2023-07-13 17:57:03 +00:00
parent 5041512453
commit 4dde79f6e2
Signed by untrusted user who does not match committer: farhan
GPG Key ID: 45FE45AD7E54F59B

View File

@ -25,6 +25,12 @@ type ActorStatsJson struct {
ActivitiesCount int `json:"activitiescount"` ActivitiesCount int `json:"activitiescount"`
} }
type GlobalStatsJson struct {
Timestamp time.Time `json:"timestamp"`
ActivitiesCount int `json:"activitiescount"`
ActorsCount int `json:"actorscount"`
}
var metricsText string var metricsText string
func enableCors(w *http.ResponseWriter) { func enableCors(w *http.ResponseWriter) {
@ -295,6 +301,35 @@ func getActorStats(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", stringarray) 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() { func main() {
pool = getDbPool() pool = getDbPool()
@ -311,6 +346,7 @@ func main() {
http.HandleFunc("/api/v1/trending", getTrending) http.HandleFunc("/api/v1/trending", getTrending)
http.HandleFunc("/api/v1/instance/stats", getInstanceStats) http.HandleFunc("/api/v1/instance/stats", getInstanceStats)
http.HandleFunc("/api/v1/actor/stats", getActorStats) 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.Print("Starting HTTP inbox on port http://0.0.0.0:6431")
log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil)) log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil))
} }