Skip to content

Commit

Permalink
fix(statistic): add tenant statistic and update cluster statistic (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
powerfooI authored Mar 7, 2024
1 parent 51d1fef commit 4dc8a5d
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 14 deletions.
44 changes: 31 additions & 13 deletions internal/dashboard/business/oceanbase/obcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
StatusDeleting = "deleting"
StatusRunning = "running"
StatusOperating = "operating"
StatusFailed = "failed"
)

func convertStatus(detailedStatus string) string {
Expand All @@ -57,6 +58,8 @@ func getStatisticStatus(obcluster *v1alpha1.OBCluster) string {
return StatusDeleting
} else if obcluster.Status.Status == StatusRunning {
return StatusRunning
} else if obcluster.Status.Status == clusterstatus.Failed {
return StatusFailed
} else {
return StatusOperating
}
Expand Down Expand Up @@ -549,22 +552,37 @@ func GetOBClusterStatistic(ctx context.Context) ([]response.OBClusterStastistic,
if err != nil {
return statisticResult, errors.Wrap(err, "failed to list obclusters")
}
statusMap := make(map[string]int)
var (
runningCount int
deletingCount int
operatingCount int
failedCount int
)
for _, obcluster := range obclusterList.Items {
statisticStatus := getStatisticStatus(&obcluster)
cnt, found := statusMap[statisticStatus]
if found {
cnt++
} else {
cnt = 1
switch getStatisticStatus(&obcluster) {
case StatusRunning:
runningCount++
case StatusDeleting:
deletingCount++
case StatusOperating:
operatingCount++
case StatusFailed:
failedCount++
}
statusMap[statisticStatus] = cnt
}
for status, count := range statusMap {
statisticResult = append(statisticResult, response.OBClusterStastistic{
Status: status,
Count: count,
statisticResult = append(statisticResult,
response.OBClusterStastistic{
Status: StatusRunning,
Count: runningCount,
}, response.OBClusterStastistic{
Status: StatusDeleting,
Count: deletingCount,
}, response.OBClusterStastistic{
Status: StatusOperating,
Count: operatingCount,
}, response.OBClusterStastistic{
Status: StatusFailed,
Count: failedCount,
})
}
return statisticResult, nil
}
40 changes: 39 additions & 1 deletion internal/dashboard/business/oceanbase/obtenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
apiconst "github.com/oceanbase/ob-operator/api/constants"
apitypes "github.com/oceanbase/ob-operator/api/types"
"github.com/oceanbase/ob-operator/api/v1alpha1"
"github.com/oceanbase/ob-operator/internal/const/status/tenantstatus"
"github.com/oceanbase/ob-operator/internal/dashboard/model/param"
"github.com/oceanbase/ob-operator/internal/dashboard/model/response"
"github.com/oceanbase/ob-operator/internal/oceanbase"
Expand Down Expand Up @@ -120,7 +121,7 @@ func buildOBTenantApiType(nn types.NamespacedName, p *param.CreateOBTenantParam)
t.Spec.Source.Restore.BakDataSource.Type = apitypes.BackupDestType(p.Source.Restore.Type)
t.Spec.Source.Restore.BakDataSource.Path = p.Source.Restore.BakDataSource

if p.Source.Restore.Until != nil {
if p.Source.Restore.Until != nil && !p.Source.Restore.Until.Unlimited {
t.Spec.Source.Restore.Until.Timestamp = p.Source.Restore.Until.Timestamp
} else {
t.Spec.Source.Restore.Until.Unlimited = true
Expand Down Expand Up @@ -501,3 +502,40 @@ func PatchTenant(ctx context.Context, nn types.NamespacedName, p *param.PatchTen
}
return buildDetailFromApiType(tenant), nil
}

// GetOBTenantStatistics returns the statistics of all tenants
// Including the number of tenants in four status: running, deleting, operating, failed
func GetOBTenantStatistics(ctx context.Context) ([]response.OBTenantStatistic, error) {
stats := []response.OBTenantStatistic{}
tenantList, err := oceanbase.ListAllOBTenants(ctx, v1.ListOptions{})
if err != nil {
return nil, oberr.Wrap(err, oberr.ErrInternal, "failed to list tenants")
}
var runningCount, deletingCount, operatingCount, failedCount int
for _, tenant := range tenantList.Items {
switch tenant.Status.Status {
case tenantstatus.Running:
runningCount++
case tenantstatus.DeletingTenant:
deletingCount++
case tenantstatus.Failed, tenantstatus.RestoreFailed:
failedCount++
default:
operatingCount++
}
}
stats = append(stats, response.OBTenantStatistic{
Status: tenantstatus.Running,
Count: runningCount,
}, response.OBTenantStatistic{
Status: tenantstatus.DeletingTenant,
Count: deletingCount,
}, response.OBTenantStatistic{
Status: "operating",
Count: operatingCount,
}, response.OBTenantStatistic{
Status: tenantstatus.Failed,
Count: failedCount,
})
return stats, nil
}
180 changes: 180 additions & 0 deletions internal/dashboard/generated/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,68 @@ const docTemplate = `{
}
}
},
"/api/v1/obtenants/statistic": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "List statistics information of tenants",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Obtenant"
],
"summary": "List statistics information of tenants",
"operationId": "GetOBTenantStatistic",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.APIResponse"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/response.OBTenantStatistic"
}
}
}
}
]
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/response.APIResponse"
}
}
}
}
},
"/api/v1/obtenants/{namespace}/{name}": {
"get": {
"security": [
Expand Down Expand Up @@ -2319,6 +2381,46 @@ const docTemplate = `{
}
},
"definitions": {
"common.AffinitySpec": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"type": {
"$ref": "#/definitions/common.AffinityType"
},
"value": {
"type": "string"
}
}
},
"common.AffinityType": {
"type": "string",
"enum": [
"NODE",
"POD",
"POD_ANTI"
],
"x-enum-varnames": [
"NodeAffinityType",
"PodAffinityType",
"PodAntiAffinityType"
]
},
"common.ClusterMode": {
"type": "string",
"enum": [
"NORMAL",
"STANDALONE",
"SERVICE"
],
"x-enum-varnames": [
"ClusterModeNormal",
"ClusterModeStandalone",
"ClusterModeService"
]
},
"common.KVPair": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2460,6 +2562,9 @@ const docTemplate = `{
"clusterName": {
"type": "string"
},
"mode": {
"$ref": "#/definitions/common.ClusterMode"
},
"monitor": {
"$ref": "#/definitions/param.MonitorSpec"
},
Expand Down Expand Up @@ -2862,6 +2967,12 @@ const docTemplate = `{
"param.ZoneTopology": {
"type": "object",
"properties": {
"affinities": {
"type": "array",
"items": {
"$ref": "#/definitions/common.AffinitySpec"
}
},
"nodeSelector": {
"type": "array",
"items": {
Expand All @@ -2871,6 +2982,12 @@ const docTemplate = `{
"replicas": {
"type": "integer"
},
"tolerations": {
"type": "array",
"items": {
"$ref": "#/definitions/common.KVPair"
}
},
"zone": {
"type": "string"
}
Expand Down Expand Up @@ -3228,6 +3345,28 @@ const docTemplate = `{
}
}
},
"response.MonitorSpec": {
"type": "object",
"properties": {
"image": {
"type": "string"
},
"resource": {
"$ref": "#/definitions/common.ResourceSpec"
}
}
},
"response.NFSVolumeSpec": {
"type": "object",
"properties": {
"address": {
"type": "string"
},
"path": {
"type": "string"
}
}
},
"response.Namespace": {
"type": "object",
"properties": {
Expand All @@ -3242,6 +3381,9 @@ const docTemplate = `{
"response.OBCluster": {
"type": "object",
"properties": {
"backupVolume": {
"$ref": "#/definitions/response.NFSVolumeSpec"
},
"clusterId": {
"type": "integer"
},
Expand All @@ -3257,12 +3399,27 @@ const docTemplate = `{
"metrics": {
"$ref": "#/definitions/response.OBMetrics"
},
"mode": {
"$ref": "#/definitions/common.ClusterMode"
},
"monitor": {
"$ref": "#/definitions/response.MonitorSpec"
},
"name": {
"type": "string"
},
"namespace": {
"type": "string"
},
"parameters": {
"type": "array",
"items": {
"$ref": "#/definitions/common.KVPair"
}
},
"rootPasswordSecret": {
"type": "string"
},
"status": {
"type": "string"
},
Expand Down Expand Up @@ -3484,9 +3641,26 @@ const docTemplate = `{
}
}
},
"response.OBTenantStatistic": {
"type": "object",
"properties": {
"count": {
"type": "integer"
},
"status": {
"type": "string"
}
}
},
"response.OBZone": {
"type": "object",
"properties": {
"affinities": {
"type": "array",
"items": {
"$ref": "#/definitions/common.AffinitySpec"
}
},
"name": {
"type": "string"
},
Expand Down Expand Up @@ -3517,6 +3691,12 @@ const docTemplate = `{
"statusDetail": {
"type": "string"
},
"tolerations": {
"type": "array",
"items": {
"$ref": "#/definitions/common.KVPair"
}
},
"zone": {
"type": "string"
}
Expand Down
Loading

0 comments on commit 4dc8a5d

Please sign in to comment.