71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package stats
|
|
|
|
import (
|
|
"clickandjoin.app/managementsystem/modules/scylladb"
|
|
"clickandjoin.app/managementsystem/modules/structs"
|
|
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/scylladb/gocqlx/v2/qb"
|
|
)
|
|
|
|
func GetStats(c *fiber.Ctx) error {
|
|
// swagger:operation GET /stats stats GetStats
|
|
// ---
|
|
// summary: Stats
|
|
// consumes:
|
|
// - application/json
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// '200':
|
|
// schema:
|
|
// "$ref": "#/definitions/StatsResponse"
|
|
// '500':
|
|
// description: Internal server error
|
|
|
|
var stats structs.StatsResponse
|
|
|
|
if count, err := getCount(gocnjhelper.DbMUsers.Name()); err == nil {
|
|
stats.Users = count
|
|
}
|
|
|
|
if count, err := getCount(gocnjhelper.DbMSessions.Name()); err == nil {
|
|
stats.UserSessions = count
|
|
}
|
|
|
|
if count, err := getCount(gocnjhelper.DbMChats.Name()); err == nil {
|
|
stats.Chats = count
|
|
}
|
|
|
|
if count, err := getCount(gocnjhelper.DbMWebSocketSessions.Name()); err == nil {
|
|
stats.WebSocketSessions = count
|
|
}
|
|
|
|
if count, err := getCount(gocnjhelper.DbMUserSignUpProcess.Name()); err == nil {
|
|
stats.UserSignUpProcesses = count
|
|
}
|
|
|
|
if count, err := getCount(gocnjhelper.DbMUserPrivacySettings.Name()); err == nil {
|
|
stats.UserPrivacySettings = count
|
|
}
|
|
|
|
if count, err := getCount(gocnjhelper.DbMUserRelationship.Name()); err == nil {
|
|
stats.UserRelationships = count
|
|
}
|
|
|
|
return c.JSON(stats)
|
|
}
|
|
|
|
func getCount(name string) (int, error) {
|
|
var count []int
|
|
|
|
q := qb.Select(name).CountAll().Query(scylladb.Session)
|
|
|
|
if err := q.Select(&count); err != nil {
|
|
gocnjhelper.LogErrorf("Failed to get count for %s, err: $s", name, err)
|
|
return 0, err
|
|
}
|
|
|
|
return count[0], nil
|
|
}
|