Adding instance statistics API call
This commit is contained in:
parent
14edc5faf2
commit
0b38f818d3
@ -12,6 +12,12 @@ import (
|
|||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type InstanceStatsJson 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) {
|
||||||
@ -48,7 +54,7 @@ GROUP BY hashtags ORDER BY count DESC LIMIT 20;`
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hashtagitems := make([]interface{}, 0);
|
hashtagitems := make([]interface{}, 0)
|
||||||
hashcount := 0
|
hashcount := 0
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var hashtag string
|
var hashtag string
|
||||||
@ -67,7 +73,7 @@ GROUP BY hashtags ORDER BY count DESC LIMIT 20;`
|
|||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
||||||
hashtagtotal := make(map[string]interface{});
|
hashtagtotal := make(map[string]interface{})
|
||||||
hashtagtotal["count"] = hashcount
|
hashtagtotal["count"] = hashcount
|
||||||
hashtagtotal["items"] = hashtagitems
|
hashtagtotal["items"] = hashtagitems
|
||||||
|
|
||||||
@ -102,7 +108,7 @@ ORDER BY 2 DESC LIMIT 20;`
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trendingitems := make([]interface{}, 0);
|
trendingitems := make([]interface{}, 0)
|
||||||
trendingcount := 0
|
trendingcount := 0
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var trendingword string
|
var trendingword string
|
||||||
@ -121,7 +127,7 @@ ORDER BY 2 DESC LIMIT 20;`
|
|||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
||||||
trendingwordtotal := make(map[string]interface{});
|
trendingwordtotal := make(map[string]interface{})
|
||||||
trendingwordtotal["count"] = trendingcount
|
trendingwordtotal["count"] = trendingcount
|
||||||
trendingwordtotal["items"] = trendingitems
|
trendingwordtotal["items"] = trendingitems
|
||||||
|
|
||||||
@ -213,6 +219,46 @@ func getSearch(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "%s", data)
|
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() {
|
func main() {
|
||||||
pool = getDbPool()
|
pool = getDbPool()
|
||||||
|
|
||||||
@ -227,6 +273,7 @@ func main() {
|
|||||||
|
|
||||||
http.HandleFunc("/api/v1/search", getSearch)
|
http.HandleFunc("/api/v1/search", getSearch)
|
||||||
http.HandleFunc("/api/v1/trending", getTrending)
|
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.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))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user