feat: MML日志记录功能接口

This commit is contained in:
TsMask
2025-08-05 17:31:31 +08:00
parent 0698f02c61
commit 96b15d4cf2
9 changed files with 186 additions and 38 deletions

View File

@@ -8,9 +8,9 @@ CREATE TABLE "mml_log" (
"ip" text(64),
"ne_type" text(32),
"ne_id" text(32),
"mml" text(1024),
"result" text(2048),
"log_time" text,
"command" text(512),
"result" text(255),
"log_time" integer,
PRIMARY KEY ("id")
);

View File

@@ -0,0 +1,19 @@
-- ----------------------------
-- Table structure for mml_log
-- ----------------------------
DROP TABLE IF EXISTS "mml_log";
CREATE TABLE "mml_log" (
"id" integer NOT NULL,
"user" text(32),
"ip" text(64),
"ne_type" text(32),
"ne_id" text(32),
"command" text(512),
"result" text(255),
"log_time" integer,
PRIMARY KEY ("id")
);
-- ----------------------------
-- Records of mml_log
-- ----------------------------

View File

@@ -1,47 +1,18 @@
-- MariaDB dump 10.19 Distrib 10.6.16-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: 192.168.2.219 Database: omc_db
-- ------------------------------------------------------
-- Server version 10.3.38-MariaDB
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `mml_log`
--
DROP TABLE IF EXISTS `mml_log`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mml_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(32) DEFAULT NULL,
`ip` varchar(64) DEFAULT NULL,
`ne_type` varchar(32) DEFAULT NULL,
`ne_id` varchar(32) DEFAULT NULL,
`mml` varchar(1024) DEFAULT NULL,
`result` varchar(2048) DEFAULT NULL,
`log_time` datetime DEFAULT NULL,
`command` varchar(512) DEFAULT NULL COMMENT '命令 ; 分隔',
`result` varchar(255) DEFAULT NULL COMMENT '成功和有错误',
`log_time` bigint DEFAULT '0' COMMENT '记录时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=467 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;
-- Dump completed on 2024-03-06 17:26:56

View File

@@ -0,0 +1,18 @@
--
-- Table structure for table `mml_log`
--
DROP TABLE IF EXISTS `mml_log`;
CREATE TABLE `mml_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(32) DEFAULT NULL,
`ip` varchar(64) DEFAULT NULL,
`ne_type` varchar(32) DEFAULT NULL,
`ne_id` varchar(32) DEFAULT NULL,
`command` varchar(512) DEFAULT NULL COMMENT '命令 ; 分隔',
`result` varchar(255) DEFAULT NULL COMMENT '成功和有错误',
`log_time` bigint DEFAULT '0' COMMENT '记录时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;
-- Dump completed on 2024-03-06 17:26:56

View File

@@ -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))
}

View 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"
}

View 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(&param).Error; err != nil {
logger.Errorf("insert err => %v", err.Error())
return 0
}
return param.ID
}

View 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)
}

View File

@@ -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,