Skip to content

Commit

Permalink
add workflow 详情、审核模版
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinwang committed Sep 9, 2023
1 parent 1382c02 commit ab0e2a6
Show file tree
Hide file tree
Showing 16 changed files with 573 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/static/*
conf.toml
conf_local.toml
/horizon.log
/bin/
build.sh
Expand Down
2 changes: 1 addition & 1 deletion config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Config struct {
var Conf Config

func InitConfig() {
if _, err := toml.DecodeFile("conf.toml", &Conf); err != nil {
if _, err := toml.DecodeFile("conf_local.toml", &Conf); err != nil {
panic("ERROR occurred:" + err.Error())
}
fmt.Printf("%s (%s)\n", Conf.Mysql.User, Conf.Mysql.Password)
Expand Down
20 changes: 18 additions & 2 deletions handler/WorkflowHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ func WorkflowGet(c *gin.Context) {
service.WorkflowSelectByList(c)
}

func WorkflowProjIdGet(c *gin.Context) {
service.WorkflowSelectByInstId(c)
func WorkflowIdGet(c *gin.Context) {
service.WorkflowSelectById(c)
}

func WorkflowPost(c *gin.Context) {
Expand All @@ -24,3 +24,19 @@ func WorkflowPut(c *gin.Context) {
func WorkflowDelete(c *gin.Context) {
service.WorkflowDelete(c)
}

func WorkflowIdProgressGet(c *gin.Context) {
service.WorkflowProgressSelectById(c)
}

func WorkflowAuditPost(c *gin.Context) {
service.WorkflowAuditUpdate(c)
}

func WorkflowCancelPost(c *gin.Context) {
service.WorkflowCancelUpdate(c)
}

func WorkflowExecutePost(c *gin.Context) {
service.WorkflowExecuteUpdate(c)
}
26 changes: 26 additions & 0 deletions handler/WorkflowTemplateHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package handler

import (
"github.com/gin-gonic/gin"
"horizon/service"
)

func WorkflowTemplateGet(c *gin.Context) {
service.WorkflowTemplateSelectByList(c)
}

func WorkflowTemplatePost(c *gin.Context) {
service.WorkflowTemplateInsert(c)
}

func WorkflowTemplatePut(c *gin.Context) {
service.WorkflowTemplateUpdate(c)
}

func WorkflowTemplateDelete(c *gin.Context) {
service.WorkflowTemplateDelete(c)
}

func WorkflowTemplateConfigPost(c *gin.Context) {
service.WorkflowTemplateConfigInsert(c)
}
10 changes: 9 additions & 1 deletion model/Instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ package model

import "time"

type InstType string

const (
MySQLType InstType = "MySQL"
DorisType InstType = "Doris"
)

type Instance struct {
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
InstId string `gorm:"type:varchar(20);uniqueIndex:uniq_inst_id;not null;comment:实例ID" json:"instId"`
Name string `gorm:"type:varchar(50);not null;comment:实例名称" json:"name"`
Type InstType `gorm:"type:varchar(20);default:'MySQL';not null;comment:实例类型" json:"type"`
Role string `gorm:"type:enum('Master', 'Slave');default:'Master';not null;comment:实例角色" json:"role"`
Ip string `gorm:"type:varchar(20);not null;comment:实例IP" json:"ip"`
Port uint16 `gorm:"not null;comment:实例端口" json:"port"`
User string `gorm:"type:varchar(50);not null;comment:用户名" json:"user"`
Password string `gorm:"type:varchar(100);not null;comment:密码" json:"password"`
Version string `gorm:"type:varchar(50);not null;comment:实例版本" json:"version"`
Status string `gorm:"type:enum('Start', 'Stop', 'Running', 'Error');default:'Running';not null;comment:实例状态" json:"status"`
InspStatus string `gorm:"type:enum('Enabled', 'Disabled');not null;default:'Enabled';comment:巡检状态;Disabled:关闭;Enabled:开启" json:"inspStatus"`
InspStatus string `gorm:"type:enum('Enabled', 'Disabled');not null;default:'Disabled';comment:巡检状态;Disabled:关闭;Enabled:开启" json:"inspStatus"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
Inspection []Inspection `gorm:"foreignKey:InstId;references:InstId" json:"inspection"`
Expand Down
13 changes: 13 additions & 0 deletions model/LocalTime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model

import (
"fmt"
"time"
)

type LocalTime time.Time

func (t *LocalTime) MarshalJSON() ([]byte, error) {
tTime := time.Time(*t)
return []byte(fmt.Sprintf("\"%v\"", tTime.Format("2006-01-02 15:04:05"))), nil
}
32 changes: 17 additions & 15 deletions model/Project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import (
)

type Project struct {
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
ProjId string `gorm:"type:varchar(20);uniqueIndex:uniq_proj_id;not null;comment:项目ID" json:"projId"`
Name string `gorm:"type:varchar(50);not null;comment:项目名称" json:"name"`
Describe string `gorm:"type:varchar(255);not null;comment:描述" json:"describe"`
Status string `gorm:"type:enum('Enabled', 'Disabled');default:'Enabled';not null;comment:正常,禁用" json:"status"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
ProjectDatasources []ProjectDatasource `gorm:"foreignKey:ProjId;references:ProjId" json:"projectDatasources"`
ProjectUsers []ProjectUser `gorm:"foreignKey:ProjId;references:ProjId" json:"projectUsers"`
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
ProjId string `gorm:"type:varchar(20);uniqueIndex:uniq_proj_id;not null;comment:项目ID" json:"projId"`
Name string `gorm:"type:varchar(50);not null;comment:项目名称" json:"name"`
Describe string `gorm:"type:varchar(255);not null;comment:描述" json:"describe"`
Status string `gorm:"type:enum('Enabled', 'Disabled');default:'Enabled';not null;comment:正常,禁用" json:"status"`
WorkflowTemplateCode uint `gorm:"not null;comment:编号" json:"workflowTemplateCode"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
ProjectDatasources []ProjectDatasource `gorm:"foreignKey:ProjId;references:ProjId" json:"projectDatasources"`
ProjectUsers []ProjectUser `gorm:"foreignKey:ProjId;references:ProjId" json:"projectUsers"`
}

type ProjectRole struct {
ID string `gorm:"type:varchar(50);primaryKey;comment:角色ID" json:"id"`
Name string `gorm:"type:varchar(20);uniqueIndex:uniq_name;not null;comment:名称" json:"name"`
Describe string `gorm:"type:varchar(255);not null;comment:描述" json:"describe"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
ProjectUsers []ProjectUser `gorm:"foreignKey:RoleId;references:ID" json:"projectUsers"`
ID string `gorm:"type:varchar(50);primaryKey;comment:角色ID" json:"id"`
Name string `gorm:"type:varchar(20);uniqueIndex:uniq_name;not null;comment:名称" json:"name"`
Describe string `gorm:"type:varchar(255);not null;comment:描述" json:"describe"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
ProjectUsers []ProjectUser `gorm:"foreignKey:RoleId;references:ID" json:"projectUsers"`
WorkflowTemplateDetails []WorkflowTemplateDetail `gorm:"foreignKey:ProjectRoleId;references:ID" json:"workflowTemplateDetails"`
}

type ProjectDatasource struct {
Expand Down
7 changes: 5 additions & 2 deletions model/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func InitDb() {
dbConf := config.Conf.Mysql
dsn := "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=3s"
dsn = fmt.Sprintf(dsn, dbConf.User, dbConf.Password, dbConf.Host, dbConf.Port, dbConf.Db)
Db, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{})
Db, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: false})
err := Db.AutoMigrate(
&User{},
&Instance{},
Expand All @@ -33,7 +33,10 @@ func InitDb() {
&ProjectDatasource{},
&ProjectUser{},
&RuleTemplate{},
&Workflow{})
&Workflow{},
&WorkflowTemplate{},
&WorkflowTemplateDetail{},
&WorkflowRecord{})
if err != nil {
log.Fatal(err.Error())
}
Expand Down
83 changes: 64 additions & 19 deletions model/workflow.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,74 @@
package model

import "time"
import (
"database/sql"
"time"
)

type workflowAuditStatus string
type workflowStatus string

const (
WorkflowStatusWaitForAudit workflowStatus = "wait_for_audit"
WorkflowStatusWaitForExecution workflowStatus = "wait_for_execution"
WorkflowStatusReject workflowStatus = "rejected"
WorkflowStatusCancel workflowStatus = "canceled"
WorkflowStatusExecuting workflowStatus = "executing"
WorkflowStatusExecFailed workflowStatus = "exec_failed"
WorkflowStatusFinish workflowStatus = "finished"
WorkflowStatusPendingAudit workflowStatus = "PendingAudit"
WorkflowStatusPendingExecution workflowStatus = "PendingExecution"
WorkflowStatusRejected workflowStatus = "Rejected"
WorkflowStatusCanceled workflowStatus = "Canceled"
WorkflowStatusExecuting workflowStatus = "Executing"
WorkflowStatusExecutionFailed workflowStatus = "ExecutionFailed"
WorkflowStatusFinished workflowStatus = "Finished"

FlowAuditStatusPendingAudit workflowAuditStatus = "PendingAudit"
FlowAuditStatusPassed workflowAuditStatus = "Passed"
FlowAuditStatusAuditRejected workflowAuditStatus = "Rejected"
)

type Workflow struct {
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
Name string `gorm:"type:varchar(50);not null;comment:名称" json:"name"`
Describe string `gorm:"type:varchar(255);not null;comment:描述" json:"describe"`
Status workflowStatus `gorm:"type:varchar(50);not null;default:'wait_for_audit';comment:状态" json:"status"`
ProjId string `gorm:"type:varchar(20);not null;comment:项目ID" json:"projId"`
InstId string `gorm:"type:varchar(20);not null;comment:实例ID" json:"instId"`
DbName string `gorm:"type:varchar(255);not null;comment:数据库名" json:"dbName"`
SqlContent string `gorm:"type:text;not null;comment:SQL内容" json:"sqlContent"`
UserName string `gorm:"type:varchar(50);not null;comment:用户名" json:"userName"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
Name string `gorm:"type:varchar(50);not null;comment:名称" json:"name"`
Describe string `gorm:"type:varchar(255);not null;comment:描述" json:"describe"`
Status workflowStatus `gorm:"type:varchar(50);not null;default:'PendingAudit';comment:状态" json:"status"`
ProjId string `gorm:"type:varchar(20);not null;comment:项目ID" json:"projId"`
InstId string `gorm:"type:varchar(20);not null;comment:实例ID" json:"instId"`
DbName string `gorm:"type:varchar(255);not null;comment:数据库名" json:"dbName"`
SqlContent string `gorm:"type:text;not null;comment:SQL内容" json:"sqlContent"`
UserName string `gorm:"type:varchar(50);not null;comment:用户名" json:"userName"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
WorkflowRecords []WorkflowRecord `gorm:"foreignKey:WorkflowId;references:ID" json:"workflowRecords"`
}

type WorkflowTemplate struct {
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
Code uint `gorm:"uniqueIndex:uniq_code;not null;comment:编号" json:"code"`
Name string `gorm:"type:varchar(50);not null;comment:名称" json:"name"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
WorkflowTemplateDetails []WorkflowTemplateDetail `gorm:"foreignKey:WorkflowTemplateId;references:ID" json:"workflowTemplateDetails"`
// Projects []Project `gorm:"foreignKey:WorkflowTemplateCode;references:Code" json:"projects"`
}

type WorkflowTemplateDetail struct {
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
WorkflowTemplateId uint `gorm:"not null;comment:工作流模版ID" json:"workflowTemplateId"`
WorkflowTemplateCode uint `gorm:"not null;comment:工作流模版编号" json:"workflowTemplateCode"`
SerialNumber uint `gorm:"not null;comment:工作流序号" json:"serialNumber"`
NodeName string `gorm:"type:varchar(20);not null;comment:节点名称" json:"nodeName"`
ProjectRoleId string `gorm:"type:varchar(50);not null;comment:项目角色ID" json:"projectRoleId"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
}

type WorkflowRecord struct {
ID uint `gorm:"primaryKey;comment:主键ID" json:"id"`
WorkflowId uint `gorm:"not null;comment:工单ID" json:"workflowId"`
WorkflowTemplateCode uint `gorm:"not null;comment:工作流模版Code" json:"workflowTemplateCode"`
FlowNodeName string `gorm:"type:varchar(20);not null;comment:节点名称" json:"flowNodeName"`
FlowSerialNumber uint `gorm:"not null;comment:工作流序号" json:"flowSerialNumber"`
AssigneeUserName string `gorm:"type:varchar(50);not null;comment:受理用户" json:"assigneeUserName"`
HandledAt sql.NullTime `gorm:"type:datetime;default null;comment:处理时间" json:"handledAt"`
Remarks string `gorm:"type:varchar(255);not null;comment:处理结果/备注" json:"remarks"`
AuditStatus workflowAuditStatus `gorm:"type:varchar(50);not null;default:'PendingAudit';comment:状态" json:"auditStatus"`
IsAudit uint `gorm:"not null;default:0;comment:审核标识(0:未审核,1:已审核)" json:"isAudit"`
CreatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp;comment:创建时间" json:"createdAt"`
UpdatedAt time.Time `gorm:"type:datetime;not null;default:current_timestamp on update current_timestamp;comment:修改时间" json:"updatedAt"`
}
14 changes: 13 additions & 1 deletion router/Router.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,20 @@ func commonRouter(r *gin.Engine) {
// workflow group
workflow := sqlAudit.Group("/workflow", authMiddleware.MiddlewareFunc())
workflow.GET("", handler.WorkflowGet)
workflow.GET("/:projId", handler.WorkflowProjIdGet)
workflow.GET("/:id", handler.WorkflowIdGet)
workflow.POST("", handler.WorkflowPost)
workflow.PUT("", handler.WorkflowPut)
workflow.DELETE("/:id", handler.WorkflowDelete)
workflow.GET("progress/:id", handler.WorkflowIdProgressGet)
workflow.POST("audit", handler.WorkflowAuditPost)
workflow.POST("cancel", handler.WorkflowCancelPost)
workflow.POST("execute", handler.WorkflowExecutePost)

// workflowTemplate group
workflowTemplate := sqlAudit.Group("/workflowTemplate", authMiddleware.MiddlewareFunc())
workflowTemplate.GET("", handler.WorkflowTemplateGet)
workflowTemplate.POST("", handler.WorkflowTemplatePost)
workflowTemplate.PUT("", handler.WorkflowTemplatePut)
workflowTemplate.DELETE("/:id", handler.WorkflowTemplateDelete)
workflowTemplate.POST("/config", handler.WorkflowTemplateConfigPost)
}
3 changes: 2 additions & 1 deletion service/InstanceService.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func InstanceInsert(c *gin.Context) {
getVersion(&instance)
}
// 获取实例ID
generateId(&instance)
instId := utils.GenerateId(&instance)
instance.InstId = instId
result := model.Db.Create(&instance)
if result.Error != nil {
c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "fail", "data": "", "err": result.Error})
Expand Down
4 changes: 4 additions & 0 deletions service/ProjectService.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func ProjectResourceConfigInsert(c *gin.Context) {
return
}
tx := model.Db.Begin()
// update project workflow template
tx.Model(&model.Project{}).
Where("proj_id = ?", permBody.ProjId).
UpdateColumn("workflow_template_code", permBody.WorkflowTemplateCode)
// first delete and add ProjectDatasources
result := tx.Where("proj_id = ?", permBody.Project.ProjId).Delete(&model.ProjectDatasource{})
if result.Error != nil {
Expand Down
Loading

0 comments on commit ab0e2a6

Please sign in to comment.