Add DB stats

This commit is contained in:
Simon Ser 2021-10-05 19:31:06 +02:00
parent 5405923aa2
commit e177977c30
3 changed files with 29 additions and 2 deletions

7
db.go
View File

@ -9,6 +9,7 @@ import (
type Database interface {
Close() error
Stats() (*DatabaseStats, error)
ListUsers() ([]User, error)
GetUser(username string) (*User, error)
@ -26,6 +27,12 @@ type Database interface {
StoreClientDeliveryReceipts(networkID int64, client string, receipts []DeliveryReceipt) error
}
type DatabaseStats struct {
Users int64
Networks int64
Channels int64
}
type User struct {
ID int64
Username string

View File

@ -205,6 +205,22 @@ func (db *SqliteDB) upgrade() error {
return tx.Commit()
}
func (db *SqliteDB) Stats() (*DatabaseStats, error) {
db.lock.RLock()
defer db.lock.RUnlock()
var stats DatabaseStats
row := db.db.QueryRow(`SELECT
(SELECT COUNT(*) FROM User) AS users,
(SELECT COUNT(*) FROM Network) AS networks,
(SELECT COUNT(*) FROM Channel) AS channels`)
if err := row.Scan(&stats.Users, &stats.Networks, &stats.Channels); err != nil {
return nil, err
}
return &stats, nil
}
func toNullString(s string) sql.NullString {
return sql.NullString{
String: s,

View File

@ -1026,7 +1026,11 @@ func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
}
func handleServiceServerStatus(dc *downstreamConn, params []string) error {
stats := dc.user.srv.Stats()
sendServicePRIVMSG(dc, fmt.Sprintf("%v users, %v downstreams", stats.Users, stats.Downstreams))
dbStats, err := dc.user.srv.db.Stats()
if err != nil {
return err
}
serverStats := dc.user.srv.Stats()
sendServicePRIVMSG(dc, fmt.Sprintf("%v/%v users, %v downstreams, %v networks, %v channels", serverStats.Users, dbStats.Users, serverStats.Downstreams, dbStats.Networks, dbStats.Channels))
return nil
}