diff --git a/db/db.go b/db/db.go index 6a17aa4a8..696ac2cb2 100644 --- a/db/db.go +++ b/db/db.go @@ -1722,6 +1722,27 @@ func GetAllStations() ([]models.Station, error) { return stations, nil } +func CountStationsByTenant(tenantName string) (int, error) { + ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) + defer cancelfunc() + conn, err := MetadataDbClient.Client.Acquire(ctx) + if err != nil { + return 0, err + } + defer conn.Release() + query := `SELECT COUNT(*) FROM stations where tenant_name = $1` + stmt, err := conn.Conn().Prepare(ctx, "get_count_stations_by_tenant", query) + if err != nil { + return 0, err + } + var count int + err = conn.Conn().QueryRow(ctx, stmt.Name, tenantName).Scan(&count) + if err != nil { + return 0, err + } + return count, nil +} + func GetAllStationsDetails() ([]models.ExtendedStation, error) { ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) defer cancelfunc() @@ -4005,6 +4026,28 @@ func InsertNewSchemaVersion(schemaVersionNumber int, userId int, username string return newSchemaVersion, rowsAffected, nil } +func CountAllSchemasByTenant(tenantName string) (int64, error) { + var count int64 + ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) + defer cancelfunc() + conn, err := MetadataDbClient.Client.Acquire(ctx) + if err != nil { + return 0, err + } + defer conn.Release() + query := `SELECT COUNT(*) FROM schemas WHERE tenant_name = $1` + stmt, err := conn.Conn().Prepare(ctx, "get_total_schemas_by_tenant", query) + if err != nil { + return 0, err + } + err = conn.Conn().QueryRow(ctx, stmt.Name, tenantName).Scan(&count) + if err != nil { + return 0, err + } + + return count, nil +} + // Integration Functions func GetIntegration(name string, tenantName string) (bool, models.Integration, error) { if tenantName != conf.GlobalAccount { @@ -4661,6 +4704,28 @@ func CountAllUsers() (int64, error) { return count, nil } +func CountAllUsersByTenant(tenantName string) (int64, error) { + var count int64 + ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) + defer cancelfunc() + conn, err := MetadataDbClient.Client.Acquire(ctx) + if err != nil { + return 0, err + } + defer conn.Release() + query := `SELECT COUNT(*) FROM users WHERE tenant_name = $1 AND username NOT LIKE '$%'` // filter memphis internal users` + stmt, err := conn.Conn().Prepare(ctx, "get_total_users_by_tenant", query) + if err != nil { + return 0, err + } + err = conn.Conn().QueryRow(ctx, stmt.Name, tenantName).Scan(&count) + if err != nil { + return 0, err + } + + return count, nil +} + func GetAllUsersByTypeAndTenantName(userType []string, tenantName string) ([]models.User, error) { ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) defer cancelfunc() diff --git a/http_server/routes/monitoring.go b/http_server/routes/monitoring.go index e5292d1c5..df6b4c507 100644 --- a/http_server/routes/monitoring.go +++ b/http_server/routes/monitoring.go @@ -26,5 +26,6 @@ func InitializeMonitoringRoutes(router *gin.RouterGroup, h *server.Handlers) { monitoringRoutes.GET("/getSystemLogs", monitoringHandler.GetSystemLogs) monitoringRoutes.GET("/downloadSystemLogs", monitoringHandler.DownloadSystemLogs) monitoringRoutes.GET("/getAvailableReplicas", monitoringHandler.GetAvailableReplicas) + monitoringRoutes.GET("/getSystemGeneralInfo", monitoringHandler.GetSystemGeneralInfo) server.AddMonitoringCloudRoutes(monitoringRoutes, monitoringHandler) } diff --git a/server/memphis_handlers_monitoring.go b/server/memphis_handlers_monitoring.go index 91674a40d..ce6359a73 100644 --- a/server/memphis_handlers_monitoring.go +++ b/server/memphis_handlers_monitoring.go @@ -1347,3 +1347,45 @@ func (mh MonitoringHandler) GetGraphOverview(c *gin.Context) { c.IndentedJSON(200, res) } + +func (mh MonitoringHandler) GetSystemGeneralInfo(c *gin.Context) { + user, err := getUserDetailsFromMiddleware(c) + if err != nil { + serv.Errorf("GetSystemGeneralInfo at getUserDetailsFromMiddleware: %v", err.Error()) + c.AbortWithStatusJSON(500, gin.H{"message": "Server error"}) + return + } + + totalAmountBrokers := 1 + + v, err := serv.Varz(nil) + if err != nil { + serv.Errorf("[tenant: %v][user: %v]GetSystemGeneralInfo at serv.Varz : %v", user.TenantName, user.Username, err.Error()) + c.AbortWithStatusJSON(500, gin.H{"message": "Server error"}) + return + } + if len(v.Cluster.URLs) > 0 { + totalAmountBrokers = len(v.Cluster.URLs) + } + + stationsCount, err := db.CountStationsByTenant(user.TenantName) + if err != nil { + serv.Errorf("[tenant: %v][user: %v]GetSystemGeneralInfo at CountStationsByTenant: %v", user.TenantName, user.Username, err.Error()) + c.AbortWithStatusJSON(500, gin.H{"message": "Server error"}) + return + } + usersCount, err := db.CountAllUsersByTenant(user.TenantName) + if err != nil { + serv.Errorf("[tenant: %v][user: %v]GetSystemGeneralInfo at CountAllUsersByTenant: %v", user.TenantName, user.Username, err.Error()) + c.AbortWithStatusJSON(500, gin.H{"message": "Server error"}) + return + } + + schemasCount, err := db.CountAllSchemasByTenant(user.TenantName) + if err != nil { + serv.Errorf("[tenant: %v][user: %v]GetSystemGeneralInfo at CountAllSchemasByTenant: %v", user.TenantName, user.Username, err.Error()) + c.AbortWithStatusJSON(500, gin.H{"message": "Server error"}) + return + } + c.IndentedJSON(200, gin.H{"total_amount_brokers": totalAmountBrokers, "total_stations": stationsCount, "total_users": usersCount, "total_schemas": schemasCount}) +}