From 96b15d4cf2742d2336ca44540f5307faee2919fd Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 5 Aug 2025 17:31:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20MML=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/database/lite/install/mml_log.sql | 6 +- build/database/lite/upgrade/upg_mml_log.sql | 19 ++++++ build/database/std/install/mml_log.sql | 37 ++---------- build/database/std/upgrade/upg_mml_log.sql | 18 ++++++ src/modules/tool/controller/mml.go | 29 ++++++++- src/modules/tool/model/mml_log.go | 18 ++++++ src/modules/tool/repository/mml_log.go | 67 +++++++++++++++++++++ src/modules/tool/service/mml_log.go | 26 ++++++++ src/modules/tool/tool.go | 4 ++ 9 files changed, 186 insertions(+), 38 deletions(-) create mode 100644 build/database/lite/upgrade/upg_mml_log.sql create mode 100644 build/database/std/upgrade/upg_mml_log.sql create mode 100644 src/modules/tool/model/mml_log.go create mode 100644 src/modules/tool/repository/mml_log.go create mode 100644 src/modules/tool/service/mml_log.go diff --git a/build/database/lite/install/mml_log.sql b/build/database/lite/install/mml_log.sql index 392309ec..8c2a5391 100644 --- a/build/database/lite/install/mml_log.sql +++ b/build/database/lite/install/mml_log.sql @@ -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") ); diff --git a/build/database/lite/upgrade/upg_mml_log.sql b/build/database/lite/upgrade/upg_mml_log.sql new file mode 100644 index 00000000..8c2a5391 --- /dev/null +++ b/build/database/lite/upgrade/upg_mml_log.sql @@ -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 +-- ---------------------------- diff --git a/build/database/std/install/mml_log.sql b/build/database/std/install/mml_log.sql index e6a817c9..22c7ca66 100644 --- a/build/database/std/install/mml_log.sql +++ b/build/database/std/install/mml_log.sql @@ -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 diff --git a/build/database/std/upgrade/upg_mml_log.sql b/build/database/std/upgrade/upg_mml_log.sql new file mode 100644 index 00000000..22c7ca66 --- /dev/null +++ b/build/database/std/upgrade/upg_mml_log.sql @@ -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 diff --git a/src/modules/tool/controller/mml.go b/src/modules/tool/controller/mml.go index caafe85e..16dcda09 100644 --- a/src/modules/tool/controller/mml.go +++ b/src/modules/tool/controller/mml.go @@ -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)) } diff --git a/src/modules/tool/model/mml_log.go b/src/modules/tool/model/mml_log.go new file mode 100644 index 00000000..383a0943 --- /dev/null +++ b/src/modules/tool/model/mml_log.go @@ -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" +} diff --git a/src/modules/tool/repository/mml_log.go b/src/modules/tool/repository/mml_log.go new file mode 100644 index 00000000..52422396 --- /dev/null +++ b/src/modules/tool/repository/mml_log.go @@ -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 +} diff --git a/src/modules/tool/service/mml_log.go b/src/modules/tool/service/mml_log.go new file mode 100644 index 00000000..f7d31cdd --- /dev/null +++ b/src/modules/tool/service/mml_log.go @@ -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) +} diff --git a/src/modules/tool/tool.go b/src/modules/tool/tool.go index de8243d6..355b238d 100644 --- a/src/modules/tool/tool.go +++ b/src/modules/tool/tool.go @@ -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,