Skip to content

Commit

Permalink
Merge pull request #2480 from actiontech/feat-overview-of-global-DBSe…
Browse files Browse the repository at this point in the history
…rvices

Feat overview of global db services
  • Loading branch information
ColdWaterLW authored Jul 12, 2024
2 parents c5d6d03 + 33eb51b commit 4fa839c
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqle/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
v1Router.GET("/workflows", v1.GetGlobalWorkflowsV1)
v1Router.GET("/rule_knowledge/db_types/:db_type/rules/:rule_name/", v1.GetRuleKnowledge)
v1Router.GET("/rule_knowledge/db_types/:db_type/custom_rules/:rule_name/", v1.GetCustomRuleKnowledge)
v1Router.GET("/workflows/statistic_of_instances", v1.GetWorkflowStatisticOfInstances)

//rule
v1Router.GET("/rules", v1.GetRules)
Expand Down
56 changes: 56 additions & 0 deletions sqle/api/controller/v1/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,62 @@ func GetWorkflowsV1(c echo.Context) error {
})
}

type GetWorkflowStatisticOfInstancesResV1 struct {
controller.BaseRes
Data []*WorkflowStatisticOfInstance `json:"data"`
}

type WorkflowStatisticOfInstance struct {
InstanceId int64 `json:"instance_id"`
UnfinishedCount int64 `json:"unfinished_count"`
}

// GetWorkflowStatisticOfInstances
// @Summary 获取实例上工单的统计信息
// @Description Get Workflows Statistic Of Instances
// @Tags workflow
// @Id GetWorkflowStatisticOfInstances
// @Security ApiKeyAuth
// @Param instance_id query string true "instance id"
// @Success 200 {object} v1.GetWorkflowStatisticOfInstancesResV1
// @router /v1/workflows/statistic_of_instances [get]
func GetWorkflowStatisticOfInstances(c echo.Context) error {
instanceIds := c.QueryParams()["instance_id"]
if len(instanceIds) == 0 {
return controller.JSONBaseErrorReq(c, fmt.Errorf("query param instance_id requied"))
}

user, err := controller.GetCurrentUser(c, dms.GetUser)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

if user.Name != model.DefaultAdminUser && user.Name != model.DefaultSysUser {
// dms-todo: 后续需要通过dms来判断权限,没这么做的原因是:目前dms接口获取用户权限时必需projectUid参数
return controller.JSONBaseErrorReq(c, fmt.Errorf("permission denied"))
}

s := model.GetStorage()
unfinishedStatuses := []string{model.WorkflowStatusWaitForAudit, model.WorkflowStatusWaitForExecution, model.WorkflowStatusExecuting}
results, err := s.GetWorkflowStatusesCountOfInstances(unfinishedStatuses, instanceIds)
if err != nil {
return err
}

workflowsInfoV1 := make([]*WorkflowStatisticOfInstance, len(results))
for k, v := range results {
workflowsInfoV1[k] = &WorkflowStatisticOfInstance{
InstanceId: v.InstanceId,
UnfinishedCount: v.Count,
}
}

return c.JSON(http.StatusOK, GetWorkflowStatisticOfInstancesResV1{
BaseRes: controller.NewBaseReq(nil),
Data: workflowsInfoV1,
})
}

type UpdateWorkflowReqV1 struct {
TaskIds []uint `json:"task_ids" form:"task_ids" valid:"required"`
}
Expand Down
62 changes: 62 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7040,6 +7040,38 @@ var doc = `{
}
}
},
"/v1/workflows/statistic_of_instances": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Get Workflows Statistic Of Instances",
"tags": [
"workflow"
],
"summary": "获取实例上工单的统计信息",
"operationId": "GetWorkflowStatisticOfInstances",
"parameters": [
{
"type": "string",
"description": "instance id",
"name": "instance_id",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/v1.GetWorkflowStatisticOfInstancesResV1"
}
}
}
}
},
"/v2/audit_files": {
"post": {
"security": [
Expand Down Expand Up @@ -11048,6 +11080,25 @@ var doc = `{
}
}
},
"v1.GetWorkflowStatisticOfInstancesResV1": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 0
},
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/v1.WorkflowStatisticOfInstance"
}
},
"message": {
"type": "string",
"example": "ok"
}
}
},
"v1.GetWorkflowStatusCountResV1": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -13395,6 +13446,17 @@ var doc = `{
}
}
},
"v1.WorkflowStatisticOfInstance": {
"type": "object",
"properties": {
"instance_id": {
"type": "integer"
},
"unfinished_count": {
"type": "integer"
}
}
},
"v1.WorkflowStatisticsResV1": {
"type": "object",
"properties": {
Expand Down
62 changes: 62 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7024,6 +7024,38 @@
}
}
},
"/v1/workflows/statistic_of_instances": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Get Workflows Statistic Of Instances",
"tags": [
"workflow"
],
"summary": "获取实例上工单的统计信息",
"operationId": "GetWorkflowStatisticOfInstances",
"parameters": [
{
"type": "string",
"description": "instance id",
"name": "instance_id",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/v1.GetWorkflowStatisticOfInstancesResV1"
}
}
}
}
},
"/v2/audit_files": {
"post": {
"security": [
Expand Down Expand Up @@ -11032,6 +11064,25 @@
}
}
},
"v1.GetWorkflowStatisticOfInstancesResV1": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 0
},
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/v1.WorkflowStatisticOfInstance"
}
},
"message": {
"type": "string",
"example": "ok"
}
}
},
"v1.GetWorkflowStatusCountResV1": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -13379,6 +13430,17 @@
}
}
},
"v1.WorkflowStatisticOfInstance": {
"type": "object",
"properties": {
"instance_id": {
"type": "integer"
},
"unfinished_count": {
"type": "integer"
}
}
},
"v1.WorkflowStatisticsResV1": {
"type": "object",
"properties": {
Expand Down
40 changes: 40 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,19 @@ definitions:
example: ok
type: string
type: object
v1.GetWorkflowStatisticOfInstancesResV1:
properties:
code:
example: 0
type: integer
data:
items:
$ref: '#/definitions/v1.WorkflowStatisticOfInstance'
type: array
message:
example: ok
type: string
type: object
v1.GetWorkflowStatusCountResV1:
properties:
code:
Expand Down Expand Up @@ -3423,6 +3436,13 @@ definitions:
minutes:
type: integer
type: object
v1.WorkflowStatisticOfInstance:
properties:
instance_id:
type: integer
unfinished_count:
type: integer
type: object
v1.WorkflowStatisticsResV1:
properties:
my_need_execute_workflow_number:
Expand Down Expand Up @@ -8861,6 +8881,26 @@ paths:
summary: 获取全局工单列表
tags:
- workflow
/v1/workflows/statistic_of_instances:
get:
description: Get Workflows Statistic Of Instances
operationId: GetWorkflowStatisticOfInstances
parameters:
- description: instance id
in: query
name: instance_id
required: true
type: string
responses:
"200":
description: OK
schema:
$ref: '#/definitions/v1.GetWorkflowStatisticOfInstancesResV1'
security:
- ApiKeyAuth: []
summary: 获取实例上工单的统计信息
tags:
- workflow
/v2/audit_files:
post:
description: Direct audit sql from SQL files and MyBatis files
Expand Down
1 change: 1 addition & 0 deletions sqle/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

const (
DefaultAdminUser = "admin"
DefaultSysUser = "sys"
)

func IsDefaultAdminUser(user string) bool {
Expand Down
22 changes: 22 additions & 0 deletions sqle/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,28 @@ func (s *Storage) IsWorkflowUnFinishedByInstanceId(instanceId int64) (bool, erro
return count > 0, errors.New(errors.ConnectStorageError, err)
}

type WorkflowCountOfInstance struct {
InstanceId int64
Count int64
}

func (s *Storage) GetWorkflowStatusesCountOfInstances(statuses, instanceIds []string) ([]WorkflowCountOfInstance, error) {
var results []WorkflowCountOfInstance
err := s.db.Table("workflow_records").
Select("workflow_instance_records.instance_id as instance_id, count(workflow_records.id) as count").
Joins("LEFT JOIN workflow_instance_records ON workflow_records.id = workflow_instance_records.workflow_record_id").
Where("workflow_records.status in ?", statuses).
Where("workflow_instance_records.instance_id in ?", instanceIds).
Where("workflow_instance_records.deleted_at IS NULL").
Group("workflow_instance_records.instance_id").
Scan(&results).Error
if err != nil {
return nil, errors.New(errors.ConnectStorageError, err)
}

return results, nil
}

func (s *Storage) GetInstanceIdsByWorkflowID(workflowID string) ([]uint64, error) {
query := `
SELECT wir.instance_id id
Expand Down

0 comments on commit 4fa839c

Please sign in to comment.