added stats
parent
37f50852a4
commit
1673f04fd5
|
@ -0,0 +1,10 @@
|
|||
package structs
|
||||
|
||||
type StatsResponse struct {
|
||||
Users int
|
||||
Chats int
|
||||
WebSocketSessions int
|
||||
UserSignUpProcesses int
|
||||
UserPrivacySettings int
|
||||
UserRelationships int
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package stats
|
||||
|
||||
import (
|
||||
"clickandjoin.app/managementsystem/modules/scylladb"
|
||||
"clickandjoin.app/managementsystem/modules/structs"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/scylladb/gocqlx/v2/qb"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
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/ChatsResponse"
|
||||
// '500':
|
||||
// description: Internal server error
|
||||
|
||||
var stats structs.StatsResponse
|
||||
|
||||
if count, err := getCount(scylladb.Users.Name()); err == nil {
|
||||
stats.Users = count
|
||||
}
|
||||
|
||||
if count, err := getCount(scylladb.Chats.Name()); err == nil {
|
||||
stats.Chats = count
|
||||
}
|
||||
|
||||
if count, err := getCount(scylladb.WebSocketSessions.Name()); err == nil {
|
||||
stats.WebSocketSessions = count
|
||||
}
|
||||
|
||||
if count, err := getCount(scylladb.UserSignUpProcess.Name()); err == nil {
|
||||
stats.UserSignUpProcesses = count
|
||||
}
|
||||
|
||||
if count, err := getCount(scylladb.UserPrivacySettings.Name()); err == nil {
|
||||
stats.UserPrivacySettings = count
|
||||
}
|
||||
|
||||
if count, err := getCount(scylladb.UserRelationship.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 {
|
||||
logrus.Errorln("Failed to get count for", name, "err:", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count[0], nil
|
||||
}
|
Loading…
Reference in New Issue