feat: 定时任务编辑入口全栈打通 + 修复ApprovalFlow编译错误 + 完成大文件拆分重构
- CronJobManager: 编辑按钮接通 onClick → openEdit(job),弹出编辑对话框预填数据,
复用新建/编辑共用 Dialog(editingId 区分模式),标题/按钮文案动态切换,
保存时根据 editingId 调用 createJob 或 updateJob — 消除占位按钮
- ApprovalFlow: 修复 API 方法名不匹配导致 tsc -b 编译失败
(getInstances→getProcesses, getMyApprovals→getProcesses({status:'pending'}),
approveInstance→approveProcess, rejectInstance→rejectProcess)
+ 添加 ApprovalFlowManager 导出别名兼容 App.tsx 导入
- 合并已完成的大文件拆分重构: automation.go→automation_cron/script/playbook,
ldap.go→ldap_ou/ldap_users, server.go→cloud_accounts/domain/platform/region,
user.go→user_admin; 前端 Dashboard/K8s/Server/Approval/Monitor 子组件拆分
// Logan.Li
This commit is contained in:
1 parent
8be63ac168
commit
a7e2351ad2
37 files changed
+7374
-4891
No files matched your search
@@ -0,0 +1,343 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"ezops/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ListCronJobs 获取定时任务列表
|
||||
// @Summary 获取定时任务列表
|
||||
// @Description 获取所有定时任务列表
|
||||
// @Tags 自动化运维
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "每页数量"
|
||||
// @Success 200 {object} models.Response{data=models.ListResponse}
|
||||
// @Router /cron-jobs [get]
|
||||
func (h *AutomationHandler) ListCronJobs(c *gin.Context) {
|
||||
var query models.PageQuery
|
||||
c.ShouldBindQuery(&query)
|
||||
|
||||
var total int64
|
||||
h.db.Model(&models.CronJob{}).Count(&total)
|
||||
|
||||
var jobs []models.CronJob
|
||||
h.db.Preload("Script").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&jobs)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: models.ListResponse{
|
||||
List: jobs,
|
||||
Total: total,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// GetCronJob 获取定时任务详情
|
||||
// @Summary 获取定时任务详情
|
||||
// @Description 获取指定定时任务的详细信息
|
||||
// @Tags 自动化运维
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param id path int true "任务ID"
|
||||
// @Success 200 {object} models.Response{data=models.CronJob}
|
||||
// @Router /cron-jobs/{id} [get]
|
||||
func (h *AutomationHandler) GetCronJob(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var job models.CronJob
|
||||
if err := h.db.Preload("Script").First(&job, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{
|
||||
Code: 404,
|
||||
Message: "定时任务不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: job,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateCronJob 创建定时任务
|
||||
// @Summary 创建定时任务
|
||||
// @Description 创建新定时任务
|
||||
// @Tags 自动化运维
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param request body models.CreateCronJobRequest true "创建请求"
|
||||
// @Success 200 {object} models.Response{data=models.CronJob}
|
||||
// @Router /cron-jobs [post]
|
||||
func (h *AutomationHandler) CreateCronJob(c *gin.Context) {
|
||||
var req models.CreateCronJobRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{
|
||||
Code: 400,
|
||||
Message: "请求参数错误: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
job := models.CronJob{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
CronExpr: req.CronExpr,
|
||||
Type: req.Type,
|
||||
Content: req.Content,
|
||||
ScriptID: req.ScriptID,
|
||||
TargetType: req.TargetType,
|
||||
Timeout: req.Timeout,
|
||||
NotifyOn: req.NotifyOn,
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
if job.Timeout == 0 {
|
||||
job.Timeout = 300
|
||||
}
|
||||
if job.NotifyOn == "" {
|
||||
job.NotifyOn = "failure"
|
||||
}
|
||||
|
||||
if err := h.db.Create(&job).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
Code: 500,
|
||||
Message: "创建失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "创建成功",
|
||||
Data: job,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateCronJob 更新定时任务
|
||||
// @Summary 更新定时任务
|
||||
// @Description 更新指定定时任务
|
||||
// @Tags 自动化运维
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param id path int true "任务ID"
|
||||
// @Param request body models.CreateCronJobRequest true "更新请求"
|
||||
// @Success 200 {object} models.Response{data=models.CronJob}
|
||||
// @Router /cron-jobs/{id} [put]
|
||||
func (h *AutomationHandler) UpdateCronJob(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var req models.CreateCronJobRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{
|
||||
Code: 400,
|
||||
Message: "请求参数错误: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var job models.CronJob
|
||||
if err := h.db.First(&job, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{
|
||||
Code: 404,
|
||||
Message: "定时任务不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"description": req.Description,
|
||||
"cron_expr": req.CronExpr,
|
||||
"type": req.Type,
|
||||
"content": req.Content,
|
||||
"script_id": req.ScriptID,
|
||||
"target_type": req.TargetType,
|
||||
"timeout": req.Timeout,
|
||||
"notify_on": req.NotifyOn,
|
||||
}
|
||||
|
||||
if err := h.db.Model(&job).Updates(updates).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
Code: 500,
|
||||
Message: "更新失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
h.db.Preload("Script").First(&job, id)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "更新成功",
|
||||
Data: job,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteCronJob 删除定时任务
|
||||
// @Summary 删除定时任务
|
||||
// @Description 删除指定定时任务
|
||||
// @Tags 自动化运维
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param id path int true "任务ID"
|
||||
// @Success 200 {object} models.Response
|
||||
// @Router /cron-jobs/{id} [delete]
|
||||
func (h *AutomationHandler) DeleteCronJob(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
if err := h.db.Delete(&models.CronJob{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
Code: 500,
|
||||
Message: "删除失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "删除成功",
|
||||
})
|
||||
}
|
||||
|
||||
// ToggleCronJob 切换定时任务状态
|
||||
// @Summary 切换定时任务状态
|
||||
// @Description 启用或禁用定时任务
|
||||
// @Tags 自动化运维
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param id path int true "任务ID"
|
||||
// @Success 200 {object} models.Response{data=models.CronJob}
|
||||
// @Router /cron-jobs/{id}/toggle [post]
|
||||
func (h *AutomationHandler) ToggleCronJob(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var job models.CronJob
|
||||
if err := h.db.First(&job, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{
|
||||
Code: 404,
|
||||
Message: "定时任务不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
job.Enabled = !job.Enabled
|
||||
if err := h.db.Save(&job).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
Code: 500,
|
||||
Message: "操作失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "操作成功",
|
||||
Data: job,
|
||||
})
|
||||
}
|
||||
|
||||
// ExecuteCronJob 执行定时任务
|
||||
// @Summary 执行定时任务
|
||||
// @Description 手动执行定时任务
|
||||
// @Tags 自动化运维
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param id path int true "任务ID"
|
||||
// @Success 200 {object} models.Response{data=models.TaskExecution}
|
||||
// @Router /cron-jobs/{id}/execute [post]
|
||||
func (h *AutomationHandler) ExecuteCronJob(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var job models.CronJob
|
||||
if err := h.db.First(&job, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{
|
||||
Code: 404,
|
||||
Message: "定时任务不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve the content to execute. For type=script, load the linked Script.
|
||||
content := job.Content
|
||||
scriptType := "shell"
|
||||
if job.Type == "script" && job.ScriptID != nil {
|
||||
var script models.Script
|
||||
if err := h.db.First(&script, *job.ScriptID).Error; err == nil {
|
||||
content = script.Content
|
||||
scriptType = script.Type
|
||||
h.db.Model(&script).UpdateColumn("run_count", script.RunCount+1)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
execution := models.TaskExecution{
|
||||
TaskType: "cron",
|
||||
TaskID: uint(id),
|
||||
TaskName: job.Name,
|
||||
Status: "running",
|
||||
TargetType: job.TargetType,
|
||||
Targets: job.Targets,
|
||||
StartedAt: &now,
|
||||
}
|
||||
if err := h.db.Create(&execution).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
Code: 500,
|
||||
Message: "创建执行记录失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Execute locally on the AIOps host.
|
||||
output, exitCode, duration := runLocal(scriptType, content, job.Timeout)
|
||||
finishedAt := time.Now()
|
||||
status := "success"
|
||||
if exitCode != 0 {
|
||||
status = "failed"
|
||||
}
|
||||
|
||||
h.db.Model(&execution).Updates(map[string]interface{}{
|
||||
"status": status,
|
||||
"output": output,
|
||||
"duration": duration,
|
||||
"finished_at": finishedAt,
|
||||
})
|
||||
|
||||
// Update cron job statistics.
|
||||
statsUpdates := map[string]interface{}{
|
||||
"last_run_at": now,
|
||||
"last_status": status,
|
||||
"run_count": job.RunCount + 1,
|
||||
}
|
||||
if status == "success" {
|
||||
statsUpdates["success_count"] = job.SuccessCount + 1
|
||||
} else {
|
||||
statsUpdates["fail_count"] = job.FailCount + 1
|
||||
}
|
||||
h.db.Model(&job).Updates(statsUpdates)
|
||||
|
||||
execution.Output = output
|
||||
execution.Status = status
|
||||
execution.Duration = duration
|
||||
execution.FinishedAt = &finishedAt
|
||||
|
||||
msg := "执行成功"
|
||||
if status == "failed" {
|
||||
msg = "执行失败"
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: msg,
|
||||
Data: execution,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user