feat: omc原始代码
This commit is contained in:
25
src/modules/api_rest/api_rest.go
Normal file
25
src/modules/api_rest/api_rest.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package chart
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/middleware"
|
||||
"nms_nbi/src/modules/chart/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> NMS 北向模块路由")
|
||||
|
||||
apiRest := router.Group("/api/rest")
|
||||
|
||||
// 关系图
|
||||
systemManagementGroup := apiRest.Group("/systemManagement")
|
||||
{
|
||||
systemManagementGroup.GET("",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewChartGraph.Load,
|
||||
)
|
||||
}
|
||||
}
|
||||
40
src/modules/chart/chart.go
Normal file
40
src/modules/chart/chart.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package chart
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/middleware"
|
||||
"nms_nbi/src/framework/middleware/collectlogs"
|
||||
"nms_nbi/src/modules/chart/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> chart 模块路由")
|
||||
|
||||
chartGroup := router.Group("/chart")
|
||||
|
||||
// 关系图
|
||||
chartGraphGroup := chartGroup.Group("/graph")
|
||||
{
|
||||
chartGraphGroup.GET("",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewChartGraph.Load,
|
||||
)
|
||||
chartGraphGroup.GET("/groups",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewChartGraph.GroupNames,
|
||||
)
|
||||
chartGraphGroup.POST("",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.chartGraph", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewChartGraph.Save,
|
||||
)
|
||||
chartGraphGroup.DELETE("/:group",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.chartGraph", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewChartGraph.Delete,
|
||||
)
|
||||
}
|
||||
}
|
||||
101
src/modules/chart/controller/chart_graph.go
Normal file
101
src/modules/chart/controller/chart_graph.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
chartService "nms_nbi/src/modules/chart/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 ChartGraphController 结构体
|
||||
var NewChartGraph = &ChartGraphController{
|
||||
chartGraphService: chartService.NewChartGraphImpl,
|
||||
}
|
||||
|
||||
// G6关系图
|
||||
//
|
||||
// PATH /graph
|
||||
type ChartGraphController struct {
|
||||
// G6关系图数据表服务
|
||||
chartGraphService chartService.IChartGraph
|
||||
}
|
||||
|
||||
// 获取关系图组名
|
||||
//
|
||||
// GET /groups
|
||||
func (s *ChartGraphController) GroupNames(c *gin.Context) {
|
||||
data := s.chartGraphService.SelectGroup()
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// 获取关系图数据
|
||||
//
|
||||
// GET /
|
||||
func (s *ChartGraphController) Load(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
Group string `form:"group" binding:"required"`
|
||||
Type string `form:"type" binding:"omitempty,oneof=node edge combo"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.chartGraphService.LoadData(querys.Group, querys.Type)
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// 保存关系图数据
|
||||
//
|
||||
// POST /
|
||||
func (s *ChartGraphController) Save(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
Group string `json:"group" binding:"required"`
|
||||
Data struct {
|
||||
Nodes []map[string]any `json:"nodes" binding:"required"`
|
||||
Edges []map[string]any `json:"edges" binding:"required"`
|
||||
Combos []map[string]any `json:"combos" binding:"required"`
|
||||
} `json:"data" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"nodes": body.Data.Nodes,
|
||||
"edges": body.Data.Edges,
|
||||
"combos": body.Data.Combos,
|
||||
}
|
||||
saveNum := s.chartGraphService.SaveData(body.Group, data)
|
||||
if saveNum > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 删除关系图数据
|
||||
//
|
||||
// DELETE /:group
|
||||
func (s *ChartGraphController) Delete(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
group := c.Param("group")
|
||||
if group == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
deleteNum := s.chartGraphService.DeleteGroup(group)
|
||||
if deleteNum > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
31
src/modules/chart/model/chart_graph.go
Normal file
31
src/modules/chart/model/chart_graph.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package model
|
||||
|
||||
// ChartGraph G6关系图数据对象 chart_graph
|
||||
type ChartGraph struct {
|
||||
RowID int64 `json:"rowId,omitempty" gorm:"column:row_id;primaryKey;autoIncrement"` // 记录ID
|
||||
RowType string `json:"rowType,omitempty" gorm:"column:row_type"` // 记录类型(node/edge/combo)
|
||||
RowGroup string `json:"rowGroup,omitempty" gorm:"column:row_group"` // 记录组名
|
||||
ID string `json:"id,omitempty" gorm:"column:id"` // 元素ID
|
||||
Type string `json:"type,omitempty" gorm:"column:type"` // node/combo 类型
|
||||
Depth int `json:"depth,omitempty" gorm:"column:depth"` // node/combo 深度
|
||||
X float64 `json:"x,omitempty" gorm:"column:x"` // node/combo 横向坐标
|
||||
Y float64 `json:"y,omitempty" gorm:"column:y"` // node/combo 纵向坐标
|
||||
Size string `json:"size,omitempty" gorm:"column:size"` // node/combo 大小-JSON数组
|
||||
Icon string `json:"icon,omitempty" gorm:"column:icon"` // node-部分类型支持图标JSON配置
|
||||
Img string `json:"img,omitempty" gorm:"column:img"` // node-img 图片
|
||||
ClipCfg string `json:"clipCfg,omitempty" gorm:"column:clip_cfg"` // node-img 图片裁剪JSON配置
|
||||
Direction string `json:"direction,omitempty" gorm:"column:direction"` // node-triangle 三角形的方向(up/down/left/right)
|
||||
Source string `json:"source,omitempty" gorm:"column:source"` // edge-边起始
|
||||
Target string `json:"target,omitempty" gorm:"column:target"` // edge-边目标
|
||||
ComboID string `json:"combo_id,omitempty" gorm:"column:combo_id"` // combo-分组
|
||||
Padding string `json:"padding,omitempty" gorm:"column:padding"` // combo-JSON分组内边距
|
||||
ParentID string `json:"parentId,omitempty" gorm:"column:parent_id"` // combo-父级分组
|
||||
Children string `json:"children,omitempty" gorm:"column:children"` // combo-JSON分组内含元素
|
||||
Style string `json:"style,omitempty" gorm:"column:style"` // 元素样式-JONS配置
|
||||
Label string `json:"label,omitempty" gorm:"column:label"` // 标签文本
|
||||
LabelCfg string `json:"labelCfg,omitempty" gorm:"column:label_cfg"` // 标签文本-JSON配置
|
||||
}
|
||||
|
||||
func (ChartGraph) TableName() string {
|
||||
return "chart_graph"
|
||||
}
|
||||
21
src/modules/chart/repository/chart_graph.go
Normal file
21
src/modules/chart/repository/chart_graph.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package repository
|
||||
|
||||
import "nms_nbi/src/modules/chart/model"
|
||||
|
||||
// G6关系图数据 数据层接口
|
||||
type IChartGraph interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(graph model.ChartGraph) []model.ChartGraph
|
||||
|
||||
// SelectGroup 查询组名
|
||||
SelectGroup() []string
|
||||
|
||||
// Insert 批量添加
|
||||
Inserts(graphs []model.ChartGraph) int64
|
||||
|
||||
// Delete 删除组数据
|
||||
DeleteGroup(rowGroup string) int64
|
||||
}
|
||||
194
src/modules/chart/repository/chart_graph.impl.go
Normal file
194
src/modules/chart/repository/chart_graph.impl.go
Normal file
@@ -0,0 +1,194 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/repo"
|
||||
"nms_nbi/src/modules/chart/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 NewChartGraphImpl 结构体
|
||||
var NewChartGraphImpl = &ChartGraphImpl{
|
||||
selectSql: `select
|
||||
row_id, row_type, row_group,
|
||||
id, type, depth, x, y, size, icon, img,
|
||||
clip_cfg, direction,
|
||||
source, target, combo_id,
|
||||
padding, parent_id, children,
|
||||
style, label, label_cfg
|
||||
from chart_graph`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"row_id": "RowID",
|
||||
"row_type": "RowType",
|
||||
"row_group": "RowGroup",
|
||||
"id": "ID",
|
||||
"type": "Type",
|
||||
"depth": "Depth",
|
||||
"x": "X",
|
||||
"y": "Y",
|
||||
"size": "Size",
|
||||
"icon": "Icon",
|
||||
"img": "Img",
|
||||
"clip_cfg": "ClipCfg",
|
||||
"direction": "Direction",
|
||||
"source": "Source",
|
||||
"target": "Target",
|
||||
"combo_id": "ComboID",
|
||||
"padding": "Padding",
|
||||
"parent_id": "ParentID",
|
||||
"children": "Children",
|
||||
"style": "Style",
|
||||
"label": "Label",
|
||||
"label_cfg": "LabelCfg",
|
||||
},
|
||||
}
|
||||
|
||||
// ChartGraphImpl G6关系图数据表 数据层处理
|
||||
type ChartGraphImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *ChartGraphImpl) convertResultRows(rows []map[string]any) []model.ChartGraph {
|
||||
arr := make([]model.ChartGraph, 0)
|
||||
for _, row := range rows {
|
||||
item := model.ChartGraph{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
func (r *ChartGraphImpl) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["rowType"]; ok && v != "" {
|
||||
conditions = append(conditions, "row_type = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["rowGroup"]; ok && v != "" {
|
||||
conditions = append(conditions, "row_group = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.ChartGraph{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from chart_graph"
|
||||
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
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *ChartGraphImpl) SelectList(graph model.ChartGraph) []model.ChartGraph {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if graph.RowType != "" {
|
||||
conditions = append(conditions, "row_type = ?")
|
||||
params = append(params, graph.RowType)
|
||||
}
|
||||
if graph.RowGroup != "" {
|
||||
conditions = append(conditions, "row_group = ?")
|
||||
params = append(params, graph.RowGroup)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by depth asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectGroup 查询组名
|
||||
func (r *ChartGraphImpl) SelectGroup() []string {
|
||||
rows := []string{}
|
||||
// 查询数量 长度为0直接返回
|
||||
querySql := "select row_group as 'str' from chart_graph GROUP BY row_group"
|
||||
strRows, err := datasource.RawDB("", querySql, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("Query err => %v", err)
|
||||
return rows
|
||||
}
|
||||
for _, v := range strRows {
|
||||
rows = append(rows, v["str"].(string))
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 批量添加
|
||||
func (r *ChartGraphImpl) Inserts(graphs []model.ChartGraph) int64 {
|
||||
tx := datasource.DefaultDB().CreateInBatches(graphs, 2000)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// Delete 删除组数据
|
||||
func (r *ChartGraphImpl) DeleteGroup(rowGroup string) int64 {
|
||||
tx := datasource.DefaultDB().Where("row_group = ?", rowGroup).Delete(&model.ChartGraph{})
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("Delete err => %v", err)
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
16
src/modules/chart/service/chart_graph.go
Normal file
16
src/modules/chart/service/chart_graph.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package service
|
||||
|
||||
// G6关系图数据 服务层接口
|
||||
type IChartGraph interface {
|
||||
// SelectGroup 查询组名
|
||||
SelectGroup() []string
|
||||
|
||||
// LoadData 查询所组图数据
|
||||
LoadData(rowGroup, rowType string) map[string]any
|
||||
|
||||
// SaveData 添加组图数据
|
||||
SaveData(rowGroup string, data map[string]any) int64
|
||||
|
||||
// DeleteGroup 删除所组图数据
|
||||
DeleteGroup(rowGroup string) int64
|
||||
}
|
||||
360
src/modules/chart/service/chart_graph.impl.go
Normal file
360
src/modules/chart/service/chart_graph.impl.go
Normal file
@@ -0,0 +1,360 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/modules/chart/model"
|
||||
chartRepository "nms_nbi/src/modules/chart/repository"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
// 实例化服务层 ChartGraphImpl 结构体
|
||||
var NewChartGraphImpl = &ChartGraphImpl{
|
||||
graphRepository: chartRepository.NewChartGraphImpl,
|
||||
}
|
||||
|
||||
// ChartGraphImpl G6关系图数据表 服务层处理
|
||||
type ChartGraphImpl struct {
|
||||
// G6关系图数据服务
|
||||
graphRepository chartRepository.IChartGraph
|
||||
}
|
||||
|
||||
// SelectGroup 查询组名
|
||||
func (s *ChartGraphImpl) SelectGroup() []string {
|
||||
return s.graphRepository.SelectGroup()
|
||||
}
|
||||
|
||||
// LoadData 查询所组图数据
|
||||
func (s *ChartGraphImpl) LoadData(rowGroup, rowType string) map[string]any {
|
||||
// 查询数据
|
||||
graph := model.ChartGraph{
|
||||
RowGroup: rowGroup,
|
||||
}
|
||||
if rowType != "" {
|
||||
graph.RowType = rowType
|
||||
}
|
||||
data := s.graphRepository.SelectList(graph)
|
||||
|
||||
// 数据项
|
||||
nodes := []map[string]any{}
|
||||
edges := []map[string]any{}
|
||||
combos := []map[string]any{}
|
||||
|
||||
for _, v := range data {
|
||||
if v.RowType == "node" {
|
||||
nodes = append(nodes, s.loadNode(v))
|
||||
}
|
||||
if v.RowType == "edge" {
|
||||
edges = append(edges, s.loadEdge(v))
|
||||
}
|
||||
if v.RowType == "combo" {
|
||||
combos = append(combos, s.loadCombo(v))
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"nodes": nodes,
|
||||
"edges": edges,
|
||||
"combos": combos,
|
||||
}
|
||||
}
|
||||
|
||||
// loadNode 图数据Node
|
||||
func (s *ChartGraphImpl) loadNode(v model.ChartGraph) map[string]any {
|
||||
node := map[string]any{
|
||||
"id": v.ID,
|
||||
"comboId": v.ComboID,
|
||||
"x": v.X,
|
||||
"y": v.Y,
|
||||
"type": v.Type,
|
||||
"depth": v.Depth,
|
||||
}
|
||||
|
||||
// 元素样式
|
||||
style := map[string]any{}
|
||||
if len(v.Style) > 7 {
|
||||
json.Unmarshal([]byte(v.Style), &style)
|
||||
}
|
||||
node["style"] = style
|
||||
|
||||
// 元素大小
|
||||
if strings.Contains(v.Size, "[") {
|
||||
sizeArr := []int64{}
|
||||
json.Unmarshal([]byte(v.Size), &sizeArr)
|
||||
node["size"] = sizeArr
|
||||
} else {
|
||||
node["size"] = parse.Number(v.Size)
|
||||
}
|
||||
|
||||
// 标签文本
|
||||
node["label"] = v.Label
|
||||
labelCfg := map[string]any{}
|
||||
if len(v.LabelCfg) > 7 {
|
||||
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
|
||||
}
|
||||
node["labelCfg"] = labelCfg
|
||||
|
||||
// 三角形属性
|
||||
if v.Type == "triangle" {
|
||||
node["direction"] = v.Direction
|
||||
}
|
||||
|
||||
// 图片属性
|
||||
if strings.Index(v.Type, "image") == 0 {
|
||||
node["img"] = v.Img
|
||||
clipCfg := map[string]any{}
|
||||
if len(v.ClipCfg) > 7 {
|
||||
json.Unmarshal([]byte(v.ClipCfg), &clipCfg)
|
||||
}
|
||||
node["clipCfg"] = clipCfg
|
||||
}
|
||||
|
||||
// 图标属性
|
||||
if v.Icon != "" {
|
||||
icon := map[string]any{}
|
||||
if len(v.Icon) > 7 {
|
||||
json.Unmarshal([]byte(v.Icon), &icon)
|
||||
}
|
||||
node["icon"] = icon
|
||||
}
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
// loadEdge 图数据Edge
|
||||
func (s *ChartGraphImpl) loadEdge(v model.ChartGraph) map[string]any {
|
||||
edge := map[string]any{
|
||||
"id": v.ID,
|
||||
"source": v.Source,
|
||||
"target": v.Target,
|
||||
"type": v.Type,
|
||||
}
|
||||
|
||||
// 元素样式
|
||||
style := map[string]any{}
|
||||
if len(v.Style) > 7 {
|
||||
json.Unmarshal([]byte(v.Style), &style)
|
||||
}
|
||||
edge["style"] = style
|
||||
|
||||
// 标签文本
|
||||
edge["label"] = v.Label
|
||||
labelCfg := map[string]any{}
|
||||
if len(v.LabelCfg) > 7 {
|
||||
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
|
||||
}
|
||||
edge["labelCfg"] = labelCfg
|
||||
|
||||
return edge
|
||||
}
|
||||
|
||||
// loadCombo 图数据Combo
|
||||
func (s *ChartGraphImpl) loadCombo(v model.ChartGraph) map[string]any {
|
||||
combo := map[string]any{
|
||||
"id": v.ID,
|
||||
"x": v.X,
|
||||
"y": v.Y,
|
||||
"type": v.Type,
|
||||
"depth": v.Depth,
|
||||
}
|
||||
|
||||
// 元素样式
|
||||
style := map[string]any{}
|
||||
if len(v.Style) > 7 {
|
||||
json.Unmarshal([]byte(v.Style), &style)
|
||||
}
|
||||
combo["style"] = style
|
||||
|
||||
// 元素大小
|
||||
if strings.Contains(v.Size, "[") {
|
||||
sizeArr := []int64{}
|
||||
json.Unmarshal([]byte(v.Size), &sizeArr)
|
||||
combo["size"] = sizeArr
|
||||
} else {
|
||||
combo["size"] = parse.Number(v.Size)
|
||||
}
|
||||
|
||||
// 元素内边距
|
||||
if strings.Contains(v.Padding, "[") {
|
||||
paddingArr := []int64{}
|
||||
json.Unmarshal([]byte(v.Padding), &paddingArr)
|
||||
combo["padding"] = paddingArr
|
||||
} else {
|
||||
combo["padding"] = parse.Number(v.Padding)
|
||||
}
|
||||
|
||||
// 标签文本
|
||||
combo["label"] = v.Label
|
||||
labelCfg := map[string]any{}
|
||||
if len(v.LabelCfg) > 7 {
|
||||
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
|
||||
}
|
||||
combo["labelCfg"] = labelCfg
|
||||
|
||||
// 分组内元素
|
||||
if v.Children != "" {
|
||||
children := []map[string]any{}
|
||||
if len(v.Children) > 7 {
|
||||
json.Unmarshal([]byte(v.Children), &children)
|
||||
}
|
||||
combo["children"] = children
|
||||
}
|
||||
|
||||
return combo
|
||||
}
|
||||
|
||||
// SaveData 添加组图数据
|
||||
func (s *ChartGraphImpl) SaveData(rowGroup string, data map[string]any) int64 {
|
||||
graphs := []model.ChartGraph{}
|
||||
nodes := data["nodes"].([]map[string]any)
|
||||
graphNodes := s.saveNode(rowGroup, nodes)
|
||||
graphs = append(graphs, graphNodes...)
|
||||
edges := data["edges"].([]map[string]any)
|
||||
graphEdges := s.saveEdge(rowGroup, edges)
|
||||
graphs = append(graphs, graphEdges...)
|
||||
combos := data["combos"].([]map[string]any)
|
||||
graphCombos := s.saveCombo(rowGroup, combos)
|
||||
graphs = append(graphs, graphCombos...)
|
||||
// 删除组数据后插入
|
||||
if len(graphs) > 0 {
|
||||
s.graphRepository.DeleteGroup(rowGroup)
|
||||
}
|
||||
return s.graphRepository.Inserts(graphs)
|
||||
}
|
||||
|
||||
// saveNode 图数据Node
|
||||
func (s *ChartGraphImpl) saveNode(rowGroup string, nodes []map[string]any) []model.ChartGraph {
|
||||
var graphs []model.ChartGraph
|
||||
for _, v := range nodes {
|
||||
node := model.ChartGraph{
|
||||
RowType: "node",
|
||||
RowGroup: rowGroup,
|
||||
ID: v["id"].(string),
|
||||
X: v["x"].(float64),
|
||||
Y: v["y"].(float64),
|
||||
Type: v["type"].(string),
|
||||
}
|
||||
if comboId, ok := v["comboId"]; ok && comboId != nil {
|
||||
node.ComboID = comboId.(string)
|
||||
}
|
||||
if depth, ok := v["depth"]; ok && depth != nil {
|
||||
node.Depth = int(depth.(float64))
|
||||
}
|
||||
if styleByte, err := json.Marshal(v["style"]); err == nil {
|
||||
node.Style = string(styleByte)
|
||||
}
|
||||
|
||||
// 元素大小
|
||||
if sizeByte, err := json.Marshal(v["size"]); err == nil {
|
||||
node.Size = string(sizeByte)
|
||||
}
|
||||
|
||||
// 标签文本
|
||||
if label, ok := v["label"]; ok && label != nil {
|
||||
node.Label = label.(string)
|
||||
}
|
||||
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
|
||||
node.LabelCfg = string(labelCfgByte)
|
||||
}
|
||||
// 三角形属性
|
||||
if direction, ok := v["direction"]; ok && direction != nil && node.Type == "triangle" {
|
||||
node.Direction = direction.(string)
|
||||
}
|
||||
// 图片属性
|
||||
if img, ok := v["img"]; ok && img != nil {
|
||||
node.Img = img.(string)
|
||||
if clipCfgByte, err := json.Marshal(v["clipCfg"]); err == nil {
|
||||
node.ClipCfg = string(clipCfgByte)
|
||||
}
|
||||
}
|
||||
// 图标属性
|
||||
if icon, ok := v["icon"]; ok && icon != nil {
|
||||
if iconByte, err := json.Marshal(icon); err == nil {
|
||||
node.Icon = string(iconByte)
|
||||
}
|
||||
}
|
||||
|
||||
graphs = append(graphs, node)
|
||||
}
|
||||
return graphs
|
||||
}
|
||||
|
||||
// saveEdge 图数据Edge
|
||||
func (s *ChartGraphImpl) saveEdge(rowGroup string, edges []map[string]any) []model.ChartGraph {
|
||||
var graphs []model.ChartGraph
|
||||
for _, v := range edges {
|
||||
edge := model.ChartGraph{
|
||||
RowType: "edge",
|
||||
RowGroup: rowGroup,
|
||||
ID: v["id"].(string),
|
||||
Source: v["source"].(string),
|
||||
Target: v["target"].(string),
|
||||
Type: v["type"].(string),
|
||||
}
|
||||
|
||||
if styleByte, err := json.Marshal(v["style"]); err == nil {
|
||||
edge.Style = string(styleByte)
|
||||
}
|
||||
|
||||
// 标签文本
|
||||
if label, ok := v["label"]; ok && label != nil {
|
||||
edge.Label = label.(string)
|
||||
}
|
||||
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
|
||||
edge.LabelCfg = string(labelCfgByte)
|
||||
}
|
||||
|
||||
graphs = append(graphs, edge)
|
||||
}
|
||||
return graphs
|
||||
}
|
||||
|
||||
// saveCombo 图数据Combo
|
||||
func (s *ChartGraphImpl) saveCombo(rowGroup string, combos []map[string]any) []model.ChartGraph {
|
||||
var graphs []model.ChartGraph
|
||||
for _, v := range combos {
|
||||
combo := model.ChartGraph{
|
||||
RowType: "combo",
|
||||
RowGroup: rowGroup,
|
||||
ID: v["id"].(string),
|
||||
X: v["x"].(float64),
|
||||
Y: v["y"].(float64),
|
||||
Type: v["type"].(string),
|
||||
}
|
||||
if depth, ok := v["depth"]; ok && depth != nil {
|
||||
combo.Depth = int(depth.(float64))
|
||||
}
|
||||
if styleByte, err := json.Marshal(v["style"]); err == nil {
|
||||
combo.Style = string(styleByte)
|
||||
}
|
||||
if paddingByte, err := json.Marshal(v["padding"]); err == nil {
|
||||
combo.Padding = string(paddingByte)
|
||||
}
|
||||
if childrenByte, err := json.Marshal(v["children"]); err == nil {
|
||||
combo.Children = string(childrenByte)
|
||||
}
|
||||
|
||||
// 元素大小
|
||||
if sizeByte, err := json.Marshal(v["size"]); err == nil {
|
||||
combo.Size = string(sizeByte)
|
||||
}
|
||||
|
||||
// 标签文本
|
||||
if label, ok := v["label"]; ok && label != nil {
|
||||
combo.Label = label.(string)
|
||||
}
|
||||
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
|
||||
combo.LabelCfg = string(labelCfgByte)
|
||||
}
|
||||
|
||||
graphs = append(graphs, combo)
|
||||
}
|
||||
return graphs
|
||||
}
|
||||
|
||||
// Delete 删除所组图数据
|
||||
func (s *ChartGraphImpl) DeleteGroup(rowGroup string) int64 {
|
||||
return s.graphRepository.DeleteGroup(rowGroup)
|
||||
}
|
||||
90
src/modules/common/common.go
Normal file
90
src/modules/common/common.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/middleware"
|
||||
"nms_nbi/src/modules/common/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> common 模块路由")
|
||||
|
||||
// 路由主页
|
||||
indexGroup := router.Group("/")
|
||||
indexGroup.GET("",
|
||||
// middleware.RateLimit(middleware.LimitOption{
|
||||
// Time: 300,
|
||||
// Count: 10,
|
||||
// Type: middleware.LIMIT_IP,
|
||||
// }),
|
||||
controller.NewIndex.Handler,
|
||||
)
|
||||
|
||||
// 系统可暴露的配置信息
|
||||
indexGroup.GET("/sys-conf", controller.NewCommont.SysConfig)
|
||||
|
||||
// 验证码操作处理
|
||||
indexGroup.GET("/captchaImage",
|
||||
// middleware.RateLimit(middleware.LimitOption{
|
||||
// Time: 300,
|
||||
// Count: 60,
|
||||
// Type: middleware.LIMIT_IP,
|
||||
// }),
|
||||
controller.NewCaptcha.Image,
|
||||
)
|
||||
|
||||
// 账号身份操作处理
|
||||
{
|
||||
indexGroup.POST("/login",
|
||||
// middleware.RateLimit(middleware.LimitOption{
|
||||
// Time: 300,
|
||||
// Count: 10,
|
||||
// Type: middleware.LIMIT_IP,
|
||||
// }),
|
||||
controller.NewAccount.Login,
|
||||
)
|
||||
indexGroup.GET("/getInfo", middleware.PreAuthorize(nil), controller.NewAccount.Info)
|
||||
indexGroup.GET("/getRouters", middleware.PreAuthorize(nil), controller.NewAccount.Router)
|
||||
indexGroup.POST("/logout",
|
||||
middleware.RateLimit(middleware.LimitOption{
|
||||
Time: 300,
|
||||
Count: 5,
|
||||
Type: middleware.LIMIT_IP,
|
||||
}),
|
||||
controller.NewAccount.Logout,
|
||||
)
|
||||
}
|
||||
|
||||
// 账号注册操作处理
|
||||
{
|
||||
indexGroup.POST("/register",
|
||||
// middleware.RateLimit(middleware.LimitOption{
|
||||
// Time: 300,
|
||||
// Count: 10,
|
||||
// Type: middleware.LIMIT_IP,
|
||||
// }),
|
||||
controller.NewRegister.Register,
|
||||
)
|
||||
}
|
||||
|
||||
// 通用请求
|
||||
commonGroup := router.Group("/common")
|
||||
{
|
||||
commonGroup.POST("/hash", middleware.PreAuthorize(nil), controller.NewCommont.Hash)
|
||||
commonGroup.GET("/i18n", controller.NewCommont.I18n)
|
||||
}
|
||||
|
||||
// 文件操作处理
|
||||
fileGroup := router.Group("/file")
|
||||
{
|
||||
fileGroup.GET("/download/:filePath", middleware.PreAuthorize(nil), controller.NewFile.Download)
|
||||
fileGroup.POST("/upload", middleware.PreAuthorize(nil), controller.NewFile.Upload)
|
||||
fileGroup.POST("/chunkCheck", middleware.PreAuthorize(nil), controller.NewFile.ChunkCheck)
|
||||
fileGroup.POST("/chunkUpload", middleware.PreAuthorize(nil), controller.NewFile.ChunkUpload)
|
||||
fileGroup.POST("/chunkMerge", middleware.PreAuthorize(nil), controller.NewFile.ChunkMerge)
|
||||
fileGroup.POST("/transferStaticFile", middleware.PreAuthorize(nil), controller.NewCommont.TransferStaticFile)
|
||||
}
|
||||
}
|
||||
173
src/modules/common/controller/account.go
Normal file
173
src/modules/common/controller/account.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/config"
|
||||
commonConstants "nms_nbi/src/framework/constants/common"
|
||||
tokenConstants "nms_nbi/src/framework/constants/token"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
tokenUtils "nms_nbi/src/framework/utils/token"
|
||||
"nms_nbi/src/framework/vo"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
libAccount "nms_nbi/src/lib_features/account"
|
||||
commonModel "nms_nbi/src/modules/common/model"
|
||||
commonService "nms_nbi/src/modules/common/service"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 AccountController 结构体
|
||||
var NewAccount = &AccountController{
|
||||
accountService: commonService.NewAccountImpl,
|
||||
sysLogLoginService: systemService.NewSysLogLoginImpl,
|
||||
}
|
||||
|
||||
// 账号身份操作处理
|
||||
//
|
||||
// PATH /
|
||||
type AccountController struct {
|
||||
// 账号身份操作服务
|
||||
accountService commonService.IAccount
|
||||
// 系统登录访问
|
||||
sysLogLoginService systemService.ISysLogLogin
|
||||
}
|
||||
|
||||
// 系统登录
|
||||
//
|
||||
// POST /login
|
||||
func (s *AccountController) Login(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var loginBody commonModel.LoginBody
|
||||
if err := c.ShouldBindJSON(&loginBody); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 当前请求信息
|
||||
ipaddr, location := ctx.IPAddrLocation(c)
|
||||
os, browser := ctx.UaOsBrowser(c)
|
||||
|
||||
// 校验验证码
|
||||
err := s.accountService.ValidateCaptcha(
|
||||
loginBody.Code,
|
||||
loginBody.UUID,
|
||||
)
|
||||
// 根据错误信息,创建系统访问记录
|
||||
if err != nil {
|
||||
s.sysLogLoginService.CreateSysLogLogin(
|
||||
loginBody.Username, commonConstants.STATUS_NO, err.Error(),
|
||||
ipaddr, location, os, browser,
|
||||
)
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
// 登录用户信息
|
||||
loginUser, err := s.accountService.LoginByUsername(loginBody.Username, loginBody.Password)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
// 生成令牌,创建系统访问记录
|
||||
tokenStr := tokenUtils.Create(&loginUser, ipaddr, location, os, browser)
|
||||
if tokenStr == "" {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
} else {
|
||||
s.accountService.UpdateLoginDateAndIP(&loginUser)
|
||||
// 登录成功
|
||||
s.sysLogLoginService.CreateSysLogLogin(
|
||||
loginBody.Username, commonConstants.STATUS_YES, "app.common.loginSuccess",
|
||||
ipaddr, location, os, browser,
|
||||
)
|
||||
}
|
||||
|
||||
// 设置登录会话-兼容旧登录方式
|
||||
libAccount.SessionToken(loginBody.Username, ipaddr)
|
||||
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
tokenConstants.RESPONSE_FIELD: tokenStr,
|
||||
}))
|
||||
}
|
||||
|
||||
// 登录用户信息
|
||||
//
|
||||
// GET /getInfo
|
||||
func (s *AccountController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
loginUser, err := ctx.LoginUser(c)
|
||||
if err != nil {
|
||||
c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
// 角色权限集合,管理员拥有所有权限
|
||||
isAdmin := config.IsAdmin(loginUser.UserID)
|
||||
roles, perms := s.accountService.RoleAndMenuPerms(loginUser.UserID, isAdmin)
|
||||
|
||||
loginUser.User.NickName = i18n.TKey(language, loginUser.User.NickName)
|
||||
loginUser.User.Remark = i18n.TKey(language, loginUser.User.Remark)
|
||||
loginUser.User.Dept.DeptName = i18n.TKey(language, loginUser.User.Dept.DeptName)
|
||||
for ri := range loginUser.User.Roles {
|
||||
loginUser.User.Roles[ri].RoleName = i18n.TKey(language, loginUser.User.Roles[ri].RoleName)
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"user": loginUser.User,
|
||||
"roles": roles,
|
||||
"permissions": perms,
|
||||
}))
|
||||
}
|
||||
|
||||
// 登录用户路由信息
|
||||
//
|
||||
// GET /getRouters
|
||||
func (s *AccountController) Router(c *gin.Context) {
|
||||
userID := ctx.LoginUserToUserID(c)
|
||||
|
||||
// 前端路由,管理员拥有所有
|
||||
isAdmin := config.IsAdmin(userID)
|
||||
buildMenus := s.accountService.RouteMenus(userID, isAdmin)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var converI18n func(language string, arr *[]vo.Router)
|
||||
converI18n = func(language string, arr *[]vo.Router) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].Meta.Title = i18n.TKey(language, (*arr)[i].Meta.Title)
|
||||
if len((*arr)[i].Children) > 0 {
|
||||
converI18n(language, &(*arr)[i].Children)
|
||||
}
|
||||
}
|
||||
}
|
||||
converI18n(language, &buildMenus)
|
||||
|
||||
c.JSON(200, result.OkData(buildMenus))
|
||||
}
|
||||
|
||||
// 系统登出
|
||||
//
|
||||
// POST /logout
|
||||
func (s *AccountController) Logout(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
tokenStr := ctx.Authorization(c)
|
||||
if tokenStr != "" {
|
||||
// 存在token时记录退出信息
|
||||
userName := tokenUtils.Remove(tokenStr)
|
||||
if userName != "" {
|
||||
// 当前请求信息
|
||||
ipaddr, location := ctx.IPAddrLocation(c)
|
||||
os, browser := ctx.UaOsBrowser(c)
|
||||
|
||||
// 创建系统访问记录 退出成功
|
||||
|
||||
s.sysLogLoginService.CreateSysLogLogin(
|
||||
userName, commonConstants.STATUS_YES, "app.common.logoutSuccess",
|
||||
ipaddr, location, os, browser,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkMsg(i18n.TKey(language, "app.common.logoutSuccess")))
|
||||
}
|
||||
129
src/modules/common/controller/captcha.go
Normal file
129
src/modules/common/controller/captcha.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/config"
|
||||
"nms_nbi/src/framework/constants/cachekey"
|
||||
"nms_nbi/src/framework/constants/captcha"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/redis"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
// 实例化控制层 CaptchaController 结构体
|
||||
var NewCaptcha = &CaptchaController{
|
||||
sysConfigService: systemService.NewSysConfigImpl,
|
||||
}
|
||||
|
||||
// 验证码操作处理
|
||||
//
|
||||
// PATH /
|
||||
type CaptchaController struct {
|
||||
// 参数配置服务
|
||||
sysConfigService systemService.ISysConfig
|
||||
}
|
||||
|
||||
// 获取验证码
|
||||
//
|
||||
// GET /captchaImage
|
||||
func (s *CaptchaController) Image(c *gin.Context) {
|
||||
// 从数据库配置获取验证码开关 true开启,false关闭
|
||||
captchaEnabledStr := s.sysConfigService.SelectConfigValueByKey("sys.account.captchaEnabled")
|
||||
captchaEnabled := parse.Boolean(captchaEnabledStr)
|
||||
if !captchaEnabled {
|
||||
c.JSON(200, result.Ok(map[string]any{
|
||||
"captchaEnabled": captchaEnabled,
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
// 生成唯一标识
|
||||
verifyKey := ""
|
||||
data := map[string]any{
|
||||
"captchaEnabled": captchaEnabled,
|
||||
"uuid": "",
|
||||
"img": "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
|
||||
}
|
||||
|
||||
// 从数据库配置获取验证码类型 math 数值计算 char 字符验证
|
||||
captchaType := s.sysConfigService.SelectConfigValueByKey("sys.account.captchaType")
|
||||
if captchaType == captcha.TYPE_MATH {
|
||||
math := config.Get("mathCaptcha").(map[string]any)
|
||||
driverCaptcha := &base64Captcha.DriverMath{
|
||||
//Height png height in pixel.
|
||||
Height: math["height"].(int),
|
||||
// Width Captcha png width in pixel.
|
||||
Width: math["width"].(int),
|
||||
//NoiseCount text noise count.
|
||||
NoiseCount: math["noise"].(int),
|
||||
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
|
||||
ShowLineOptions: base64Captcha.OptionShowHollowLine,
|
||||
}
|
||||
if math["color"].(bool) {
|
||||
//BgColor captcha image background color (optional)
|
||||
driverCaptcha.BgColor = parse.Color(math["background"].(string))
|
||||
}
|
||||
// 验证码生成
|
||||
id, question, answer := driverCaptcha.GenerateIdQuestionAnswer()
|
||||
// 验证码表达式解析输出
|
||||
item, err := driverCaptcha.DrawCaptcha(question)
|
||||
if err != nil {
|
||||
logger.Infof("Generate Id Question Answer %s %s : %v", captchaType, question, err)
|
||||
} else {
|
||||
data["uuid"] = id
|
||||
data["img"] = item.EncodeB64string()
|
||||
expiration := captcha.EXPIRATION * time.Second
|
||||
verifyKey = cachekey.CAPTCHA_CODE_KEY + id
|
||||
redis.SetByExpire("", verifyKey, answer, expiration)
|
||||
}
|
||||
}
|
||||
if captchaType == captcha.TYPE_CHAR {
|
||||
char := config.Get("charCaptcha").(map[string]any)
|
||||
driverCaptcha := &base64Captcha.DriverString{
|
||||
//Height png height in pixel.
|
||||
Height: char["height"].(int),
|
||||
// Width Captcha png width in pixel.
|
||||
Width: char["width"].(int),
|
||||
//NoiseCount text noise count.
|
||||
NoiseCount: char["noise"].(int),
|
||||
//Length random string length.
|
||||
Length: char["size"].(int),
|
||||
//Source is a unicode which is the rand string from.
|
||||
Source: char["chars"].(string),
|
||||
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
|
||||
ShowLineOptions: base64Captcha.OptionShowHollowLine,
|
||||
}
|
||||
if char["color"].(bool) {
|
||||
//BgColor captcha image background color (optional)
|
||||
driverCaptcha.BgColor = parse.Color(char["background"].(string))
|
||||
}
|
||||
// 验证码生成
|
||||
id, question, answer := driverCaptcha.GenerateIdQuestionAnswer()
|
||||
// 验证码表达式解析输出
|
||||
item, err := driverCaptcha.DrawCaptcha(question)
|
||||
if err != nil {
|
||||
logger.Infof("Generate Id Question Answer %s %s : %v", captchaType, question, err)
|
||||
} else {
|
||||
data["uuid"] = id
|
||||
data["img"] = item.EncodeB64string()
|
||||
expiration := captcha.EXPIRATION * time.Second
|
||||
verifyKey = cachekey.CAPTCHA_CODE_KEY + id
|
||||
redis.SetByExpire("", verifyKey, answer, expiration)
|
||||
}
|
||||
}
|
||||
|
||||
// 本地开发下返回验证码结果,方便接口调试
|
||||
if config.Env() == "local" {
|
||||
text, _ := redis.Get("", verifyKey)
|
||||
data["text"] = text
|
||||
c.JSON(200, result.Ok(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
72
src/modules/common/controller/common.go
Normal file
72
src/modules/common/controller/common.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
commonService "nms_nbi/src/modules/common/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 CommontController 结构体
|
||||
var NewCommont = &CommontController{
|
||||
commontService: commonService.NewCommontImpl,
|
||||
}
|
||||
|
||||
// 通用请求
|
||||
//
|
||||
// PATH /
|
||||
type CommontController struct {
|
||||
// 通用请求服务
|
||||
commontService commonService.ICommont
|
||||
}
|
||||
|
||||
// 哈希加密
|
||||
//
|
||||
// POST /hash
|
||||
func (s *CommontController) Hash(c *gin.Context) {
|
||||
c.String(200, "commont Hash")
|
||||
}
|
||||
|
||||
// 多语言处理
|
||||
//
|
||||
// GET /i18n
|
||||
func (s *CommontController) I18n(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
|
||||
i18nLang := i18n.TKey(language, "i18n")
|
||||
hello := i18n.TKey(language, "hello")
|
||||
ivs := i18n.TTemplate(language, "errorFields", nil)
|
||||
errorFields := i18n.TTemplate(language, "errorFields", map[string]any{
|
||||
"num": 1000,
|
||||
"hello": "你好",
|
||||
"h2o2": false,
|
||||
"fileName": " ====",
|
||||
})
|
||||
c.JSON(200, map[string]any{
|
||||
"lang": language,
|
||||
"i18nLang": i18nLang,
|
||||
"ivs": ivs,
|
||||
"hello": hello,
|
||||
"errorFields": errorFields,
|
||||
})
|
||||
}
|
||||
|
||||
// 系统可暴露的配置信息
|
||||
//
|
||||
// GET /sys-conf
|
||||
func (s *CommontController) SysConfig(c *gin.Context) {
|
||||
data := s.commontService.SystemConfigInfo()
|
||||
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
converI18n := func(language string, arr *map[string]string) {
|
||||
for k, v := range *arr {
|
||||
(*arr)[k] = i18n.TKey(language, v)
|
||||
}
|
||||
}
|
||||
converI18n(language, &data)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
235
src/modules/common/controller/file.go
Normal file
235
src/modules/common/controller/file.go
Normal file
@@ -0,0 +1,235 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/config"
|
||||
"nms_nbi/src/framework/constants/uploadsubpath"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/file"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 FileController 结构体
|
||||
var NewFile = &FileController{}
|
||||
|
||||
// 文件操作处理
|
||||
//
|
||||
// PATH /
|
||||
type FileController struct{}
|
||||
|
||||
// 下载文件
|
||||
//
|
||||
// GET /download/:filePath
|
||||
func (s *FileController) Download(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
filePath := c.Param("filePath")
|
||||
if len(filePath) < 8 {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// base64解析出地址
|
||||
decodedBytes, err := base64.StdEncoding.DecodeString(filePath)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||||
return
|
||||
}
|
||||
routerPath := string(decodedBytes)
|
||||
// 地址文件名截取
|
||||
fileName := routerPath[strings.LastIndex(routerPath, "/")+1:]
|
||||
|
||||
// 响应头
|
||||
c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+url.QueryEscape(fileName)+`"`)
|
||||
c.Writer.Header().Set("Accept-Ranges", "bytes")
|
||||
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
||||
|
||||
// 断点续传
|
||||
headerRange := c.GetHeader("Range")
|
||||
resultMap, err := file.ReadUploadFileStream(routerPath, headerRange)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
if headerRange != "" {
|
||||
c.Writer.Header().Set("Content-Range", fmt.Sprint(resultMap["range"]))
|
||||
c.Writer.Header().Set("Content-Length", fmt.Sprint(resultMap["chunkSize"]))
|
||||
c.Status(206)
|
||||
} else {
|
||||
c.Writer.Header().Set("Content-Length", fmt.Sprint(resultMap["fileSize"]))
|
||||
c.Status(200)
|
||||
|
||||
}
|
||||
c.Writer.Write(resultMap["data"].([]byte))
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
//
|
||||
// POST /upload
|
||||
func (s *FileController) Upload(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// 上传的文件
|
||||
formFile, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 子路径
|
||||
subPath := c.PostForm("subPath")
|
||||
if _, ok := uploadsubpath.UploadSubpath[subPath]; !ok {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 上传文件转存
|
||||
upFilePath, err := file.TransferUploadFile(formFile, subPath, nil)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
newFileName := upFilePath[strings.LastIndex(upFilePath, "/")+1:]
|
||||
c.JSON(200, result.OkData(map[string]string{
|
||||
"url": "http://" + c.Request.Host + upFilePath,
|
||||
"fileName": upFilePath,
|
||||
"newFileName": newFileName,
|
||||
"originalFileName": formFile.Filename,
|
||||
}))
|
||||
}
|
||||
|
||||
// 切片文件检查
|
||||
//
|
||||
// POST /chunkCheck
|
||||
func (s *FileController) ChunkCheck(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
// 唯一标识
|
||||
Identifier string `json:"identifier" binding:"required"`
|
||||
// 文件名
|
||||
FileName string `json:"fileName" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindJSON(&body)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 读取标识目录
|
||||
chunks, err := file.ChunkCheckFile(body.Identifier, body.FileName)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(chunks))
|
||||
}
|
||||
|
||||
// 切片文件合并
|
||||
//
|
||||
// POST /chunkMerge
|
||||
func (s *FileController) ChunkMerge(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
// 唯一标识
|
||||
Identifier string `json:"identifier" binding:"required"`
|
||||
// 文件名
|
||||
FileName string `json:"fileName" binding:"required"`
|
||||
// 子路径类型
|
||||
SubPath string `json:"subPath" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindJSON(&body)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
if _, ok := uploadsubpath.UploadSubpath[body.SubPath]; !ok {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 切片文件合并
|
||||
mergeFilePath, err := file.ChunkMergeFile(body.Identifier, body.FileName, body.SubPath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
newFileName := mergeFilePath[strings.LastIndex(mergeFilePath, "/")+1:]
|
||||
c.JSON(200, result.OkData(map[string]string{
|
||||
"url": "http://" + c.Request.Host + mergeFilePath,
|
||||
"fileName": mergeFilePath,
|
||||
"newFileName": newFileName,
|
||||
"originalFileName": body.FileName,
|
||||
}))
|
||||
}
|
||||
|
||||
// 切片文件上传
|
||||
//
|
||||
// POST /chunkUpload
|
||||
func (s *FileController) ChunkUpload(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// 切片编号
|
||||
index := c.PostForm("index")
|
||||
// 切片唯一标识
|
||||
identifier := c.PostForm("identifier")
|
||||
// 上传的文件
|
||||
formFile, err := c.FormFile("file")
|
||||
if index == "" || identifier == "" || err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 上传文件转存
|
||||
chunkFilePath, err := file.TransferChunkUploadFile(formFile, index, identifier)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(206, result.OkData(chunkFilePath))
|
||||
}
|
||||
|
||||
// 转存指定对应文件到静态目录
|
||||
//
|
||||
// POST /transferStaticFile
|
||||
func (s *CommontController) TransferStaticFile(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
UploadPath string `json:"uploadPath" binding:"required"`
|
||||
StaticPath string `json:"staticPath" binding:"required"`
|
||||
Language string `json:"language" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 取语言前缀
|
||||
lang := strings.SplitN(body.Language, "_", 2)[0]
|
||||
|
||||
// 默认静态资源
|
||||
static := config.Get("staticFile.default").(map[string]any)
|
||||
dir, err := filepath.Abs(static["dir"].(string))
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
delPrefix := strings.Replace(body.StaticPath, static["prefix"].(string), "", 1)
|
||||
staticPath := strings.Replace(delPrefix, "{language}", lang, 1)
|
||||
newFile := fmt.Sprintf("%s%s", dir, staticPath)
|
||||
|
||||
err = file.CopyUploadFile(body.UploadPath, newFile)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
urlPath := strings.Replace(newFile, dir, static["prefix"].(string), 1)
|
||||
c.JSON(200, result.OkData(filepath.ToSlash(urlPath)))
|
||||
}
|
||||
29
src/modules/common/controller/index.go
Normal file
29
src/modules/common/controller/index.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/src/framework/config"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 IndexController 结构体
|
||||
var NewIndex = &IndexController{}
|
||||
|
||||
// 路由主页
|
||||
//
|
||||
// PATH /
|
||||
type IndexController struct{}
|
||||
|
||||
// 根路由
|
||||
//
|
||||
// GET /
|
||||
func (s *IndexController) Handler(c *gin.Context) {
|
||||
name := config.Get("framework.name").(string)
|
||||
version := config.Get("framework.version").(string)
|
||||
// str := "欢迎使用%s核心网管理平台,当前版本:%s,请通过前台地址访问。"
|
||||
str := "Welcome to the %s Core Network Management Platform, current version: %s, please access via the frontend address."
|
||||
c.JSON(200, result.OkMsg(fmt.Sprintf(str, name, version)))
|
||||
}
|
||||
91
src/modules/common/controller/register.go
Normal file
91
src/modules/common/controller/register.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
commonConstants "nms_nbi/src/framework/constants/common"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/regular"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
commonModel "nms_nbi/src/modules/common/model"
|
||||
commonService "nms_nbi/src/modules/common/service"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 RegisterController 结构体
|
||||
var NewRegister = &RegisterController{
|
||||
registerService: commonService.NewRegisterImpl,
|
||||
sysLogLoginService: systemService.NewSysLogLoginImpl,
|
||||
}
|
||||
|
||||
// 账号注册操作处理
|
||||
//
|
||||
// PATH /
|
||||
type RegisterController struct {
|
||||
// 账号注册操作服务
|
||||
registerService commonService.IRegister
|
||||
// 系统登录访问
|
||||
sysLogLoginService systemService.ISysLogLogin
|
||||
}
|
||||
|
||||
// 账号注册
|
||||
//
|
||||
// GET /register
|
||||
func (s *RegisterController) Register(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var registerBody commonModel.RegisterBody
|
||||
if err := c.ShouldBindJSON(®isterBody); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 判断必传参数
|
||||
if !regular.ValidUsername(registerBody.Username) {
|
||||
// 账号不能以数字开头,可包含大写小写字母,数字,且不少于5位
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "register.errUsername")))
|
||||
return
|
||||
}
|
||||
if !regular.ValidPassword(registerBody.Password) {
|
||||
// 登录密码至少包含大小写字母、数字、特殊符号,且不少于6位
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "register.errPasswd")))
|
||||
return
|
||||
}
|
||||
if registerBody.Password != registerBody.ConfirmPassword {
|
||||
// 用户确认输入密码不一致
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "register.errPasswdNotEq")))
|
||||
return
|
||||
}
|
||||
|
||||
// 当前请求信息
|
||||
ipaddr, location := ctx.IPAddrLocation(c)
|
||||
os, browser := ctx.UaOsBrowser(c)
|
||||
|
||||
// 校验验证码
|
||||
err := s.registerService.ValidateCaptcha(
|
||||
registerBody.Code,
|
||||
registerBody.UUID,
|
||||
)
|
||||
// 根据错误信息,创建系统访问记录
|
||||
if err != nil {
|
||||
s.sysLogLoginService.CreateSysLogLogin(
|
||||
registerBody.Username, commonConstants.STATUS_NO, err.Error(),
|
||||
ipaddr, location, os, browser,
|
||||
)
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := s.registerService.ByUserName(registerBody.Username, registerBody.Password, registerBody.UserType)
|
||||
if err == nil {
|
||||
msg := i18n.TTemplate(language, "register.successMsg", map[string]any{"name": registerBody.Username, "id": userID})
|
||||
s.sysLogLoginService.CreateSysLogLogin(
|
||||
registerBody.Username, commonConstants.STATUS_YES, msg,
|
||||
ipaddr, location, os, browser,
|
||||
)
|
||||
// 注册成功
|
||||
c.JSON(200, result.OkMsg(i18n.TKey(language, "register.success")))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
}
|
||||
16
src/modules/common/model/login_body.go
Normal file
16
src/modules/common/model/login_body.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
// LoginBody 用户登录对象
|
||||
type LoginBody struct {
|
||||
// Username 用户名
|
||||
Username string `json:"username" binding:"required"`
|
||||
|
||||
// Password 用户密码
|
||||
Password string `json:"password" binding:"required"`
|
||||
|
||||
// Code 验证码
|
||||
Code string `json:"code"`
|
||||
|
||||
// UUID 验证码唯一标识
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
22
src/modules/common/model/register_body.go
Normal file
22
src/modules/common/model/register_body.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package model
|
||||
|
||||
// RegisterBody 用户注册对象
|
||||
type RegisterBody struct {
|
||||
// Username 用户名
|
||||
Username string `json:"username" binding:"required"`
|
||||
|
||||
// Password 用户密码
|
||||
Password string `json:"password" binding:"required"`
|
||||
|
||||
// ConfirmPassword 用户确认密码
|
||||
ConfirmPassword string `json:"confirmPassword" binding:"required"`
|
||||
|
||||
// Code 验证码
|
||||
Code string `json:"code"`
|
||||
|
||||
// UUID 验证码唯一标识
|
||||
UUID string `json:"uuid"`
|
||||
|
||||
// UserType 标记用户类型
|
||||
UserType string `json:"userType"`
|
||||
}
|
||||
24
src/modules/common/service/account.go
Normal file
24
src/modules/common/service/account.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package service
|
||||
|
||||
import "nms_nbi/src/framework/vo"
|
||||
|
||||
// 账号身份操作服务 服务层接口
|
||||
type IAccount interface {
|
||||
// ValidateCaptcha 校验验证码
|
||||
ValidateCaptcha(code, uuid string) error
|
||||
|
||||
// LoginByUsername 登录生成token
|
||||
LoginByUsername(username, password string) (vo.LoginUser, error)
|
||||
|
||||
// UpdateLoginDateAndIP 更新登录时间和IP
|
||||
UpdateLoginDateAndIP(loginUser *vo.LoginUser) bool
|
||||
|
||||
// ClearLoginRecordCache 清除错误记录次数
|
||||
ClearLoginRecordCache(username string) bool
|
||||
|
||||
// RoleAndMenuPerms 角色和菜单数据权限
|
||||
RoleAndMenuPerms(userId string, isAdmin bool) ([]string, []string)
|
||||
|
||||
// RouteMenus 前端路由所需要的菜单
|
||||
RouteMenus(userId string, isAdmin bool) []vo.Router
|
||||
}
|
||||
190
src/modules/common/service/account.impl.go
Normal file
190
src/modules/common/service/account.impl.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/config"
|
||||
adminConstants "nms_nbi/src/framework/constants/admin"
|
||||
"nms_nbi/src/framework/constants/cachekey"
|
||||
"nms_nbi/src/framework/constants/common"
|
||||
"nms_nbi/src/framework/redis"
|
||||
"nms_nbi/src/framework/utils/crypto"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo"
|
||||
"nms_nbi/src/modules/system/model"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
)
|
||||
|
||||
// 实例化服务层 AccountImpl 结构体
|
||||
var NewAccountImpl = &AccountImpl{
|
||||
sysUserService: systemService.NewSysUserImpl,
|
||||
sysConfigService: systemService.NewSysConfigImpl,
|
||||
sysRoleService: systemService.NewSysRoleImpl,
|
||||
sysMenuService: systemService.NewSysMenuImpl,
|
||||
}
|
||||
|
||||
// 账号身份操作服务 服务层处理
|
||||
type AccountImpl struct {
|
||||
// 用户信息服务
|
||||
sysUserService systemService.ISysUser
|
||||
// 参数配置服务
|
||||
sysConfigService systemService.ISysConfig
|
||||
// 角色服务
|
||||
sysRoleService systemService.ISysRole
|
||||
// 菜单服务
|
||||
sysMenuService systemService.ISysMenu
|
||||
}
|
||||
|
||||
// ValidateCaptcha 校验验证码
|
||||
func (s *AccountImpl) ValidateCaptcha(code, uuid string) error {
|
||||
// 验证码检查,从数据库配置获取验证码开关 true开启,false关闭
|
||||
captchaEnabledStr := s.sysConfigService.SelectConfigValueByKey("sys.account.captchaEnabled")
|
||||
if !parse.Boolean(captchaEnabledStr) {
|
||||
return nil
|
||||
}
|
||||
if code == "" || uuid == "" {
|
||||
// 验证码信息错误
|
||||
return fmt.Errorf("captcha.err")
|
||||
}
|
||||
verifyKey := cachekey.CAPTCHA_CODE_KEY + uuid
|
||||
captcha, _ := redis.Get("", verifyKey)
|
||||
if captcha == "" {
|
||||
// 验证码已失效
|
||||
return fmt.Errorf("captcha.errValid")
|
||||
}
|
||||
redis.Del("", verifyKey)
|
||||
if captcha != code {
|
||||
// 验证码错误
|
||||
return fmt.Errorf("captcha.err")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoginByUsername 登录创建用户信息
|
||||
func (s *AccountImpl) LoginByUsername(username, password string) (vo.LoginUser, error) {
|
||||
loginUser := vo.LoginUser{}
|
||||
|
||||
// 检查密码重试次数
|
||||
retrykey, retryCount, lockTime, err := s.passwordRetryCount(username)
|
||||
if err != nil {
|
||||
return loginUser, err
|
||||
}
|
||||
|
||||
// 查询用户登录账号
|
||||
sysUser := s.sysUserService.SelectUserByUserName(username)
|
||||
if sysUser.UserName != username {
|
||||
return loginUser, fmt.Errorf("login.errNameOrPasswd")
|
||||
}
|
||||
if sysUser.DelFlag == common.STATUS_YES {
|
||||
// 对不起,您的账号已被删除
|
||||
return loginUser, fmt.Errorf("login.errDelFlag")
|
||||
}
|
||||
if sysUser.Status == common.STATUS_NO {
|
||||
return loginUser, fmt.Errorf("login.errStatus")
|
||||
}
|
||||
|
||||
// 检验用户密码
|
||||
compareBool := crypto.BcryptCompare(password, sysUser.Password)
|
||||
if !compareBool {
|
||||
redis.SetByExpire("", retrykey, retryCount+1, lockTime)
|
||||
// 用户不存在或密码错误
|
||||
return loginUser, fmt.Errorf("login.errNameOrPasswd")
|
||||
} else {
|
||||
// 清除错误记录次数
|
||||
s.ClearLoginRecordCache(username)
|
||||
}
|
||||
|
||||
// 登录用户信息
|
||||
loginUser.UserID = sysUser.UserID
|
||||
loginUser.DeptID = sysUser.DeptID
|
||||
loginUser.User = sysUser
|
||||
// 用户权限组标识
|
||||
isAdmin := config.IsAdmin(sysUser.UserID)
|
||||
if isAdmin {
|
||||
loginUser.Permissions = []string{adminConstants.PERMISSION}
|
||||
} else {
|
||||
perms := s.sysMenuService.SelectMenuPermsByUserId(sysUser.UserID)
|
||||
loginUser.Permissions = parse.RemoveDuplicates(perms)
|
||||
}
|
||||
return loginUser, nil
|
||||
}
|
||||
|
||||
// UpdateLoginDateAndIP 更新登录时间和IP
|
||||
func (s *AccountImpl) UpdateLoginDateAndIP(loginUser *vo.LoginUser) bool {
|
||||
sysUser := loginUser.User
|
||||
userInfo := model.SysUser{
|
||||
UserID: sysUser.UserID,
|
||||
LoginIP: sysUser.LoginIP,
|
||||
LoginDate: sysUser.LoginDate,
|
||||
UpdateBy: sysUser.UserName,
|
||||
}
|
||||
rows := s.sysUserService.UpdateUser(userInfo)
|
||||
return rows > 0
|
||||
}
|
||||
|
||||
// ClearLoginRecordCache 清除错误记录次数
|
||||
func (s *AccountImpl) ClearLoginRecordCache(username string) bool {
|
||||
cacheKey := cachekey.PWD_ERR_CNT_KEY + username
|
||||
hasKey, _ := redis.Has("", cacheKey)
|
||||
if hasKey {
|
||||
delOk, _ := redis.Del("", cacheKey)
|
||||
return delOk
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// passwordRetryCount 密码重试次数
|
||||
func (s *AccountImpl) passwordRetryCount(username string) (string, int64, time.Duration, error) {
|
||||
// 从数据库配置获取登录次数和错误锁定时间
|
||||
maxRetryCountStr := s.sysConfigService.SelectConfigValueByKey("sys.user.maxRetryCount")
|
||||
lockTimeStr := s.sysConfigService.SelectConfigValueByKey("sys.user.lockTime")
|
||||
|
||||
// 验证登录次数和错误锁定时间
|
||||
maxRetryCount := parse.Number(maxRetryCountStr)
|
||||
lockTime := parse.Number(lockTimeStr)
|
||||
// 验证缓存记录次数
|
||||
retrykey := cachekey.PWD_ERR_CNT_KEY + username
|
||||
retryCount, err := redis.Get("", retrykey)
|
||||
if retryCount == "" || err != nil {
|
||||
retryCount = "0"
|
||||
}
|
||||
// 是否超过错误值
|
||||
retryCountInt64 := parse.Number(retryCount)
|
||||
if retryCountInt64 >= maxRetryCount {
|
||||
// 密码输入错误多次,帐户已被锁定
|
||||
errorMsg := fmt.Errorf("login.errRetryPasswd")
|
||||
return retrykey, retryCountInt64, time.Duration(lockTime) * time.Minute, errorMsg
|
||||
}
|
||||
return retrykey, retryCountInt64, time.Duration(lockTime) * time.Minute, nil
|
||||
}
|
||||
|
||||
// RoleAndMenuPerms 角色和菜单数据权限
|
||||
func (s *AccountImpl) RoleAndMenuPerms(userId string, isAdmin bool) ([]string, []string) {
|
||||
if isAdmin {
|
||||
return []string{adminConstants.ROLE_KEY}, []string{adminConstants.PERMISSION}
|
||||
} else {
|
||||
// 角色key
|
||||
roleGroup := []string{}
|
||||
roles := s.sysRoleService.SelectRoleListByUserId(userId)
|
||||
for _, role := range roles {
|
||||
roleGroup = append(roleGroup, role.RoleKey)
|
||||
}
|
||||
// 菜单权限key
|
||||
perms := s.sysMenuService.SelectMenuPermsByUserId(userId)
|
||||
return parse.RemoveDuplicates(roleGroup), parse.RemoveDuplicates(perms)
|
||||
}
|
||||
}
|
||||
|
||||
// RouteMenus 前端路由所需要的菜单
|
||||
func (s *AccountImpl) RouteMenus(userId string, isAdmin bool) []vo.Router {
|
||||
var buildMenus []vo.Router
|
||||
if isAdmin {
|
||||
menus := s.sysMenuService.SelectMenuTreeByUserId("*")
|
||||
buildMenus = s.sysMenuService.BuildRouteMenus(menus, "")
|
||||
} else {
|
||||
menus := s.sysMenuService.SelectMenuTreeByUserId(userId)
|
||||
buildMenus = s.sysMenuService.BuildRouteMenus(menus, "")
|
||||
}
|
||||
return buildMenus
|
||||
}
|
||||
7
src/modules/common/service/commont.go
Normal file
7
src/modules/common/service/commont.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package service
|
||||
|
||||
// 通用请求 服务层接口
|
||||
type ICommont interface {
|
||||
// SystemConfigInfo 系统配置信息
|
||||
SystemConfigInfo() map[string]string
|
||||
}
|
||||
67
src/modules/common/service/commont.impl.go
Normal file
67
src/modules/common/service/commont.impl.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/lib/global"
|
||||
"nms_nbi/src/framework/config"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
)
|
||||
|
||||
// 实例化服务层 CommontImpl 结构体
|
||||
var NewCommontImpl = &CommontImpl{
|
||||
sysUserService: systemService.NewSysUserImpl,
|
||||
sysConfigService: systemService.NewSysConfigImpl,
|
||||
}
|
||||
|
||||
// 通用请求 服务层处理
|
||||
type CommontImpl struct {
|
||||
// 用户信息服务
|
||||
sysUserService systemService.ISysUser
|
||||
// 参数配置服务
|
||||
sysConfigService systemService.ISysConfig
|
||||
}
|
||||
|
||||
// SystemConfigInfo 系统配置信息
|
||||
func (s *CommontImpl) SystemConfigInfo() map[string]string {
|
||||
infoMap := map[string]string{}
|
||||
// 获取打包注入的全局变量信息
|
||||
infoMap["version"] = global.Version
|
||||
infoMap["buildTime"] = global.BuildTime
|
||||
infoMap["goVer"] = global.GoVer
|
||||
// 序列号
|
||||
infoMap["serialNum"] = fmt.Sprint(config.Get("omc.sn"))
|
||||
// 获取LOGO类型
|
||||
logoType := s.sysConfigService.SelectConfigValueByKey("sys.logo.type")
|
||||
infoMap["logoType"] = logoType
|
||||
// 获取LOGO文件
|
||||
filePathIcon := s.sysConfigService.SelectConfigValueByKey("sys.logo.filePathIcon")
|
||||
infoMap["filePathIcon"] = filePathIcon
|
||||
filePathBrand := s.sysConfigService.SelectConfigValueByKey("sys.logo.filePathBrand")
|
||||
infoMap["filePathBrand"] = filePathBrand
|
||||
// 获取系统名称
|
||||
title := s.sysConfigService.SelectConfigValueByKey("sys.title")
|
||||
infoMap["title"] = title
|
||||
// 获取版权声明
|
||||
copyright := s.sysConfigService.SelectConfigValueByKey("sys.copyright")
|
||||
infoMap["copyright"] = copyright
|
||||
// 获取是否开启用户注册功能
|
||||
registerUser := s.sysConfigService.SelectConfigValueByKey("sys.account.registerUser")
|
||||
infoMap["registerUser"] = registerUser
|
||||
// 获取登录界面背景
|
||||
loginBackground := s.sysConfigService.SelectConfigValueByKey("sys.loginBackground")
|
||||
infoMap["loginBackground"] = loginBackground
|
||||
// 系统设置-官网网址
|
||||
officialUrl := s.sysConfigService.SelectConfigValueByKey("sys.officialUrl")
|
||||
infoMap["officialUrl"] = officialUrl
|
||||
// 系统设置-系统使用文档
|
||||
helpDoc := s.sysConfigService.SelectConfigValueByKey("sys.helpDoc")
|
||||
infoMap["helpDoc"] = helpDoc
|
||||
// 国际化切换
|
||||
i18nOpen := s.sysConfigService.SelectConfigValueByKey("sys.i18n.open")
|
||||
infoMap["i18nOpen"] = i18nOpen
|
||||
// 国际化默认语言
|
||||
i18nDefault := s.sysConfigService.SelectConfigValueByKey("sys.i18n.default")
|
||||
infoMap["i18nDefault"] = i18nDefault
|
||||
return infoMap
|
||||
}
|
||||
10
src/modules/common/service/register.go
Normal file
10
src/modules/common/service/register.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package service
|
||||
|
||||
// 账号注册操作处理 服务层接口
|
||||
type IRegister interface {
|
||||
// ValidateCaptcha 校验验证码
|
||||
ValidateCaptcha(code, uuid string) error
|
||||
|
||||
// ByUserName 账号注册
|
||||
ByUserName(username, password, userType string) (string, error)
|
||||
}
|
||||
106
src/modules/common/service/register.impl.go
Normal file
106
src/modules/common/service/register.impl.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/src/framework/constants/cachekey"
|
||||
"nms_nbi/src/framework/constants/common"
|
||||
"nms_nbi/src/framework/redis"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
systemModel "nms_nbi/src/modules/system/model"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
)
|
||||
|
||||
// 实例化服务层 RegisterImpl 结构体
|
||||
var NewRegisterImpl = &RegisterImpl{
|
||||
sysUserService: systemService.NewSysUserImpl,
|
||||
sysConfigService: systemService.NewSysConfigImpl,
|
||||
sysRoleService: systemService.NewSysRoleImpl,
|
||||
}
|
||||
|
||||
// 账号注册操作处理 服务层处理
|
||||
type RegisterImpl struct {
|
||||
// 用户信息服务
|
||||
sysUserService systemService.ISysUser
|
||||
// 参数配置服务
|
||||
sysConfigService systemService.ISysConfig
|
||||
// 角色服务
|
||||
sysRoleService systemService.ISysRole
|
||||
}
|
||||
|
||||
// ValidateCaptcha 校验验证码
|
||||
func (s *RegisterImpl) ValidateCaptcha(code, uuid string) error {
|
||||
// 验证码检查,从数据库配置获取验证码开关 true开启,false关闭
|
||||
captchaEnabledStr := s.sysConfigService.SelectConfigValueByKey("sys.account.captchaEnabled")
|
||||
if !parse.Boolean(captchaEnabledStr) {
|
||||
return nil
|
||||
}
|
||||
if code == "" || uuid == "" {
|
||||
return fmt.Errorf("verification code information error")
|
||||
}
|
||||
verifyKey := cachekey.CAPTCHA_CODE_KEY + uuid
|
||||
captcha, err := redis.Get("", verifyKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the verification code has expired")
|
||||
}
|
||||
redis.Del("", verifyKey)
|
||||
if captcha != code {
|
||||
return fmt.Errorf("verification code error")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ByUserName 账号注册
|
||||
func (s *RegisterImpl) ByUserName(username, password, userType string) (string, error) {
|
||||
// 是否开启用户注册功能 true开启,false关闭
|
||||
registerUserStr := s.sysConfigService.SelectConfigValueByKey("sys.account.registerUser")
|
||||
captchaEnabled := parse.Boolean(registerUserStr)
|
||||
if !captchaEnabled {
|
||||
return "", fmt.Errorf("failed to register user [%s]. Sorry, the system has closed the external user registration channel", username)
|
||||
}
|
||||
|
||||
// 检查用户登录账号是否唯一
|
||||
uniqueUserName := s.sysUserService.CheckUniqueUserName(username, "")
|
||||
if !uniqueUserName {
|
||||
return "", fmt.Errorf("failed to register user [%s], registered account already exists", username)
|
||||
}
|
||||
|
||||
sysUser := systemModel.SysUser{
|
||||
UserName: username,
|
||||
NickName: username, // 昵称使用名称账号
|
||||
Password: password, // 原始密码
|
||||
Status: common.STATUS_YES, // 账号状态激活
|
||||
DeptID: "100", // 归属部门为根节点
|
||||
CreateBy: "register", // 创建来源
|
||||
}
|
||||
// 标记用户类型
|
||||
if userType == "" {
|
||||
sysUser.UserType = "sys"
|
||||
}
|
||||
// 新增用户的角色管理
|
||||
sysUser.RoleIDs = s.registerRoleInit(userType)
|
||||
// 新增用户的岗位管理
|
||||
sysUser.PostIDs = s.registerPostInit(userType)
|
||||
|
||||
insertId := s.sysUserService.InsertUser(sysUser)
|
||||
if insertId != "" {
|
||||
return insertId, nil
|
||||
}
|
||||
return "", fmt.Errorf("failed to register user [%s]. Please contact the system administrator", username)
|
||||
}
|
||||
|
||||
// registerRoleInit 注册初始角色
|
||||
func (s *RegisterImpl) registerRoleInit(userType string) []string {
|
||||
if userType == "sys" {
|
||||
return []string{}
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// registerPostInit 注册初始岗位
|
||||
func (s *RegisterImpl) registerPostInit(userType string) []string {
|
||||
if userType == "sys" {
|
||||
return []string{}
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
23
src/modules/crontask/crontask.go
Normal file
23
src/modules/crontask/crontask.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package crontask
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/modules/crontask/processor"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Setup 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> crontask 模块路由")
|
||||
|
||||
// 启动时需要的初始参数
|
||||
InitLoad()
|
||||
|
||||
}
|
||||
|
||||
// InitLoad 初始参数
|
||||
func InitLoad() {
|
||||
// 初始化定时任务处理
|
||||
processor.InitCronQueue()
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package backupEtcFromNE
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/lib/dborm"
|
||||
"nms_nbi/lib/global"
|
||||
"nms_nbi/lib/log"
|
||||
"nms_nbi/restagent/config"
|
||||
"nms_nbi/src/framework/cron"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
type BarParams struct {
|
||||
Duration int `json:"duration"`
|
||||
TableName string `json:"tableName"`
|
||||
ColName string `json:"colName"` // column name of time string
|
||||
Extras string `json:"extras"` // extras condition for where
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
log.Infof("execute %d,last progress: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
// var params BarParams
|
||||
|
||||
// err := json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
log.Infof("Repeat %v Job ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
var nes []dborm.NeInfo
|
||||
_, err := dborm.XormGetAllNeInfo(&nes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var successfulNEs, failureNEs []string
|
||||
for _, neInfo := range nes {
|
||||
neTypeUpper := strings.ToUpper(neInfo.NeType)
|
||||
neTypeLower := strings.ToLower(neInfo.NeType)
|
||||
nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
isExist, err := global.PathExists(nePath)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to PathExists:", err)
|
||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
continue
|
||||
}
|
||||
if isExist {
|
||||
err = os.RemoveAll(nePath)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to RemoveAll:", err)
|
||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
continue
|
||||
}
|
||||
}
|
||||
err = os.MkdirAll(nePath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to MkdirAll:", err)
|
||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
continue
|
||||
}
|
||||
|
||||
var scpCmd string
|
||||
ipType := global.ParseIPAddr(neInfo.Ip)
|
||||
omcNetypeLower := strings.ToLower(config.GetYamlConfig().OMC.NeType)
|
||||
etcListIMS := "{*.yaml,mmtel,vars.cfg}"
|
||||
if config.GetYamlConfig().NE.EtcListIMS != "" {
|
||||
etcListIMS = config.GetYamlConfig().NE.EtcListIMS
|
||||
}
|
||||
switch neTypeLower {
|
||||
case omcNetypeLower:
|
||||
if ipType == global.IsIPv4 {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@%s:%s/etc/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.OmcDir, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
} else {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@[%s]:%s/etc/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.OmcDir, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
}
|
||||
|
||||
case "ims":
|
||||
if ipType == global.IsIPv4 {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@%s:%s/%s/%s %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir, neTypeLower,
|
||||
etcListIMS, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
} else {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@[%s]:%s/%s/%s %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir, neTypeLower,
|
||||
etcListIMS, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
}
|
||||
|
||||
case "mme":
|
||||
if ipType == global.IsIPv4 {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@%s:%s/%s/*.conf %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir,
|
||||
neTypeLower, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
} else {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@[%s]:%s/%s/*.conf %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir,
|
||||
neTypeLower, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
}
|
||||
|
||||
default:
|
||||
if ipType == global.IsIPv4 {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@%s:%s/%s/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir,
|
||||
neTypeLower, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
} else {
|
||||
scpCmd = fmt.Sprintf("scp -r %s@[%s]:%s/%s/*.yaml %s/etc/%s", config.GetYamlConfig().NE.User,
|
||||
neInfo.Ip, config.GetYamlConfig().NE.EtcDir,
|
||||
neTypeLower, config.GetYamlConfig().OMC.Backup, neTypeLower)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
zipFile := fmt.Sprintf("%s-%s-etc-%s.zip", neTypeLower, strings.ToLower(neInfo.NeId), time.Now().Format(global.DateData))
|
||||
zipFilePath := config.GetYamlConfig().OMC.Backup + "/" + zipFile
|
||||
zipCmd := fmt.Sprintf("cd %s/etc && zip -r %s %s/*", config.GetYamlConfig().OMC.Backup, zipFilePath, neTypeLower)
|
||||
|
||||
command := fmt.Sprintf("%s&&%s", scpCmd, zipCmd)
|
||||
|
||||
log.Trace("command:", command)
|
||||
out, err := global.ExecCmd(command)
|
||||
if err != nil {
|
||||
log.Error("Faile to exec command:", err)
|
||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
continue
|
||||
}
|
||||
log.Trace("command output:", out)
|
||||
|
||||
md5Sum, err := global.GetFileMD5Sum(zipFilePath)
|
||||
if err != nil {
|
||||
log.Error("Faile to md5sum:", err)
|
||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
continue
|
||||
}
|
||||
//log.Debug("md5Str:", md5Sum)
|
||||
path := config.GetYamlConfig().OMC.Backup
|
||||
neBackup := dborm.NeBackup{NeType: neTypeUpper, NeId: neInfo.NeId, FileName: zipFile, Path: path, Md5Sum: md5Sum}
|
||||
_, err = dborm.XormInsertTableOne("ne_backup", neBackup)
|
||||
if err != nil {
|
||||
log.Error("Faile to XormInsertTableOne:", err)
|
||||
failureNEs = append(failureNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
continue
|
||||
}
|
||||
successfulNEs = append(successfulNEs, neInfo.NeType+"/"+neInfo.NeId)
|
||||
}
|
||||
|
||||
log.Infof("successfulNEs: %s failureNEs: %s", successfulNEs, failureNEs)
|
||||
// result
|
||||
return map[string]any{
|
||||
"successfulNEs": successfulNEs,
|
||||
"failureNEs": failureNEs,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package delExpiredNeBackup
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/lib/dborm"
|
||||
"nms_nbi/lib/log"
|
||||
"nms_nbi/src/framework/cron"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
type BarParams struct {
|
||||
Duration int `json:"duration"`
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
log.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
var params BarParams
|
||||
duration := 60
|
||||
|
||||
err := json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
||||
if err == nil {
|
||||
duration = params.Duration
|
||||
}
|
||||
log.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// // 实现任务处理逻辑
|
||||
// i := 0
|
||||
// s.progress = i
|
||||
// for i < 5 {
|
||||
// // 获取任务进度
|
||||
// progress := s.progress
|
||||
// log.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
|
||||
// }
|
||||
where := fmt.Sprintf("NOW()>ADDDATE(`create_time`,interval %d day)", duration)
|
||||
affected, err := dborm.XormDeleteDataByWhere(where, "ne_backup")
|
||||
if err != nil {
|
||||
// panic(fmt.Sprintf("Failed to XormDeleteDataByWhere:%v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// delete expired files in backup directory
|
||||
// todo ...
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"msg": "sucess",
|
||||
"affected": affected,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package deleteExpiredRecord
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/lib/dborm"
|
||||
"nms_nbi/lib/log"
|
||||
"nms_nbi/src/framework/cron"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
type BarParams struct {
|
||||
Duration int `json:"duration"`
|
||||
TableName string `json:"tableName"`
|
||||
ColName string `json:"colName"` // column name of time string
|
||||
Extras string `json:"extras"` // extras condition for where
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
log.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
var params BarParams
|
||||
|
||||
err := json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//duration = params.Duration
|
||||
log.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// // 实现任务处理逻辑
|
||||
// i := 0
|
||||
// s.progress = i
|
||||
// for i < 5 {
|
||||
// // 获取任务进度
|
||||
// progress := s.progress
|
||||
// log.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
|
||||
// }
|
||||
|
||||
var where string
|
||||
if params.Extras == "" {
|
||||
where = fmt.Sprintf("NOW()>ADDDATE(`%s`,interval %d day)", params.ColName, params.Duration)
|
||||
} else {
|
||||
where = fmt.Sprintf("NOW()>ADDDATE(`%s`,interval %d day) and %s", params.ColName, params.Duration, params.Extras)
|
||||
}
|
||||
|
||||
affected, err := dborm.XormDeleteDataByWhere(where, params.TableName)
|
||||
if err != nil {
|
||||
// panic(fmt.Sprintf("Failed to XormDeleteDataByWhere:%v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"msg": "sucess",
|
||||
"affected": affected,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
package genNeStateAlarm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/features/fm"
|
||||
"nms_nbi/lib/dborm"
|
||||
"nms_nbi/lib/global"
|
||||
"nms_nbi/lib/log"
|
||||
"nms_nbi/restagent/config"
|
||||
"nms_nbi/src/framework/cron"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
type BarParams struct {
|
||||
AlarmID string `json:"alarmID"`
|
||||
AlarmCode int `json:"alarmCode"`
|
||||
AlarmTitle string `json:"alarmTitle"`
|
||||
AlarmType string `json:"alarmType"`
|
||||
OrigSeverity string `json:"origSeverity"`
|
||||
ObjectUID string `json:"objectUID"`
|
||||
ObjectName string `json:"objectName"`
|
||||
ObjectType string `json:"objectType"`
|
||||
SpecificProblem string `json:"specificProblem"`
|
||||
SpecificProblemID string `json:"specificProblemID"`
|
||||
AddInfo string `json:"AddInfo"`
|
||||
Threshold int64 `json:"threshold"`
|
||||
}
|
||||
|
||||
// type BarParams struct {
|
||||
// Duration int `json:"duration"`
|
||||
// }
|
||||
|
||||
type Alarm struct {
|
||||
Id int `json:"-" xorm:"pk 'id' autoincr"`
|
||||
AlarmSeq int `json:"alarmSeq"`
|
||||
AlarmId string `json:"alarmId" xorm:"alarm_id"`
|
||||
NeId string `json:"neId"`
|
||||
AlarmCode int `json:"alarmCode"`
|
||||
AlarmTitle string `json:"alarmTitle"`
|
||||
EventTime string `json:"eventTime"`
|
||||
AlarmType string `json:"alarmType"`
|
||||
OrigSeverity string `json:"origSeverity"`
|
||||
PerceivedSeverity string `json:"perceivedSeverity"`
|
||||
PVFlag string `json:"pvFlag" xorm:"pv_flag"`
|
||||
NeName string `json:"neName"`
|
||||
NeType string `json:"neType"`
|
||||
ObjectUid string `json:"objectUid" xorm:"object_uid"`
|
||||
ObjectName string `json:"objectName" xorm:"object_name"`
|
||||
ObjectType string `json:"objectType" xorm:"object_type"`
|
||||
LocationInfo string `json:"locationInfo"`
|
||||
Province string `json:"province"`
|
||||
AlarmStatus int `json:"alarmStatus" xorm:"alarm_status"`
|
||||
SpecificProblem string `json:"specificProblem"`
|
||||
SpecificProblemID string `json:"specificProblemID" xorm:"specific_problem_id"`
|
||||
AddInfo string `json:"addInfo"`
|
||||
|
||||
// ClearType int `json:"-" xorm:"clear_type"` // 0: Unclear, 1: Auto clear, 2: Manual clear
|
||||
// ClearTime sql.NullTime `json:"-" xorm:"clear_time"`
|
||||
}
|
||||
|
||||
var client = resty.New()
|
||||
|
||||
func init() {
|
||||
client.
|
||||
SetTimeout(time.Duration(400 * time.Millisecond))
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
var err error
|
||||
|
||||
s.count++
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
var alarmDefine BarParams
|
||||
|
||||
err = json.Unmarshal([]byte(sysJob.TargetParams), &alarmDefine)
|
||||
if err != nil {
|
||||
log.Error("Failed to Unmarshal:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var nes []dborm.NeInfo
|
||||
_, err = dborm.XormGetAllNeInfo(&nes)
|
||||
if err != nil {
|
||||
log.Error("Failed to get all ne info:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
succActiveAlarmNum := 0
|
||||
failActiveAlarmNum := 0
|
||||
succClearAlarmNum := 0
|
||||
failClearAlarmNum := 0
|
||||
|
||||
for _, ne := range nes {
|
||||
//log.Debug("ne:", ne)
|
||||
|
||||
sql := fmt.Sprintf("select * from ne_state where ne_type = '%s' and ne_id = '%s' order by timestamp desc limit 1", ne.NeType, ne.NeId)
|
||||
neState, err := dborm.XormGetDataBySQL(sql)
|
||||
if err != nil {
|
||||
log.Error("Failed to get ne_state:", err)
|
||||
continue
|
||||
}
|
||||
if len(*neState) == 0 {
|
||||
log.Warn("Not found record in ne_state:")
|
||||
continue
|
||||
}
|
||||
//log.Debug("neState:", *neState)
|
||||
|
||||
// params := "10000"
|
||||
|
||||
// alarmDefine, err := dborm.XormGetAlarmDefine(params)
|
||||
// if err != nil {
|
||||
// log.Error("Failed to get alarm_define:", err)
|
||||
// continue
|
||||
// } else if alarmDefine == nil {
|
||||
// log.Error("Not found data from alarm_define")
|
||||
// continue
|
||||
// }
|
||||
|
||||
// log.Debug("alarmDefine:", alarmDefine)
|
||||
|
||||
sql = fmt.Sprintf("select * from alarm where alarm_id = '%s' and ne_type='%s' and ne_id = '%s' order by event_time desc limit 1",
|
||||
alarmDefine.AlarmID, ne.NeType, ne.RmUID)
|
||||
alarm, err := dborm.XormGetDataBySQL(sql)
|
||||
if err != nil {
|
||||
log.Error("Failed to get alarm:", err)
|
||||
continue
|
||||
}
|
||||
//log.Debug("alarm:", *alarm)
|
||||
|
||||
var timestamp string
|
||||
if len(*neState) == 0 {
|
||||
log.Infof("Not found ne_state neType:%s, neId:%s", ne.NeType, ne.NeId)
|
||||
timestamp = ne.UpdateTime
|
||||
} else {
|
||||
timestamp = (*neState)[0]["timestamp"]
|
||||
}
|
||||
|
||||
// 解析日期时间字符串为时间对象
|
||||
seconds, err := global.GetSecondsSinceDatetime(timestamp)
|
||||
if err != nil {
|
||||
log.Error("Failed to GetSecondsSinceDatetime:", err)
|
||||
continue
|
||||
}
|
||||
log.Debugf("timestamp:%s seconds:%d", timestamp, seconds)
|
||||
|
||||
if seconds <= alarmDefine.Threshold {
|
||||
if len(*alarm) == 0 || (*alarm)[0]["alarm_status"] == fm.AlarmStatusClearString {
|
||||
continue
|
||||
}
|
||||
|
||||
// clear alarm, todo
|
||||
var alarmSeq int = 1
|
||||
threshold := strconv.FormatInt(alarmDefine.Threshold, 10)
|
||||
SpecificProblem := strings.ReplaceAll(alarmDefine.SpecificProblem, "{threshold}", threshold)
|
||||
locationInfo := fmt.Sprintf("SystemManagement.State: NE heartbeat timestamp=%s,threshold=%v", timestamp, alarmDefine.Threshold)
|
||||
alarmData := &Alarm{
|
||||
AlarmSeq: alarmSeq,
|
||||
AlarmId: alarmDefine.AlarmID,
|
||||
NeId: ne.RmUID,
|
||||
NeType: ne.NeType,
|
||||
NeName: ne.NeName,
|
||||
Province: ne.Province,
|
||||
PVFlag: ne.PvFlag,
|
||||
AlarmCode: alarmDefine.AlarmCode,
|
||||
AlarmTitle: alarmDefine.AlarmTitle,
|
||||
AlarmType: alarmDefine.AlarmType,
|
||||
AlarmStatus: fm.AlarmStatusClear,
|
||||
OrigSeverity: alarmDefine.OrigSeverity,
|
||||
ObjectUid: alarmDefine.ObjectUID,
|
||||
ObjectName: alarmDefine.ObjectName,
|
||||
ObjectType: alarmDefine.ObjectType,
|
||||
LocationInfo: locationInfo,
|
||||
SpecificProblem: SpecificProblem,
|
||||
SpecificProblemID: alarmDefine.SpecificProblemID,
|
||||
AddInfo: alarmDefine.AddInfo,
|
||||
EventTime: time.Now().Local().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
alarmArray := &[]Alarm{*alarmData}
|
||||
body, _ := json.Marshal(alarmArray)
|
||||
//log.Debug("body: ", string(body))
|
||||
|
||||
var response *resty.Response
|
||||
requestURI := fmt.Sprintf("/api/rest/faultManagement/v1/elementType/%s/objectType/alarms", ne.NeType)
|
||||
restHost := fmt.Sprintf("http://127.0.0.1:%d", config.GetYamlConfig().Rest[0].Port)
|
||||
requestURL := fmt.Sprintf("%s%s", restHost, requestURI)
|
||||
log.Debug("requestURL: POST ", requestURL)
|
||||
response, err = client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
SetBody(body).
|
||||
Post(requestURL)
|
||||
if err != nil {
|
||||
log.Error("Failed to post:", err)
|
||||
failClearAlarmNum++
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug("StatusCode: ", response.StatusCode())
|
||||
switch response.StatusCode() {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
succClearAlarmNum++
|
||||
default:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
failClearAlarmNum++
|
||||
}
|
||||
} else {
|
||||
var alarmSeq int = 1
|
||||
if len(*alarm) > 0 && (*alarm)[0]["alarm_status"] == fm.AlarmStatusActiveString {
|
||||
log.Info("System state alarm has exist")
|
||||
continue
|
||||
}
|
||||
|
||||
threshold := strconv.FormatInt(alarmDefine.Threshold, 10)
|
||||
SpecificProblem := strings.ReplaceAll(alarmDefine.SpecificProblem, "{threshold}", threshold)
|
||||
locationInfo := fmt.Sprintf("SystemManagement.State: NE heartbeat timestamp=%s,threshold=%v", timestamp, alarmDefine.Threshold)
|
||||
alarmData := &Alarm{
|
||||
AlarmSeq: alarmSeq,
|
||||
AlarmId: alarmDefine.AlarmID,
|
||||
NeId: ne.RmUID,
|
||||
NeType: ne.NeType,
|
||||
NeName: ne.NeName,
|
||||
Province: ne.Province,
|
||||
PVFlag: ne.PvFlag,
|
||||
AlarmCode: alarmDefine.AlarmCode,
|
||||
AlarmTitle: alarmDefine.AlarmTitle,
|
||||
AlarmType: alarmDefine.AlarmType,
|
||||
AlarmStatus: fm.AlarmStatusActive,
|
||||
OrigSeverity: alarmDefine.OrigSeverity,
|
||||
ObjectUid: alarmDefine.ObjectUID,
|
||||
ObjectName: alarmDefine.ObjectName,
|
||||
ObjectType: alarmDefine.ObjectType,
|
||||
LocationInfo: locationInfo,
|
||||
SpecificProblem: SpecificProblem,
|
||||
SpecificProblemID: alarmDefine.SpecificProblemID,
|
||||
AddInfo: alarmDefine.AddInfo,
|
||||
EventTime: time.Now().Local().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
alarmArray := &[]Alarm{*alarmData}
|
||||
body, _ := json.Marshal(alarmArray)
|
||||
//log.Debug("body: ", string(body))
|
||||
|
||||
var response *resty.Response
|
||||
requestURI := fmt.Sprintf("/api/rest/faultManagement/v1/elementType/%s/objectType/alarms", ne.NeType)
|
||||
restHost := fmt.Sprintf("http://127.0.0.1:%d", config.GetYamlConfig().Rest[0].Port)
|
||||
requestURL := fmt.Sprintf("%s%s", restHost, requestURI)
|
||||
log.Debug("requestURL: POST ", requestURL)
|
||||
response, err = client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
SetBody(body).
|
||||
Post(requestURL)
|
||||
if err != nil {
|
||||
log.Error("Failed to post:", err)
|
||||
failActiveAlarmNum++
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug("StatusCode: ", response.StatusCode())
|
||||
switch response.StatusCode() {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
succActiveAlarmNum++
|
||||
default:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
failActiveAlarmNum++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"succActiveAlarmNum": succActiveAlarmNum,
|
||||
"failActiveAlarmNum": failActiveAlarmNum,
|
||||
"succClearAlarmNum": succClearAlarmNum,
|
||||
"failClearAlarmNum": failClearAlarmNum,
|
||||
}, nil
|
||||
}
|
||||
158
src/modules/crontask/processor/getStateFromNE/getStateFromNE.go
Normal file
158
src/modules/crontask/processor/getStateFromNE/getStateFromNE.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package getStateFromNE
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/lib/dborm"
|
||||
"nms_nbi/lib/log"
|
||||
"nms_nbi/restagent/config"
|
||||
"nms_nbi/src/framework/cron"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
type BarParams struct {
|
||||
Duration int `json:"duration"`
|
||||
}
|
||||
|
||||
type CpuUsage struct {
|
||||
NfCpuUsage uint16 `json:"nfCpuUsage"`
|
||||
SysCpuUsage uint16 `json:"sysCpuUsage"`
|
||||
}
|
||||
|
||||
type MemUsage struct {
|
||||
TotalMem uint32 `json:"totalMem"`
|
||||
NfUsedMem uint32 `json:"nfUsedMem"`
|
||||
SysMemUsage uint16 `json:"sysMemUsage"`
|
||||
}
|
||||
|
||||
type PartitionInfo struct {
|
||||
Total uint32 `json:"total"` // MB
|
||||
Used uint32 `json:"used"` // MB
|
||||
}
|
||||
|
||||
type DiskSpace struct {
|
||||
PartitionNum uint8 `json:"partitionNum"`
|
||||
|
||||
PartitionInfo []PartitionInfo `json:"partitionInfo"`
|
||||
}
|
||||
|
||||
type SystemState struct {
|
||||
Version string `json:"version"`
|
||||
Capability uint32 `json:"capability"`
|
||||
SerialNum string `json:"serialNum"`
|
||||
ExpiryDate string `json:"expiryDate"`
|
||||
//Timestamp string `json:"timestamp"`
|
||||
|
||||
CpuUsage CpuUsage `json:"cpuUsage"`
|
||||
MemUsage MemUsage `json:"memUsage"`
|
||||
|
||||
DiskSpace DiskSpace `json:"diskSpace"`
|
||||
}
|
||||
|
||||
var client = resty.New()
|
||||
|
||||
func init() {
|
||||
client.
|
||||
SetTimeout(time.Duration(400 * time.Millisecond))
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
var err error
|
||||
|
||||
s.count++
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
var params BarParams
|
||||
|
||||
_ = json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
||||
// if err == nil {
|
||||
// duration = params.Duration
|
||||
// }
|
||||
|
||||
var nes []dborm.NeInfo
|
||||
_, err = dborm.XormGetAllNeInfo(&nes)
|
||||
if err != nil {
|
||||
log.Error("Failed to get all ne info:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
failNum := 0
|
||||
succNum := 0
|
||||
for _, ne := range nes {
|
||||
requestURI := fmt.Sprintf("/api/rest/systemManagement/v1/elementType/%s/objectType/systemState", strings.ToLower(ne.NeType))
|
||||
requestURL := fmt.Sprintf("http://%s:%s%s", ne.Ip, ne.Port, requestURI)
|
||||
log.Debug("requestURL: Get", requestURL)
|
||||
response, err := client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
Get(requestURL)
|
||||
if err != nil {
|
||||
log.Error("Failed to Get:", err)
|
||||
failNum++
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug("StatusCode: ", response.StatusCode())
|
||||
switch response.StatusCode() {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
|
||||
log.Trace("response body:", string(response.Body()))
|
||||
state := new(SystemState)
|
||||
_ = json.Unmarshal(response.Body(), &state)
|
||||
var dateStr *string = nil
|
||||
if state.ExpiryDate != "" && state.ExpiryDate != "-" {
|
||||
dateStr = &state.ExpiryDate
|
||||
}
|
||||
neState := new(dborm.NeState)
|
||||
neState.NeType = ne.NeType
|
||||
neState.NeId = ne.NeId
|
||||
neState.Version = state.Version
|
||||
neState.Capability = state.Capability
|
||||
neState.SerialNum = state.SerialNum
|
||||
neState.ExpiryDate = *dateStr
|
||||
cu, _ := json.Marshal(state.CpuUsage)
|
||||
neState.CpuUsage = string(cu)
|
||||
mu, _ := json.Marshal(state.MemUsage)
|
||||
neState.MemUsage = string(mu)
|
||||
ds, _ := json.Marshal(state.DiskSpace)
|
||||
neState.DiskSpace = string(ds)
|
||||
log.Trace("neState:", neState)
|
||||
_, err := dborm.XormInsertNeState(neState)
|
||||
if err != nil {
|
||||
log.Error("Failed to insert ne_state:", err)
|
||||
failNum++
|
||||
continue
|
||||
}
|
||||
succNum++
|
||||
default:
|
||||
log.Trace("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
failNum++
|
||||
}
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"succNum": succNum,
|
||||
"failNum": failNum,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package monitorsysresource
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/src/framework/logger"
|
||||
monitorService "nms_nbi/src/modules/monitor/service"
|
||||
)
|
||||
|
||||
var NewProcessor = &MonitorSysResourceProcessor{
|
||||
monitorService: monitorService.NewMonitorImpl,
|
||||
count: 0,
|
||||
openDataCancel: false,
|
||||
}
|
||||
|
||||
// MonitorSysResourceProcessor 系统资源CPU/IO/Netword收集
|
||||
type MonitorSysResourceProcessor struct {
|
||||
// 服务器系统相关信息服务
|
||||
monitorService monitorService.IMonitor
|
||||
// 执行次数
|
||||
count int
|
||||
// 是否已经开启数据通道
|
||||
openDataCancel bool
|
||||
}
|
||||
|
||||
func (s *MonitorSysResourceProcessor) Execute(data any) (any, error) {
|
||||
s.count++ // 执行次数加一
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// 读取参数值
|
||||
var params struct {
|
||||
Interval float64 `json:"interval"`
|
||||
}
|
||||
err := json.Unmarshal([]byte(sysJob.TargetParams), ¶ms)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("json params 'interval' err: %v", err)
|
||||
}
|
||||
|
||||
// 避免重复开启chan通道
|
||||
if !s.openDataCancel {
|
||||
s.monitorService.RunMonitorDataCancel(false, params.Interval)
|
||||
s.openDataCancel = true
|
||||
}
|
||||
|
||||
s.monitorService.RunMonitor()
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
result := map[string]any{
|
||||
"count": s.count,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
23
src/modules/crontask/processor/processor.go
Normal file
23
src/modules/crontask/processor/processor.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/src/modules/crontask/processor/backupEtcFromNE"
|
||||
"nms_nbi/src/modules/crontask/processor/delExpiredNeBackup"
|
||||
"nms_nbi/src/modules/crontask/processor/deleteExpiredRecord"
|
||||
"nms_nbi/src/modules/crontask/processor/genNeStateAlarm"
|
||||
"nms_nbi/src/modules/crontask/processor/getStateFromNE"
|
||||
monitorsysresource "nms_nbi/src/modules/crontask/processor/monitor_sys_resource"
|
||||
)
|
||||
|
||||
// InitCronQueue 初始定时任务队列
|
||||
func InitCronQueue() {
|
||||
// 监控-系统资源
|
||||
cron.CreateQueue("monitor_sys_resource", monitorsysresource.NewProcessor)
|
||||
// delete expired NE backup file
|
||||
cron.CreateQueue("delExpiredNeBackup", delExpiredNeBackup.NewProcessor)
|
||||
cron.CreateQueue("deleteExpiredRecord", deleteExpiredRecord.NewProcessor)
|
||||
cron.CreateQueue("backupEtcFromNE", backupEtcFromNE.NewProcessor)
|
||||
cron.CreateQueue("getStateFromNE", getStateFromNE.NewProcessor)
|
||||
cron.CreateQueue("genNeStateAlarm", genNeStateAlarm.NewProcessor)
|
||||
}
|
||||
60
src/modules/monitor/controller/monitor.go
Normal file
60
src/modules/monitor/controller/monitor.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/monitor/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 MonitorInfoController 结构体
|
||||
var NewMonitor = &MonitorController{
|
||||
monitorService: service.NewMonitorImpl,
|
||||
}
|
||||
|
||||
// 服务器资源监控信息
|
||||
//
|
||||
// PATH /monitor
|
||||
type MonitorController struct {
|
||||
// 服务器系统相关信息服务
|
||||
monitorService service.IMonitor
|
||||
}
|
||||
|
||||
// 资源监控信息加载
|
||||
//
|
||||
// GET /load
|
||||
func (s *MonitorController) Load(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
// 数据类型all/load/cpu/memory/io/network
|
||||
Type string `form:"type" binding:"required,oneof=all load cpu memory io network"`
|
||||
// 开始时间
|
||||
StartTime int64 `form:"startTime" binding:"required"`
|
||||
// 结束时间
|
||||
EndTime int64 `form:"endTime" binding:"required"`
|
||||
// 网元类型
|
||||
NeType string `form:"neType"`
|
||||
// 网元ID
|
||||
NeID string `form:"neId"`
|
||||
// 名称,networ和iok时有效
|
||||
Name string `form:"name"`
|
||||
}
|
||||
err := c.ShouldBindQuery(&querys)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
data := s.monitorService.SelectMonitorInfo(map[string]any{
|
||||
"type": querys.Type,
|
||||
"startTime": querys.StartTime,
|
||||
"endTime": querys.EndTime,
|
||||
"neType": querys.NeType,
|
||||
"neId": querys.NeID,
|
||||
"name": querys.Name,
|
||||
})
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
162
src/modules/monitor/controller/sys_cache.go
Normal file
162
src/modules/monitor/controller/sys_cache.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/constants/cachekey"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/redis"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
caches := []model.SysCache{
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.user"), cachekey.LOGIN_TOKEN_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.sys_config"), cachekey.SYS_CONFIG_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.sys_dict"), cachekey.SYS_DICT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.captcha_codes"), cachekey.CAPTCHA_CODE_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.repeat_submit"), cachekey.REPEAT_SUBMIT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.rate_limit"), cachekey.RATE_LIMIT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.pwd_err_cnt"), cachekey.PWD_ERR_CNT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.ne_info"), cachekey.NE_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.ne_data"), cachekey.NE_DATA_KEY),
|
||||
}
|
||||
c.JSON(200, result.OkData(caches))
|
||||
}
|
||||
|
||||
// 缓存名称下键名列表
|
||||
//
|
||||
// GET /getKeys/:cacheName
|
||||
func (s *SysCacheController) Keys(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cacheName := c.Param("cacheName")
|
||||
if cacheName == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cacheName := c.Param("cacheName")
|
||||
cacheKey := c.Param("cacheKey")
|
||||
if cacheName == "" || cacheKey == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cacheName := c.Param("cacheName")
|
||||
if cacheName == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cacheName := c.Param("cacheName")
|
||||
cacheKey := c.Param("cacheKey")
|
||||
if cacheName == "" || cacheKey == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
caches := []model.SysCache{
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.user"), cachekey.LOGIN_TOKEN_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.sys_config"), cachekey.SYS_CONFIG_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.sys_dict"), cachekey.SYS_DICT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.captcha_codes"), cachekey.CAPTCHA_CODE_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.repeat_submit"), cachekey.REPEAT_SUBMIT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.rate_limit"), cachekey.RATE_LIMIT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.pwd_err_cnt"), cachekey.PWD_ERR_CNT_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.ne_info"), cachekey.NE_KEY),
|
||||
model.NewSysCacheNames(i18n.TKey(language, "cache.name.ne_data"), cachekey.NE_DATA_KEY),
|
||||
}
|
||||
for _, v := range caches {
|
||||
cacheKeys, err := redis.GetKeys("", v.CacheName+":*")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
redis.DelKeys("", cacheKeys)
|
||||
}
|
||||
c.JSON(200, result.Ok(nil))
|
||||
}
|
||||
388
src/modules/monitor/controller/sys_job.go
Normal file
388
src/modules/monitor/controller/sys_job.go
Normal file
@@ -0,0 +1,388 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/file"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
"nms_nbi/src/modules/monitor/service"
|
||||
systemService "nms_nbi/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)
|
||||
|
||||
rows := data["rows"].([]model.SysJob)
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
converI18n := func(language string, arr *[]model.SysJob) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 调度任务信息
|
||||
//
|
||||
// GET /:jobId
|
||||
func (s *SysJobController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
jobId := c.Param("jobId")
|
||||
if jobId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.sysJobService.SelectJobById(jobId)
|
||||
if data.JobID == jobId {
|
||||
// 处理多语言
|
||||
data.JobName = i18n.TKey(language, data.JobName)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysJobController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.SysJob
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.JobID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查cron表达式格式
|
||||
if parse.CronExpression(body.CronExpression) == 0 {
|
||||
// 调度任务新增【%s】失败,Cron表达式不正确
|
||||
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查任务调用传入参数是否json格式
|
||||
if body.TargetParams != "" {
|
||||
// 调度任务新增【%s】失败,任务传入参数json字符串不正确
|
||||
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": 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 {
|
||||
// 调度任务新增【%s】失败,同任务组内有相同任务名称
|
||||
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": 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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.SysJob
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.JobID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查cron表达式格式
|
||||
if parse.CronExpression(body.CronExpression) == 0 {
|
||||
// 调度任务修改【%s】失败,Cron表达式不正确
|
||||
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查任务调用传入参数是否json格式
|
||||
if body.TargetParams != "" {
|
||||
// 调度任务修改【%s】失败,任务传入参数json字符串不正确
|
||||
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": 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
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
job := s.sysJobService.SelectJobById(body.JobID)
|
||||
if job.JobID != body.JobID {
|
||||
// 没有可访问调度任务数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, body.JobID)
|
||||
if !uniqueJob {
|
||||
// 调度任务修改【%s】失败,同任务组内有相同任务名称
|
||||
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, job.JobName)
|
||||
if i18nValue != job.JobName {
|
||||
i18n.UpdateKeyValue(language, job.JobName, body.JobName)
|
||||
body.JobName = job.JobName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, job.Remark)
|
||||
if i18nValue2 != job.Remark {
|
||||
i18n.UpdateKeyValue(language, job.Remark, body.Remark)
|
||||
body.Remark = job.Remark
|
||||
}
|
||||
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
jobIds := c.Param("jobIds")
|
||||
if jobIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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 := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 调度任务修改状态
|
||||
//
|
||||
// PUT /changeStatus
|
||||
func (s *SysJobController) Status(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
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, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
job := s.sysJobService.SelectJobById(body.JobId)
|
||||
if job.JobID != body.JobId {
|
||||
// 没有可访问调度任务数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 与旧值相等不变更
|
||||
if job.Status == body.Status {
|
||||
// 变更状态与旧值相等!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.statusEq")))
|
||||
return
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
job.Status = body.Status
|
||||
job.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysJobService.UpdateJob(job)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 调度任务立即执行一次
|
||||
//
|
||||
// PUT /run/:jobId
|
||||
func (s *SysJobController) Run(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
jobId := c.Param("jobId")
|
||||
if jobId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
job := s.sysJobService.SelectJobById(jobId)
|
||||
if job.JobID != jobId {
|
||||
// 没有可访问调度任务数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
data := s.sysJobService.SelectJobPage(querys)
|
||||
if data["total"].(int64) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysJob)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysJob) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
// 导出文件名称
|
||||
fileName := fmt.Sprintf("job_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||||
// 第一行表头标题
|
||||
headerCells := map[string]string{
|
||||
"A1": i18n.TKey(language, "job.export.jobID"),
|
||||
"B1": i18n.TKey(language, "job.export.jobName"),
|
||||
"C1": i18n.TKey(language, "job.export.jobGroupName"),
|
||||
"D1": i18n.TKey(language, "job.export.invokeTarget"),
|
||||
"E1": i18n.TKey(language, "job.export.targetParams"),
|
||||
"F1": i18n.TKey(language, "job.export.cronExpression"),
|
||||
"G1": i18n.TKey(language, "job.export.status"),
|
||||
"H1": i18n.TKey(language, "job.export.remark"),
|
||||
}
|
||||
// 读取任务组名字典数据
|
||||
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 = i18n.TKey(language, v.DictLabel)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 状态
|
||||
statusValue := i18n.TKey(language, "dictData.fail")
|
||||
if row.Status == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.success")
|
||||
}
|
||||
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: statusValue,
|
||||
"H" + 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)
|
||||
}
|
||||
197
src/modules/monitor/controller/sys_job_log.go
Normal file
197
src/modules/monitor/controller/sys_job_log.go
Normal file
@@ -0,0 +1,197 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/date"
|
||||
"nms_nbi/src/framework/utils/file"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
"nms_nbi/src/modules/monitor/service"
|
||||
systemService "nms_nbi/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)
|
||||
// 任务ID优先级更高
|
||||
if v, ok := querys["jobId"]; ok && v != nil {
|
||||
jobInfo := service.NewSysJobImpl.SelectJobById(v.(string))
|
||||
querys["jobName"] = jobInfo.JobName
|
||||
querys["jobGroup"] = jobInfo.JobGroup
|
||||
}
|
||||
data := s.sysJobLogService.SelectJobLogPage(querys)
|
||||
|
||||
rows := data["rows"].([]model.SysJobLog)
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
converI18n := func(language string, arr *[]model.SysJobLog) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 调度任务日志信息
|
||||
//
|
||||
// GET /:jobLogId
|
||||
func (s *SysJobLogController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
jobLogId := c.Param("jobLogId")
|
||||
if jobLogId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
jobLogIds := c.Param("jobLogIds")
|
||||
if jobLogIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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 {
|
||||
// 删除成功:%d
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": 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("Export data record is empty"))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysJobLog)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
converI18n := func(language string, arr *[]model.SysJobLog) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
// 导出文件名称
|
||||
fileName := fmt.Sprintf("jobLog_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||||
// 第一行表头标题
|
||||
headerCells := map[string]string{
|
||||
"A1": i18n.TKey(language, "job.export.jobLogID"),
|
||||
"B1": i18n.TKey(language, "job.export.jobName"),
|
||||
"C1": i18n.TKey(language, "job.export.jobGroupName"),
|
||||
"D1": i18n.TKey(language, "job.export.invokeTarget"),
|
||||
"E1": i18n.TKey(language, "job.export.targetParams"),
|
||||
"F1": i18n.TKey(language, "job.export.jobID"),
|
||||
"G1": i18n.TKey(language, "job.export.jobLogStatus"),
|
||||
"H1": i18n.TKey(language, "job.export.jobLogTime"),
|
||||
}
|
||||
// 读取任务组名字典数据
|
||||
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 = i18n.TKey(language, v.DictLabel)
|
||||
break
|
||||
}
|
||||
}
|
||||
// 状态
|
||||
statusValue := i18n.TKey(language, "dictData.fail")
|
||||
if row.Status == "1" {
|
||||
statusValue = i18n.TKey(language, "dictData.success")
|
||||
}
|
||||
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)
|
||||
}
|
||||
131
src/modules/monitor/controller/sys_user_online.go
Normal file
131
src/modules/monitor/controller/sys_user_online.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/constants/cachekey"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/redis"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
"nms_nbi/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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
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 != "" {
|
||||
onlineUser.LoginLocation = i18n.TKey(language, onlineUser.LoginLocation)
|
||||
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) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
tokenId := c.Param("tokenId")
|
||||
if tokenId == "" || tokenId == "*" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
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))
|
||||
}
|
||||
35
src/modules/monitor/controller/system_info.go
Normal file
35
src/modules/monitor/controller/system_info.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/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{
|
||||
"cpu": s.systemInfogService.CPUInfo(),
|
||||
"memory": s.systemInfogService.MemoryInfo(),
|
||||
"network": s.systemInfogService.NetworkInfo(),
|
||||
"time": s.systemInfogService.TimeInfo(),
|
||||
"system": s.systemInfogService.SystemInfo(),
|
||||
"disk": s.systemInfogService.DiskInfo(),
|
||||
}))
|
||||
}
|
||||
29
src/modules/monitor/model/monitor_base.go
Normal file
29
src/modules/monitor/model/monitor_base.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
// MonitorBase 监控_基本信息 monitor_base
|
||||
type MonitorBase struct {
|
||||
// id
|
||||
ID int64 `json:"id" gorm:"primaryKey"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// cpu使用率
|
||||
CPU float64 `json:"cpu"`
|
||||
// cpu平均使用率
|
||||
LoadUsage float64 `json:"loadUsage"`
|
||||
// cpu使用1分钟
|
||||
CPULoad1 float64 `json:"cpuLoad1"`
|
||||
// cpu使用5分钟
|
||||
CPULoad5 float64 `json:"cpuLoad5"`
|
||||
// cpu使用15分钟
|
||||
CPULoad15 float64 `json:"cpuLoad15"`
|
||||
// 内存使用率
|
||||
Memory float64 `json:"memory"`
|
||||
// 网元ID
|
||||
NeType string `json:"neType"`
|
||||
// 网元类型
|
||||
NeID string `json:"neId"`
|
||||
}
|
||||
|
||||
func (MonitorBase) TableName() string {
|
||||
return "monitor_base"
|
||||
}
|
||||
27
src/modules/monitor/model/monitor_io.go
Normal file
27
src/modules/monitor/model/monitor_io.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
// MonitorIO 监控_磁盘IO monitor_io
|
||||
type MonitorIO struct {
|
||||
// id
|
||||
ID int64 `json:"id" gorm:"primaryKey"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// 磁盘名
|
||||
Name string `json:"name"`
|
||||
// 读取K
|
||||
Read int64 `json:"read"`
|
||||
// 写入K
|
||||
Write int64 `json:"write"`
|
||||
// 次数
|
||||
Count int64 `json:"count"`
|
||||
// 耗时
|
||||
Time int64 `json:"time"`
|
||||
// 网元ID
|
||||
NeType string `json:"neType"`
|
||||
// 网元类型
|
||||
NeID string `json:"neId"`
|
||||
}
|
||||
|
||||
func (MonitorIO) TableName() string {
|
||||
return "monitor_io"
|
||||
}
|
||||
23
src/modules/monitor/model/monitor_network.go
Normal file
23
src/modules/monitor/model/monitor_network.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
// MonitorNetwork 监控_网络IO monitor_network
|
||||
type MonitorNetwork struct {
|
||||
// id
|
||||
ID int64 `json:"id" gorm:"primaryKey"`
|
||||
// 创建时间
|
||||
CreateTime int64 `json:"createTime"`
|
||||
// 网卡名
|
||||
Name string `json:"name"`
|
||||
// 上行
|
||||
Up float64 `json:"up"`
|
||||
// 下行
|
||||
Down float64 `json:"down"`
|
||||
// 网元ID 本机#号
|
||||
NeType string `json:"neType"`
|
||||
// 网元类型 本机#号
|
||||
NeID string `json:"neId"`
|
||||
}
|
||||
|
||||
func (MonitorNetwork) TableName() string {
|
||||
return "monitor_network"
|
||||
}
|
||||
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: "",
|
||||
}
|
||||
}
|
||||
35
src/modules/monitor/model/sys_job.go
Normal file
35
src/modules/monitor/model/sys_job.go
Normal file
@@ -0,0 +1,35 @@
|
||||
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"`
|
||||
// 是否记录任务日志
|
||||
SaveLog string `json:"saveLog"`
|
||||
// 创建者
|
||||
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"`
|
||||
}
|
||||
169
src/modules/monitor/monitor.go
Normal file
169
src/modules/monitor/monitor.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/middleware"
|
||||
"nms_nbi/src/framework/middleware/collectlogs"
|
||||
"nms_nbi/src/framework/middleware/repeat"
|
||||
"nms_nbi/src/modules/monitor/controller"
|
||||
"nms_nbi/src/modules/monitor/processor"
|
||||
"nms_nbi/src/modules/monitor/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Setup 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> monitor 模块路由")
|
||||
|
||||
// 启动时需要的初始参数
|
||||
InitLoad()
|
||||
|
||||
// 服务器资源监控信息
|
||||
monitorGroup := router.Group("/monitor")
|
||||
{
|
||||
monitorGroup.GET("/load",
|
||||
// middleware.PreAuthorize(nil),
|
||||
controller.NewMonitor.Load,
|
||||
)
|
||||
}
|
||||
|
||||
// 服务器服务信息
|
||||
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("log.operate.title.sysJobLog", 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("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_CLEAN)),
|
||||
controller.NewSysJobLog.Clean,
|
||||
)
|
||||
sysJobLogGroup.POST("/export",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:export"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", 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("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_INSERT)),
|
||||
controller.NewSysJob.Add,
|
||||
)
|
||||
sysJobGroup.PUT("",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:edit"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewSysJob.Edit,
|
||||
)
|
||||
sysJobGroup.DELETE("/:jobIds",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", 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("log.operate.title.sysJob", 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("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewSysJob.Run,
|
||||
)
|
||||
sysJobGroup.PUT("/resetQueueJob",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_CLEAN)),
|
||||
controller.NewSysJob.ResetQueueJob,
|
||||
)
|
||||
sysJobGroup.POST("/export",
|
||||
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:export"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", 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()
|
||||
}
|
||||
61
src/modules/monitor/processor/bar/bar.go
Normal file
61
src/modules/monitor/processor/bar/bar.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package bar
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/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, error) {
|
||||
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
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
result := map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
53
src/modules/monitor/processor/foo/foo.go
Normal file
53
src/modules/monitor/processor/foo/foo.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package foo
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/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, error) {
|
||||
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
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
result := map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
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 (
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/src/modules/monitor/processor/bar"
|
||||
"nms_nbi/src/modules/monitor/processor/foo"
|
||||
"nms_nbi/src/modules/monitor/processor/simple"
|
||||
)
|
||||
|
||||
// InitCronQueue 初始定时任务队列
|
||||
func InitCronQueue() {
|
||||
cron.CreateQueue("simple", simple.NewProcessor)
|
||||
cron.CreateQueue("foo", foo.NewProcessor)
|
||||
cron.CreateQueue("bar", bar.NewProcessor)
|
||||
}
|
||||
27
src/modules/monitor/processor/simple/simple.go
Normal file
27
src/modules/monitor/processor/simple/simple.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package simple
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/src/framework/logger"
|
||||
)
|
||||
|
||||
var NewProcessor = &simpleProcessor{}
|
||||
|
||||
// simple 队列任务处理
|
||||
type simpleProcessor struct{}
|
||||
|
||||
func (s *simpleProcessor) Execute(data any) (any, error) {
|
||||
options := data.(cron.JobData)
|
||||
|
||||
sysJob := options.SysJob
|
||||
logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
result := map[string]any{
|
||||
"repeat": options.Repeat,
|
||||
"jobName": sysJob.JobName,
|
||||
"invokeTarget": sysJob.InvokeTarget,
|
||||
"targetParams": sysJob.TargetParams,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
33
src/modules/monitor/repository/monitor.go
Normal file
33
src/modules/monitor/repository/monitor.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package repository
|
||||
|
||||
import "nms_nbi/src/modules/monitor/model"
|
||||
|
||||
// IMonitor 监控服务资源相关信息 数据接口
|
||||
type IMonitor interface {
|
||||
// CreateMonitorBase 创建监控_基本信息
|
||||
CreateMonitorBase(m model.MonitorBase) error
|
||||
|
||||
// DelMonitorBase 删除监控_基本信息
|
||||
DelMonitorBase(ltTime int64) error
|
||||
|
||||
// SelectMonitorBase 查询监控_基本信息
|
||||
SelectMonitorBase(query map[string]any) []model.MonitorBase
|
||||
|
||||
// BatchCreateMonitorIO 批量创建监控_IO
|
||||
BatchCreateMonitorIO(ioList []model.MonitorIO) error
|
||||
|
||||
// DelMonitorIO 删除监控_IO
|
||||
DelMonitorIO(ltTime int64) error
|
||||
|
||||
// SelectMonitorIO 查询监控_IO
|
||||
SelectMonitorIO(query map[string]any) []model.MonitorIO
|
||||
|
||||
// BatchCreateMonitorNet 批量创建监控_网络
|
||||
BatchCreateMonitorNet(netList []model.MonitorNetwork) error
|
||||
|
||||
// DelMonitorNet 删除监控_网络
|
||||
DelMonitorNet(ltTime int64) error
|
||||
|
||||
// SelectMonitorNetwork 查询监控_网络
|
||||
SelectMonitorNetwork(query map[string]any) []model.MonitorNetwork
|
||||
}
|
||||
106
src/modules/monitor/repository/monitor.impl.go
Normal file
106
src/modules/monitor/repository/monitor.impl.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 实例化数据层 MonitorImpl 结构体
|
||||
var NewMonitorImpl = &MonitorImpl{
|
||||
db: datasource.DefaultDB,
|
||||
}
|
||||
|
||||
// MonitorImpl 监控服务资源相关信息 数据层处理
|
||||
type MonitorImpl struct {
|
||||
// 数据库实例
|
||||
db func() *gorm.DB
|
||||
}
|
||||
|
||||
// CreateMonitorBase 创建监控_基本信息
|
||||
func (r *MonitorImpl) CreateMonitorBase(m model.MonitorBase) error {
|
||||
return r.db().Create(&m).Error
|
||||
}
|
||||
|
||||
// DelMonitorBase 删除监控_基本信息
|
||||
func (r *MonitorImpl) DelMonitorBase(ltTime int64) error {
|
||||
return r.db().Where("create_time < ?", ltTime).Delete(&model.MonitorBase{}).Error
|
||||
}
|
||||
|
||||
// SelectMonitorBase 查询监控_基本信息
|
||||
func (r *MonitorImpl) SelectMonitorBase(query map[string]any) []model.MonitorBase {
|
||||
var bases []model.MonitorBase
|
||||
dbConn := r.db()
|
||||
if query["neType"] != "" && query["neId"] != "" {
|
||||
dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])
|
||||
}
|
||||
dbConn = dbConn.Where("create_time >= ? and create_time <= ?", query["startTime"], query["endTime"])
|
||||
err := dbConn.Order("create_time asc").Find(&bases).Error
|
||||
if err != nil {
|
||||
logger.Errorf("SelectMonitorBase %v", err)
|
||||
return bases
|
||||
}
|
||||
return bases
|
||||
}
|
||||
|
||||
// BatchCreateMonitorIO 批量创建监控_IO
|
||||
func (r *MonitorImpl) BatchCreateMonitorIO(ioList []model.MonitorIO) error {
|
||||
return r.db().CreateInBatches(ioList, len(ioList)).Error
|
||||
}
|
||||
|
||||
// DelMonitorIO 删除监控_IO
|
||||
func (r *MonitorImpl) DelMonitorIO(ltTime int64) error {
|
||||
return r.db().Where("create_time < ?", ltTime).Delete(&model.MonitorIO{}).Error
|
||||
}
|
||||
|
||||
// SelectMonitorIO 查询监控_IO
|
||||
func (r *MonitorImpl) SelectMonitorIO(query map[string]any) []model.MonitorIO {
|
||||
var ios []model.MonitorIO
|
||||
dbConn := r.db()
|
||||
if query["name"] != "" {
|
||||
dbConn = dbConn.Where("name = ?", query["name"])
|
||||
}
|
||||
if query["neType"] != "" && query["neId"] != "" {
|
||||
dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])
|
||||
}
|
||||
dbConn = dbConn.Where("create_time >= ? and create_time <= ?", query["startTime"], query["endTime"])
|
||||
err := dbConn.Order("create_time asc").Find(&ios).Error
|
||||
if err != nil {
|
||||
logger.Errorf("SelectMonitorIO %v", err)
|
||||
return ios
|
||||
}
|
||||
return ios
|
||||
}
|
||||
|
||||
// BatchCreateMonitorNet 批量创建监控_网络
|
||||
func (r *MonitorImpl) BatchCreateMonitorNet(netList []model.MonitorNetwork) error {
|
||||
return r.db().CreateInBatches(netList, len(netList)).Error
|
||||
}
|
||||
|
||||
// DelMonitorNet 删除监控_网络
|
||||
func (r *MonitorImpl) DelMonitorNet(ltTime int64) error {
|
||||
return r.db().Where("create_time < ?", ltTime).Delete(&model.MonitorNetwork{}).Error
|
||||
}
|
||||
|
||||
// SelectMonitorNetwork 查询监控_网络
|
||||
func (r *MonitorImpl) SelectMonitorNetwork(query map[string]any) []model.MonitorNetwork {
|
||||
var networks []model.MonitorNetwork
|
||||
dbConn := r.db()
|
||||
if query["name"] != "" {
|
||||
dbConn = dbConn.Where("name = ?", query["name"])
|
||||
} else {
|
||||
dbConn = dbConn.Where("name = ?", "all")
|
||||
}
|
||||
if query["neType"] != "" && query["neId"] != "" {
|
||||
dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])
|
||||
}
|
||||
dbConn = dbConn.Where("create_time >= ? and create_time <= ?", query["startTime"], query["endTime"])
|
||||
err := dbConn.Order("create_time asc").Find(&networks).Error
|
||||
if err != nil {
|
||||
logger.Errorf("SelectMonitorNetwork %v", err)
|
||||
return networks
|
||||
}
|
||||
return networks
|
||||
}
|
||||
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 (
|
||||
"nms_nbi/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
|
||||
}
|
||||
343
src/modules/monitor/repository/sys_job.impl.go
Normal file
343
src/modules/monitor/repository/sys_job.impl.go
Normal file
@@ -0,0 +1,343 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/repo"
|
||||
"nms_nbi/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, save_log, 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",
|
||||
"save_log": "SaveLog",
|
||||
"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.Sprint(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
|
||||
}
|
||||
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.SaveLog != "" {
|
||||
params["save_log"] = sysJob.SaveLog
|
||||
}
|
||||
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
|
||||
}
|
||||
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.SaveLog != "" {
|
||||
params["save_log"] = sysJob.SaveLog
|
||||
}
|
||||
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 (
|
||||
"nms_nbi/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"
|
||||
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/utils/date"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/repo"
|
||||
"nms_nbi/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 = ?")
|
||||
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
|
||||
}
|
||||
14
src/modules/monitor/service/monitor.go
Normal file
14
src/modules/monitor/service/monitor.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package service
|
||||
|
||||
// IMonitor 服务器系统相关信息 服务层接口
|
||||
type IMonitor interface {
|
||||
// RunMonitor 执行资源监控
|
||||
RunMonitor()
|
||||
|
||||
// RunMonitorDataCancel 启动资源监控数据存储io/network通道 移除之前的chan上下文后在设置新的均值
|
||||
// interval 采集的平均值(分钟)
|
||||
RunMonitorDataCancel(removeBefore bool, interval float64)
|
||||
|
||||
// SelectMonitorInfo 查询监控资源信息
|
||||
SelectMonitorInfo(query map[string]any) map[string]any
|
||||
}
|
||||
272
src/modules/monitor/service/monitor.impl.go
Normal file
272
src/modules/monitor/service/monitor.impl.go
Normal file
@@ -0,0 +1,272 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
"nms_nbi/src/modules/monitor/repository"
|
||||
systemService "nms_nbi/src/modules/system/service"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
)
|
||||
|
||||
// 实例化服务层 MonitorImpl 结构体
|
||||
var NewMonitorImpl = &MonitorImpl{
|
||||
sysConfigService: systemService.NewSysConfigImpl,
|
||||
monitorRepository: repository.NewMonitorImpl,
|
||||
diskIO: make(chan []disk.IOCountersStat, 2),
|
||||
netIO: make(chan []net.IOCountersStat, 2),
|
||||
}
|
||||
|
||||
// MonitorImpl 服务器系统相关信息 服务层处理
|
||||
type MonitorImpl struct {
|
||||
// 参数配置服务
|
||||
sysConfigService systemService.ISysConfig
|
||||
// 监控服务资源数据信息
|
||||
monitorRepository repository.IMonitor
|
||||
// 磁盘网络IO 数据通道
|
||||
diskIO chan ([]disk.IOCountersStat)
|
||||
netIO chan ([]net.IOCountersStat)
|
||||
}
|
||||
|
||||
// RunMonitor 执行资源监控
|
||||
func (s *MonitorImpl) RunMonitor() {
|
||||
var itemBase model.MonitorBase
|
||||
itemBase.CreateTime = time.Now().UnixMilli()
|
||||
itemBase.NeType = "#"
|
||||
itemBase.NeID = "#"
|
||||
totalPercent, _ := cpu.Percent(3*time.Second, false)
|
||||
if len(totalPercent) == 1 {
|
||||
itemBase.CPU = totalPercent[0]
|
||||
}
|
||||
cpuCount, _ := cpu.Counts(false)
|
||||
|
||||
loadInfo, _ := load.Avg()
|
||||
itemBase.CPULoad1 = loadInfo.Load1
|
||||
itemBase.CPULoad5 = loadInfo.Load5
|
||||
itemBase.CPULoad15 = loadInfo.Load15
|
||||
itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
|
||||
|
||||
memoryInfo, _ := mem.VirtualMemory()
|
||||
itemBase.Memory = memoryInfo.UsedPercent
|
||||
|
||||
if err := s.monitorRepository.CreateMonitorBase(itemBase); err != nil {
|
||||
logger.Errorf("CreateMonitorBase err: %v", err)
|
||||
}
|
||||
|
||||
// 将当前资源发送到chan中处理保存
|
||||
s.loadDiskIO()
|
||||
s.loadNetIO()
|
||||
|
||||
// 监控系统资源-保留天数
|
||||
storeDays := s.sysConfigService.SelectConfigValueByKey("monitor.sysResource.storeDays")
|
||||
if storeDays != "" {
|
||||
storeDays, _ := strconv.Atoi(storeDays)
|
||||
ltTime := time.Now().AddDate(0, 0, -storeDays).UnixMilli()
|
||||
_ = s.monitorRepository.DelMonitorBase(ltTime)
|
||||
_ = s.monitorRepository.DelMonitorIO(ltTime)
|
||||
_ = s.monitorRepository.DelMonitorNet(ltTime)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MonitorImpl) loadDiskIO() {
|
||||
ioStat, _ := disk.IOCounters()
|
||||
var diskIOList []disk.IOCountersStat
|
||||
for _, io := range ioStat {
|
||||
diskIOList = append(diskIOList, io)
|
||||
}
|
||||
s.diskIO <- diskIOList
|
||||
}
|
||||
|
||||
func (s *MonitorImpl) loadNetIO() {
|
||||
netStat, _ := net.IOCounters(true)
|
||||
netStatAll, _ := net.IOCounters(false)
|
||||
var netList []net.IOCountersStat
|
||||
netList = append(netList, netStat...)
|
||||
netList = append(netList, netStatAll...)
|
||||
s.netIO <- netList
|
||||
}
|
||||
|
||||
// monitorCancel 监控搜集IO/Network上下文
|
||||
var monitorCancel context.CancelFunc
|
||||
|
||||
// RunMonitorDataCancel 启动资源监控数据存储io/network通道 移除之前的chan上下文后在设置新的均值
|
||||
// interval 采集的平均值(分钟)
|
||||
func (s *MonitorImpl) RunMonitorDataCancel(removeBefore bool, interval float64) {
|
||||
// 是否取消之前的
|
||||
if removeBefore {
|
||||
monitorCancel()
|
||||
}
|
||||
|
||||
// 上下文控制
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
monitorCancel = cancel
|
||||
|
||||
// chanl 通道进行存储数据
|
||||
go s.saveIODataToDB(ctx, interval)
|
||||
go s.saveNetDataToDB(ctx, interval)
|
||||
}
|
||||
|
||||
func (s *MonitorImpl) saveIODataToDB(ctx context.Context, interval float64) {
|
||||
defer close(s.diskIO)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case ioStat := <-s.diskIO:
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case ioStat2 := <-s.diskIO:
|
||||
var ioList []model.MonitorIO
|
||||
timeMilli := time.Now().UnixMilli()
|
||||
for _, io2 := range ioStat2 {
|
||||
for _, io1 := range ioStat {
|
||||
if io2.Name == io1.Name {
|
||||
var itemIO model.MonitorIO
|
||||
itemIO.CreateTime = timeMilli
|
||||
itemIO.NeType = "#"
|
||||
itemIO.NeID = "#"
|
||||
itemIO.Name = io1.Name
|
||||
|
||||
if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes {
|
||||
itemIO.Read = int64(float64(io2.ReadBytes-io1.ReadBytes) / interval / 60)
|
||||
}
|
||||
if io2.WriteBytes != 0 && io1.WriteBytes != 0 && io2.WriteBytes > io1.WriteBytes {
|
||||
itemIO.Write = int64(float64(io2.WriteBytes-io1.WriteBytes) / interval / 60)
|
||||
}
|
||||
|
||||
if io2.ReadCount != 0 && io1.ReadCount != 0 && io2.ReadCount > io1.ReadCount {
|
||||
itemIO.Count = int64(float64(io2.ReadCount-io1.ReadCount) / interval / 60)
|
||||
}
|
||||
writeCount := int64(0)
|
||||
if io2.WriteCount != 0 && io1.WriteCount != 0 && io2.WriteCount > io1.WriteCount {
|
||||
writeCount = int64(float64(io2.WriteCount-io1.WriteCount) / interval * 60)
|
||||
}
|
||||
if writeCount > itemIO.Count {
|
||||
itemIO.Count = writeCount
|
||||
}
|
||||
|
||||
if io2.ReadTime != 0 && io1.ReadTime != 0 && io2.ReadTime > io1.ReadTime {
|
||||
itemIO.Time = int64(float64(io2.ReadTime-io1.ReadTime) / interval / 60)
|
||||
}
|
||||
writeTime := int64(0)
|
||||
if io2.WriteTime != 0 && io1.WriteTime != 0 && io2.WriteTime > io1.WriteTime {
|
||||
writeTime = int64(float64(io2.WriteTime-io1.WriteTime) / interval / 60)
|
||||
}
|
||||
if writeTime > itemIO.Time {
|
||||
itemIO.Time = writeTime
|
||||
}
|
||||
ioList = append(ioList, itemIO)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := s.monitorRepository.BatchCreateMonitorIO(ioList); err != nil {
|
||||
logger.Errorf("BatchCreateMonitorIO err: %v", err)
|
||||
}
|
||||
s.diskIO <- ioStat2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MonitorImpl) saveNetDataToDB(ctx context.Context, interval float64) {
|
||||
defer close(s.netIO)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case netStat := <-s.netIO:
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case netStat2 := <-s.netIO:
|
||||
var netList []model.MonitorNetwork
|
||||
timeMilli := time.Now().UnixMilli()
|
||||
for _, net2 := range netStat2 {
|
||||
for _, net1 := range netStat {
|
||||
if net2.Name == net1.Name {
|
||||
var itemNet model.MonitorNetwork
|
||||
itemNet.CreateTime = timeMilli
|
||||
itemNet.NeType = "#"
|
||||
itemNet.NeID = "#"
|
||||
itemNet.Name = net1.Name
|
||||
|
||||
if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent {
|
||||
itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / interval / 60
|
||||
}
|
||||
if net2.BytesRecv != 0 && net1.BytesRecv != 0 && net2.BytesRecv > net1.BytesRecv {
|
||||
itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / interval / 60
|
||||
}
|
||||
netList = append(netList, itemNet)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.monitorRepository.BatchCreateMonitorNet(netList); err != nil {
|
||||
logger.Errorf("BatchCreateMonitorNet err: %v", err)
|
||||
}
|
||||
s.netIO <- netStat2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SelectMonitorInfo 查询监控资源信息
|
||||
func (s *MonitorImpl) SelectMonitorInfo(query map[string]any) map[string]any {
|
||||
infoType := query["type"]
|
||||
startTimeMilli := query["startTime"]
|
||||
endTimeMilli := query["endTime"]
|
||||
neType := query["neType"]
|
||||
neId := query["neId"]
|
||||
name := query["name"]
|
||||
|
||||
// 返回数据
|
||||
backDatas := map[string]any{}
|
||||
|
||||
// 基本信息
|
||||
if infoType == "all" || infoType == "load" || infoType == "cpu" || infoType == "memory" {
|
||||
rows := s.monitorRepository.SelectMonitorBase(map[string]any{
|
||||
"startTime": startTimeMilli,
|
||||
"endTime": endTimeMilli,
|
||||
"neType": neType,
|
||||
"neId": neId,
|
||||
})
|
||||
backDatas["base"] = rows
|
||||
}
|
||||
|
||||
// 磁盘IO
|
||||
if infoType == "all" || infoType == "io" {
|
||||
rows := s.monitorRepository.SelectMonitorIO(map[string]any{
|
||||
"startTime": startTimeMilli,
|
||||
"endTime": endTimeMilli,
|
||||
"neType": neType,
|
||||
"neId": neId,
|
||||
"name": name,
|
||||
})
|
||||
backDatas["io"] = rows
|
||||
}
|
||||
|
||||
// 网络
|
||||
if infoType == "all" || infoType == "network" {
|
||||
rows := s.monitorRepository.SelectMonitorNetwork(map[string]any{
|
||||
"startTime": startTimeMilli,
|
||||
"endTime": endTimeMilli,
|
||||
"neType": neType,
|
||||
"neId": neId,
|
||||
"name": name,
|
||||
})
|
||||
backDatas["network"] = rows
|
||||
}
|
||||
|
||||
return backDatas
|
||||
}
|
||||
183
src/modules/monitor/service/monitor_test.go
Normal file
183
src/modules/monitor/service/monitor_test.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
}
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
s := MonitorInfo{}
|
||||
s.load(0.5) // 0.5 半分钟
|
||||
|
||||
fmt.Println(s)
|
||||
select {}
|
||||
}
|
||||
|
||||
// MonitorInfo 机器资源信息
|
||||
type MonitorInfo struct {
|
||||
MonitorBase MonitorBase `json:"base"` // 监控_基本信息
|
||||
MonitorIO []MonitorIO `json:"io"` // 监控_磁盘IO
|
||||
MonitorNetwork []MonitorNetwork `json:"network"` // 监控_网络IO
|
||||
}
|
||||
|
||||
// load 执行资源获取
|
||||
func (m *MonitorInfo) load(interval float64) {
|
||||
var itemBase MonitorBase
|
||||
itemBase.CreateTime = time.Now().UnixMilli()
|
||||
|
||||
totalPercent, _ := cpu.Percent(3*time.Second, false)
|
||||
if len(totalPercent) == 1 {
|
||||
itemBase.CPU = totalPercent[0]
|
||||
}
|
||||
cpuCount, _ := cpu.Counts(false)
|
||||
|
||||
loadInfo, _ := load.Avg()
|
||||
itemBase.CPULoad1 = loadInfo.Load1
|
||||
itemBase.CPULoad5 = loadInfo.Load5
|
||||
itemBase.CPULoad15 = loadInfo.Load15
|
||||
itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
|
||||
|
||||
memoryInfo, _ := mem.VirtualMemory()
|
||||
itemBase.Memory = memoryInfo.UsedPercent
|
||||
|
||||
m.MonitorBase = itemBase
|
||||
|
||||
// 求平均
|
||||
m.MonitorIO = loadDiskIO(interval)
|
||||
m.MonitorNetwork = loadNetIO(interval)
|
||||
}
|
||||
|
||||
// MonitorBase 监控_基本信息 monitor_base
|
||||
type MonitorBase struct {
|
||||
CreateTime int64 `json:"createTime"` // 创建时间
|
||||
CPU float64 `json:"cpu"` // cpu使用率
|
||||
LoadUsage float64 `json:"loadUsage"` // cpu平均使用率
|
||||
CPULoad1 float64 `json:"cpuLoad1"` // cpu使用1分钟
|
||||
CPULoad5 float64 `json:"cpuLoad5"` // cpu使用5分钟
|
||||
CPULoad15 float64 `json:"cpuLoad15"` // cpu使用15分钟
|
||||
Memory float64 `json:"memory"` // 内存使用率
|
||||
}
|
||||
|
||||
// MonitorIO 监控_磁盘IO monitor_io
|
||||
type MonitorIO struct {
|
||||
CreateTime int64 `json:"createTime"` // 创建时间
|
||||
Name string `json:"name"` // 磁盘名
|
||||
Read int64 `json:"read"` // 读取K
|
||||
Write int64 `json:"write"` // 写入K
|
||||
Count int64 `json:"count"` // 次数
|
||||
Time int64 `json:"time"` // 耗时
|
||||
}
|
||||
|
||||
// MonitorNetwork 监控_网络IO monitor_network
|
||||
type MonitorNetwork struct {
|
||||
CreateTime int64 `json:"createTime"` // 创建时间
|
||||
Name string `json:"name"` // 网卡名
|
||||
Up float64 `json:"up"` // 上行
|
||||
Down float64 `json:"down"` // 下行
|
||||
}
|
||||
|
||||
// loadDiskIO 磁盘读写,interval表示采集的平均值(分钟)
|
||||
func loadDiskIO(interval float64) []MonitorIO {
|
||||
ioStat, _ := disk.IOCounters()
|
||||
|
||||
time.Sleep(time.Duration(interval) * time.Minute)
|
||||
|
||||
ioStat2, _ := disk.IOCounters()
|
||||
var ioList []MonitorIO
|
||||
timeMilli := time.Now().UnixMilli()
|
||||
for _, io2 := range ioStat2 {
|
||||
for _, io1 := range ioStat {
|
||||
if io2.Name == io1.Name {
|
||||
var itemIO MonitorIO
|
||||
itemIO.CreateTime = timeMilli
|
||||
itemIO.Name = io1.Name
|
||||
|
||||
if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes {
|
||||
itemIO.Read = int64(float64(io2.ReadBytes-io1.ReadBytes) / interval / 60)
|
||||
}
|
||||
if io2.WriteBytes != 0 && io1.WriteBytes != 0 && io2.WriteBytes > io1.WriteBytes {
|
||||
itemIO.Write = int64(float64(io2.WriteBytes-io1.WriteBytes) / interval / 60)
|
||||
}
|
||||
|
||||
if io2.ReadCount != 0 && io1.ReadCount != 0 && io2.ReadCount > io1.ReadCount {
|
||||
itemIO.Count = int64(float64(io2.ReadCount-io1.ReadCount) / interval / 60)
|
||||
}
|
||||
writeCount := int64(0)
|
||||
if io2.WriteCount != 0 && io1.WriteCount != 0 && io2.WriteCount > io1.WriteCount {
|
||||
writeCount = int64(float64(io2.WriteCount-io1.WriteCount) / interval * 60)
|
||||
}
|
||||
if writeCount > itemIO.Count {
|
||||
itemIO.Count = writeCount
|
||||
}
|
||||
|
||||
if io2.ReadTime != 0 && io1.ReadTime != 0 && io2.ReadTime > io1.ReadTime {
|
||||
itemIO.Time = int64(float64(io2.ReadTime-io1.ReadTime) / interval / 60)
|
||||
}
|
||||
writeTime := int64(0)
|
||||
if io2.WriteTime != 0 && io1.WriteTime != 0 && io2.WriteTime > io1.WriteTime {
|
||||
writeTime = int64(float64(io2.WriteTime-io1.WriteTime) / interval / 60)
|
||||
}
|
||||
if writeTime > itemIO.Time {
|
||||
itemIO.Time = writeTime
|
||||
}
|
||||
ioList = append(ioList, itemIO)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return ioList
|
||||
}
|
||||
|
||||
// loadNetIO 网络接口(包括虚拟接口),interval表示采集的平均值(分钟)
|
||||
func loadNetIO(interval float64) []MonitorNetwork {
|
||||
// 获取当前时刻
|
||||
netStat, _ := net.IOCounters(true)
|
||||
netStatAll, _ := net.IOCounters(false)
|
||||
var netStatList []net.IOCountersStat
|
||||
netStatList = append(netStatList, netStat...)
|
||||
netStatList = append(netStatList, netStatAll...)
|
||||
|
||||
time.Sleep(time.Duration(interval) * time.Minute)
|
||||
|
||||
// 获取结束时刻
|
||||
netStat2, _ := net.IOCounters(true)
|
||||
netStat2All, _ := net.IOCounters(false)
|
||||
var netStat2List []net.IOCountersStat
|
||||
netStat2List = append(netStat2List, netStat2...)
|
||||
netStat2List = append(netStat2List, netStat2All...)
|
||||
|
||||
var netList []MonitorNetwork
|
||||
timeMilli := time.Now().UnixMilli()
|
||||
for _, net2 := range netStat2List {
|
||||
for _, net1 := range netStatList {
|
||||
if net2.Name == net1.Name {
|
||||
var itemNet MonitorNetwork
|
||||
itemNet.CreateTime = timeMilli
|
||||
itemNet.Name = net1.Name
|
||||
|
||||
// 如果结束时刻发送字节数和当前时刻发送字节数都不为零,并且结束时刻发送字节数大于当前时刻发送字节数
|
||||
if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent {
|
||||
itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / interval / 60
|
||||
}
|
||||
if net2.BytesRecv != 0 && net1.BytesRecv != 0 && net2.BytesRecv > net1.BytesRecv {
|
||||
itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / interval / 60
|
||||
}
|
||||
netList = append(netList, itemNet)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return netList
|
||||
}
|
||||
35
src/modules/monitor/service/sys_job.go
Normal file
35
src/modules/monitor/service/sys_job.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"nms_nbi/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)
|
||||
|
||||
// RunQueueJob 立即运行一次调度任务
|
||||
RunQueueJob(sysJob model.SysJob) bool
|
||||
|
||||
// ResetQueueJob 重置初始调度任务
|
||||
ResetQueueJob()
|
||||
}
|
||||
169
src/modules/monitor/service/sys_job.impl.go
Normal file
169
src/modules/monitor/service/sys_job.impl.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/src/framework/constants/common"
|
||||
"nms_nbi/src/framework/cron"
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
"nms_nbi/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, fmt.Errorf("there is no accessible scheduling task data")
|
||||
}
|
||||
if len(jobs) == len(jobIds) {
|
||||
// 清除任务
|
||||
for _, job := range jobs {
|
||||
r.deleteQueueJob(job)
|
||||
}
|
||||
rows := r.sysJobRepository.DeleteJobByIds(jobIds)
|
||||
return rows, nil
|
||||
}
|
||||
// 删除调度任务信息失败!
|
||||
return 0, fmt.Errorf("failed to delete scheduling task information")
|
||||
}
|
||||
|
||||
// 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 (
|
||||
"nms_nbi/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 (
|
||||
"nms_nbi/src/modules/monitor/model"
|
||||
"nms_nbi/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 (
|
||||
"nms_nbi/src/framework/vo"
|
||||
"nms_nbi/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 (
|
||||
"nms_nbi/src/framework/vo"
|
||||
"nms_nbi/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
|
||||
}
|
||||
22
src/modules/monitor/service/system_info.go
Normal file
22
src/modules/monitor/service/system_info.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package service
|
||||
|
||||
// ISystemInfo 服务器系统相关信息 服务层接口
|
||||
type ISystemInfo interface {
|
||||
// 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
|
||||
}
|
||||
173
src/modules/monitor/service/system_info.impl.go
Normal file
173
src/modules/monitor/service/system_info.impl.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/config"
|
||||
"nms_nbi/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{}
|
||||
|
||||
// SystemInfo 系统信息
|
||||
func (s *SystemInfoImpl) SystemInfo() map[string]any {
|
||||
info, err := host.Info()
|
||||
if err != nil {
|
||||
info.Platform = err.Error()
|
||||
}
|
||||
// 获取主机运行时间
|
||||
bootTime := time.Since(time.Unix(int64(info.BootTime), 0)).Seconds()
|
||||
// 获取程序运行时间
|
||||
runTime := time.Since(config.RunTime()).Abs().Seconds()
|
||||
return map[string]any{
|
||||
"platform": info.Platform,
|
||||
"platformVersion": info.PlatformVersion,
|
||||
"arch": info.KernelArch,
|
||||
"archVersion": info.KernelVersion,
|
||||
"os": info.OS,
|
||||
"hostname": info.Hostname,
|
||||
"bootTime": int64(bootTime),
|
||||
"processId": os.Getpid(),
|
||||
"runArch": runtime.GOARCH,
|
||||
"runVersion": runtime.Version(),
|
||||
"runTime": int64(runTime),
|
||||
}
|
||||
}
|
||||
|
||||
// TimeInfo 系统时间信息
|
||||
func (s *SystemInfoImpl) TimeInfo() map[string]string {
|
||||
now := time.Now()
|
||||
// 获取当前时间
|
||||
current := now.Format("2006-01-02 15:04:05")
|
||||
// 获取时区
|
||||
timezone := now.Format("-0700 MST")
|
||||
// 获取时区名称
|
||||
timezoneName := now.Format("MST")
|
||||
|
||||
return map[string]string{
|
||||
"current": current,
|
||||
"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 int = 0
|
||||
var speed string = "未知"
|
||||
var model string = "未知"
|
||||
cpuInfo, err := cpu.Info()
|
||||
if err == nil {
|
||||
core = runtime.NumCPU()
|
||||
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
|
||||
}
|
||||
81
src/modules/network_data/controller/amf.go
Normal file
81
src/modules/network_data/controller/amf.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
neDataService "nms_nbi/src/modules/network_data/service"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 AMFController 结构体
|
||||
var NewAMFController = &AMFController{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
ueEventService: neDataService.NewUEEventImpl,
|
||||
}
|
||||
|
||||
// 网元AMF
|
||||
//
|
||||
// PATH /amf
|
||||
type AMFController struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
// CDR会话事件服务
|
||||
ueEventService neDataService.IUEEvent
|
||||
}
|
||||
|
||||
// UE会话列表
|
||||
//
|
||||
// GET /ue/list
|
||||
func (s *AMFController) UEList(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys model.UEEventQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
querys.RmUID = neInfo.RmUID
|
||||
|
||||
// 查询数据
|
||||
data := s.ueEventService.SelectPage(querys)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// UE会话删除
|
||||
//
|
||||
// DELETE /ue/:ueIds
|
||||
func (s *AMFController) UERemove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
ueIds := c.Param("ueIds")
|
||||
if ueIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(ueIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.ueEventService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
81
src/modules/network_data/controller/ims.go
Normal file
81
src/modules/network_data/controller/ims.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
neDataService "nms_nbi/src/modules/network_data/service"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 IMSController 结构体
|
||||
var NewIMSController = &IMSController{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
cdrEventService: neDataService.NewCDREventImpl,
|
||||
}
|
||||
|
||||
// 网元IMS
|
||||
//
|
||||
// PATH /ims
|
||||
type IMSController struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
// CDR会话事件服务
|
||||
cdrEventService neDataService.ICDREvent
|
||||
}
|
||||
|
||||
// CDR会话列表
|
||||
//
|
||||
// GET /cdr/list
|
||||
func (s *IMSController) CDRList(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys model.CDREventQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
querys.RmUID = neInfo.RmUID
|
||||
|
||||
// 查询数据
|
||||
data := s.cdrEventService.SelectPage(querys)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// CDR会话删除
|
||||
//
|
||||
// DELETE /cdr/:cdrIds
|
||||
func (s *IMSController) CDRRemove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cdrIds := c.Param("cdrIds")
|
||||
if cdrIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(cdrIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.cdrEventService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
85
src/modules/network_data/controller/perf_kpi.go
Normal file
85
src/modules/network_data/controller/perf_kpi.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/lib/core/utils/date"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
neDataService "nms_nbi/src/modules/network_data/service"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 PerfKPIController 结构体
|
||||
var NewPerfKPIController = &PerfKPIController{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
perfKPIService: neDataService.NewPerfKPIImpl,
|
||||
}
|
||||
|
||||
// 性能统计
|
||||
//
|
||||
// PATH /kpi
|
||||
type PerfKPIController struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
// 统计信息服务
|
||||
perfKPIService neDataService.IPerfKPI
|
||||
}
|
||||
|
||||
// 获取统计数据
|
||||
//
|
||||
// GET /data
|
||||
func (s *PerfKPIController) GoldKPI(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys model.GoldKPIQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 时间格式校验
|
||||
startTime := date.ParseStrToDate(querys.StartTime, date.YYYY_MM_DD_HH_MM_SS)
|
||||
if startTime.IsZero() {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
endTime := date.ParseStrToDate(querys.EndTime, date.YYYY_MM_DD_HH_MM_SS)
|
||||
if endTime.IsZero() {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
if querys.Interval < 5 || querys.Interval > 3600 {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
querys.RmUID = neInfo.RmUID
|
||||
|
||||
// 查询数据
|
||||
kpiData := s.perfKPIService.SelectGoldKPI(querys)
|
||||
c.JSON(200, result.OkData(kpiData))
|
||||
}
|
||||
|
||||
// 获取统计标题
|
||||
//
|
||||
// GET /title
|
||||
func (s *PerfKPIController) Title(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neType := c.Query("neType")
|
||||
if neType == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
kpiTitles := s.perfKPIService.SelectGoldKPITitle(neType)
|
||||
|
||||
c.JSON(200, result.OkData(kpiTitles))
|
||||
}
|
||||
55
src/modules/network_data/controller/upf.go
Normal file
55
src/modules/network_data/controller/upf.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
neDataService "nms_nbi/src/modules/network_data/service"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 实例化控制层 UPFController 结构体
|
||||
var NewUPFController = &UPFController{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
perfKPIService: neDataService.NewPerfKPIImpl,
|
||||
}
|
||||
|
||||
// 网元UPF
|
||||
//
|
||||
// PATH /upf
|
||||
type UPFController struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
// 统计信息服务
|
||||
perfKPIService neDataService.IPerfKPI
|
||||
}
|
||||
|
||||
// 总流量数 N3上行 N6下行
|
||||
// 单位 比特(bit)
|
||||
//
|
||||
// GET /totalFlow
|
||||
func (s *UPFController) TotalFlow(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `json:"neType" form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
Day int `form:"day" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); querys.Day < 0 || err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.perfKPIService.SelectUPFTotalFlow(neInfo.NeType, neInfo.RmUID, querys.Day)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
28
src/modules/network_data/model/cdr_event.go
Normal file
28
src/modules/network_data/model/cdr_event.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// CDREvent CDR会话对象 cdr_event
|
||||
type CDREvent struct {
|
||||
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
NeType string `json:"neType" gorm:"column:ne_type"`
|
||||
NeName string `json:"neName" gorm:"column:ne_name"`
|
||||
RmUID string `json:"rmUID" gorm:"column:rm_uid"`
|
||||
Timestamp int64 `json:"timestamp" gorm:"column:timestamp"`
|
||||
CDRJSONStr string `json:"cdrJSON" gorm:"column:cdr_json"`
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
|
||||
}
|
||||
|
||||
// CDREventQuery CDR会话对象查询参数结构体
|
||||
type CDREventQuery struct {
|
||||
NeType string `json:"neType" form:"neType" binding:"required"` // 网元类型, 暂时支持IMS
|
||||
NeID string `json:"neId" form:"neId" binding:"required"`
|
||||
RmUID string `json:"rmUID" form:"rmUID"`
|
||||
RecordType string `json:"recordType" form:"recordType"` // 记录行为 MOC MTC MOSM MTSM
|
||||
StartTime string `json:"startTime" form:"startTime"`
|
||||
EndTime string `json:"endTime" form:"endTime"`
|
||||
SortField string `json:"sortField" form:"sortField" binding:"omitempty,oneof=timestamp"` // 排序字段,填写结果字段
|
||||
SortOrder string `json:"sortOrder" form:"sortOrder" binding:"omitempty,oneof=asc desc"` // 排序升降序,asc desc
|
||||
PageNum int64 `json:"pageNum" form:"pageNum" binding:"required"`
|
||||
PageSize int64 `json:"pageSize" form:"pageSize" binding:"required"`
|
||||
}
|
||||
23
src/modules/network_data/model/perf_kpi.go
Normal file
23
src/modules/network_data/model/perf_kpi.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
// GoldKPITitle 黄金指标标题信息对象 kpi_title
|
||||
type GoldKPITitle struct {
|
||||
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
NeType string `json:"neType" gorm:"column:ne_type"`
|
||||
KPIID string `json:"kpiId" gorm:"column:kpi_id"`
|
||||
TitleJson string `json:"titleJson" gorm:"column:title_json"`
|
||||
CnTitle string `json:"cnTitle" gorm:"column:cn_title"`
|
||||
EnTitle string `json:"enTitle" gorm:"column:en_title"`
|
||||
}
|
||||
|
||||
// GoldKPIQuery 黄金指标查询参数结构体
|
||||
type GoldKPIQuery struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
StartTime string `form:"startTime" binding:"required"`
|
||||
EndTime string `form:"endTime" binding:"required"`
|
||||
Interval int64 `form:"interval" binding:"required"`
|
||||
RmUID string `form:"rmUID"`
|
||||
SortField string `form:"sortField" binding:"omitempty,oneof=timeGroup"`
|
||||
SortOrder string `form:"sortOrder" binding:"omitempty,oneof=asc desc"`
|
||||
}
|
||||
29
src/modules/network_data/model/ue_event.go
Normal file
29
src/modules/network_data/model/ue_event.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// UEEvent UE会话对象 ue_event
|
||||
type UEEvent struct {
|
||||
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
NeType string `json:"neType" gorm:"column:ne_type"`
|
||||
NeName string `json:"neName" gorm:"column:ne_name"`
|
||||
RmUID string `json:"rmUID" gorm:"column:rm_uid"`
|
||||
Timestamp int64 `json:"timestamp" gorm:"column:timestamp"`
|
||||
EventType string `json:"eventType" gorm:"column:event_type"`
|
||||
EventJSONStr string `json:"eventJSON" gorm:"column:event_json"`
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
|
||||
}
|
||||
|
||||
// UEEventQuery UE会话对象查询参数结构体
|
||||
type UEEventQuery struct {
|
||||
NeType string `json:"neType" form:"neType" binding:"required"` // 网元类型, 暂时支持AMF
|
||||
NeID string `json:"neId" form:"neId" binding:"required"`
|
||||
RmUID string `json:"rmUID" form:"rmUID"`
|
||||
EventType string `json:"eventType" form:"eventType"` // 事件类型 auth-result detach cm-state
|
||||
StartTime string `json:"startTime" form:"startTime"`
|
||||
EndTime string `json:"endTime" form:"endTime"`
|
||||
SortField string `json:"sortField" form:"sortField" binding:"omitempty,oneof=timestamp"` // 排序字段,填写结果字段
|
||||
SortOrder string `json:"sortOrder" form:"sortOrder" binding:"omitempty,oneof=asc desc"` // 排序升降序,asc desc
|
||||
PageNum int64 `json:"pageNum" form:"pageNum" binding:"required"`
|
||||
PageSize int64 `json:"pageSize" form:"pageSize" binding:"required"`
|
||||
}
|
||||
72
src/modules/network_data/network_data.go
Normal file
72
src/modules/network_data/network_data.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package networkdata
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/middleware"
|
||||
"nms_nbi/src/framework/middleware/collectlogs"
|
||||
"nms_nbi/src/modules/network_data/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> network_data 模块路由")
|
||||
|
||||
neDataGroup := router.Group("/neData")
|
||||
|
||||
// 性能统计信息
|
||||
kpiGroup := neDataGroup.Group("/kpi")
|
||||
{
|
||||
kpiGroup.GET("/title",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewPerfKPIController.Title,
|
||||
)
|
||||
kpiGroup.GET("/data",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewPerfKPIController.GoldKPI,
|
||||
)
|
||||
}
|
||||
|
||||
// 网元IMS
|
||||
imsGroup := neDataGroup.Group("/ims")
|
||||
{
|
||||
// CDR会话事件列表
|
||||
imsGroup.GET("/cdr/list",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewIMSController.CDRList,
|
||||
)
|
||||
// CDR会话删除
|
||||
imsGroup.DELETE("/cdr/:cdrIds",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.imsCDR", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewIMSController.CDRRemove,
|
||||
)
|
||||
}
|
||||
|
||||
// 网元AMF
|
||||
amfGroup := neDataGroup.Group("/amf")
|
||||
{
|
||||
// UE会话事件
|
||||
amfGroup.GET("/ue/list",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewAMFController.UEList,
|
||||
)
|
||||
// UE会话删除
|
||||
amfGroup.DELETE("/ue/:ueIds",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.amfUE", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewAMFController.UERemove,
|
||||
)
|
||||
}
|
||||
|
||||
// 网元UPF
|
||||
upfGroup := neDataGroup.Group("/upf")
|
||||
{
|
||||
upfGroup.GET("/totalFlow",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewUPFController.TotalFlow,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
15
src/modules/network_data/repository/cdr_event.go
Normal file
15
src/modules/network_data/repository/cdr_event.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package repository
|
||||
|
||||
import "nms_nbi/src/modules/network_data/model"
|
||||
|
||||
// CDR会话事件 数据层接口
|
||||
type ICDREvent interface {
|
||||
// SelectPage 根据条件分页查询
|
||||
SelectPage(querys model.CDREventQuery) map[string]any
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(cdrIds []string) []model.CDREvent
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(cdrIds []string) int64
|
||||
}
|
||||
171
src/modules/network_data/repository/cdr_event.impl.go
Normal file
171
src/modules/network_data/repository/cdr_event.impl.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/utils/date"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/repo"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 CDREventImpl 结构体
|
||||
var NewCDREventImpl = &CDREventImpl{
|
||||
selectSql: `select id, ne_type, ne_name, rm_uid, timestamp, cdr_json, created_at from cdr_event`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_name": "NeName",
|
||||
"rm_uid": "RmUID",
|
||||
"timestamp": "Timestamp",
|
||||
"cdr_json": "CDRJSONStr",
|
||||
"created_at": "CreatedAt",
|
||||
},
|
||||
}
|
||||
|
||||
// CDREventImpl CDR会话事件 数据层处理
|
||||
type CDREventImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *CDREventImpl) convertResultRows(rows []map[string]any) []model.CDREvent {
|
||||
arr := make([]model.CDREvent, 0)
|
||||
for _, row := range rows {
|
||||
item := model.CDREvent{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *CDREventImpl) SelectPage(querys model.CDREventQuery) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if querys.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, querys.NeType)
|
||||
}
|
||||
if querys.RmUID != "" {
|
||||
conditions = append(conditions, "rm_uid = ?")
|
||||
params = append(params, querys.RmUID)
|
||||
}
|
||||
if querys.RmUID != "" {
|
||||
conditions = append(conditions, "rm_uid = ?")
|
||||
params = append(params, querys.RmUID)
|
||||
}
|
||||
if querys.StartTime != "" {
|
||||
conditions = append(conditions, "timestamp >= ?")
|
||||
beginDate := date.ParseStrToDate(querys.StartTime, date.YYYY_MM_DD_HH_MM_SS)
|
||||
params = append(params, beginDate.Unix())
|
||||
}
|
||||
if querys.EndTime != "" {
|
||||
conditions = append(conditions, "timestamp <= ?")
|
||||
endDate := date.ParseStrToDate(querys.EndTime, date.YYYY_MM_DD_HH_MM_SS)
|
||||
params = append(params, endDate.Unix())
|
||||
}
|
||||
if querys.RecordType != "" {
|
||||
recordTypes := strings.Split(querys.RecordType, ",")
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(recordTypes))
|
||||
conditions = append(conditions, fmt.Sprintf("JSON_EXTRACT(cdr_json, '$.recordType') in (%s)", placeholder))
|
||||
for _, recordType := range recordTypes {
|
||||
params = append(params, recordType)
|
||||
}
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.CDREvent{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from cdr_event"
|
||||
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(querys.PageNum, querys.PageSize)
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 排序
|
||||
orderSql := ""
|
||||
if querys.SortField != "" {
|
||||
sortSql := querys.SortField
|
||||
if querys.SortOrder != "" {
|
||||
if querys.SortOrder == "desc" {
|
||||
sortSql += " desc "
|
||||
} else {
|
||||
sortSql += " asc "
|
||||
}
|
||||
}
|
||||
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + orderSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *CDREventImpl) SelectByIds(cdrIds []string) []model.CDREvent {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cdrIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cdrIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.CDREvent{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *CDREventImpl) DeleteByIds(cdrIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cdrIds))
|
||||
sql := "delete from cdr_event where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cdrIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
15
src/modules/network_data/repository/perf_kpi.go
Normal file
15
src/modules/network_data/repository/perf_kpi.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package repository
|
||||
|
||||
import "nms_nbi/src/modules/network_data/model"
|
||||
|
||||
// 性能统计 数据层接口
|
||||
type IPerfKPI interface {
|
||||
// SelectGoldKPI 通过网元指标数据信息
|
||||
SelectGoldKPI(query model.GoldKPIQuery, kpiIds []string) []map[string]any
|
||||
|
||||
// SelectGoldKPITitle 网元对应的指标名称
|
||||
SelectGoldKPITitle(neType string) []model.GoldKPITitle
|
||||
|
||||
// SelectUPFTotalFlow 查询UPF总流量 N3上行 N6下行
|
||||
SelectUPFTotalFlow(neType, rmUID, startDate, endDate string) map[string]any
|
||||
}
|
||||
133
src/modules/network_data/repository/perf_kpi.impl.go
Normal file
133
src/modules/network_data/repository/perf_kpi.impl.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 PerfKPIImpl 结构体
|
||||
var NewPerfKPIImpl = &PerfKPIImpl{}
|
||||
|
||||
// PerfKPIImpl 性能统计 数据层处理
|
||||
type PerfKPIImpl struct{}
|
||||
|
||||
// SelectGoldKPI 通过网元指标数据信息
|
||||
func (r *PerfKPIImpl) SelectGoldKPI(query model.GoldKPIQuery, kpiIds []string) []map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if query.RmUID != "" {
|
||||
conditions = append(conditions, "gk.rm_uid = ?")
|
||||
params = append(params, query.RmUID)
|
||||
}
|
||||
if query.NeType != "" {
|
||||
conditions = append(conditions, "gk.ne_type = ?")
|
||||
params = append(params, query.NeType)
|
||||
}
|
||||
if query.StartTime != "" {
|
||||
conditions = append(conditions, "gk.start_time >= ?")
|
||||
params = append(params, query.StartTime)
|
||||
}
|
||||
if query.EndTime != "" {
|
||||
conditions = append(conditions, "gk.start_time <= ?")
|
||||
params = append(params, query.EndTime)
|
||||
}
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询字段列
|
||||
timeFormat := "DATE_FORMAT(gk.start_time, '%Y-%m-%d %H:%i:')"
|
||||
secondGroup := fmt.Sprintf("LPAD(FLOOR(SECOND(gk.start_time) / %d) * %d, 2, '0')", query.Interval, query.Interval)
|
||||
groupByField := fmt.Sprintf("CONCAT( %s, %s ) AS timeGroup", timeFormat, secondGroup)
|
||||
if query.Interval > 60 {
|
||||
minute := query.Interval / 60
|
||||
timeFormat = "DATE_FORMAT(gk.start_time, '%Y-%m-%d %H:')"
|
||||
minuteGroup := fmt.Sprintf("LPAD(FLOOR(MINUTE(gk.start_time) / %d) * %d, 2, '0')", minute, minute)
|
||||
groupByField = fmt.Sprintf("CONCAT( %s, %s ) AS timeGroup", timeFormat, minuteGroup)
|
||||
}
|
||||
var fields = []string{
|
||||
groupByField,
|
||||
"min(CASE WHEN gk.index != '' THEN gk.index ELSE 0 END) AS startIndex",
|
||||
"min(CASE WHEN gk.ne_type != '' THEN gk.ne_type ELSE 0 END) AS neType",
|
||||
"min(CASE WHEN gk.ne_name != '' THEN gk.ne_name ELSE 0 END) AS neName",
|
||||
}
|
||||
for _, kid := range kpiIds {
|
||||
// 特殊字段,只取最后一次收到的非0值
|
||||
if kid == "AMF.01" || kid == "UDM.01" || kid == "UDM.02" || kid == "UDM.03" || kid == "SMF.01" {
|
||||
str := fmt.Sprintf("IFNULL(SUBSTRING_INDEX(GROUP_CONCAT( CASE WHEN gk.kpi_id = '%s' and gk.VALUE != 0 THEN gk.VALUE END ), ',', 1), 0) AS '%s'", kid, kid)
|
||||
fields = append(fields, str)
|
||||
} else {
|
||||
str := fmt.Sprintf("sum(CASE WHEN gk.kpi_id = '%s' THEN gk.value ELSE 0 END) AS '%s'", kid, kid)
|
||||
fields = append(fields, str)
|
||||
}
|
||||
}
|
||||
fieldsSql := strings.Join(fields, ",")
|
||||
|
||||
// 查询数据
|
||||
if query.SortField == "" {
|
||||
query.SortField = "timeGroup"
|
||||
}
|
||||
if query.SortOrder == "" {
|
||||
query.SortOrder = "desc"
|
||||
}
|
||||
orderSql := fmt.Sprintf(" order by %s %s", query.SortField, query.SortOrder)
|
||||
querySql := fmt.Sprintf("SELECT %s FROM gold_kpi gk %s GROUP BY timeGroup %s", fieldsSql, whereSql, orderSql)
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// SelectGoldKPITitle 网元对应的指标名称
|
||||
func (r *PerfKPIImpl) SelectGoldKPITitle(neType string) []model.GoldKPITitle {
|
||||
result := []model.GoldKPITitle{}
|
||||
tx := datasource.DefaultDB().Table("kpi_title").Where("ne_type = ?", neType).Find(&result)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("Find err => %v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectUPFTotalFlow 查询UPF总流量 N3上行 N6下行
|
||||
func (r *PerfKPIImpl) SelectUPFTotalFlow(neType, rmUID, startDate, endDate string) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neType != "" {
|
||||
conditions = append(conditions, "gk.ne_type = ?")
|
||||
params = append(params, neType)
|
||||
}
|
||||
if rmUID != "" {
|
||||
conditions = append(conditions, "gk.rm_uid = ?")
|
||||
params = append(params, rmUID)
|
||||
}
|
||||
if startDate != "" {
|
||||
conditions = append(conditions, "gk.date >= ?")
|
||||
params = append(params, startDate)
|
||||
}
|
||||
if endDate != "" {
|
||||
conditions = append(conditions, "gk.date <= ?")
|
||||
params = append(params, endDate)
|
||||
}
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := fmt.Sprintf("SELECT sum( CASE WHEN gk.kpi_id = 'UPF.03' THEN gk.VALUE ELSE 0 END ) AS 'up', sum( CASE WHEN gk.kpi_id = 'UPF.06' THEN gk.VALUE ELSE 0 END ) AS 'down' FROM gold_kpi gk %s", whereSql)
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
return results[0]
|
||||
}
|
||||
15
src/modules/network_data/repository/ue_event.go
Normal file
15
src/modules/network_data/repository/ue_event.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package repository
|
||||
|
||||
import "nms_nbi/src/modules/network_data/model"
|
||||
|
||||
// UE会话事件 数据层接口
|
||||
type IUEEvent interface {
|
||||
// SelectPage 根据条件分页查询
|
||||
SelectPage(querys model.UEEventQuery) map[string]any
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(ueIds []string) []model.UEEvent
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ueIds []string) int64
|
||||
}
|
||||
168
src/modules/network_data/repository/ue_event.impl.go
Normal file
168
src/modules/network_data/repository/ue_event.impl.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/datasource"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/utils/date"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/repo"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 UEEventImpl 结构体
|
||||
var NewUEEventImpl = &UEEventImpl{
|
||||
selectSql: `select id, ne_type, ne_name, rm_uid, timestamp, event_type, event_json, created_at from ue_event`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_name": "NeName",
|
||||
"rm_uid": "RmUID",
|
||||
"timestamp": "Timestamp",
|
||||
"event_type": "EventType",
|
||||
"event_json": "EventJSONStr",
|
||||
"created_at": "CreatedAt",
|
||||
},
|
||||
}
|
||||
|
||||
// UEEventImpl UE会话事件 数据层处理
|
||||
type UEEventImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *UEEventImpl) convertResultRows(rows []map[string]any) []model.UEEvent {
|
||||
arr := make([]model.UEEvent, 0)
|
||||
for _, row := range rows {
|
||||
item := model.UEEvent{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *UEEventImpl) SelectPage(querys model.UEEventQuery) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if querys.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, querys.NeType)
|
||||
}
|
||||
if querys.RmUID != "" {
|
||||
conditions = append(conditions, "rm_uid = ?")
|
||||
params = append(params, querys.RmUID)
|
||||
}
|
||||
if querys.StartTime != "" {
|
||||
conditions = append(conditions, "timestamp >= ?")
|
||||
beginDate := date.ParseStrToDate(querys.StartTime, date.YYYY_MM_DD_HH_MM_SS)
|
||||
params = append(params, beginDate.Unix())
|
||||
}
|
||||
if querys.EndTime != "" {
|
||||
conditions = append(conditions, "timestamp <= ?")
|
||||
endDate := date.ParseStrToDate(querys.EndTime, date.YYYY_MM_DD_HH_MM_SS)
|
||||
params = append(params, endDate.Unix())
|
||||
}
|
||||
if querys.EventType != "" {
|
||||
eventTypes := strings.Split(querys.EventType, ",")
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(eventTypes))
|
||||
conditions = append(conditions, fmt.Sprintf("event_type in (%s)", placeholder))
|
||||
for _, eventType := range eventTypes {
|
||||
params = append(params, eventType)
|
||||
}
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.CDREvent{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ue_event"
|
||||
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(querys.PageNum, querys.PageSize)
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 排序
|
||||
orderSql := ""
|
||||
if querys.SortField != "" {
|
||||
sortSql := querys.SortField
|
||||
if querys.SortOrder != "" {
|
||||
if querys.SortOrder == "desc" {
|
||||
sortSql += " desc "
|
||||
} else {
|
||||
sortSql += " asc "
|
||||
}
|
||||
}
|
||||
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + orderSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *UEEventImpl) SelectByIds(ueIds []string) []model.UEEvent {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(ueIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(ueIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.UEEvent{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *UEEventImpl) DeleteByIds(ueIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(ueIds))
|
||||
sql := "delete from ue_event where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(ueIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
12
src/modules/network_data/service/cdr_event.go
Normal file
12
src/modules/network_data/service/cdr_event.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package service
|
||||
|
||||
import "nms_nbi/src/modules/network_data/model"
|
||||
|
||||
// CDR会话事件 服务层接口
|
||||
type ICDREvent interface {
|
||||
// SelectPage 根据条件分页查询
|
||||
SelectPage(querys model.CDREventQuery) map[string]any
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(cdrIds []string) (int64, error)
|
||||
}
|
||||
40
src/modules/network_data/service/cdr_event.impl.go
Normal file
40
src/modules/network_data/service/cdr_event.impl.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
"nms_nbi/src/modules/network_data/repository"
|
||||
)
|
||||
|
||||
// 实例化数据层 CDREventImpl 结构体
|
||||
var NewCDREventImpl = &CDREventImpl{
|
||||
cdrEventRepository: repository.NewCDREventImpl,
|
||||
}
|
||||
|
||||
// CDREventImpl CDR会话事件 服务层处理
|
||||
type CDREventImpl struct {
|
||||
// CDR会话事件数据信息
|
||||
cdrEventRepository repository.ICDREvent
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *CDREventImpl) SelectPage(querys model.CDREventQuery) map[string]any {
|
||||
return r.cdrEventRepository.SelectPage(querys)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *CDREventImpl) DeleteByIds(cdrIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
ids := r.cdrEventRepository.SelectByIds(cdrIds)
|
||||
if len(ids) <= 0 {
|
||||
return 0, fmt.Errorf("not data")
|
||||
}
|
||||
|
||||
if len(ids) == len(cdrIds) {
|
||||
rows := r.cdrEventRepository.DeleteByIds(cdrIds)
|
||||
return rows, nil
|
||||
}
|
||||
// 删除信息失败!
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
15
src/modules/network_data/service/perf_kpi.go
Normal file
15
src/modules/network_data/service/perf_kpi.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package service
|
||||
|
||||
import "nms_nbi/src/modules/network_data/model"
|
||||
|
||||
// 性能统计 服务层接口
|
||||
type IPerfKPI interface {
|
||||
// SelectGoldKPI 通过网元指标数据信息
|
||||
SelectGoldKPI(query model.GoldKPIQuery) []map[string]any
|
||||
|
||||
// SelectGoldKPITitle 网元对应的指标名称
|
||||
SelectGoldKPITitle(neType string) []model.GoldKPITitle
|
||||
|
||||
// SelectUPFTotalFlow 查询UPF总流量 N3上行 N6下行
|
||||
SelectUPFTotalFlow(neType, rmUID string, day int) map[string]any
|
||||
}
|
||||
76
src/modules/network_data/service/perf_kpi.impl.go
Normal file
76
src/modules/network_data/service/perf_kpi.impl.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/constants/cachekey"
|
||||
"nms_nbi/src/framework/redis"
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
"nms_nbi/src/modules/network_data/repository"
|
||||
)
|
||||
|
||||
// 实例化数据层 PerfKPIImpl 结构体
|
||||
var NewPerfKPIImpl = &PerfKPIImpl{
|
||||
perfKPIRepository: repository.NewPerfKPIImpl,
|
||||
}
|
||||
|
||||
// PerfKPIImpl 性能统计 服务层处理
|
||||
type PerfKPIImpl struct {
|
||||
// 性能统计数据信息
|
||||
perfKPIRepository repository.IPerfKPI
|
||||
}
|
||||
|
||||
// SelectGoldKPI 通过网元指标数据信息
|
||||
func (r *PerfKPIImpl) SelectGoldKPI(query model.GoldKPIQuery) []map[string]any {
|
||||
// 获取数据指标id
|
||||
var kpiIds []string
|
||||
kpiTitles := r.perfKPIRepository.SelectGoldKPITitle(query.NeType)
|
||||
for _, kpiId := range kpiTitles {
|
||||
kpiIds = append(kpiIds, kpiId.KPIID)
|
||||
}
|
||||
|
||||
data := r.perfKPIRepository.SelectGoldKPI(query, kpiIds)
|
||||
if data == nil {
|
||||
return []map[string]any{}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// SelectGoldKPITitle 网元对应的指标名称
|
||||
func (r *PerfKPIImpl) SelectGoldKPITitle(neType string) []model.GoldKPITitle {
|
||||
return r.perfKPIRepository.SelectGoldKPITitle(neType)
|
||||
}
|
||||
|
||||
// SelectUPFTotalFlow 查询UPF总流量 N3上行 N6下行
|
||||
func (r *PerfKPIImpl) SelectUPFTotalFlow(neType, rmUID string, day int) map[string]any {
|
||||
// 获取当前日期
|
||||
now := time.Now()
|
||||
endDate := now.Format("2006-01-02")
|
||||
// 将当前日期前几天数
|
||||
afterDays := now.AddDate(0, 0, -day)
|
||||
startDate := afterDays.Format("2006-01-02")
|
||||
|
||||
var info map[string]any
|
||||
|
||||
// 读取缓存数据 小于2分钟重新缓存
|
||||
key := fmt.Sprintf("%sUPF:totalFlow:%s_%d", cachekey.NE_DATA_KEY, rmUID, day)
|
||||
infoStr, _ := redis.Get("", key)
|
||||
if infoStr != "" {
|
||||
json.Unmarshal([]byte(infoStr), &info)
|
||||
expireSecond, _ := redis.GetExpire("", key)
|
||||
expireMinute := (time.Duration(int64(expireSecond)) * time.Second)
|
||||
if expireMinute > 2*time.Minute {
|
||||
return info
|
||||
}
|
||||
}
|
||||
|
||||
info = r.perfKPIRepository.SelectUPFTotalFlow(neType, rmUID, startDate, endDate)
|
||||
|
||||
// 保存到缓存
|
||||
infoJSON, _ := json.Marshal(info)
|
||||
redis.SetByExpire("", key, string(infoJSON), time.Duration(10)*time.Minute)
|
||||
|
||||
return info
|
||||
}
|
||||
12
src/modules/network_data/service/ue_event.go
Normal file
12
src/modules/network_data/service/ue_event.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package service
|
||||
|
||||
import "nms_nbi/src/modules/network_data/model"
|
||||
|
||||
// UE会话事件 服务层接口
|
||||
type IUEEvent interface {
|
||||
// SelectPage 根据条件分页查询
|
||||
SelectPage(querys model.UEEventQuery) map[string]any
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ueIds []string) (int64, error)
|
||||
}
|
||||
40
src/modules/network_data/service/ue_event.impl.go
Normal file
40
src/modules/network_data/service/ue_event.impl.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nms_nbi/src/modules/network_data/model"
|
||||
"nms_nbi/src/modules/network_data/repository"
|
||||
)
|
||||
|
||||
// 实例化数据层 UEEventImpl 结构体
|
||||
var NewUEEventImpl = &UEEventImpl{
|
||||
ueEventRepository: repository.NewUEEventImpl,
|
||||
}
|
||||
|
||||
// UEEventImpl UE会话事件 服务层处理
|
||||
type UEEventImpl struct {
|
||||
// UE会话事件数据信息
|
||||
ueEventRepository repository.IUEEvent
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *UEEventImpl) SelectPage(querys model.UEEventQuery) map[string]any {
|
||||
return r.ueEventRepository.SelectPage(querys)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *UEEventImpl) DeleteByIds(ueIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
ids := r.ueEventRepository.SelectByIds(ueIds)
|
||||
if len(ids) <= 0 {
|
||||
return 0, fmt.Errorf("no data")
|
||||
}
|
||||
|
||||
if len(ids) == len(ueIds) {
|
||||
rows := r.ueEventRepository.DeleteByIds(ueIds)
|
||||
return rows, nil
|
||||
}
|
||||
// 删除信息失败!
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
158
src/modules/network_element/controller/ne_action.go
Normal file
158
src/modules/network_element/controller/ne_action.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/file"
|
||||
"nms_nbi/src/framework/utils/ssh"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeActionController 结构体
|
||||
var NewNeAction = &NeActionController{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
}
|
||||
|
||||
// 网元处理请求
|
||||
//
|
||||
// PATH /action
|
||||
type NeActionController struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
}
|
||||
|
||||
// 发送文件到网元端
|
||||
//
|
||||
// POST /pushFile
|
||||
func (s *NeActionController) PushFile(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"`
|
||||
NeID string `json:"neId" binding:"required"`
|
||||
UploadPath string `json:"uploadPath" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeID)
|
||||
if neInfo.NeId != body.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
// 本地文件
|
||||
localPath := file.ParseUploadFilePath(body.UploadPath)
|
||||
nePath := "/tmp" //config.Get("mml.upload").(string)
|
||||
// 复制到远程
|
||||
err := ssh.FileSCPLocalToNe(neInfo.IP, localPath, nePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 网元端文件路径
|
||||
fileName := localPath[strings.LastIndex(localPath, "/")+1:]
|
||||
neFilePath := fmt.Sprintf("%s/%s", nePath, fileName)
|
||||
c.JSON(200, result.OkData(filepath.ToSlash(neFilePath)))
|
||||
}
|
||||
|
||||
// 获取文件从网元到本地
|
||||
//
|
||||
// GET /pullFile
|
||||
func (s *NeActionController) PullFile(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
Path string `form:"path" binding:"required"`
|
||||
FileName string `form:"fileName" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
nePath := fmt.Sprintf("%s/%s", querys.Path, querys.FileName)
|
||||
localPath := fmt.Sprintf("/tmp/omc/pullFile/%s", querys.FileName)
|
||||
err := ssh.FileSCPNeToLocal(neInfo.IP, nePath, localPath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.FileAttachment(localPath, querys.FileName)
|
||||
}
|
||||
|
||||
// 网元端文件列表
|
||||
//
|
||||
// GET /files
|
||||
func (s *NeActionController) Files(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
Path string `form:"path" binding:"required"`
|
||||
PageNum int64 `form:"pageNum" binding:"required"`
|
||||
PageSize int64 `form:"pageSize" binding:"required"`
|
||||
Search string `form:"search"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
totalSize, rows, err := ssh.FileList(querys.Path, neInfo.IP, querys.Search)
|
||||
if err != nil {
|
||||
c.JSON(200, result.Ok(map[string]any{
|
||||
"path": querys.Path,
|
||||
"totalSize": totalSize,
|
||||
"total": len(rows),
|
||||
"rows": []ssh.FileListRow{},
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
// 对数组进行切片分页
|
||||
lenNum := int64(len(rows))
|
||||
start := (querys.PageNum - 1) * querys.PageSize
|
||||
end := start + querys.PageSize
|
||||
var splitRows []ssh.FileListRow
|
||||
if start >= lenNum {
|
||||
splitRows = []ssh.FileListRow{}
|
||||
} else if end >= lenNum {
|
||||
splitRows = rows[start:]
|
||||
} else {
|
||||
splitRows = rows[start:end]
|
||||
}
|
||||
|
||||
c.JSON(200, result.Ok(map[string]any{
|
||||
"path": querys.Path,
|
||||
"totalSize": totalSize,
|
||||
"total": lenNum,
|
||||
"rows": splitRows,
|
||||
}))
|
||||
}
|
||||
357
src/modules/network_element/controller/ne_host.go
Normal file
357
src/modules/network_element/controller/ne_host.go
Normal file
@@ -0,0 +1,357 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/ssh"
|
||||
"nms_nbi/src/framework/utils/telnet"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeHostController 结构体
|
||||
var NewNeHost = &NeHostController{
|
||||
neHostService: neService.NewNeHostImpl,
|
||||
}
|
||||
|
||||
// 网元主机连接请求
|
||||
//
|
||||
// PATH /host
|
||||
type NeHostController struct {
|
||||
// 网元主机连接服务
|
||||
neHostService neService.INeHost
|
||||
}
|
||||
|
||||
// 网元主机列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeHostController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.neHostService.SelectPage(querys)
|
||||
|
||||
rows := data["rows"].([]model.NeHost)
|
||||
arr := &rows
|
||||
for i := range *arr {
|
||||
(*arr)[i].Password = "-"
|
||||
(*arr)[i].PrivateKey = "-"
|
||||
(*arr)[i].PassPhrase = "-"
|
||||
}
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 网元主机信息
|
||||
//
|
||||
// GET /:hostId
|
||||
func (s *NeHostController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
hostId := c.Param("hostId")
|
||||
if hostId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neHost := s.neHostService.SelectById(hostId)
|
||||
if neHost.HostID != hostId {
|
||||
// 没有可访问主机信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neHost))
|
||||
}
|
||||
|
||||
// 网元主机新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeHostController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.HostID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, "")
|
||||
if !uniqueHost {
|
||||
// 主机信息操作【%s】失败,同组内名称已存在
|
||||
msg := i18n.TTemplate(language, "neHost.errKeyExists", map[string]any{"name": body.Title})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.neHostService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元主机修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeHostController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.HostID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, body.HostID)
|
||||
if !uniqueHost {
|
||||
// 主机信息操作【%s】失败,同组内名称已存在
|
||||
msg := i18n.TTemplate(language, "neHost.errKeyExists", map[string]any{"name": body.Title})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neHost := s.neHostService.SelectById(body.HostID)
|
||||
if neHost.HostID != body.HostID {
|
||||
// 没有可访问主机信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.neHostService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元主机删除
|
||||
//
|
||||
// DELETE /:hostIds
|
||||
func (s *NeHostController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
hostIds := c.Param("hostIds")
|
||||
if hostIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(hostIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.neHostService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 网元主机测试连接
|
||||
//
|
||||
// POST /test
|
||||
func (s *NeHostController) Test(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
if body.HostType == "ssh" {
|
||||
var connSSH ssh.ConnSSH
|
||||
body.CopyTo(&connSSH)
|
||||
|
||||
client, err := connSSH.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
|
||||
if body.HostType == "telnet" {
|
||||
var connTelnet telnet.ConnTelnet
|
||||
body.CopyTo(&connTelnet)
|
||||
|
||||
client, err := connTelnet.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 网元主机发送命令
|
||||
//
|
||||
// POST /cmd
|
||||
func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
HostID string `json:"hostId" binding:"required"` // 主机ID
|
||||
Cmd string `json:"cmd" binding:"required"` // 执行命令
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neHost := s.neHostService.SelectById(body.HostID)
|
||||
if neHost.HostID != body.HostID {
|
||||
// 没有可访问主机信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
if neHost.HostType == "ssh" {
|
||||
var connSSH ssh.ConnSSH
|
||||
neHost.CopyTo(&connSSH)
|
||||
|
||||
client, err := connSSH.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// 执行命令
|
||||
output, err := client.RunCMD(body.Cmd)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(output))
|
||||
return
|
||||
}
|
||||
|
||||
if neHost.HostType == "telnet" {
|
||||
var connTelnet telnet.ConnTelnet
|
||||
neHost.CopyTo(&connTelnet)
|
||||
|
||||
client, err := connTelnet.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// 执行命令
|
||||
output, err := client.RunCMD(body.Cmd)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(output))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 网元主机SSH方式检查服务器环境
|
||||
//
|
||||
// POST /checkBySSH
|
||||
func (s *NeHostController) CheckBySSH(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var connSSH ssh.ConnSSH
|
||||
body.CopyTo(&connSSH)
|
||||
|
||||
// 创建链接SSH客户端
|
||||
client, err := connSSH.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// 结果信息数据
|
||||
data := map[string]any{
|
||||
"addr": body.Addr, // 服务器地址
|
||||
"kernelName": "-", // 内核名称 -s
|
||||
"nodename": "-", // 网络节点主机名 -n
|
||||
"kernelRelease": "-", // 内核发布版本 -r
|
||||
"machine": "-", // 机器硬件名称 -m
|
||||
"prettyName": "-", // 系统发行版本
|
||||
"sudo": false, // 可提权
|
||||
"sshLink": false, // 可直连
|
||||
}
|
||||
|
||||
// 执行命令 检查系统环境
|
||||
output, err := client.RunCMD("uname -snrm && cat /etc/os-release | grep PRETTY_NAME")
|
||||
if err != nil {
|
||||
c.JSON(200, result.OkData(data))
|
||||
return
|
||||
}
|
||||
output = strings.TrimSuffix(output, "\n")
|
||||
sysInfoArr := strings.SplitN(output, "\n", 2)
|
||||
if len(sysInfoArr) == 2 {
|
||||
// uname -snrm
|
||||
baseInfoArr := strings.SplitN(sysInfoArr[0], " ", 4)
|
||||
data["kernelName"] = baseInfoArr[0]
|
||||
data["nodename"] = baseInfoArr[1]
|
||||
data["kernelRelease"] = baseInfoArr[2]
|
||||
data["machine"] = baseInfoArr[3]
|
||||
// cat /etc/os-release | grep PRETTY_NAME
|
||||
prettyName := sysInfoArr[1]
|
||||
index := strings.Index(prettyName, `"`)
|
||||
if index != -1 {
|
||||
data["prettyName"] = prettyName[index+1 : len(prettyName)-1]
|
||||
}
|
||||
}
|
||||
// 执行命令 检查sudo权限
|
||||
_, err = client.RunCMD("sudo -n uname")
|
||||
if err == nil {
|
||||
data["sudo"] = true
|
||||
} else {
|
||||
data["sudo"] = false
|
||||
}
|
||||
|
||||
// 本地免密创建链接直连
|
||||
lcoalConnSSH := ssh.ConnSSH{
|
||||
User: body.User,
|
||||
Addr: body.Addr,
|
||||
Port: body.Port,
|
||||
}
|
||||
lcoalClient, err := lcoalConnSSH.NewClientByLocalPrivate()
|
||||
if err == nil {
|
||||
data["sshLink"] = true
|
||||
} else {
|
||||
data["sshLink"] = false
|
||||
}
|
||||
defer lcoalClient.Close()
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
153
src/modules/network_element/controller/ne_host_cmd.go
Normal file
153
src/modules/network_element/controller/ne_host_cmd.go
Normal file
@@ -0,0 +1,153 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeHostCmdController 结构体
|
||||
var NewNeHostCmd = &NeHostCmdController{
|
||||
neHostCmdService: neService.NewNeHostCmdImpl,
|
||||
}
|
||||
|
||||
// 网元主机命令请求
|
||||
//
|
||||
// PATH /hostCmd
|
||||
type NeHostCmdController struct {
|
||||
// 网元主机命令服务
|
||||
neHostCmdService neService.INeHostCmd
|
||||
}
|
||||
|
||||
// 网元主机命令列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeHostCmdController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.neHostCmdService.SelectPage(querys)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 网元主机命令信息
|
||||
//
|
||||
// GET /:cmdId
|
||||
func (s *NeHostCmdController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cmdId := c.Param("cmdId")
|
||||
if cmdId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neHost := s.neHostCmdService.SelectById(cmdId)
|
||||
if neHost.CmdID != cmdId {
|
||||
// 没有可访问主机命令数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neHost))
|
||||
}
|
||||
|
||||
// 网元主机命令新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeHostCmdController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeHostCmd
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.CmdID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, "")
|
||||
if !uniqueHostCmd {
|
||||
// 主机命令操作【%s】失败,同组内名称已存在
|
||||
msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.neHostCmdService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元主机命令修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeHostCmdController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeHostCmd
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.CmdID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, body.CmdID)
|
||||
if !uniqueHostCmd {
|
||||
// 主机命令操作【%s】失败,同组内名称已存在
|
||||
msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neHost := s.neHostCmdService.SelectById(body.CmdID)
|
||||
if neHost.CmdID != body.CmdID {
|
||||
// 没有可访问主机命令数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.neHostCmdService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元主机命令删除
|
||||
//
|
||||
// DELETE /:cmdIds
|
||||
func (s *NeHostCmdController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
cmdIds := c.Param("cmdIds")
|
||||
if cmdIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(cmdIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.neHostCmdService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
294
src/modules/network_element/controller/ne_info.go
Normal file
294
src/modules/network_element/controller/ne_info.go
Normal file
@@ -0,0 +1,294 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeInfoController 结构体
|
||||
var NewNeInfo = &NeInfoController{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
}
|
||||
|
||||
// 网元信息请求
|
||||
//
|
||||
// PATH /
|
||||
type NeInfoController struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
}
|
||||
|
||||
// neStateCacheMap 网元状态缓存最后一次成功的信息
|
||||
var neStateCacheMap sync.Map
|
||||
var mutex sync.Mutex
|
||||
|
||||
// 网元信息状态
|
||||
//
|
||||
// GET /state
|
||||
func (s *NeInfoController) State(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
neKey := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
|
||||
|
||||
// 网元直连
|
||||
resData, err := neService.NeState(neInfo)
|
||||
if err != nil {
|
||||
mutex.Lock()
|
||||
// 异常取上次缓存
|
||||
resDataCache, ok := neStateCacheMap.Load(neKey)
|
||||
if ok && resDataCache != nil {
|
||||
resDataCache.(map[string]any)["online"] = false
|
||||
} else {
|
||||
resDataCache = map[string]any{
|
||||
"online": false,
|
||||
"neId": neInfo.NeId,
|
||||
"neName": neInfo.NeName,
|
||||
"neType": neInfo.NeType,
|
||||
"neIP": neInfo.IP,
|
||||
}
|
||||
}
|
||||
neStateCacheMap.Store(neKey, resDataCache)
|
||||
mutex.Unlock()
|
||||
c.JSON(200, result.OkData(resDataCache))
|
||||
return
|
||||
}
|
||||
|
||||
// 存入缓存
|
||||
resData["online"] = true
|
||||
mutex.Lock()
|
||||
neStateCacheMap.Store(neKey, resData)
|
||||
mutex.Unlock()
|
||||
c.JSON(200, result.OkData(resData))
|
||||
}
|
||||
|
||||
// 网元信息列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeInfoController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
bandStatus := false
|
||||
if v, ok := querys["bandStatus"]; ok && v != nil {
|
||||
bandStatus = parse.Boolean(v)
|
||||
}
|
||||
data := s.neInfoService.SelectPage(querys, bandStatus)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 网元信息列表全部无分页
|
||||
//
|
||||
// GET /listAll
|
||||
func (s *NeInfoController) ListAll(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType"`
|
||||
NeId string `form:"neId"`
|
||||
BandStatus string `form:"bandStatus"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询实体参数
|
||||
ne := model.NeInfo{}
|
||||
if querys.NeType != "" {
|
||||
ne.NeType = querys.NeType
|
||||
}
|
||||
if querys.NeId != "" {
|
||||
ne.NeId = querys.NeId
|
||||
}
|
||||
bandStatus := parse.Boolean(querys.BandStatus)
|
||||
neList := s.neInfoService.SelectList(ne, bandStatus)
|
||||
if len(neList) == 0 {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(neList))
|
||||
}
|
||||
|
||||
// 网元信息
|
||||
//
|
||||
// GET /:infoId
|
||||
func (s *NeInfoController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
infoId := c.Param("infoId")
|
||||
if infoId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neHost := s.neInfoService.SelectById(infoId, true)
|
||||
if neHost.ID != infoId {
|
||||
// 没有可访问网元信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neInfo.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neHost))
|
||||
}
|
||||
|
||||
// 网元neType和neID查询
|
||||
//
|
||||
// GET /
|
||||
func (s *NeInfoController) NeTypeAndID(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(neInfo))
|
||||
}
|
||||
|
||||
// 网元信息新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeInfoController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeInfo
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueInfo := s.neInfoService.CheckUniqueNeTypeAndNeId(body.NeType, body.NeId, "")
|
||||
if !uniqueInfo {
|
||||
// 网元信息操作【%s】失败,同类型下标识已存在
|
||||
msg := i18n.TTemplate(language, "neInfo.errKeyExists", map[string]any{"key": body.NeId})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取网元状态是否正常
|
||||
_, err = neService.NeState(body)
|
||||
if err != nil {
|
||||
body.Status = "1"
|
||||
} else {
|
||||
// 下发网管配置信息给网元
|
||||
_, err = neService.NeConfigOMC(body)
|
||||
if err == nil {
|
||||
body.Status = "0"
|
||||
} else {
|
||||
body.Status = "3"
|
||||
}
|
||||
}
|
||||
|
||||
insertId := s.neInfoService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元信息修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeInfoController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeInfo
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHostCmd := s.neInfoService.CheckUniqueNeTypeAndNeId(body.NeType, body.NeId, body.ID)
|
||||
if !uniqueHostCmd {
|
||||
// 网元信息操作【%s】失败,同类型下标识已存在
|
||||
msg := i18n.TTemplate(language, "neInfo.errKeyExists", map[string]any{"key": body.NeId})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neInfo := s.neInfoService.SelectById(body.ID, false)
|
||||
if neInfo.ID != body.ID {
|
||||
// 没有可访问网元信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neInfo.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取网元状态是否正常
|
||||
_, err = neService.NeState(body)
|
||||
if err != nil {
|
||||
body.Status = "1"
|
||||
} else {
|
||||
// 下发网管配置信息给网元
|
||||
_, err = neService.NeConfigOMC(body)
|
||||
if err == nil {
|
||||
body.Status = "0"
|
||||
} else {
|
||||
body.Status = "3"
|
||||
}
|
||||
}
|
||||
|
||||
rows := s.neInfoService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元信息删除
|
||||
//
|
||||
// DELETE /:infoIds
|
||||
func (s *NeInfoController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
infoIds := c.Param("infoIds")
|
||||
if infoIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(infoIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.neInfoService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
181
src/modules/network_element/controller/ne_software.go
Normal file
181
src/modules/network_element/controller/ne_software.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeSoftwareController 结构体
|
||||
var NewNeSoftware = &NeSoftwareController{
|
||||
neSoftwareService: neService.NewNeSoftwareImpl,
|
||||
}
|
||||
|
||||
// 网元软件包信息请求
|
||||
//
|
||||
// PATH /software
|
||||
type NeSoftwareController struct {
|
||||
// 网元软件包信息服务
|
||||
neSoftwareService neService.INeSoftware
|
||||
}
|
||||
|
||||
// 网元软件包信息列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeSoftwareController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.neSoftwareService.SelectPage(querys)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 网元软件包信息信息
|
||||
//
|
||||
// GET /:softwareId
|
||||
func (s *NeSoftwareController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
softwareId := c.Param("softwareId")
|
||||
if softwareId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neSoftware := s.neSoftwareService.SelectById(softwareId)
|
||||
if neSoftware.ID != softwareId {
|
||||
// 没有可访问网元版本数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neSoftware))
|
||||
}
|
||||
|
||||
// 网元软件包信息新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeSoftwareController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeSoftware
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndFileNameAndVersion(body.NeType, body.FileName, body.Version, "")
|
||||
if !uniqueSoftware {
|
||||
// 网元软件包操作【%s】失败,网元类型与文件名版本已存在
|
||||
msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.FileName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
insertId := s.neSoftwareService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元软件包信息修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeSoftwareController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeSoftware
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndFileNameAndVersion(body.NeType, body.FileName, body.Version, body.ID)
|
||||
if !uniqueSoftware {
|
||||
// 网元软件包操作【%s】失败,网元类型与文件名版本已存在
|
||||
msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.FileName})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neSoftware := s.neSoftwareService.SelectById(body.ID)
|
||||
if neSoftware.ID != body.ID {
|
||||
// 没有可访问网元版本数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
rows := s.neSoftwareService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元软件包信息删除
|
||||
//
|
||||
// DELETE /:softwareIds
|
||||
func (s *NeSoftwareController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
softwareIds := c.Param("softwareIds")
|
||||
if softwareIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(softwareIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.neSoftwareService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 网元软件包安装
|
||||
//
|
||||
// POST /install
|
||||
func (s *NeSoftwareController) Install(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeSoftware
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neSoftwares := s.neSoftwareService.SelectList(body)
|
||||
if len(neSoftwares) == 0 {
|
||||
// 没有可访问网元版本数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData")))
|
||||
return
|
||||
}
|
||||
neSoftware := neSoftwares[0]
|
||||
|
||||
// 进行安装
|
||||
output, err := s.neSoftwareService.Install(neSoftware)
|
||||
if err != nil {
|
||||
c.JSON(200, result.OkData(output))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
151
src/modules/network_element/controller/ne_version.go
Normal file
151
src/modules/network_element/controller/ne_version.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeVersionController 结构体
|
||||
var NewNeVersion = &NeVersionController{
|
||||
neVersionService: neService.NewNeVersionImpl,
|
||||
}
|
||||
|
||||
// 网元版本信息请求
|
||||
//
|
||||
// PATH /version
|
||||
type NeVersionController struct {
|
||||
// 网元版本信息服务
|
||||
neVersionService neService.INeVersion
|
||||
}
|
||||
|
||||
// 网元版本信息列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeVersionController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.neVersionService.SelectPage(querys)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 网元版本信息信息
|
||||
//
|
||||
// GET /:versionId
|
||||
func (s *NeVersionController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
versionId := c.Param("versionId")
|
||||
if versionId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neVersion := s.neVersionService.SelectById(versionId)
|
||||
if neVersion.ID != versionId {
|
||||
// 没有可访问网元版本数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neVersion.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neVersion))
|
||||
}
|
||||
|
||||
// 网元版本信息新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeVersionController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeVersion
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueInfo := s.neVersionService.CheckUniqueTypeAndID(body.NeType, body.NeId, "")
|
||||
if !uniqueInfo {
|
||||
// 网元版本操作【%s】失败,网元类型信息已存在
|
||||
msg := i18n.TTemplate(language, "neVersion.errKeyExists", map[string]any{"name": body.NeType})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
insertId := s.neVersionService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元版本信息修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeVersionController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeVersion
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueInfo := s.neVersionService.CheckUniqueTypeAndID(body.NeType, body.NeId, body.ID)
|
||||
if !uniqueInfo {
|
||||
// 网元版本操作【%s】失败,网元类型信息已存在
|
||||
msg := i18n.TTemplate(language, "neVersion.errKeyExists", map[string]any{"name": body.NeType})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neVersion := s.neVersionService.SelectById(body.ID)
|
||||
if neVersion.ID != body.ID {
|
||||
// 没有可访问网元版本数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neVersion.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
rows := s.neVersionService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元版本信息删除
|
||||
//
|
||||
// DELETE /:versionIds
|
||||
func (s *NeVersionController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
versionIds := c.Param("versionIds")
|
||||
if versionIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(versionIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.neVersionService.DeleteByIds(uniqueIDs)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
}
|
||||
472
src/modules/network_element/controller/udm_auth.go
Normal file
472
src/modules/network_element/controller/udm_auth.go
Normal file
@@ -0,0 +1,472 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mmlclient "nms_nbi/lib/core/mml_client"
|
||||
"nms_nbi/src/framework/constants/uploadsubpath"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/file"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/ssh"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 UDMAuthController 结构体
|
||||
var NewUDMAuth = &UDMAuthController{
|
||||
udmAuthService: neService.NewUDMAuthImpl,
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
}
|
||||
|
||||
// UDM鉴权用户请求
|
||||
//
|
||||
// PATH /udm/auth
|
||||
type UDMAuthController struct {
|
||||
// UDM鉴权信息服务
|
||||
udmAuthService neService.IUDMAuth
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
}
|
||||
|
||||
// UDM鉴权用户-获取全部保存数据库
|
||||
//
|
||||
// POST /resetData/:neId
|
||||
func (s *UDMAuthController) ResetData(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neId = ""
|
||||
data := s.udmAuthService.Save(neId)
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM鉴权用户
|
||||
//
|
||||
// GET /list
|
||||
func (s *UDMAuthController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
querys["neId"] = ""
|
||||
data := s.udmAuthService.Page(querys)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-信息
|
||||
//
|
||||
// GET /:neId/:imsi
|
||||
func (s *UDMAuthController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
imsi := c.Param("imsi")
|
||||
if neId == "" || imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("dsp authdat:imsi=%s", imsi)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToMap(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
c.JSON(200, result.ErrMsg("No Auth Data"))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询数据库是否存在并存入
|
||||
neId = ""
|
||||
var userInfo model.UDMAuth
|
||||
list := s.udmAuthService.List(model.UDMAuth{NeID: neId, Imsi: imsi})
|
||||
if len(list) > 0 {
|
||||
userInfo = list[0]
|
||||
// 返回查询的用户信息
|
||||
userInfo.Amf = data["amf"]
|
||||
userInfo.AlgoIndex = data["algo"]
|
||||
userInfo.Opc = data["opc"]
|
||||
userInfo.Ki = data["ki"]
|
||||
} else {
|
||||
userInfo := model.UDMAuth{
|
||||
Imsi: imsi,
|
||||
Amf: data["amf"],
|
||||
AlgoIndex: data["algo"],
|
||||
Opc: data["opc"],
|
||||
Ki: data["ki"],
|
||||
}
|
||||
s.udmAuthService.Insert(neId, userInfo)
|
||||
}
|
||||
c.JSON(200, result.OkData(userInfo))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-增加
|
||||
//
|
||||
// POST /:neId
|
||||
func (s *UDMAuthController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var body model.UDMAuth
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.Imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("add authdat:imsi=%s,ki=%s,amf=%s,algo=%s,opc=%s", body.Imsi, body.Ki, body.Amf, body.AlgoIndex, body.Opc)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmAuthService.Insert(neId, body)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-批量添加
|
||||
//
|
||||
// POST /:neId/:num
|
||||
func (s *UDMAuthController) Adds(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
num := c.Param("num")
|
||||
if neId == "" || num == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var body model.UDMAuth
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.Imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("baa authdat:start_imsi=%s,sub_num=%s,ki=%s,amf=%s,algo=%s,opc=%s", body.Imsi, num, body.Ki, body.Amf, body.AlgoIndex, body.Opc)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmAuthService.Inserts(neId, body, num)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-修改
|
||||
//
|
||||
// PUT /:neId
|
||||
func (s *UDMAuthController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var body model.UDMAuth
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.Imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("mod authdata:imsi=%s", body.Imsi)
|
||||
// 修改的参数名称
|
||||
if body.Ki != "" {
|
||||
msg += fmt.Sprintf(",ki=%s", body.Ki)
|
||||
}
|
||||
if body.Amf != "" {
|
||||
msg += fmt.Sprintf(",amf=%s", body.Amf)
|
||||
}
|
||||
if body.AlgoIndex != "" {
|
||||
msg += fmt.Sprintf(",algo=%s", body.AlgoIndex)
|
||||
}
|
||||
if body.Opc != "" {
|
||||
msg += fmt.Sprintf(",opc=%s", body.Opc)
|
||||
}
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmAuthService.Update(neId, body)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-删除
|
||||
//
|
||||
// DELETE /:neId/:imsi
|
||||
func (s *UDMAuthController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
imsi := c.Param("imsi")
|
||||
if neId == "" || imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
imsiArr := strings.Split(imsi, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(imsiArr)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
resultData := map[string]string{}
|
||||
for _, imsi := range uniqueIDs {
|
||||
msg := fmt.Sprintf("del authdat:imsi=%s", imsi)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
resultData[imsi] = err.Error()
|
||||
}
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmAuthService.Delete(neId, imsi)
|
||||
resultData[imsi] = data
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(resultData))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-批量删除
|
||||
//
|
||||
// DELETE /:neId/:imsi/:num
|
||||
func (s *UDMAuthController) Removes(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
imsi := c.Param("imsi")
|
||||
num := c.Param("num")
|
||||
if neId == "" || imsi == "" || num == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("bde authdat:start_imsi=%s,sub_num=%s", imsi, num)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmAuthService.Deletes(neId, imsi, num)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM鉴权用户-导出
|
||||
//
|
||||
// POST /export
|
||||
func (s *UDMAuthController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeId string `json:"neId" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
if !(body.Type == "csv" || body.Type == "txt") {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errImportUserAuthFileFormat")))
|
||||
return
|
||||
}
|
||||
|
||||
neId := ""
|
||||
list := s.udmAuthService.List(model.UDMAuth{NeID: neId})
|
||||
// 文件名
|
||||
fileName := fmt.Sprintf("OMC_AUTH_USER_EXPORT_%s_%d.%s", neId, time.Now().UnixMilli(), body.Type)
|
||||
filePath := fmt.Sprintf("%s/%s", file.ParseUploadFileDir(uploadsubpath.EXPORT), fileName)
|
||||
|
||||
if body.Type == "csv" {
|
||||
// 转换数据
|
||||
data := [][]string{}
|
||||
data = append(data, []string{"imsi", "ki", "algo", "amf", "opc"})
|
||||
for _, v := range list {
|
||||
data = append(data, []string{v.Imsi, v.Ki, v.AlgoIndex, v.Amf, v.Opc})
|
||||
}
|
||||
// 输出到文件
|
||||
err := file.WriterCSVFile(data, filePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if body.Type == "txt" {
|
||||
// 转换数据
|
||||
data := [][]string{}
|
||||
for _, v := range list {
|
||||
data = append(data, []string{v.Imsi, v.Ki, v.AlgoIndex, v.Amf, v.Opc})
|
||||
}
|
||||
// 输出到文件
|
||||
err = file.WriterTxtFile(data, filePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.FileAttachment(filePath, fileName)
|
||||
}
|
||||
|
||||
// UDM鉴权用户-导入
|
||||
//
|
||||
// POST /import
|
||||
func (s *UDMAuthController) Import(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.PostForm("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
formFile, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 获取文件名
|
||||
if !(strings.HasSuffix(formFile.Filename, ".csv") || strings.HasSuffix(formFile.Filename, ".txt")) {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errImportUserAuthFileFormat")))
|
||||
return
|
||||
}
|
||||
// 上传文件转存
|
||||
upFilePath, err := file.TransferUploadFile(formFile, uploadsubpath.IMPORT, nil)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
// 本地文件
|
||||
localPath := file.ParseUploadFilePath(upFilePath)
|
||||
nePath := "/tmp" //config.Get("mml.upload").(string)
|
||||
// 复制到远程
|
||||
err = ssh.FileSCPLocalToNe(neInfo.IP, localPath, nePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
fileName := localPath[strings.LastIndex(localPath, "/")+1:]
|
||||
msg := fmt.Sprintf("import authdat:path=%s", fmt.Sprintf("%s/%s", nePath, fileName))
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
if strings.HasSuffix(fileName, ".csv") {
|
||||
data := file.ReadCSVFile(localPath)
|
||||
neId = ""
|
||||
go s.udmAuthService.InsertCSV(neId, data)
|
||||
}
|
||||
if strings.HasSuffix(fileName, ".txt") {
|
||||
data := file.ReadTxtFile(localPath)
|
||||
neId = ""
|
||||
go s.udmAuthService.InsertTxt(neId, data)
|
||||
}
|
||||
}
|
||||
c.JSON(200, result.OkMsg(data))
|
||||
}
|
||||
550
src/modules/network_element/controller/udm_sub.go
Normal file
550
src/modules/network_element/controller/udm_sub.go
Normal file
@@ -0,0 +1,550 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mmlclient "nms_nbi/lib/core/mml_client"
|
||||
"nms_nbi/src/framework/constants/uploadsubpath"
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/utils/file"
|
||||
"nms_nbi/src/framework/utils/parse"
|
||||
"nms_nbi/src/framework/utils/ssh"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
"nms_nbi/src/modules/network_element/model"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 UDMSubController 结构体
|
||||
var NewUDMSub = &UDMSubController{
|
||||
udmSubService: neService.NewUDMSubImpl,
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
}
|
||||
|
||||
// UDM签约用户请求
|
||||
//
|
||||
// PATH /udm/sub
|
||||
type UDMSubController struct {
|
||||
// UDM鉴权信息服务
|
||||
udmSubService neService.IUDMSub
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
}
|
||||
|
||||
// UDM签约用户-获取全部保存数据库
|
||||
//
|
||||
// POST /resetData/:neId
|
||||
func (s *UDMSubController) ResetData(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neId = ""
|
||||
data := s.udmSubService.Save(neId)
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM签约用户
|
||||
//
|
||||
// GET /list
|
||||
func (s *UDMSubController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
querys["neId"] = ""
|
||||
data := s.udmSubService.Page(querys)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// UDM签约用户-信息
|
||||
//
|
||||
// GET /:neId/:imsi
|
||||
func (s *UDMSubController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
imsi := c.Param("imsi")
|
||||
if neId == "" || imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("dsp udmuser:imsi=%s", imsi)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToMap(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
c.JSON(200, result.ErrMsg("No Subs Data"))
|
||||
return
|
||||
}
|
||||
|
||||
// 解析返回的数据
|
||||
cnType, _ := strconv.ParseInt(data["CNType"][:4], 0, 64)
|
||||
rat, _ := strconv.ParseInt(data["RAT"][:4], 0, 64)
|
||||
msisdn := data["MSISDN"]
|
||||
imsMsisdnLen := strings.Index(msisdn, ",")
|
||||
if imsMsisdnLen != -1 {
|
||||
msisdn = msisdn[:imsMsisdnLen]
|
||||
}
|
||||
userInfo := model.UDMSub{
|
||||
Imsi: imsi,
|
||||
Msisdn: msisdn,
|
||||
Ambr: data["AMBR"],
|
||||
Arfb: data["AreaForbidden"],
|
||||
Cn: fmt.Sprint(cnType),
|
||||
SmData: data["SM-Data(snssai+dnn[1..n])"],
|
||||
Sar: data["ServiceAreaRestriction"],
|
||||
Nssai: data["NSSAI"],
|
||||
SmfSel: data["Smf-Selection"],
|
||||
Rat: fmt.Sprint(rat),
|
||||
}
|
||||
// 1,64,24,65,def_eps,1,2,010200000000,-
|
||||
if v, ok := data["EPS-Data"]; ok {
|
||||
userInfo.EpsDat = v
|
||||
arr := strings.Split(v, ",")
|
||||
userInfo.EpsFlag = arr[0]
|
||||
userInfo.EpsOdb = arr[1]
|
||||
userInfo.HplmnOdb = arr[2]
|
||||
userInfo.Ard = arr[3]
|
||||
userInfo.Epstpl = arr[4]
|
||||
userInfo.ContextId = arr[5]
|
||||
userInfo.ApnContext = arr[7]
|
||||
userInfo.StaticIp = arr[8]
|
||||
}
|
||||
|
||||
// 查询数据库是否存在并存入更新
|
||||
neId = ""
|
||||
list := s.udmSubService.List(model.UDMSub{NeID: neId, Imsi: imsi})
|
||||
if len(list) > 0 {
|
||||
listItme := list[0]
|
||||
userInfo.ID = listItme.ID
|
||||
s.udmSubService.Update(neId, userInfo)
|
||||
} else {
|
||||
s.udmSubService.Insert(neId, userInfo)
|
||||
}
|
||||
c.JSON(200, result.OkData(userInfo))
|
||||
}
|
||||
|
||||
// UDM签约用户-增加
|
||||
//
|
||||
// POST /:neId
|
||||
func (s *UDMSubController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var body model.UDMSub
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.Imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("add udmuser:imsi=%s,msisdn=%s,ambr=%s,nssai=%s,arfb=%s,sar=%s,rat=%s,cn=%s,smf_sel=%s,sm_data=%s,eps_flag=%s,eps_odb=%s,hplmn_odb=%s,ard=%s,epstpl=%s,context_id=%s,apn_context=%s",
|
||||
body.Imsi, body.Msisdn, body.Ambr, body.Nssai, body.Arfb, body.Sar, body.Rat, body.Cn, body.SmfSel, body.SmData, body.EpsFlag, body.EpsOdb, body.HplmnOdb, body.Ard, body.Epstpl, body.ContextId, body.ApnContext)
|
||||
// static_ip指给4G UE分配的静态IP,没有可不带此字段名,批量添加IP会自动递增
|
||||
if body.StaticIp != "" {
|
||||
msg += fmt.Sprintf(",static_ip=%s", body.StaticIp)
|
||||
}
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmSubService.Insert(neId, body)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM签约用户-批量添加
|
||||
//
|
||||
// POST /:neId/:num
|
||||
func (s *UDMSubController) Adds(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
num := c.Param("num")
|
||||
if neId == "" || num == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var body model.UDMSub
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.Imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("baa udmuser:start_imsi=%s,start_msisdn=%s,sub_num=%s,ambr=%s,nssai=%s,arfb=%s,sar=%s,rat=%s,cn=%s,smf_sel=%s,sm_data=%s,eps_flag=%s,eps_odb=%s,hplmn_odb=%s,ard=%s,epstpl=%s,context_id=%s,apn_context=%s",
|
||||
body.Imsi, body.Msisdn, num, body.Ambr, body.Nssai, body.Arfb, body.Sar, body.Rat, body.Cn, body.SmfSel, body.SmData, body.EpsFlag, body.EpsOdb, body.HplmnOdb, body.Ard, body.Epstpl, body.ContextId, body.ApnContext)
|
||||
// static_ip指给4G UE分配的静态IP,没有可不带此字段名,批量添加IP会自动递增
|
||||
if body.StaticIp != "" {
|
||||
msg += fmt.Sprintf(",static_ip=%s", body.StaticIp)
|
||||
}
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmSubService.Inserts(neId, body, num)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM签约用户-修改
|
||||
//
|
||||
// PUT /:neId
|
||||
func (s *UDMSubController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
var body model.UDMSub
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.Imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("mod udmuser:imsi=%s", body.Imsi)
|
||||
// 修改的参数名称
|
||||
if body.Msisdn != "" {
|
||||
msg += fmt.Sprintf(",msisdn=%s", body.Msisdn)
|
||||
}
|
||||
if body.Ambr != "" {
|
||||
msg += fmt.Sprintf(",ambr=%s", body.Ambr)
|
||||
}
|
||||
if body.Nssai != "" {
|
||||
msg += fmt.Sprintf(",nssai=%s", body.Nssai)
|
||||
}
|
||||
if body.Arfb != "" {
|
||||
msg += fmt.Sprintf(",arfb=%s", body.Arfb)
|
||||
}
|
||||
if body.Sar != "" {
|
||||
msg += fmt.Sprintf(",sar=%s", body.Sar)
|
||||
}
|
||||
if body.Rat != "" {
|
||||
msg += fmt.Sprintf(",rat=%s", body.Rat)
|
||||
}
|
||||
if body.Cn != "" {
|
||||
msg += fmt.Sprintf(",cn=%s", body.Cn)
|
||||
}
|
||||
if body.SmfSel != "" {
|
||||
msg += fmt.Sprintf(",smf_sel=%s", body.SmfSel)
|
||||
}
|
||||
if body.SmData != "" {
|
||||
msg += fmt.Sprintf(",sm_data=%s", body.SmData)
|
||||
}
|
||||
if body.EpsDat != "" {
|
||||
msg += fmt.Sprintf(",eps_dat=%s", body.EpsDat)
|
||||
}
|
||||
if body.EpsFlag != "" {
|
||||
msg += fmt.Sprintf(",eps_flag=%s", body.EpsFlag)
|
||||
}
|
||||
if body.EpsOdb != "" {
|
||||
msg += fmt.Sprintf(",eps_odb=%s", body.EpsOdb)
|
||||
}
|
||||
if body.HplmnOdb != "" {
|
||||
msg += fmt.Sprintf(",hplmn_odb=%s", body.HplmnOdb)
|
||||
}
|
||||
if body.Epstpl != "" {
|
||||
msg += fmt.Sprintf(",epstpl=%s", body.Epstpl)
|
||||
}
|
||||
if body.Ard != "" {
|
||||
msg += fmt.Sprintf(",ard=%s", body.Ard)
|
||||
}
|
||||
if body.ContextId != "" {
|
||||
msg += fmt.Sprintf(",context_id=%s", body.ContextId)
|
||||
}
|
||||
if body.ApnContext != "" {
|
||||
msg += fmt.Sprintf(",apn_context=%s", body.ApnContext)
|
||||
}
|
||||
if body.StaticIp != "" {
|
||||
msg += fmt.Sprintf(",static_ip=%s", body.StaticIp)
|
||||
}
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmSubService.Update(neId, body)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM签约用户-删除
|
||||
//
|
||||
// DELETE /:neId/:imsi
|
||||
func (s *UDMSubController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
imsi := c.Param("imsi")
|
||||
if neId == "" || imsi == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
imsiArr := strings.Split(imsi, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(imsiArr)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
resultData := map[string]string{}
|
||||
for _, imsi := range uniqueIDs {
|
||||
msg := fmt.Sprintf("del udmuser:imsi=%s", imsi)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
resultData[imsi] = err.Error()
|
||||
}
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmSubService.Delete(neId, imsi)
|
||||
resultData[imsi] = data
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(resultData))
|
||||
}
|
||||
|
||||
// UDM签约用户-批量删除
|
||||
//
|
||||
// DELETE /:neId/:imsi/:num
|
||||
func (s *UDMSubController) Removes(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.Param("neId")
|
||||
imsi := c.Param("imsi")
|
||||
num := c.Param("num")
|
||||
if neId == "" || imsi == "" || num == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("bde udmuser:start_imsi=%s,sub_num=%s", imsi, num)
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
neId = ""
|
||||
s.udmSubService.Deletes(neId, imsi, num)
|
||||
}
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// UDM签约用户-导出
|
||||
//
|
||||
// POST /export
|
||||
func (s *UDMSubController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeId string `json:"neId" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
if !(body.Type == "csv" || body.Type == "txt") {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errImportUserSubFileFormat")))
|
||||
return
|
||||
}
|
||||
|
||||
neId := ""
|
||||
list := s.udmSubService.List(model.UDMSub{NeID: neId})
|
||||
// 文件名
|
||||
fileName := fmt.Sprintf("OMC_SUB_USER_EXPORT_%s_%d.%s", neId, time.Now().UnixMilli(), body.Type)
|
||||
filePath := fmt.Sprintf("%s/%s", file.ParseUploadFileDir(uploadsubpath.EXPORT), fileName)
|
||||
|
||||
if body.Type == "csv" {
|
||||
// 转换数据
|
||||
data := [][]string{}
|
||||
data = append(data, []string{"imsi", "msisdn", "ambr", "nssai", "arfb", "sar", "rat", "cn", "smf_sel", "sm_dat", "eps_dat"})
|
||||
for _, v := range list {
|
||||
epsDat := fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s,%s", v.EpsFlag, v.EpsOdb, v.HplmnOdb, v.Ard, v.Epstpl, v.ContextId, v.ApnContext, v.StaticIp)
|
||||
data = append(data, []string{v.Imsi, v.Msisdn, v.Ambr, v.Nssai, v.Arfb, v.Sar, v.Rat, v.Cn, v.SmfSel, v.SmData, epsDat})
|
||||
}
|
||||
// 输出到文件
|
||||
err = file.WriterCSVFile(data, filePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if body.Type == "txt" {
|
||||
// 转换数据
|
||||
data := [][]string{}
|
||||
for _, v := range list {
|
||||
epsDat := fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s,%s", v.EpsFlag, v.EpsOdb, v.HplmnOdb, v.Ard, v.Epstpl, v.ContextId, v.ApnContext, v.StaticIp)
|
||||
data = append(data, []string{v.Imsi, v.Msisdn, v.Ambr, v.Nssai, v.Arfb, v.Sar, v.Rat, v.Cn, v.SmfSel, v.SmData, epsDat})
|
||||
}
|
||||
// 输出到文件
|
||||
err = file.WriterTxtFile(data, filePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.FileAttachment(filePath, fileName)
|
||||
}
|
||||
|
||||
// UDM签约用户-导入
|
||||
//
|
||||
// POST /import
|
||||
func (s *UDMSubController) Import(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
neId := c.PostForm("neId")
|
||||
if neId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
formFile, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 获取文件名
|
||||
if !(strings.HasSuffix(formFile.Filename, ".csv") || strings.HasSuffix(formFile.Filename, ".txt")) {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errImportUserSubFileFormat")))
|
||||
return
|
||||
}
|
||||
// 上传文件转存
|
||||
upFilePath, err := file.TransferUploadFile(formFile, uploadsubpath.IMPORT, nil)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询网元获取IP
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
|
||||
// 本地文件
|
||||
localPath := file.ParseUploadFilePath(upFilePath)
|
||||
nePath := "/tmp" //config.Get("mml.upload").(string)
|
||||
// 复制到远程
|
||||
err = ssh.FileSCPLocalToNe(neInfo.IP, localPath, nePath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
fileName := localPath[strings.LastIndex(localPath, "/")+1:]
|
||||
msg := fmt.Sprintf("import udmuser:path=%s", fmt.Sprintf("%s/%s", nePath, fileName))
|
||||
|
||||
// 发送MML
|
||||
data, err := mmlclient.MMLSendMsgToString(neInfo.IP, msg)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 命令ok时
|
||||
if strings.Contains(data, "ok") {
|
||||
if strings.HasSuffix(fileName, ".csv") {
|
||||
data := file.ReadCSVFile(localPath)
|
||||
neId = ""
|
||||
go s.udmSubService.InsertCSV(neId, data)
|
||||
}
|
||||
if strings.HasSuffix(fileName, ".txt") {
|
||||
data := file.ReadTxtFile(localPath)
|
||||
neId = ""
|
||||
go s.udmSubService.InsertTxt(neId, data)
|
||||
}
|
||||
}
|
||||
c.JSON(200, result.OkMsg(data))
|
||||
}
|
||||
40
src/modules/network_element/model/ne_host.go
Normal file
40
src/modules/network_element/model/ne_host.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package model
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// NeHost 网元主机表 ne_host
|
||||
type NeHost struct {
|
||||
HostID string `json:"hostId" gorm:"column:host_id"` // 主机主键
|
||||
HostType string `json:"hostType" gorm:"column:host_type" binding:"oneof=ssh telnet"` // 主机类型 ssh telnet
|
||||
GroupID string `json:"groupId" gorm:"column:group_id"` // 分组(0默认 1网元 2系统)
|
||||
Title string `json:"title" gorm:"column:title"` // 标题名称
|
||||
Addr string `json:"addr" gorm:"column:addr" binding:"required"` // 主机地址
|
||||
Port int64 `json:"port" gorm:"column:port" binding:"required,number,max=65535,min=1"` // SSH端口
|
||||
User string `json:"user" gorm:"column:user" binding:"required"` // 主机用户名
|
||||
AuthMode string `json:"authMode" gorm:"column:auth_mode" binding:"oneof=0 1"` // 认证模式(0密码 1主机私钥)
|
||||
Password string `json:"password" gorm:"column:password"` // 认证密码
|
||||
PrivateKey string `json:"privateKey" gorm:"column:private_key"` // 认证私钥
|
||||
PassPhrase string `json:"passPhrase" gorm:"column:pass_phrase"` // 认证私钥密码
|
||||
Remark string `json:"remark" gorm:"column:remark"` // 备注
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"` // 创建者
|
||||
CreateTime int64 `json:"createTime" gorm:"column:create_time"` // 创建时间
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by"` // 更新者
|
||||
UpdateTime int64 `json:"updateTime" gorm:"column:update_time"` // 更新时间
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (NeHost) TableName() string {
|
||||
return "ne_host"
|
||||
}
|
||||
|
||||
// CopyTo 网元主机信息将josn同key名的结构体复制给另一个结构体
|
||||
func (m *NeHost) CopyTo(to interface{}) error {
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = json.Unmarshal(b, to); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
20
src/modules/network_element/model/ne_host_cmd.go
Normal file
20
src/modules/network_element/model/ne_host_cmd.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package model
|
||||
|
||||
// NeHostCmd 网元主机命令表 ne_host_cmd
|
||||
type NeHostCmd struct {
|
||||
CmdID string `json:"cmdId" gorm:"column:cmd_id"` // 命令主键
|
||||
CmdType string `json:"cmdType" gorm:"column:cmd_type"` // 命令类型
|
||||
GroupID string `json:"groupId" gorm:"column:group_id"` // 分组(0默认 1快速命令)
|
||||
Title string `json:"title" gorm:"column:title" binding:"required"` // 标题名称
|
||||
Command string `json:"command" gorm:"column:command" binding:"required"` // 命令字符串
|
||||
Remark string `json:"remark" gorm:"column:remark"` // 备注
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"` // 创建者
|
||||
CreateTime int64 `json:"createTime" gorm:"column:create_time"` // 创建时间
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by"` // 更新者
|
||||
UpdateTime int64 `json:"updateTime" gorm:"column:update_time"` // 更新时间
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (*NeHostCmd) TableName() string {
|
||||
return "ne_host_cmd"
|
||||
}
|
||||
28
src/modules/network_element/model/ne_info.go
Normal file
28
src/modules/network_element/model/ne_info.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package model
|
||||
|
||||
// NeInfo 网元信息对象 ne_info
|
||||
type NeInfo struct {
|
||||
ID string `json:"id"`
|
||||
NeType string `json:"neType" binding:"required"`
|
||||
NeId string `json:"neId" binding:"required"`
|
||||
RmUID string `json:"rmUid"`
|
||||
NeName string `json:"neName"`
|
||||
IP string `json:"ip" binding:"required"`
|
||||
Port int64 `json:"port" binding:"required,number,max=65535,min=1"`
|
||||
PvFlag string `json:"pvFlag" binding:"oneof=PNF VNF"` // enum('PNF','VNF')
|
||||
Province string `json:"province"`
|
||||
VendorName string `json:"vendorName"`
|
||||
Dn string `json:"dn"`
|
||||
NeAddress string `json:"neAddress"`
|
||||
Status string `json:"status"` // 0: 在线 1: 下线 2: 备用 3: 待下发配置
|
||||
UpdateTime string `json:"updateTime"`
|
||||
HostIDs string `json:"hostIds"` // 网元主机ID组 数据格式(ssh,telnet,telnet)
|
||||
|
||||
// ====== 非数据库字段属性 ======
|
||||
|
||||
// 服务状态
|
||||
ServerState map[string]any `json:"serverState,omitempty"`
|
||||
|
||||
// 主机对象组
|
||||
Hosts []NeHost `json:"hosts,omitempty"`
|
||||
}
|
||||
21
src/modules/network_element/model/ne_software.go
Normal file
21
src/modules/network_element/model/ne_software.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// NeSoftware 网元软件包 ne_software
|
||||
type NeSoftware struct {
|
||||
ID string `json:"id" gorm:"id"`
|
||||
NeType string `json:"neType" gorm:"ne_type" binding:"required"` // 网元类型
|
||||
FileName string `json:"fileName" gorm:"file_name" binding:"required"` // 包名称
|
||||
Path string `json:"path" gorm:"path"` // 包路径
|
||||
Version string `json:"version" gorm:"version" binding:"required"` // 包版本
|
||||
Md5Sum string `json:"md5Sum" gorm:"md5_sum"` // --无使用 md5签名
|
||||
Status string `json:"status" gorm:"status"` // --无使用
|
||||
Comment string `json:"comment" gorm:"comment"` // 包说明
|
||||
UpdateTime time.Time `json:"updateTime" gorm:"update_time"` // 上传时间
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (*NeSoftware) TableName() string {
|
||||
return "ne_software"
|
||||
}
|
||||
23
src/modules/network_element/model/ne_version.go
Normal file
23
src/modules/network_element/model/ne_version.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// NeVersion 网元版本信息 ne_version
|
||||
type NeVersion struct {
|
||||
ID string `json:"id" gorm:"id"`
|
||||
NeType string `json:"neType" gorm:"ne_type" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" gorm:"ne_id" binding:"required"` // 网元ID
|
||||
Version string `json:"version" gorm:"version"` // 当前版本
|
||||
FilePath string `json:"filePath" gorm:"file_path"` // 当前软件包
|
||||
PreVersion string `json:"preVersion" gorm:"pre_version"` // 上一版本
|
||||
PreFile string `json:"preFile" gorm:"pre_file"` // 上一版本软件包
|
||||
NewVersion string `json:"newVersion" gorm:"new_version"` // 下一版本
|
||||
NewFile string `json:"newFile" gorm:"new_file"` // 下一版本软件包
|
||||
Status string `json:"status" gorm:"status" binding:"oneof=Uploaded Inactive Active"` // 当前状态 (Uploaded下一版本上传 Inactive下一版本待激活 Active当前已激活)
|
||||
UpdateTime time.Time `json:"updateTime" gorm:"update_time"` // 更新时间
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (*NeVersion) TableName() string {
|
||||
return "ne_version"
|
||||
}
|
||||
17
src/modules/network_element/model/udm_auth.go
Normal file
17
src/modules/network_element/model/udm_auth.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package model
|
||||
|
||||
// UDMAuth UDM鉴权用户对象 u_auth_user
|
||||
type UDMAuth struct {
|
||||
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Imsi string `json:"imsi" gorm:"column:imsi"` // SIM卡号
|
||||
Amf string `json:"amf" gorm:"column:amf"` // ANF
|
||||
Status string `json:"status" gorm:"column:status"` // 状态
|
||||
Ki string `json:"ki" gorm:"column:ki"` // ki
|
||||
AlgoIndex string `json:"algoIndex" gorm:"column:algo_index"` // AlgoIndex
|
||||
Opc string `json:"opc" gorm:"column:opc"`
|
||||
NeID string `json:"neId" gorm:"column:ne_id"` // UDM网元标识-子系统
|
||||
}
|
||||
|
||||
func (UDMAuth) TableName() string {
|
||||
return "u_auth_user"
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user