feat: 合并Gin_Vue
This commit is contained in:
149
src/modules/monitor/controller/sys_cache.go
Normal file
149
src/modules/monitor/controller/sys_cache.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/constants/cachekey"
|
||||
"ems.agt/src/framework/redis"
|
||||
"ems.agt/src/framework/vo/result"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysCacheController 结构体
|
||||
var NewSysCache = &SysCacheController{}
|
||||
|
||||
// 缓存监控信息
|
||||
//
|
||||
// PATH /monitor/cache
|
||||
type SysCacheController struct{}
|
||||
|
||||
// Redis信息
|
||||
//
|
||||
// GET /
|
||||
func (s *SysCacheController) Info(c *gin.Context) {
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"info": redis.Info(""),
|
||||
"dbSize": redis.KeySize(""),
|
||||
"commandStats": redis.CommandStats(""),
|
||||
}))
|
||||
}
|
||||
|
||||
// 缓存名称列表
|
||||
//
|
||||
// GET /getNames
|
||||
func (s *SysCacheController) Names(c *gin.Context) {
|
||||
caches := []model.SysCache{
|
||||
model.NewSysCacheNames("用户信息", cachekey.LOGIN_TOKEN_KEY),
|
||||
model.NewSysCacheNames("配置信息", cachekey.SYS_CONFIG_KEY),
|
||||
model.NewSysCacheNames("数据字典", cachekey.SYS_DICT_KEY),
|
||||
model.NewSysCacheNames("验证码", cachekey.CAPTCHA_CODE_KEY),
|
||||
model.NewSysCacheNames("防重提交", cachekey.REPEAT_SUBMIT_KEY),
|
||||
model.NewSysCacheNames("限流处理", cachekey.RATE_LIMIT_KEY),
|
||||
model.NewSysCacheNames("密码错误次数", cachekey.PWD_ERR_CNT_KEY),
|
||||
}
|
||||
c.JSON(200, result.OkData(caches))
|
||||
}
|
||||
|
||||
// 缓存名称下键名列表
|
||||
//
|
||||
// GET /getKeys/:cacheName
|
||||
func (s *SysCacheController) Keys(c *gin.Context) {
|
||||
cacheName := c.Param("cacheName")
|
||||
if cacheName == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
caches := []model.SysCache{}
|
||||
|
||||
// 遍历组装
|
||||
cacheKeys, _ := redis.GetKeys("", cacheName+":*")
|
||||
for _, key := range cacheKeys {
|
||||
caches = append(caches, model.NewSysCacheKeys(cacheName, key))
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(caches))
|
||||
}
|
||||
|
||||
// 缓存内容
|
||||
//
|
||||
// GET /getValue/:cacheName/:cacheKey
|
||||
func (s *SysCacheController) Value(c *gin.Context) {
|
||||
cacheName := c.Param("cacheName")
|
||||
cacheKey := c.Param("cacheKey")
|
||||
if cacheName == "" || cacheKey == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
cacheValue, err := redis.Get("", cacheName+":"+cacheKey)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
sysCache := model.NewSysCacheValue(cacheName, cacheKey, cacheValue)
|
||||
c.JSON(200, result.OkData(sysCache))
|
||||
}
|
||||
|
||||
// 删除缓存名称下键名列表
|
||||
//
|
||||
// DELETE /clearCacheName/:cacheName
|
||||
func (s *SysCacheController) ClearCacheName(c *gin.Context) {
|
||||
cacheName := c.Param("cacheName")
|
||||
if cacheName == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
cacheKeys, err := redis.GetKeys("", cacheName+":*")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
ok, _ := redis.DelKeys("", cacheKeys)
|
||||
if ok {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 删除缓存键名
|
||||
//
|
||||
// DELETE /clearCacheKey/:cacheName/:cacheKey
|
||||
func (s *SysCacheController) ClearCacheKey(c *gin.Context) {
|
||||
cacheName := c.Param("cacheName")
|
||||
cacheKey := c.Param("cacheKey")
|
||||
if cacheName == "" || cacheKey == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
ok, _ := redis.Del("", cacheName+":"+cacheKey)
|
||||
if ok {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 安全清理缓存名称
|
||||
//
|
||||
// DELETE /clearCacheSafe
|
||||
func (s *SysCacheController) ClearCacheSafe(c *gin.Context) {
|
||||
caches := []model.SysCache{
|
||||
model.NewSysCacheNames("配置信息", cachekey.SYS_CONFIG_KEY),
|
||||
model.NewSysCacheNames("数据字典", cachekey.SYS_DICT_KEY),
|
||||
model.NewSysCacheNames("验证码", cachekey.CAPTCHA_CODE_KEY),
|
||||
model.NewSysCacheNames("防重提交", cachekey.REPEAT_SUBMIT_KEY),
|
||||
model.NewSysCacheNames("限流处理", cachekey.RATE_LIMIT_KEY),
|
||||
model.NewSysCacheNames("密码错误次数", cachekey.PWD_ERR_CNT_KEY),
|
||||
}
|
||||
for _, v := range caches {
|
||||
cacheKeys, err := redis.GetKeys("", v.CacheName+":*")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
redis.DelKeys("", cacheKeys)
|
||||
}
|
||||
c.JSON(200, result.Ok(nil))
|
||||
}
|
||||
338
src/modules/monitor/controller/sys_job.go
Normal file
338
src/modules/monitor/controller/sys_job.go
Normal file
@@ -0,0 +1,338 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/utils/ctx"
|
||||
"ems.agt/src/framework/utils/file"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/vo/result"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/service"
|
||||
systemService "ems.agt/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysJobLogController 结构体
|
||||
var NewSysJob = &SysJobController{
|
||||
sysJobService: service.NewSysJobImpl,
|
||||
sysDictDataService: systemService.NewSysDictDataImpl,
|
||||
}
|
||||
|
||||
// 调度任务信息
|
||||
//
|
||||
// PATH /monitor/job
|
||||
type SysJobController struct {
|
||||
// 调度任务服务
|
||||
sysJobService service.ISysJob
|
||||
// 字典数据服务
|
||||
sysDictDataService systemService.ISysDictData
|
||||
}
|
||||
|
||||
// 调度任务列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *SysJobController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.sysJobService.SelectJobPage(querys)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 调度任务信息
|
||||
//
|
||||
// GET /:jobId
|
||||
func (s *SysJobController) Info(c *gin.Context) {
|
||||
jobId := c.Param("jobId")
|
||||
if jobId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.sysJobService.SelectJobById(jobId)
|
||||
if data.JobID == jobId {
|
||||
c.JSON(200, result.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysJobController) Add(c *gin.Context) {
|
||||
var body model.SysJob
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.JobID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查cron表达式格式
|
||||
if parse.CronExpression(body.CronExpression) == 0 {
|
||||
msg := fmt.Sprintf("调度任务新增【%s】失败,Cron表达式不正确", body.JobName)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查任务调用传入参数是否json格式
|
||||
if body.TargetParams != "" {
|
||||
msg := fmt.Sprintf("调度任务新增【%s】失败,任务传入参数json字符串不正确", body.JobName)
|
||||
if len(body.TargetParams) < 7 {
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
if !json.Valid([]byte(body.TargetParams)) {
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, "")
|
||||
if !uniqueJob {
|
||||
msg := fmt.Sprintf("调度任务新增【%s】失败,同任务组内有相同任务名称", body.JobName)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysJobService.InsertJob(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysJobController) Edit(c *gin.Context) {
|
||||
var body model.SysJob
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.JobID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查cron表达式格式
|
||||
if parse.CronExpression(body.CronExpression) == 0 {
|
||||
msg := fmt.Sprintf("调度任务修改【%s】失败,Cron表达式不正确", body.JobName)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查任务调用传入参数是否json格式
|
||||
if body.TargetParams != "" {
|
||||
msg := fmt.Sprintf("调度任务修改【%s】失败,任务传入参数json字符串不正确", body.JobName)
|
||||
if len(body.TargetParams) < 7 {
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
if !json.Valid([]byte(body.TargetParams)) {
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, body.JobID)
|
||||
if !uniqueJob {
|
||||
msg := fmt.Sprintf("调度任务修改【%s】失败,同任务组内有相同任务名称", body.JobName)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysJobService.UpdateJob(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务删除
|
||||
//
|
||||
// DELETE /:jobIds
|
||||
func (s *SysJobController) Remove(c *gin.Context) {
|
||||
jobIds := c.Param("jobIds")
|
||||
if jobIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(jobIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.sysJobService.DeleteJobByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 调度任务修改状态
|
||||
//
|
||||
// PUT /changeStatus
|
||||
func (s *SysJobController) Status(c *gin.Context) {
|
||||
var body struct {
|
||||
// 任务ID
|
||||
JobId string `json:"jobId" binding:"required"`
|
||||
// 状态
|
||||
Status string `json:"status" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
job := s.sysJobService.SelectJobById(body.JobId)
|
||||
if job.JobID != body.JobId {
|
||||
c.JSON(200, result.ErrMsg("没有权限访问调度任务数据!"))
|
||||
return
|
||||
}
|
||||
|
||||
// 与旧值相等不变更
|
||||
if job.Status == body.Status {
|
||||
c.JSON(200, result.ErrMsg("变更状态与旧值相等!"))
|
||||
return
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
job.Status = body.Status
|
||||
job.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
ok := s.sysJobService.ChangeStatus(job)
|
||||
if ok {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务立即执行一次
|
||||
//
|
||||
// PUT /run/:jobId
|
||||
func (s *SysJobController) Run(c *gin.Context) {
|
||||
jobId := c.Param("jobId")
|
||||
if jobId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
job := s.sysJobService.SelectJobById(jobId)
|
||||
if job.JobID != jobId {
|
||||
c.JSON(200, result.ErrMsg("没有权限访问调度任务数据!"))
|
||||
return
|
||||
}
|
||||
|
||||
ok := s.sysJobService.RunQueueJob(job)
|
||||
if ok {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务重置刷新队列
|
||||
//
|
||||
// PUT /resetQueueJob
|
||||
func (s *SysJobController) ResetQueueJob(c *gin.Context) {
|
||||
s.sysJobService.ResetQueueJob()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
}
|
||||
|
||||
// 导出调度任务信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysJobController) Export(c *gin.Context) {
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
data := s.sysJobService.SelectJobPage(querys)
|
||||
if data["total"].(int64) == 0 {
|
||||
c.JSON(200, result.ErrMsg("导出数据记录为空"))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysJob)
|
||||
|
||||
// 导出文件名称
|
||||
fileName := fmt.Sprintf("job_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||||
// 第一行表头标题
|
||||
headerCells := map[string]string{
|
||||
"A1": "任务编号",
|
||||
"B1": "任务名称",
|
||||
"C1": "任务组名",
|
||||
"D1": "调用目标",
|
||||
"E1": "传入参数",
|
||||
"F1": "执行表达式",
|
||||
"G1": "出错策略",
|
||||
"H1": "并发执行",
|
||||
"I1": "任务状态",
|
||||
"J1": "备注说明",
|
||||
}
|
||||
// 读取任务组名字典数据
|
||||
dictSysJobGroup := s.sysDictDataService.SelectDictDataByType("sys_job_group")
|
||||
// 从第二行开始的数据
|
||||
dataCells := make([]map[string]any, 0)
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
// 任务组名
|
||||
sysJobGroup := ""
|
||||
for _, v := range dictSysJobGroup {
|
||||
if row.JobGroup == v.DictValue {
|
||||
sysJobGroup = v.DictLabel
|
||||
break
|
||||
}
|
||||
}
|
||||
misfirePolicy := "放弃执行"
|
||||
if row.MisfirePolicy == "1" {
|
||||
misfirePolicy = "立即执行"
|
||||
} else if row.MisfirePolicy == "2" {
|
||||
misfirePolicy = "执行一次"
|
||||
}
|
||||
concurrent := "禁止"
|
||||
if row.Concurrent == "1" {
|
||||
concurrent = "允许"
|
||||
}
|
||||
// 状态
|
||||
statusValue := "失败"
|
||||
if row.Status == "1" {
|
||||
statusValue = "成功"
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.JobID,
|
||||
"B" + idx: row.JobName,
|
||||
"C" + idx: sysJobGroup,
|
||||
"D" + idx: row.InvokeTarget,
|
||||
"E" + idx: row.TargetParams,
|
||||
"F" + idx: row.CronExpression,
|
||||
"G" + idx: misfirePolicy,
|
||||
"H" + idx: concurrent,
|
||||
"I" + idx: statusValue,
|
||||
"J" + idx: row.Remark,
|
||||
})
|
||||
}
|
||||
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.FileAttachment(saveFilePath, fileName)
|
||||
}
|
||||
167
src/modules/monitor/controller/sys_job_log.go
Normal file
167
src/modules/monitor/controller/sys_job_log.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/utils/ctx"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/file"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/vo/result"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/service"
|
||||
systemService "ems.agt/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysJobLogController 结构体
|
||||
var NewSysJobLog = &SysJobLogController{
|
||||
sysJobLogService: service.NewSysJobLogImpl,
|
||||
sysDictDataService: systemService.NewSysDictDataImpl,
|
||||
}
|
||||
|
||||
// 调度任务日志信息
|
||||
//
|
||||
// PATH /monitor/jobLog
|
||||
type SysJobLogController struct {
|
||||
// 调度任务日志服务
|
||||
sysJobLogService service.ISysJobLog
|
||||
// 字典数据服务
|
||||
sysDictDataService systemService.ISysDictData
|
||||
}
|
||||
|
||||
// 调度任务日志列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *SysJobLogController) List(c *gin.Context) {
|
||||
// 查询参数转换map
|
||||
querys := ctx.QueryMap(c)
|
||||
list := s.sysJobLogService.SelectJobLogPage(querys)
|
||||
c.JSON(200, result.Ok(list))
|
||||
}
|
||||
|
||||
// 调度任务日志信息
|
||||
//
|
||||
// GET /:jobLogId
|
||||
func (s *SysJobLogController) Info(c *gin.Context) {
|
||||
jobLogId := c.Param("jobLogId")
|
||||
if jobLogId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
data := s.sysJobLogService.SelectJobLogById(jobLogId)
|
||||
if data.JobLogID == jobLogId {
|
||||
c.JSON(200, result.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务日志删除
|
||||
//
|
||||
// DELETE /:jobLogIds
|
||||
func (s *SysJobLogController) Remove(c *gin.Context) {
|
||||
jobLogIds := c.Param("jobLogIds")
|
||||
if jobLogIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(jobLogIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows := s.sysJobLogService.DeleteJobLogByIds(uniqueIDs)
|
||||
if rows > 0 {
|
||||
msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务日志清空
|
||||
//
|
||||
// DELETE /clean
|
||||
func (s *SysJobLogController) Clean(c *gin.Context) {
|
||||
err := s.sysJobLogService.CleanJobLog()
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Ok(nil))
|
||||
}
|
||||
|
||||
// 导出调度任务日志信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysJobLogController) Export(c *gin.Context) {
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
data := s.sysJobLogService.SelectJobLogPage(querys)
|
||||
if data["total"].(int64) == 0 {
|
||||
c.JSON(200, result.ErrMsg("导出数据记录为空"))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysJobLog)
|
||||
|
||||
// 导出文件名称
|
||||
fileName := fmt.Sprintf("jobLog_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||||
// 第一行表头标题
|
||||
headerCells := map[string]string{
|
||||
"A1": "日志序号",
|
||||
"B1": "任务名称",
|
||||
"C1": "任务组名",
|
||||
"D1": "调用目标",
|
||||
"E1": "传入参数",
|
||||
"F1": "日志信息",
|
||||
"G1": "执行状态",
|
||||
"H1": "记录时间",
|
||||
}
|
||||
// 读取任务组名字典数据
|
||||
dictSysJobGroup := s.sysDictDataService.SelectDictDataByType("sys_job_group")
|
||||
// 从第二行开始的数据
|
||||
dataCells := make([]map[string]any, 0)
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
// 任务组名
|
||||
sysJobGroup := ""
|
||||
for _, v := range dictSysJobGroup {
|
||||
if row.JobGroup == v.DictValue {
|
||||
sysJobGroup = v.DictLabel
|
||||
break
|
||||
}
|
||||
}
|
||||
// 状态
|
||||
statusValue := "失败"
|
||||
if row.Status == "1" {
|
||||
statusValue = "成功"
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.JobLogID,
|
||||
"B" + idx: row.JobName,
|
||||
"C" + idx: sysJobGroup,
|
||||
"D" + idx: row.InvokeTarget,
|
||||
"E" + idx: row.TargetParams,
|
||||
"F" + idx: row.JobMsg,
|
||||
"G" + idx: statusValue,
|
||||
"H" + idx: date.ParseDateToStr(row.CreateTime, date.YYYY_MM_DD_HH_MM_SS),
|
||||
})
|
||||
}
|
||||
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.FileAttachment(saveFilePath, fileName)
|
||||
}
|
||||
126
src/modules/monitor/controller/sys_user_online.go
Normal file
126
src/modules/monitor/controller/sys_user_online.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/constants/cachekey"
|
||||
"ems.agt/src/framework/redis"
|
||||
"ems.agt/src/framework/vo"
|
||||
"ems.agt/src/framework/vo/result"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysUserOnlineController 结构体
|
||||
var NewSysUserOnline = &SysUserOnlineController{
|
||||
sysUserOnlineService: service.NewSysUserOnlineImpl,
|
||||
}
|
||||
|
||||
// 在线用户监控
|
||||
//
|
||||
// PATH /monitor/online
|
||||
type SysUserOnlineController struct {
|
||||
// 在线用户服务
|
||||
sysUserOnlineService service.ISysUserOnline
|
||||
}
|
||||
|
||||
// 在线用户列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *SysUserOnlineController) List(c *gin.Context) {
|
||||
ipaddr := c.Query("ipaddr")
|
||||
userName := c.Query("userName")
|
||||
|
||||
// 获取所有在线用户key
|
||||
keys, _ := redis.GetKeys("", cachekey.LOGIN_TOKEN_KEY+"*")
|
||||
|
||||
// 分批获取
|
||||
arr := make([]string, 0)
|
||||
for i := 0; i < len(keys); i += 20 {
|
||||
end := i + 20
|
||||
if end > len(keys) {
|
||||
end = len(keys)
|
||||
}
|
||||
chunk := keys[i:end]
|
||||
values, _ := redis.GetBatch("", chunk)
|
||||
for _, v := range values {
|
||||
arr = append(arr, v.(string))
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历字符串信息解析组合可用对象
|
||||
userOnlines := make([]model.SysUserOnline, 0)
|
||||
for _, str := range arr {
|
||||
if str == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
var loginUser vo.LoginUser
|
||||
err := json.Unmarshal([]byte(str), &loginUser)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
onlineUser := s.sysUserOnlineService.LoginUserToUserOnline(loginUser)
|
||||
if onlineUser.TokenID != "" {
|
||||
userOnlines = append(userOnlines, onlineUser)
|
||||
}
|
||||
}
|
||||
|
||||
// 根据查询条件过滤
|
||||
filteredUserOnlines := make([]model.SysUserOnline, 0)
|
||||
if ipaddr != "" && userName != "" {
|
||||
for _, o := range userOnlines {
|
||||
if strings.Contains(o.IPAddr, ipaddr) && strings.Contains(o.UserName, userName) {
|
||||
filteredUserOnlines = append(filteredUserOnlines, o)
|
||||
}
|
||||
}
|
||||
} else if ipaddr != "" {
|
||||
for _, o := range userOnlines {
|
||||
if strings.Contains(o.IPAddr, ipaddr) {
|
||||
filteredUserOnlines = append(filteredUserOnlines, o)
|
||||
}
|
||||
}
|
||||
} else if userName != "" {
|
||||
for _, o := range userOnlines {
|
||||
if strings.Contains(o.UserName, userName) {
|
||||
filteredUserOnlines = append(filteredUserOnlines, o)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
filteredUserOnlines = userOnlines
|
||||
}
|
||||
|
||||
// 按登录时间排序
|
||||
sort.Slice(filteredUserOnlines, func(i, j int) bool {
|
||||
return filteredUserOnlines[j].LoginTime > filteredUserOnlines[i].LoginTime
|
||||
})
|
||||
|
||||
c.JSON(200, result.Ok(map[string]any{
|
||||
"total": len(filteredUserOnlines),
|
||||
"rows": filteredUserOnlines,
|
||||
}))
|
||||
}
|
||||
|
||||
// 在线用户强制退出
|
||||
//
|
||||
// DELETE /:tokenId
|
||||
func (s *SysUserOnlineController) ForceLogout(c *gin.Context) {
|
||||
tokenId := c.Param("tokenId")
|
||||
if tokenId == "" || tokenId == "*" {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 删除token
|
||||
ok, _ := redis.Del("", cachekey.LOGIN_TOKEN_KEY+tokenId)
|
||||
if ok {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
36
src/modules/monitor/controller/system_info.go
Normal file
36
src/modules/monitor/controller/system_info.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/vo/result"
|
||||
"ems.agt/src/modules/monitor/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 SystemInfoController 结构体
|
||||
var NewSystemInfo = &SystemInfoController{
|
||||
systemInfogService: service.NewSystemInfoImpl,
|
||||
}
|
||||
|
||||
// 服务器监控信息
|
||||
//
|
||||
// PATH /monitor/system-info
|
||||
type SystemInfoController struct {
|
||||
// 服务器系统相关信息服务
|
||||
systemInfogService service.ISystemInfo
|
||||
}
|
||||
|
||||
// 服务器信息
|
||||
//
|
||||
// GET /
|
||||
func (s *SystemInfoController) Info(c *gin.Context) {
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"project": s.systemInfogService.ProjectInfo(),
|
||||
"cpu": s.systemInfogService.CPUInfo(),
|
||||
"memory": s.systemInfogService.MemoryInfo(),
|
||||
"network": s.systemInfogService.NetworkInfo(),
|
||||
"time": s.systemInfogService.TimeInfo(),
|
||||
"system": s.systemInfogService.SystemInfo(),
|
||||
"disk": s.systemInfogService.DiskInfo(),
|
||||
}))
|
||||
}
|
||||
41
src/modules/monitor/model/sys_cache.go
Normal file
41
src/modules/monitor/model/sys_cache.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package model
|
||||
|
||||
import "strings"
|
||||
|
||||
// SysCache 缓存信息对象
|
||||
type SysCache struct {
|
||||
CacheName string `json:"cacheName"` // 缓存名称
|
||||
CacheKey string `json:"cacheKey"` // 缓存键名
|
||||
CacheValue string `json:"cacheValue"` // 缓存内容
|
||||
Remark string `json:"remark"` // 备注
|
||||
}
|
||||
|
||||
// NewSysCacheNames 创建新的缓存名称列表项实例
|
||||
func NewSysCacheNames(cacheName string, cacheKey string) SysCache {
|
||||
return SysCache{
|
||||
CacheName: cacheKey[:len(cacheKey)-1],
|
||||
CacheKey: "",
|
||||
CacheValue: "",
|
||||
Remark: cacheName,
|
||||
}
|
||||
}
|
||||
|
||||
// NewSysCacheKeys 创建新的缓存键名列表项实例
|
||||
func NewSysCacheKeys(cacheName string, cacheKey string) SysCache {
|
||||
return SysCache{
|
||||
CacheName: cacheName,
|
||||
CacheKey: strings.Replace(cacheKey, cacheName+":", "", 1),
|
||||
CacheValue: "",
|
||||
Remark: "",
|
||||
}
|
||||
}
|
||||
|
||||
// NewSysCacheValue 创建新的缓存键名内容项实例
|
||||
func NewSysCacheValue(cacheName string, cacheKey string, cacheValue string) SysCache {
|
||||
return SysCache{
|
||||
CacheName: cacheName,
|
||||
CacheKey: cacheKey,
|
||||
CacheValue: cacheValue,
|
||||
Remark: "",
|
||||
}
|
||||
}
|
||||
33
src/modules/monitor/model/sys_job.go
Normal file
33
src/modules/monitor/model/sys_job.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
// SysJob 调度任务信息表 sys_job
|
||||
type SysJob struct {
|
||||
// 任务ID
|
||||
JobID string `json:"jobId"`
|
||||
// 任务名称
|
||||
JobName string `json:"jobName" binding:"required"`
|
||||
// 任务组名
|
||||
JobGroup string `json:"jobGroup" binding:"required"`
|
||||
// 调用目标字符串
|
||||
InvokeTarget string `json:"invokeTarget" binding:"required"`
|
||||
// 调用目标传入参数
|
||||
TargetParams string `json:"targetParams"`
|
||||
// cron执行表达式
|
||||
CronExpression string `json:"cronExpression" binding:"required"`
|
||||
// 计划执行错误策略(1立即执行 2执行一次 3放弃执行)
|
||||
MisfirePolicy string `json:"misfirePolicy"`
|
||||
// 是否并发执行(0禁止 1允许)
|
||||
Concurrent string `json:"concurrent"`
|
||||
// 任务状态(0暂停 1正常)
|
||||
Status string `json:"status"`
|
||||
// 创建者
|
||||
CreateBy string `json:"createBy"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// 更新者
|
||||
UpdateBy string `json:"updateBy"`
|
||||
// 更新时间
|
||||
UpdateTime int64 `json:"updateTime"`
|
||||
// 备注
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
23
src/modules/monitor/model/sys_job_log.go
Normal file
23
src/modules/monitor/model/sys_job_log.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
// SysJobLog 定时任务调度日志表 sys_job_log
|
||||
type SysJobLog struct {
|
||||
// 日志序号
|
||||
JobLogID string `json:"jobLogId"`
|
||||
// 任务名称
|
||||
JobName string `json:"jobName"`
|
||||
// 任务组名
|
||||
JobGroup string `json:"jobGroup"`
|
||||
// 调用目标字符串
|
||||
InvokeTarget string `json:"invokeTarget"`
|
||||
// 调用目标传入参数
|
||||
TargetParams string `json:"targetParams"`
|
||||
// 日志信息
|
||||
JobMsg string `json:"jobMsg"`
|
||||
// 执行状态(0失败 1正常)
|
||||
Status string `json:"status"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// 消耗时间(毫秒)
|
||||
CostTime int64 `json:"costTime"`
|
||||
}
|
||||
21
src/modules/monitor/model/sys_user_online.go
Normal file
21
src/modules/monitor/model/sys_user_online.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
// SysUserOnline 当前在线会话对象
|
||||
type SysUserOnline struct {
|
||||
// 会话编号
|
||||
TokenID string `json:"tokenId"`
|
||||
// 部门名称
|
||||
DeptName string `json:"deptName"`
|
||||
// 用户名称
|
||||
UserName string `json:"userName"`
|
||||
// 登录IP地址
|
||||
IPAddr string `json:"ipaddr"`
|
||||
// 登录地址
|
||||
LoginLocation string `json:"loginLocation"`
|
||||
// 浏览器类型
|
||||
Browser string `json:"browser"`
|
||||
// 操作系统
|
||||
OS string `json:"os"`
|
||||
// 登录时间
|
||||
LoginTime int64 `json:"loginTime"`
|
||||
}
|
||||
160
src/modules/monitor/monitor.go
Normal file
160
src/modules/monitor/monitor.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/middleware"
|
||||
"ems.agt/src/framework/middleware/collectlogs"
|
||||
"ems.agt/src/framework/middleware/repeat"
|
||||
"ems.agt/src/modules/monitor/controller"
|
||||
"ems.agt/src/modules/monitor/processor"
|
||||
"ems.agt/src/modules/monitor/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Setup 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> monitor 模块路由")
|
||||
|
||||
// 启动时需要的初始参数
|
||||
InitLoad()
|
||||
|
||||
// 服务器服务信息
|
||||
router.GET("/monitor/system-info",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:system:info"}}),
|
||||
controller.NewSystemInfo.Info,
|
||||
)
|
||||
|
||||
// 缓存服务信息
|
||||
sysCacheGroup := router.Group("/monitor/cache")
|
||||
{
|
||||
sysCacheGroup.GET("",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:info"}}),
|
||||
controller.NewSysCache.Info,
|
||||
)
|
||||
sysCacheGroup.GET("/getNames",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
|
||||
controller.NewSysCache.Names,
|
||||
)
|
||||
sysCacheGroup.GET("/getKeys/:cacheName",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
|
||||
controller.NewSysCache.Keys,
|
||||
)
|
||||
sysCacheGroup.GET("/getValue/:cacheName/:cacheKey",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:query"}}),
|
||||
controller.NewSysCache.Value,
|
||||
)
|
||||
sysCacheGroup.DELETE("/clearCacheName/:cacheName",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
|
||||
controller.NewSysCache.ClearCacheName,
|
||||
)
|
||||
sysCacheGroup.DELETE("/clearCacheKey/:cacheName/:cacheKey",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
|
||||
controller.NewSysCache.ClearCacheKey,
|
||||
)
|
||||
sysCacheGroup.DELETE("/clearCacheSafe",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
|
||||
controller.NewSysCache.ClearCacheSafe,
|
||||
)
|
||||
}
|
||||
|
||||
// 调度任务日志信息
|
||||
sysJobLogGroup := router.Group("/monitor/jobLog")
|
||||
{
|
||||
sysJobLogGroup.GET("/list",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:list"}}),
|
||||
controller.NewSysJobLog.List,
|
||||
)
|
||||
sysJobLogGroup.GET("/:jobLogId",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:query"}}),
|
||||
controller.NewSysJobLog.Info,
|
||||
)
|
||||
sysJobLogGroup.DELETE("/:jobLogIds",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务日志信息", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewSysJobLog.Remove,
|
||||
)
|
||||
sysJobLogGroup.DELETE("/clean",
|
||||
repeat.RepeatSubmit(5),
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务日志信息", collectlogs.BUSINESS_TYPE_CLEAN)),
|
||||
controller.NewSysJobLog.Clean,
|
||||
)
|
||||
sysJobLogGroup.POST("/export",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:export"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务日志信息", collectlogs.BUSINESS_TYPE_EXPORT)),
|
||||
controller.NewSysJobLog.Export,
|
||||
)
|
||||
}
|
||||
|
||||
// 调度任务信息
|
||||
sysJobGroup := router.Group("/monitor/job")
|
||||
{
|
||||
sysJobGroup.GET("/list",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:list"}}),
|
||||
controller.NewSysJob.List,
|
||||
)
|
||||
sysJobGroup.GET("/:jobId",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:query"}}),
|
||||
controller.NewSysJob.Info,
|
||||
)
|
||||
sysJobGroup.POST("",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:add"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_INSERT)),
|
||||
controller.NewSysJob.Add,
|
||||
)
|
||||
sysJobGroup.PUT("",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:edit"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewSysJob.Edit,
|
||||
)
|
||||
sysJobGroup.DELETE("/:jobIds",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewSysJob.Remove,
|
||||
)
|
||||
sysJobGroup.PUT("/changeStatus",
|
||||
repeat.RepeatSubmit(5),
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewSysJob.Status,
|
||||
)
|
||||
sysJobGroup.PUT("/run/:jobId",
|
||||
repeat.RepeatSubmit(10),
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewSysJob.Run,
|
||||
)
|
||||
sysJobGroup.PUT("/resetQueueJob",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_CLEAN)),
|
||||
controller.NewSysJob.ResetQueueJob,
|
||||
)
|
||||
sysJobGroup.POST("/export",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:export"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("调度任务信息", collectlogs.BUSINESS_TYPE_EXPORT)),
|
||||
controller.NewSysJob.Export,
|
||||
)
|
||||
}
|
||||
|
||||
// 在线用户监控
|
||||
sysUserOnlineGroup := router.Group("/monitor/online")
|
||||
{
|
||||
sysUserOnlineGroup.GET("/list",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:online:list"}}),
|
||||
controller.NewSysUserOnline.List,
|
||||
)
|
||||
sysUserOnlineGroup.DELETE("/:tokenId",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:online:forceLogout"}}),
|
||||
controller.NewSysUserOnline.ForceLogout,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// InitLoad 初始参数
|
||||
func InitLoad() {
|
||||
// 初始化定时任务处理
|
||||
processor.InitCronQueue()
|
||||
// 启动时,初始化调度任务
|
||||
service.NewSysJobImpl.ResetQueueJob()
|
||||
}
|
||||
60
src/modules/monitor/processor/bar/bar.go
Normal file
60
src/modules/monitor/processor/bar/bar.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package bar
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/framework/logger"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) any {
|
||||
logger.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// 实现任务处理逻辑
|
||||
i := 0
|
||||
s.progress = i
|
||||
for i < 5 {
|
||||
// 获取任务进度
|
||||
progress := s.progress
|
||||
logger.Infof("jonId: %s => 任务进度:%d", sysJob.JobID, progress)
|
||||
// 延迟响应
|
||||
time.Sleep(time.Second * 2)
|
||||
// 程序中途执行错误
|
||||
if i == 3 {
|
||||
// arr := [1]int{1}
|
||||
// arr[i] = 3
|
||||
// fmt.Println(arr)
|
||||
// return "i = 3"
|
||||
panic("程序中途执行错误")
|
||||
}
|
||||
i++
|
||||
// 改变任务进度
|
||||
s.progress = i
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
}
|
||||
52
src/modules/monitor/processor/foo/foo.go
Normal file
52
src/modules/monitor/processor/foo/foo.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package foo
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/framework/logger"
|
||||
)
|
||||
|
||||
var NewProcessor = &FooProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// foo 队列任务处理
|
||||
type FooProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
func (s *FooProcessor) Execute(data any) any {
|
||||
logger.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// 实现任务处理逻辑
|
||||
i := 0
|
||||
s.progress = i
|
||||
for i < 20 {
|
||||
// 获取任务进度
|
||||
progress := s.progress
|
||||
logger.Infof("jonId: %s => 任务进度:%d", sysJob.JobID, progress)
|
||||
// 延迟响应
|
||||
time.Sleep(time.Second * 2)
|
||||
i++
|
||||
// 改变任务进度
|
||||
s.progress = i
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
}
|
||||
15
src/modules/monitor/processor/processor.go
Normal file
15
src/modules/monitor/processor/processor.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/modules/monitor/processor/bar"
|
||||
"ems.agt/src/modules/monitor/processor/foo"
|
||||
"ems.agt/src/modules/monitor/processor/simple"
|
||||
)
|
||||
|
||||
// InitCronQueue 初始定时任务队列
|
||||
func InitCronQueue() {
|
||||
cron.CreateQueue("simple", simple.NewProcessor)
|
||||
cron.CreateQueue("foo", foo.NewProcessor)
|
||||
cron.CreateQueue("bar", bar.NewProcessor)
|
||||
}
|
||||
26
src/modules/monitor/processor/simple/simple.go
Normal file
26
src/modules/monitor/processor/simple/simple.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package simple
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/framework/logger"
|
||||
)
|
||||
|
||||
var NewProcessor = &simpleProcessor{}
|
||||
|
||||
// simple 队列任务处理
|
||||
type simpleProcessor struct{}
|
||||
|
||||
func (s *simpleProcessor) Execute(data any) any {
|
||||
options := data.(cron.JobData)
|
||||
|
||||
sysJob := options.SysJob
|
||||
logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
}
|
||||
29
src/modules/monitor/repository/sys_job.go
Normal file
29
src/modules/monitor/repository/sys_job.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysJob 调度任务表 数据层接口
|
||||
type ISysJob interface {
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
SelectJobPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
SelectJobList(sysJob model.SysJob) []model.SysJob
|
||||
|
||||
// SelectJobByIds 通过调度ID查询调度任务信息
|
||||
SelectJobByIds(jobIds []string) []model.SysJob
|
||||
|
||||
// CheckUniqueJob 校验调度任务是否唯一
|
||||
CheckUniqueJob(sysJob model.SysJob) string
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
InsertJob(sysJob model.SysJob) string
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
UpdateJob(sysJob model.SysJob) int64
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
DeleteJobByIds(jobIds []string) int64
|
||||
}
|
||||
344
src/modules/monitor/repository/sys_job.impl.go
Normal file
344
src/modules/monitor/repository/sys_job.impl.go
Normal file
@@ -0,0 +1,344 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysJobImpl 结构体
|
||||
var NewSysJobImpl = &SysJobImpl{
|
||||
selectSql: `select job_id, job_name, job_group, invoke_target, target_params, cron_expression,
|
||||
misfire_policy, concurrent, status, create_by, create_time, remark from sys_job`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"job_id": "JobID",
|
||||
"job_name": "JobName",
|
||||
"job_group": "JobGroup",
|
||||
"invoke_target": "InvokeTarget",
|
||||
"target_params": "TargetParams",
|
||||
"cron_expression": "CronExpression",
|
||||
"misfire_policy": "MisfirePolicy",
|
||||
"concurrent": "Concurrent",
|
||||
"status": "Status",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
}
|
||||
|
||||
// SysJobImpl 调度任务表 数据层处理
|
||||
type SysJobImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysJobImpl) convertResultRows(rows []map[string]any) []model.SysJob {
|
||||
arr := make([]model.SysJob, 0)
|
||||
for _, row := range rows {
|
||||
sysJob := model.SysJob{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysJob, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysJob)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["jobName"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["jobGroup"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["invokeTarget"]; ok && v != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysJob{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_job"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobList(sysJob model.SysJob) []model.SysJob {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysJob.JobName != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, sysJob.JobName)
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, sysJob.JobGroup)
|
||||
}
|
||||
if sysJob.InvokeTarget != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, sysJob.InvokeTarget)
|
||||
}
|
||||
if sysJob.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysJob.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysJob{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectJobByIds 通过调度ID查询调度任务信息
|
||||
func (r *SysJobImpl) SelectJobByIds(jobIds []string) []model.SysJob {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(jobIds))
|
||||
querySql := r.selectSql + " where job_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(jobIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysJob{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueJob 校验调度任务是否唯一
|
||||
func (r *SysJobImpl) CheckUniqueJob(sysJob model.SysJob) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysJob.JobName != "" {
|
||||
conditions = append(conditions, "job_name = ?")
|
||||
params = append(params, sysJob.JobName)
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, sysJob.JobGroup)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select job_id as 'str' from sys_job " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
func (r *SysJobImpl) InsertJob(sysJob model.SysJob) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysJob.JobID != "" {
|
||||
params["job_id"] = sysJob.JobID
|
||||
}
|
||||
if sysJob.JobName != "" {
|
||||
params["job_name"] = sysJob.JobName
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
params["job_group"] = sysJob.JobGroup
|
||||
}
|
||||
if sysJob.InvokeTarget != "" {
|
||||
params["invoke_target"] = sysJob.InvokeTarget
|
||||
}
|
||||
if sysJob.TargetParams != "" {
|
||||
params["target_params"] = sysJob.TargetParams
|
||||
}
|
||||
if sysJob.CronExpression != "" {
|
||||
params["cron_expression"] = sysJob.CronExpression
|
||||
}
|
||||
if sysJob.MisfirePolicy != "" {
|
||||
params["misfire_policy"] = sysJob.MisfirePolicy
|
||||
}
|
||||
if sysJob.Concurrent != "" {
|
||||
params["concurrent"] = sysJob.Concurrent
|
||||
}
|
||||
if sysJob.Status != "" {
|
||||
params["status"] = sysJob.Status
|
||||
}
|
||||
if sysJob.Remark != "" {
|
||||
params["remark"] = sysJob.Remark
|
||||
}
|
||||
if sysJob.CreateBy != "" {
|
||||
params["create_by"] = sysJob.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_job (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
func (r *SysJobImpl) UpdateJob(sysJob model.SysJob) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysJob.JobName != "" {
|
||||
params["job_name"] = sysJob.JobName
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
params["job_group"] = sysJob.JobGroup
|
||||
}
|
||||
if sysJob.InvokeTarget != "" {
|
||||
params["invoke_target"] = sysJob.InvokeTarget
|
||||
}
|
||||
if sysJob.TargetParams != "" {
|
||||
params["target_params"] = sysJob.TargetParams
|
||||
}
|
||||
if sysJob.CronExpression != "" {
|
||||
params["cron_expression"] = sysJob.CronExpression
|
||||
}
|
||||
if sysJob.MisfirePolicy != "" {
|
||||
params["misfire_policy"] = sysJob.MisfirePolicy
|
||||
}
|
||||
if sysJob.Concurrent != "" {
|
||||
params["concurrent"] = sysJob.Concurrent
|
||||
}
|
||||
if sysJob.Status != "" {
|
||||
params["status"] = sysJob.Status
|
||||
}
|
||||
if sysJob.Remark != "" {
|
||||
params["remark"] = sysJob.Remark
|
||||
}
|
||||
if sysJob.UpdateBy != "" {
|
||||
params["update_by"] = sysJob.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_job set " + strings.Join(keys, ",") + " where job_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysJob.JobID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
func (r *SysJobImpl) DeleteJobByIds(jobIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(jobIds))
|
||||
sql := "delete from sys_job where job_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(jobIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
26
src/modules/monitor/repository/sys_job_log.go
Normal file
26
src/modules/monitor/repository/sys_job_log.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 调度任务日志表 数据层接口
|
||||
type ISysJobLog interface {
|
||||
// 分页查询调度任务日志集合
|
||||
SelectJobLogPage(query map[string]any) map[string]any
|
||||
|
||||
// 查询调度任务日志集合
|
||||
SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog
|
||||
|
||||
// 通过调度ID查询调度任务日志信息
|
||||
SelectJobLogById(jobLogId string) model.SysJobLog
|
||||
|
||||
// 新增调度任务日志信息
|
||||
InsertJobLog(sysJobLog model.SysJobLog) string
|
||||
|
||||
// 批量删除调度任务日志信息
|
||||
DeleteJobLogByIds(jobLogId []string) int64
|
||||
|
||||
// 清空调度任务日志
|
||||
CleanJobLog() error
|
||||
}
|
||||
272
src/modules/monitor/repository/sys_job_log.impl.go
Normal file
272
src/modules/monitor/repository/sys_job_log.impl.go
Normal file
@@ -0,0 +1,272 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysJobLogImpl 结构体
|
||||
var NewSysJobLogImpl = &SysJobLogImpl{
|
||||
selectSql: `select job_log_id, job_name, job_group, invoke_target,
|
||||
target_params, job_msg, status, create_time, cost_time from sys_job_log`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"job_log_id": "JobLogID",
|
||||
"job_name": "JobName",
|
||||
"job_group": "JobGroup",
|
||||
"invoke_target": "InvokeTarget",
|
||||
"target_params": "TargetParams",
|
||||
"job_msg": "JobMsg",
|
||||
"status": "Status",
|
||||
"create_time": "CreateTime",
|
||||
"cost_time": "CostTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysJobLogImpl 调度任务日志表 数据层处理
|
||||
type SysJobLogImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysJobLogImpl) convertResultRows(rows []map[string]any) []model.SysJobLog {
|
||||
arr := make([]model.SysJobLog, 0)
|
||||
for _, row := range rows {
|
||||
sysJobLog := model.SysJobLog{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysJobLog, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysJobLog)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// 分页查询调度任务日志集合
|
||||
func (r *SysJobLogImpl) SelectJobLogPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["jobName"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["jobGroup"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["invokeTarget"]; ok && v != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysJobLog{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_job_log"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by job_log_id desc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// 查询调度任务日志集合
|
||||
func (r *SysJobLogImpl) SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysJobLog.JobName != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, sysJobLog.JobName)
|
||||
}
|
||||
if sysJobLog.JobGroup != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, sysJobLog.JobGroup)
|
||||
}
|
||||
if sysJobLog.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysJobLog.Status)
|
||||
}
|
||||
if sysJobLog.InvokeTarget != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, sysJobLog.InvokeTarget)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysJobLog{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// 通过调度ID查询调度任务日志信息
|
||||
func (r *SysJobLogImpl) SelectJobLogById(jobLogId string) model.SysJobLog {
|
||||
querySql := r.selectSql + " where job_log_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{jobLogId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.SysJobLog{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysJobLog{}
|
||||
}
|
||||
|
||||
// 新增调度任务日志信息
|
||||
func (r *SysJobLogImpl) InsertJobLog(sysJobLog model.SysJobLog) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
if sysJobLog.JobLogID != "" {
|
||||
params["job_log_id"] = sysJobLog.JobLogID
|
||||
}
|
||||
if sysJobLog.JobName != "" {
|
||||
params["job_name"] = sysJobLog.JobName
|
||||
}
|
||||
if sysJobLog.JobGroup != "" {
|
||||
params["job_group"] = sysJobLog.JobGroup
|
||||
}
|
||||
if sysJobLog.InvokeTarget != "" {
|
||||
params["invoke_target"] = sysJobLog.InvokeTarget
|
||||
}
|
||||
if sysJobLog.TargetParams != "" {
|
||||
params["target_params"] = sysJobLog.TargetParams
|
||||
}
|
||||
if sysJobLog.JobMsg != "" {
|
||||
params["job_msg"] = sysJobLog.JobMsg
|
||||
}
|
||||
if sysJobLog.Status != "" {
|
||||
params["status"] = sysJobLog.Status
|
||||
}
|
||||
if sysJobLog.CostTime > 0 {
|
||||
params["cost_time"] = sysJobLog.CostTime
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_job_log (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// 批量删除调度任务日志信息
|
||||
func (r *SysJobLogImpl) DeleteJobLogByIds(jobLogIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(jobLogIds))
|
||||
sql := "delete from sys_job_log where job_log_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(jobLogIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// 清空调度任务日志
|
||||
func (r *SysJobLogImpl) CleanJobLog() error {
|
||||
sql := "truncate table sys_job_log"
|
||||
_, err := datasource.ExecDB("", sql, []any{})
|
||||
return err
|
||||
}
|
||||
38
src/modules/monitor/service/sys_job.go
Normal file
38
src/modules/monitor/service/sys_job.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysJob 调度任务信息 服务层接口
|
||||
type ISysJob interface {
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
SelectJobPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
SelectJobList(sysJob model.SysJob) []model.SysJob
|
||||
|
||||
// SelectJobById 通过调度ID查询调度任务信息
|
||||
SelectJobById(jobId string) model.SysJob
|
||||
|
||||
// CheckUniqueJobName 校验调度任务名称和组是否唯一
|
||||
CheckUniqueJobName(jobName, jobGroup, jobId string) bool
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
InsertJob(sysJob model.SysJob) string
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
UpdateJob(sysJob model.SysJob) int64
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
DeleteJobByIds(jobIds []string) (int64, error)
|
||||
|
||||
// ChangeStatus 任务调度状态修改
|
||||
ChangeStatus(sysJob model.SysJob) bool
|
||||
|
||||
// RunQueueJob 立即运行一次调度任务
|
||||
RunQueueJob(sysJob model.SysJob) bool
|
||||
|
||||
// ResetQueueJob 重置初始调度任务
|
||||
ResetQueueJob()
|
||||
}
|
||||
190
src/modules/monitor/service/sys_job.impl.go
Normal file
190
src/modules/monitor/service/sys_job.impl.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"ems.agt/src/framework/constants/common"
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 SysJobImpl 结构体
|
||||
var NewSysJobImpl = &SysJobImpl{
|
||||
sysJobRepository: repository.NewSysJobImpl,
|
||||
}
|
||||
|
||||
// SysJobImpl 调度任务 服务层处理
|
||||
type SysJobImpl struct {
|
||||
// 调度任务数据信息
|
||||
sysJobRepository repository.ISysJob
|
||||
}
|
||||
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobPage(query map[string]any) map[string]any {
|
||||
return r.sysJobRepository.SelectJobPage(query)
|
||||
}
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobList(sysJob model.SysJob) []model.SysJob {
|
||||
return r.sysJobRepository.SelectJobList(sysJob)
|
||||
}
|
||||
|
||||
// SelectJobById 通过调度ID查询调度任务信息
|
||||
func (r *SysJobImpl) SelectJobById(jobId string) model.SysJob {
|
||||
if jobId == "" {
|
||||
return model.SysJob{}
|
||||
}
|
||||
jobs := r.sysJobRepository.SelectJobByIds([]string{jobId})
|
||||
if len(jobs) > 0 {
|
||||
return jobs[0]
|
||||
}
|
||||
return model.SysJob{}
|
||||
}
|
||||
|
||||
// CheckUniqueJobName 校验调度任务名称和组是否唯一
|
||||
func (r *SysJobImpl) CheckUniqueJobName(jobName, jobGroup, jobId string) bool {
|
||||
uniqueId := r.sysJobRepository.CheckUniqueJob(model.SysJob{
|
||||
JobName: jobName,
|
||||
JobGroup: jobGroup,
|
||||
})
|
||||
if uniqueId == jobId {
|
||||
return true
|
||||
}
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
func (r *SysJobImpl) InsertJob(sysJob model.SysJob) string {
|
||||
insertId := r.sysJobRepository.InsertJob(sysJob)
|
||||
if insertId == "" && sysJob.Status == common.STATUS_YES {
|
||||
sysJob.JobID = insertId
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
return insertId
|
||||
}
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
func (r *SysJobImpl) UpdateJob(sysJob model.SysJob) int64 {
|
||||
rows := r.sysJobRepository.UpdateJob(sysJob)
|
||||
if rows > 0 {
|
||||
//状态正常添加队列任务
|
||||
if sysJob.Status == common.STATUS_YES {
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
// 状态禁用删除队列任务
|
||||
if sysJob.Status == common.STATUS_NO {
|
||||
r.deleteQueueJob(sysJob)
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
func (r *SysJobImpl) DeleteJobByIds(jobIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
jobs := r.sysJobRepository.SelectJobByIds(jobIds)
|
||||
if len(jobs) <= 0 {
|
||||
return 0, errors.New("没有权限访问调度任务数据!")
|
||||
}
|
||||
if len(jobs) == len(jobIds) {
|
||||
// 清除任务
|
||||
for _, job := range jobs {
|
||||
r.deleteQueueJob(job)
|
||||
}
|
||||
rows := r.sysJobRepository.DeleteJobByIds(jobIds)
|
||||
return rows, nil
|
||||
}
|
||||
return 0, errors.New("删除调度任务信息失败!")
|
||||
}
|
||||
|
||||
// ChangeStatus 任务调度状态修改
|
||||
func (r *SysJobImpl) ChangeStatus(sysJob model.SysJob) bool {
|
||||
// 更新状态
|
||||
newSysJob := model.SysJob{
|
||||
JobID: sysJob.JobID,
|
||||
Status: sysJob.Status,
|
||||
UpdateBy: sysJob.UpdateBy,
|
||||
}
|
||||
rows := r.sysJobRepository.UpdateJob(newSysJob)
|
||||
if rows > 0 {
|
||||
//状态正常添加队列任务
|
||||
if sysJob.Status == common.STATUS_YES {
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
// 状态禁用删除队列任务
|
||||
if sysJob.Status == common.STATUS_NO {
|
||||
r.deleteQueueJob(sysJob)
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ResetQueueJob 重置初始调度任务
|
||||
func (r *SysJobImpl) ResetQueueJob() {
|
||||
// 获取注册的队列名称
|
||||
queueNames := cron.QueueNames()
|
||||
if len(queueNames) == 0 {
|
||||
return
|
||||
}
|
||||
// 查询系统中定义状态为正常启用的任务
|
||||
sysJobs := r.sysJobRepository.SelectJobList(model.SysJob{
|
||||
Status: common.STATUS_YES,
|
||||
})
|
||||
for _, sysJob := range sysJobs {
|
||||
for _, name := range queueNames {
|
||||
if name == sysJob.InvokeTarget {
|
||||
r.insertQueueJob(sysJob, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunQueueJob 立即运行一次调度任务
|
||||
func (r *SysJobImpl) RunQueueJob(sysJob model.SysJob) bool {
|
||||
return r.insertQueueJob(sysJob, false)
|
||||
}
|
||||
|
||||
// insertQueueJob 添加调度任务
|
||||
func (r *SysJobImpl) insertQueueJob(sysJob model.SysJob, repeat bool) bool {
|
||||
// 获取队列 Processor
|
||||
queue := cron.GetQueue(sysJob.InvokeTarget)
|
||||
if queue.Name != sysJob.InvokeTarget {
|
||||
return false
|
||||
}
|
||||
|
||||
// 给执行任务数据参数
|
||||
options := cron.JobData{
|
||||
Repeat: repeat,
|
||||
SysJob: sysJob,
|
||||
}
|
||||
|
||||
// 不是重复任务的情况,立即执行一次
|
||||
if !repeat {
|
||||
// 执行单次任务
|
||||
status := queue.RunJob(options, cron.JobOptions{
|
||||
JobId: sysJob.JobID,
|
||||
})
|
||||
// 执行中或等待中的都返回正常
|
||||
return status == cron.Active || status == cron.Waiting
|
||||
}
|
||||
|
||||
// 执行重复任务
|
||||
queue.RunJob(options, cron.JobOptions{
|
||||
JobId: sysJob.JobID,
|
||||
Cron: sysJob.CronExpression,
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// deleteQueueJob 删除调度任务
|
||||
func (r *SysJobImpl) deleteQueueJob(sysJob model.SysJob) bool {
|
||||
// 获取队列 Processor
|
||||
queue := cron.GetQueue(sysJob.InvokeTarget)
|
||||
if queue.Name != sysJob.InvokeTarget {
|
||||
return false
|
||||
}
|
||||
return queue.RemoveJob(sysJob.JobID)
|
||||
}
|
||||
23
src/modules/monitor/service/sys_job_log.go
Normal file
23
src/modules/monitor/service/sys_job_log.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysJobLog 调度任务日志 服务层接口
|
||||
type ISysJobLog interface {
|
||||
// SelectJobLogPage 分页查询调度任务日志集合
|
||||
SelectJobLogPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectJobLogList 查询调度任务日志集合
|
||||
SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog
|
||||
|
||||
// SelectJobLogById 通过调度ID查询调度任务日志信息
|
||||
SelectJobLogById(jobLogId string) model.SysJobLog
|
||||
|
||||
// DeleteJobLogByIds 批量删除调度任务日志信息
|
||||
DeleteJobLogByIds(jobLogIds []string) int64
|
||||
|
||||
// CleanJobLog 清空调度任务日志
|
||||
CleanJobLog() error
|
||||
}
|
||||
42
src/modules/monitor/service/sys_job_log.impl.go
Normal file
42
src/modules/monitor/service/sys_job_log.impl.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
"ems.agt/src/modules/monitor/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 SysJobLogImpl 结构体
|
||||
var NewSysJobLogImpl = &SysJobLogImpl{
|
||||
sysJobLogRepository: repository.NewSysJobLogImpl,
|
||||
}
|
||||
|
||||
// SysJobLogImpl 调度任务日志 服务层处理
|
||||
type SysJobLogImpl struct {
|
||||
// 调度任务日志数据信息
|
||||
sysJobLogRepository repository.ISysJobLog
|
||||
}
|
||||
|
||||
// SelectJobLogPage 分页查询调度任务日志集合
|
||||
func (s *SysJobLogImpl) SelectJobLogPage(query map[string]any) map[string]any {
|
||||
return s.sysJobLogRepository.SelectJobLogPage(query)
|
||||
}
|
||||
|
||||
// SelectJobLogList 查询调度任务日志集合
|
||||
func (s *SysJobLogImpl) SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog {
|
||||
return s.sysJobLogRepository.SelectJobLogList(sysJobLog)
|
||||
}
|
||||
|
||||
// SelectJobLogById 通过调度ID查询调度任务日志信息
|
||||
func (s *SysJobLogImpl) SelectJobLogById(jobLogId string) model.SysJobLog {
|
||||
return s.sysJobLogRepository.SelectJobLogById(jobLogId)
|
||||
}
|
||||
|
||||
// DeleteJobLogByIds 批量删除调度任务日志信息
|
||||
func (s *SysJobLogImpl) DeleteJobLogByIds(jobLogIds []string) int64 {
|
||||
return s.sysJobLogRepository.DeleteJobLogByIds(jobLogIds)
|
||||
}
|
||||
|
||||
// CleanJobLog 清空调度任务日志
|
||||
func (s *SysJobLogImpl) CleanJobLog() error {
|
||||
return s.sysJobLogRepository.CleanJobLog()
|
||||
}
|
||||
12
src/modules/monitor/service/sys_user_online.go
Normal file
12
src/modules/monitor/service/sys_user_online.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/vo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysUserOnline 在线用户 服务层接口
|
||||
type ISysUserOnline interface {
|
||||
// LoginUserToUserOnline 设置在线用户信息
|
||||
LoginUserToUserOnline(loginUser vo.LoginUser) model.SysUserOnline
|
||||
}
|
||||
33
src/modules/monitor/service/sys_user_online.impl.go
Normal file
33
src/modules/monitor/service/sys_user_online.impl.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/vo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 实例化服务层 SysUserOnlineImpl 结构体
|
||||
var NewSysUserOnlineImpl = &SysUserOnlineImpl{}
|
||||
|
||||
// SysUserOnlineImpl 在线用户 服务层处理
|
||||
type SysUserOnlineImpl struct{}
|
||||
|
||||
// LoginUserToUserOnline 设置在线用户信息
|
||||
func (r *SysUserOnlineImpl) LoginUserToUserOnline(loginUser vo.LoginUser) model.SysUserOnline {
|
||||
if loginUser.UserID == "" {
|
||||
return model.SysUserOnline{}
|
||||
}
|
||||
|
||||
sysUserOnline := model.SysUserOnline{
|
||||
TokenID: loginUser.UUID,
|
||||
UserName: loginUser.User.UserName,
|
||||
IPAddr: loginUser.IPAddr,
|
||||
LoginLocation: loginUser.LoginLocation,
|
||||
Browser: loginUser.Browser,
|
||||
OS: loginUser.OS,
|
||||
LoginTime: loginUser.LoginTime,
|
||||
}
|
||||
if loginUser.User.DeptID != "" {
|
||||
sysUserOnline.DeptName = loginUser.User.Dept.DeptName
|
||||
}
|
||||
return sysUserOnline
|
||||
}
|
||||
25
src/modules/monitor/service/system_info.go
Normal file
25
src/modules/monitor/service/system_info.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package service
|
||||
|
||||
// ISystemInfo 服务器系统相关信息 服务层接口
|
||||
type ISystemInfo interface {
|
||||
// ProjectInfo 程序项目信息
|
||||
ProjectInfo() map[string]any
|
||||
|
||||
// SystemInfo 系统信息
|
||||
SystemInfo() map[string]any
|
||||
|
||||
// TimeInfo 系统时间信息
|
||||
TimeInfo() map[string]string
|
||||
|
||||
// MemoryInfo 内存信息
|
||||
MemoryInfo() map[string]any
|
||||
|
||||
// CPUInfo CPU信息
|
||||
CPUInfo() map[string]any
|
||||
|
||||
// NetworkInfo 网络信息
|
||||
NetworkInfo() map[string]string
|
||||
|
||||
// DiskInfo 磁盘信息
|
||||
DiskInfo() []map[string]string
|
||||
}
|
||||
236
src/modules/monitor/service/system_info.impl.go
Normal file
236
src/modules/monitor/service/system_info.impl.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/config"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
)
|
||||
|
||||
// 实例化服务层 SystemInfoImpl 结构体
|
||||
var NewSystemInfoImpl = &SystemInfoImpl{}
|
||||
|
||||
// SystemInfoImpl 服务器系统相关信息 服务层处理
|
||||
type SystemInfoImpl struct{}
|
||||
|
||||
// ProjectInfo 程序项目信息
|
||||
func (s *SystemInfoImpl) ProjectInfo() map[string]any {
|
||||
// 获取工作目录
|
||||
appDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
appDir = ""
|
||||
}
|
||||
// 项目依赖
|
||||
dependencies := s.dependencies()
|
||||
return map[string]any{
|
||||
"appDir": appDir,
|
||||
"env": config.Env(),
|
||||
"name": config.Get("framework.name"),
|
||||
"version": config.Get("framework.version"),
|
||||
"dependencies": dependencies,
|
||||
}
|
||||
}
|
||||
|
||||
// dependencies 读取mod内项目包依赖
|
||||
func (s *SystemInfoImpl) dependencies() map[string]string {
|
||||
var pkgs = make(map[string]string)
|
||||
|
||||
// 打开 go.mod 文件
|
||||
file, err := os.Open("go.mod")
|
||||
if err != nil {
|
||||
return pkgs
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 使用 bufio.Scanner 逐行读取文件内容
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
// 行不为空,不以module\require开头,不带有 // indirect 注释,则解析包名和版本
|
||||
prefixLine := strings.HasPrefix(line, "module") || strings.HasPrefix(line, "require") || strings.HasPrefix(line, "go ")
|
||||
suffixLine := strings.HasSuffix(line, ")") || strings.HasSuffix(line, "// indirect")
|
||||
if line == "" || prefixLine || suffixLine {
|
||||
continue
|
||||
}
|
||||
|
||||
modInfo := strings.Split(line, " ")
|
||||
if len(modInfo) >= 2 {
|
||||
moduleName := strings.TrimSpace(modInfo[0])
|
||||
version := strings.TrimSpace(modInfo[1])
|
||||
pkgs[moduleName] = version
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
pkgs["scanner-err"] = err.Error()
|
||||
}
|
||||
return pkgs
|
||||
}
|
||||
|
||||
// SystemInfo 系统信息
|
||||
func (s *SystemInfoImpl) SystemInfo() map[string]any {
|
||||
info, err := host.Info()
|
||||
if err != nil {
|
||||
info.Platform = err.Error()
|
||||
}
|
||||
// 用户目录
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
homeDir = ""
|
||||
}
|
||||
cmd, err := os.Executable()
|
||||
if err != nil {
|
||||
cmd = ""
|
||||
}
|
||||
return map[string]any{
|
||||
"platform": info.Platform,
|
||||
"go": runtime.Version(),
|
||||
"processId": os.Getpid(),
|
||||
"arch": info.KernelArch,
|
||||
"uname": runtime.GOARCH,
|
||||
"release": info.OS,
|
||||
"hostname": info.Hostname,
|
||||
"homeDir": homeDir,
|
||||
"cmd": cmd,
|
||||
"execCommand": strings.Join(os.Args, " "),
|
||||
}
|
||||
}
|
||||
|
||||
// TimeInfo 系统时间信息
|
||||
func (s *SystemInfoImpl) TimeInfo() map[string]string {
|
||||
// 获取当前时间
|
||||
current := time.Now().Format("2006-01-02 15:04:05")
|
||||
// 获取程序运行时间
|
||||
uptime := time.Since(config.RunTime()).String()
|
||||
// 获取时区
|
||||
timezone := time.Now().Format("-0700 MST")
|
||||
// 获取时区名称
|
||||
timezoneName := time.Now().Format("MST")
|
||||
|
||||
return map[string]string{
|
||||
"current": current,
|
||||
"uptime": uptime,
|
||||
"timezone": timezone,
|
||||
"timezoneName": timezoneName,
|
||||
}
|
||||
}
|
||||
|
||||
// MemoryInfo 内存信息
|
||||
func (s *SystemInfoImpl) MemoryInfo() map[string]any {
|
||||
memInfo, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
memInfo.UsedPercent = 0
|
||||
memInfo.Available = 0
|
||||
memInfo.Total = 0
|
||||
}
|
||||
|
||||
var memStats runtime.MemStats
|
||||
runtime.ReadMemStats(&memStats)
|
||||
|
||||
return map[string]any{
|
||||
"usage": fmt.Sprintf("%.2f", memInfo.UsedPercent), // 内存利用率
|
||||
"freemem": parse.Bit(float64(memInfo.Available)), // 可用内存大小(GB)
|
||||
"totalmem": parse.Bit(float64(memInfo.Total)), // 总内存大小(GB)
|
||||
"rss": parse.Bit(float64(memStats.Sys)), // 常驻内存大小(RSS)
|
||||
"heapTotal": parse.Bit(float64(memStats.HeapSys)), // 堆总大小
|
||||
"heapUsed": parse.Bit(float64(memStats.HeapAlloc)), // 堆已使用大小
|
||||
"external": parse.Bit(float64(memStats.Sys - memStats.HeapSys)), // 外部内存大小(非堆)
|
||||
}
|
||||
}
|
||||
|
||||
// CPUInfo CPU信息
|
||||
func (s *SystemInfoImpl) CPUInfo() map[string]any {
|
||||
var core int32 = 0
|
||||
var speed string = "未知"
|
||||
var model string = "未知"
|
||||
cpuInfo, err := cpu.Info()
|
||||
if err == nil {
|
||||
core = cpuInfo[0].Cores
|
||||
speed = fmt.Sprintf("%.0fMHz", cpuInfo[0].Mhz)
|
||||
model = strings.TrimSpace(cpuInfo[0].ModelName)
|
||||
}
|
||||
|
||||
useds := []string{}
|
||||
cpuPercent, err := cpu.Percent(0, true)
|
||||
if err == nil {
|
||||
for _, v := range cpuPercent {
|
||||
useds = append(useds, fmt.Sprintf("%.2f", v))
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"model": model,
|
||||
"speed": speed,
|
||||
"core": core,
|
||||
"coreUsed": useds,
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkInfo 网络信息
|
||||
func (s *SystemInfoImpl) NetworkInfo() map[string]string {
|
||||
ipAddrs := make(map[string]string)
|
||||
interfaces, err := net.Interfaces()
|
||||
if err == nil {
|
||||
for _, iface := range interfaces {
|
||||
name := iface.Name
|
||||
if name[len(name)-1] == '0' {
|
||||
name = name[0 : len(name)-1]
|
||||
name = strings.Trim(name, "")
|
||||
}
|
||||
// ignore localhost
|
||||
if name == "lo" {
|
||||
continue
|
||||
}
|
||||
var addrs []string
|
||||
for _, v := range iface.Addrs {
|
||||
prefix := strings.Split(v.Addr, "/")[0]
|
||||
if strings.Contains(prefix, "::") {
|
||||
addrs = append(addrs, fmt.Sprintf("IPv6 %s", prefix))
|
||||
}
|
||||
if strings.Contains(prefix, ".") {
|
||||
addrs = append(addrs, fmt.Sprintf("IPv4 %s", prefix))
|
||||
}
|
||||
}
|
||||
ipAddrs[name] = strings.Join(addrs, " / ")
|
||||
}
|
||||
}
|
||||
return ipAddrs
|
||||
}
|
||||
|
||||
// DiskInfo 磁盘信息
|
||||
func (s *SystemInfoImpl) DiskInfo() []map[string]string {
|
||||
disks := make([]map[string]string, 0)
|
||||
|
||||
partitions, err := disk.Partitions(false)
|
||||
if err != nil {
|
||||
return disks
|
||||
}
|
||||
|
||||
for _, partition := range partitions {
|
||||
usage, err := disk.Usage(partition.Mountpoint)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
disks = append(disks, map[string]string{
|
||||
"size": parse.Bit(float64(usage.Total)),
|
||||
"used": parse.Bit(float64(usage.Used)),
|
||||
"avail": parse.Bit(float64(usage.Free)),
|
||||
"pcent": fmt.Sprintf("%.1f%%", usage.UsedPercent),
|
||||
"target": partition.Device,
|
||||
})
|
||||
}
|
||||
return disks
|
||||
}
|
||||
Reference in New Issue
Block a user