feat: MML日志记录功能接口
This commit is contained in:
@@ -4,13 +4,14 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
neService "be.ems/src/modules/network_element/service"
|
||||
"be.ems/src/modules/tool/model"
|
||||
"be.ems/src/modules/tool/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 MMLController 结构体
|
||||
@@ -18,6 +19,7 @@ var NewMML = &MMLController{
|
||||
neInfoService: neService.NewNeInfo,
|
||||
mmlSystemService: service.NewMMLSystem,
|
||||
mmlSubscriberService: service.NewMMLSubscriber,
|
||||
mmlLogService: service.NewMMLLog,
|
||||
}
|
||||
|
||||
// MML 网元MML
|
||||
@@ -27,6 +29,7 @@ type MMLController struct {
|
||||
neInfoService *neService.NeInfo // 网元信息服务
|
||||
mmlSystemService *service.MMLSystem
|
||||
mmlSubscriberService *service.MMLSubscriber
|
||||
mmlLogService *service.MMLLog
|
||||
}
|
||||
|
||||
// SystemList MML网元列表
|
||||
@@ -47,6 +50,15 @@ func (s MMLController) SubscriberList(c *gin.Context) {
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// LogList MML日志列表
|
||||
//
|
||||
// GET /log/list
|
||||
func (s MMLController) LogList(c *gin.Context) {
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.mmlLogService.FindByPage(query)
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// Command MML命令执行
|
||||
//
|
||||
// POST /command
|
||||
@@ -89,6 +101,7 @@ func (s MMLController) Command(c *gin.Context) {
|
||||
}
|
||||
// 发送MML
|
||||
result := []string{}
|
||||
resultStr := "Success"
|
||||
for _, v := range body.Command {
|
||||
if v == "" {
|
||||
continue
|
||||
@@ -96,9 +109,21 @@ func (s MMLController) Command(c *gin.Context) {
|
||||
output, err := telnetClient.RunCMD(v + "\r\n")
|
||||
if err != nil {
|
||||
result = append(result, err.Error())
|
||||
resultStr = "there is an error"
|
||||
continue
|
||||
}
|
||||
result = append(result, strings.TrimSpace(output))
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
mmlLog := model.MMLLog{
|
||||
NeType: body.NeType,
|
||||
NeId: body.NeId,
|
||||
User: reqctx.LoginUserToUserName(c),
|
||||
Ip: c.ClientIP(),
|
||||
Command: strings.Join(body.Command, ";"),
|
||||
Result: resultStr,
|
||||
}
|
||||
s.mmlLogService.Insert(mmlLog)
|
||||
c.JSON(200, resp.OkData(result))
|
||||
}
|
||||
|
||||
18
src/modules/tool/model/mml_log.go
Normal file
18
src/modules/tool/model/mml_log.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
// MMLLog MML网元命令
|
||||
type MMLLog struct {
|
||||
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
User string `json:"user" gorm:"column:user"`
|
||||
Ip string `json:"ip" gorm:"column:ip"`
|
||||
NeType string `json:"neType" gorm:"column:ne_type"`
|
||||
NeId string `json:"neId" gorm:"column:ne_id"`
|
||||
Command string `json:"command" gorm:"column:command"` // 命令
|
||||
Result string `json:"result" gorm:"column:result"`
|
||||
LogTime int64 `json:"logTime" gorm:"column:log_time"`
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (*MMLLog) TableName() string {
|
||||
return "mml_log"
|
||||
}
|
||||
67
src/modules/tool/repository/mml_log.go
Normal file
67
src/modules/tool/repository/mml_log.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/modules/tool/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 MMLLog 结构体
|
||||
var NewMMLLog = &MMLLog{}
|
||||
|
||||
// MMLLog MML日志 数据层处理
|
||||
type MMLLog struct{}
|
||||
|
||||
// SelectByPage 分页查询集合
|
||||
func (r MMLLog) SelectByPage(query map[string]string) ([]model.MMLLog, int64) {
|
||||
tx := db.DB("").Model(&model.MMLLog{})
|
||||
// 查询条件拼接
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
if v, ok := query["beginTime"]; ok && v != "" {
|
||||
if len(v) == 10 {
|
||||
v = fmt.Sprintf("%s000", v)
|
||||
}
|
||||
tx = tx.Where("log_time >= ?", v)
|
||||
}
|
||||
if v, ok := query["endTime"]; ok && v != "" {
|
||||
if len(v) == 10 {
|
||||
v = fmt.Sprintf("%s999", v)
|
||||
}
|
||||
tx = tx.Where("log_time <= ?", v)
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.MMLLog{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// Insert 新增信息 返回新增数据ID
|
||||
func (r MMLLog) Insert(param model.MMLLog) int64 {
|
||||
param.LogTime = time.Now().UnixMilli()
|
||||
// 执行插入
|
||||
if err := db.DB("").Create(¶m).Error; err != nil {
|
||||
logger.Errorf("insert err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return param.ID
|
||||
}
|
||||
26
src/modules/tool/service/mml_log.go
Normal file
26
src/modules/tool/service/mml_log.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"be.ems/src/modules/tool/model"
|
||||
"be.ems/src/modules/tool/repository"
|
||||
)
|
||||
|
||||
// 实例化数据层 MMLLog 结构体
|
||||
var NewMMLLog = &MMLLog{
|
||||
mmlLogRepository: repository.NewMMLLog,
|
||||
}
|
||||
|
||||
// MMLLog MML网元日志 服务层处理
|
||||
type MMLLog struct {
|
||||
mmlLogRepository *repository.MMLLog
|
||||
}
|
||||
|
||||
// FindByPage 分页查询列表数据
|
||||
func (s MMLLog) FindByPage(query map[string]string) ([]model.MMLLog, int64) {
|
||||
return s.mmlLogRepository.SelectByPage(query)
|
||||
}
|
||||
|
||||
// Insert 新增日志
|
||||
func (s MMLLog) Insert(param model.MMLLog) int64 {
|
||||
return s.mmlLogRepository.Insert(param)
|
||||
}
|
||||
@@ -67,6 +67,10 @@ func Setup(router *gin.Engine) {
|
||||
middleware.AuthorizeUser(nil),
|
||||
mml.SubscriberList,
|
||||
)
|
||||
mmlGroup.GET("/log/list",
|
||||
middleware.AuthorizeUser(nil),
|
||||
mml.LogList,
|
||||
)
|
||||
mmlGroup.POST("/command",
|
||||
middleware.AuthorizeUser(nil),
|
||||
mml.Command,
|
||||
|
||||
Reference in New Issue
Block a user