Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GetSystemGeneralInfo endpoint #1408

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions http_server/routes/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
42 changes: 42 additions & 0 deletions server/memphis_handlers_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}