Adding global statistics API call
This commit is contained in:
parent
5041512453
commit
4dde79f6e2
@ -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))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user