fix: 跟踪任务hlr调试
This commit is contained in:
67
src/modules/network_element/fetch_link/hlr.go
Normal file
67
src/modules/network_element/fetch_link/hlr.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
// 网元HLR服务8080端口。
|
||||||
|
// 融合到UDM网元,也许是UDM的HLR服务。
|
||||||
|
|
||||||
|
package fetchlink
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"be.ems/src/framework/logger"
|
||||||
|
"be.ems/src/framework/utils/fetch"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HLRTraceStart HLR跟踪任务开始
|
||||||
|
//
|
||||||
|
// data参数 {traceID:"跟踪任务ID", imsi:"IMSI和MSISDN必填一个,都带的话以IMSI为准", msisdn:""}
|
||||||
|
func HLRTraceStart(hlrIP string, data map[string]any) (string, error) {
|
||||||
|
// 网元参数配置新增(array)
|
||||||
|
neUrl := fmt.Sprintf("http://%s:8080/trace-manage/v1/add-task", hlrIP)
|
||||||
|
resBytes, err := fetch.PostJSON(neUrl, data, nil)
|
||||||
|
var resData map[string]string
|
||||||
|
if err != nil {
|
||||||
|
errStr := err.Error()
|
||||||
|
logger.Warnf("HLRTraceStart Post \"%s\"", neUrl)
|
||||||
|
logger.Errorf("HLRTraceStart %s", errStr)
|
||||||
|
return "", fmt.Errorf("NeService HLR API Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化结果
|
||||||
|
err = json.Unmarshal(resBytes, &resData)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("HLRTraceStart Unmarshal %s", err.Error())
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if v, ok := resData["code"]; ok && v == "0" {
|
||||||
|
return strings.TrimSpace(strings.ToLower(resData["message"])), nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf(resData["message"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// HLRTraceStop HLR跟踪任务停止
|
||||||
|
//
|
||||||
|
// data参数 {traceIDArray: ["跟踪任务ID数组"]}
|
||||||
|
func HLRTraceStop(hlrIP string, data map[string]any) (string, error) {
|
||||||
|
// 网元参数配置新增(array)
|
||||||
|
neUrl := fmt.Sprintf("http://%s:8080/trace-manage/v1/delete-task", hlrIP)
|
||||||
|
resBytes, err := fetch.PostJSON(neUrl, data, nil)
|
||||||
|
var resData map[string]string
|
||||||
|
if err != nil {
|
||||||
|
errStr := err.Error()
|
||||||
|
logger.Warnf("HLRTraceStop Post \"%s\"", neUrl)
|
||||||
|
logger.Errorf("HLRTraceStop %s", errStr)
|
||||||
|
return "", fmt.Errorf("NeService HLR API Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化结果
|
||||||
|
err = json.Unmarshal(resBytes, &resData)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("HLRTraceStop Unmarshal %s", err.Error())
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if v, ok := resData["code"]; ok && v == "0" {
|
||||||
|
return strings.TrimSpace(strings.ToLower(resData["message"])), nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf(resData["message"])
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// 网元UDM服务,可能是8080、33030端口服务
|
||||||
|
// 融合的UDM网元视情况调整。
|
||||||
|
|
||||||
package fetchlink
|
package fetchlink
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -17,17 +17,17 @@ import (
|
|||||||
// 实例化控制层 TraceTaskHlrController 结构体
|
// 实例化控制层 TraceTaskHlrController 结构体
|
||||||
var NewTraceTaskHlr = &TraceTaskHlrController{
|
var NewTraceTaskHlr = &TraceTaskHlrController{
|
||||||
neInfoService: neService.NewNeInfoImpl,
|
neInfoService: neService.NewNeInfoImpl,
|
||||||
traceTaskHlrService: traceService.NewTraceTaskHlrImpl,
|
traceTaskHlrService: traceService.NewTraceTaskHlr,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 网元SMF
|
// 跟踪任务网元HLR
|
||||||
//
|
//
|
||||||
// PATH /task/hlr
|
// PATH /task/hlr
|
||||||
type TraceTaskHlrController struct {
|
type TraceTaskHlrController struct {
|
||||||
// 网元信息服务
|
// 网元信息服务
|
||||||
neInfoService neService.INeInfo
|
neInfoService neService.INeInfo
|
||||||
// 跟踪_任务给HRL网元信息服务
|
// 跟踪_任务给HRL网元信息服务
|
||||||
traceTaskHlrService traceService.ITraceTaskHlr
|
traceTaskHlrService *traceService.TraceTaskHlr
|
||||||
}
|
}
|
||||||
|
|
||||||
// 跟踪任务列表
|
// 跟踪任务列表
|
||||||
@@ -116,7 +116,7 @@ func (s *TraceTaskHlrController) Start(c *gin.Context) {
|
|||||||
func (s *TraceTaskHlrController) Stop(c *gin.Context) {
|
func (s *TraceTaskHlrController) Stop(c *gin.Context) {
|
||||||
language := ctx.AcceptLanguage(c)
|
language := ctx.AcceptLanguage(c)
|
||||||
var body struct {
|
var body struct {
|
||||||
ID string `json:"id"` // 任务ID
|
ID string `json:"id" binding:"required"` // 任务ID
|
||||||
}
|
}
|
||||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||||
@@ -155,7 +155,8 @@ func (s *TraceTaskHlrController) Stop(c *gin.Context) {
|
|||||||
func (s *TraceTaskHlrController) File(c *gin.Context) {
|
func (s *TraceTaskHlrController) File(c *gin.Context) {
|
||||||
language := ctx.AcceptLanguage(c)
|
language := ctx.AcceptLanguage(c)
|
||||||
var body struct {
|
var body struct {
|
||||||
ID string `json:"id"` // 任务ID
|
ID string `json:"id" binding:"required"` // 任务ID
|
||||||
|
Dir string `json:"dir" binding:"required"` // 网元文件目录
|
||||||
}
|
}
|
||||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||||
@@ -168,8 +169,7 @@ func (s *TraceTaskHlrController) File(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
task.UpdateBy = ctx.LoginUserToUserName(c)
|
list, err := s.traceTaskHlrService.File(task.TraceId, body.Dir)
|
||||||
list, err := s.traceTaskHlrService.File(task)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, result.ErrMsg(err.Error()))
|
c.JSON(200, result.ErrMsg(err.Error()))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,24 +1,301 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import "be.ems/src/modules/trace/model"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"be.ems/src/framework/datasource"
|
||||||
|
"be.ems/src/framework/logger"
|
||||||
|
"be.ems/src/framework/utils/parse"
|
||||||
|
"be.ems/src/framework/utils/repo"
|
||||||
|
"be.ems/src/modules/trace/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 实例化数据层 TraceTaskHlr 结构体
|
||||||
|
var NewTraceTaskHlr = &TraceTaskHlr{
|
||||||
|
selectSql: `select id, trace_id, imsi, msisdn, start_time, end_time, status, msg, remark, create_by, create_time, update_by, update_time from trace_task_hlr`,
|
||||||
|
|
||||||
|
resultMap: map[string]string{
|
||||||
|
"id": "ID",
|
||||||
|
"trace_id": "TraceId",
|
||||||
|
"imsi": "IMSI",
|
||||||
|
"msisdn": "MSISDN",
|
||||||
|
"start_time": "StartTime",
|
||||||
|
"end_time": "EndTime",
|
||||||
|
"status": "Status",
|
||||||
|
"msg": "Msg",
|
||||||
|
"remark": "Remark",
|
||||||
|
"create_by": "CreateBy",
|
||||||
|
"create_time": "CreateTime",
|
||||||
|
"update_by": "UpdateBy",
|
||||||
|
"update_time": "UpdateTime",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// CDREventIMSImpl 跟踪_任务给HRL网元 数据层处理
|
||||||
|
type TraceTaskHlr struct {
|
||||||
|
// 查询视图对象SQL
|
||||||
|
selectSql string
|
||||||
|
// 结果字段与实体映射
|
||||||
|
resultMap map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// convertResultRows 将结果记录转实体结果组
|
||||||
|
func (r *TraceTaskHlr) convertResultRows(rows []map[string]any) []model.TraceTaskHlr {
|
||||||
|
arr := make([]model.TraceTaskHlr, 0)
|
||||||
|
for _, row := range rows {
|
||||||
|
item := model.TraceTaskHlr{}
|
||||||
|
for key, value := range row {
|
||||||
|
if keyMapper, ok := r.resultMap[key]; ok {
|
||||||
|
repo.SetFieldValue(&item, keyMapper, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr = append(arr, item)
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
// 跟踪_任务给HRL网元 数据层接口
|
|
||||||
type ITraceTaskHlr interface {
|
|
||||||
// SelectPage 根据条件分页查询
|
// SelectPage 根据条件分页查询
|
||||||
SelectPage(querys model.TraceTaskHlrQuery) map[string]any
|
func (r *TraceTaskHlr) SelectPage(querys model.TraceTaskHlrQuery) map[string]any {
|
||||||
|
// 查询条件拼接
|
||||||
|
var conditions []string
|
||||||
|
var params []any
|
||||||
|
if querys.IMSI != "" {
|
||||||
|
conditions = append(conditions, "imsi like concat(?, '%')")
|
||||||
|
params = append(params, querys.IMSI)
|
||||||
|
}
|
||||||
|
if querys.MSISDN != "" {
|
||||||
|
conditions = append(conditions, "msisdn like concat(?, '%')")
|
||||||
|
params = append(params, querys.MSISDN)
|
||||||
|
}
|
||||||
|
if querys.StartTime != "" && len(querys.StartTime) == 13 {
|
||||||
|
conditions = append(conditions, "start_time >= ?")
|
||||||
|
params = append(params, querys.StartTime)
|
||||||
|
}
|
||||||
|
if querys.EndTime != "" && len(querys.EndTime) == 13 {
|
||||||
|
conditions = append(conditions, "end_time <= ?")
|
||||||
|
params = append(params, querys.EndTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询条件语句
|
||||||
|
whereSql := ""
|
||||||
|
if len(conditions) > 0 {
|
||||||
|
whereSql += " where " + strings.Join(conditions, " and ")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := map[string]any{
|
||||||
|
"total": 0,
|
||||||
|
"rows": []model.TraceTaskHlr{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询数量 长度为0直接返回
|
||||||
|
totalSql := "select count(1) as 'total' from trace_task_hlr"
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// SelectList 根据实体查询
|
// SelectList 根据实体查询
|
||||||
SelectList(neHost model.TraceTaskHlr) []model.TraceTaskHlr
|
func (r *TraceTaskHlr) SelectList(task model.TraceTaskHlr) []model.TraceTaskHlr {
|
||||||
|
// 查询条件拼接
|
||||||
|
var conditions []string
|
||||||
|
var params []any
|
||||||
|
if task.IMSI != "" {
|
||||||
|
conditions = append(conditions, "imsi = ?")
|
||||||
|
params = append(params, task.IMSI)
|
||||||
|
}
|
||||||
|
if task.MSISDN != "" {
|
||||||
|
conditions = append(conditions, "msisdn = ?")
|
||||||
|
params = append(params, task.MSISDN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询条件语句
|
||||||
|
whereSql := ""
|
||||||
|
if len(conditions) > 0 {
|
||||||
|
whereSql += " where " + strings.Join(conditions, " and ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询数据
|
||||||
|
querySql := r.selectSql + whereSql + " order by id desc "
|
||||||
|
results, err := datasource.RawDB("", querySql, params)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("query err => %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换实体
|
||||||
|
return r.convertResultRows(results)
|
||||||
|
}
|
||||||
|
|
||||||
// SelectByIds 通过ID查询
|
// SelectByIds 通过ID查询
|
||||||
SelectByIds(ids []string) []model.TraceTaskHlr
|
func (r *TraceTaskHlr) SelectByIds(ids []string) []model.TraceTaskHlr {
|
||||||
|
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
||||||
|
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||||
|
parameters := repo.ConvertIdsSlice(ids)
|
||||||
|
results, err := datasource.RawDB("", querySql, parameters)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("query err => %v", err)
|
||||||
|
return []model.TraceTaskHlr{}
|
||||||
|
}
|
||||||
|
// 转换实体
|
||||||
|
return r.convertResultRows(results)
|
||||||
|
}
|
||||||
|
|
||||||
// Insert 新增信息
|
// Insert 新增信息
|
||||||
Insert(task model.TraceTaskHlr) string
|
func (r *TraceTaskHlr) Insert(task model.TraceTaskHlr) string {
|
||||||
|
// 参数拼接
|
||||||
|
params := make(map[string]any)
|
||||||
|
if task.TraceId != "" {
|
||||||
|
params["trace_id"] = task.TraceId
|
||||||
|
}
|
||||||
|
if task.IMSI != "" {
|
||||||
|
params["imsi"] = task.IMSI
|
||||||
|
}
|
||||||
|
if task.MSISDN != "" {
|
||||||
|
params["msisdn"] = task.MSISDN
|
||||||
|
}
|
||||||
|
if task.StartTime != 0 {
|
||||||
|
params["start_time"] = task.StartTime
|
||||||
|
}
|
||||||
|
if task.EndTime != 0 {
|
||||||
|
params["end_time"] = task.EndTime
|
||||||
|
}
|
||||||
|
if task.Status != "" {
|
||||||
|
params["status"] = task.Status
|
||||||
|
}
|
||||||
|
if task.Msg != "" {
|
||||||
|
params["msg"] = task.Msg
|
||||||
|
}
|
||||||
|
if task.Remark != "" {
|
||||||
|
params["remark"] = task.Remark
|
||||||
|
}
|
||||||
|
if task.CreateBy != "" {
|
||||||
|
params["create_by"] = task.CreateBy
|
||||||
|
params["create_time"] = time.Now().UnixMilli()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建执行语句
|
||||||
|
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||||
|
sql := "insert into trace_task_hlr (" + 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
|
||||||
|
}
|
||||||
|
|
||||||
// Update 修改信息
|
// Update 修改信息
|
||||||
Update(task model.TraceTaskHlr) int64
|
func (r *TraceTaskHlr) Update(task model.TraceTaskHlr) int64 {
|
||||||
|
// 参数拼接
|
||||||
|
params := make(map[string]any)
|
||||||
|
if task.TraceId != "" {
|
||||||
|
params["trace_id"] = task.TraceId
|
||||||
|
}
|
||||||
|
if task.IMSI != "" {
|
||||||
|
params["imsi"] = task.IMSI
|
||||||
|
}
|
||||||
|
if task.MSISDN != "" {
|
||||||
|
params["msisdn"] = task.MSISDN
|
||||||
|
}
|
||||||
|
if task.StartTime != 0 {
|
||||||
|
params["start_time"] = task.StartTime
|
||||||
|
}
|
||||||
|
if task.EndTime != 0 {
|
||||||
|
params["end_time"] = task.EndTime
|
||||||
|
}
|
||||||
|
if task.Status != "" {
|
||||||
|
params["status"] = task.Status
|
||||||
|
}
|
||||||
|
if task.Msg != "" {
|
||||||
|
params["msg"] = task.Msg
|
||||||
|
}
|
||||||
|
if task.Remark != "" {
|
||||||
|
params["remark"] = task.Remark
|
||||||
|
}
|
||||||
|
if task.UpdateBy != "" {
|
||||||
|
params["update_by"] = task.UpdateBy
|
||||||
|
params["update_time"] = time.Now().UnixMilli()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建执行语句
|
||||||
|
keys, values := repo.KeyValueByUpdate(params)
|
||||||
|
sql := "update trace_task_hlr set " + strings.Join(keys, ",") + " where id = ?"
|
||||||
|
|
||||||
|
// 执行更新
|
||||||
|
values = append(values, task.ID)
|
||||||
|
rows, err := datasource.ExecDB("", sql, values)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("update row : %v", err.Error())
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteByIds 批量删除信息
|
// DeleteByIds 批量删除信息
|
||||||
DeleteByIds(ids []string) int64
|
func (r *TraceTaskHlr) DeleteByIds(ids []string) int64 {
|
||||||
|
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
||||||
|
sql := "delete from trace_task_hlr where id in (" + placeholder + ")"
|
||||||
|
parameters := repo.ConvertIdsSlice(ids)
|
||||||
|
results, err := datasource.ExecDB("", sql, parameters)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("delete err => %v", err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return results
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,301 +0,0 @@
|
|||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"be.ems/src/framework/datasource"
|
|
||||||
"be.ems/src/framework/logger"
|
|
||||||
"be.ems/src/framework/utils/parse"
|
|
||||||
"be.ems/src/framework/utils/repo"
|
|
||||||
"be.ems/src/modules/trace/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 实例化数据层 TraceTaskHlr 结构体
|
|
||||||
var NewTraceTaskHlrImpl = &TraceTaskHlrImpl{
|
|
||||||
selectSql: `select id, trace_id, imsi, msisdn, start_time, end_time, status, msg, remark, create_by, create_time, update_by, update_time from trace_task_hlr`,
|
|
||||||
|
|
||||||
resultMap: map[string]string{
|
|
||||||
"id": "ID",
|
|
||||||
"trace_id": "TraceId",
|
|
||||||
"imsi": "IMSI",
|
|
||||||
"msisdn": "MSISDN",
|
|
||||||
"start_time": "StartTime",
|
|
||||||
"end_time": "EndTime",
|
|
||||||
"status": "Status",
|
|
||||||
"msg": "Msg",
|
|
||||||
"remark": "Remark",
|
|
||||||
"create_by": "CreateBy",
|
|
||||||
"create_time": "CreateTime",
|
|
||||||
"update_by": "UpdateBy",
|
|
||||||
"update_time": "UpdateTime",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// CDREventIMSImpl 跟踪_任务给HRL网元 数据层处理
|
|
||||||
type TraceTaskHlrImpl struct {
|
|
||||||
// 查询视图对象SQL
|
|
||||||
selectSql string
|
|
||||||
// 结果字段与实体映射
|
|
||||||
resultMap map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertResultRows 将结果记录转实体结果组
|
|
||||||
func (r *TraceTaskHlrImpl) convertResultRows(rows []map[string]any) []model.TraceTaskHlr {
|
|
||||||
arr := make([]model.TraceTaskHlr, 0)
|
|
||||||
for _, row := range rows {
|
|
||||||
item := model.TraceTaskHlr{}
|
|
||||||
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 *TraceTaskHlrImpl) SelectPage(querys model.TraceTaskHlrQuery) map[string]any {
|
|
||||||
// 查询条件拼接
|
|
||||||
var conditions []string
|
|
||||||
var params []any
|
|
||||||
if querys.IMSI != "" {
|
|
||||||
conditions = append(conditions, "imsi like concat(?, '%')")
|
|
||||||
params = append(params, querys.IMSI)
|
|
||||||
}
|
|
||||||
if querys.MSISDN != "" {
|
|
||||||
conditions = append(conditions, "msisdn like concat(?, '%')")
|
|
||||||
params = append(params, querys.MSISDN)
|
|
||||||
}
|
|
||||||
if querys.StartTime != "" && len(querys.StartTime) == 13 {
|
|
||||||
conditions = append(conditions, "start_time >= ?")
|
|
||||||
params = append(params, querys.StartTime)
|
|
||||||
}
|
|
||||||
if querys.EndTime != "" && len(querys.EndTime) == 13 {
|
|
||||||
conditions = append(conditions, "end_time <= ?")
|
|
||||||
params = append(params, querys.EndTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建查询条件语句
|
|
||||||
whereSql := ""
|
|
||||||
if len(conditions) > 0 {
|
|
||||||
whereSql += " where " + strings.Join(conditions, " and ")
|
|
||||||
}
|
|
||||||
|
|
||||||
result := map[string]any{
|
|
||||||
"total": 0,
|
|
||||||
"rows": []model.TraceTaskHlr{},
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询数量 长度为0直接返回
|
|
||||||
totalSql := "select count(1) as 'total' from trace_task_hlr"
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// SelectList 根据实体查询
|
|
||||||
func (r *TraceTaskHlrImpl) SelectList(neHost model.TraceTaskHlr) []model.TraceTaskHlr {
|
|
||||||
// 查询条件拼接
|
|
||||||
var conditions []string
|
|
||||||
var params []any
|
|
||||||
if neHost.IMSI != "" {
|
|
||||||
conditions = append(conditions, "imsi = ?")
|
|
||||||
params = append(params, neHost.IMSI)
|
|
||||||
}
|
|
||||||
if neHost.MSISDN != "" {
|
|
||||||
conditions = append(conditions, "msisdn = ?")
|
|
||||||
params = append(params, neHost.MSISDN)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建查询条件语句
|
|
||||||
whereSql := ""
|
|
||||||
if len(conditions) > 0 {
|
|
||||||
whereSql += " where " + strings.Join(conditions, " and ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询数据
|
|
||||||
querySql := r.selectSql + whereSql + " order by id desc "
|
|
||||||
results, err := datasource.RawDB("", querySql, params)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("query err => %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 转换实体
|
|
||||||
return r.convertResultRows(results)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SelectByIds 通过ID查询
|
|
||||||
func (r *TraceTaskHlrImpl) SelectByIds(ids []string) []model.TraceTaskHlr {
|
|
||||||
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
|
||||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
|
||||||
parameters := repo.ConvertIdsSlice(ids)
|
|
||||||
results, err := datasource.RawDB("", querySql, parameters)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("query err => %v", err)
|
|
||||||
return []model.TraceTaskHlr{}
|
|
||||||
}
|
|
||||||
// 转换实体
|
|
||||||
return r.convertResultRows(results)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert 新增信息
|
|
||||||
func (r *TraceTaskHlrImpl) Insert(task model.TraceTaskHlr) string {
|
|
||||||
// 参数拼接
|
|
||||||
params := make(map[string]any)
|
|
||||||
if task.TraceId != "" {
|
|
||||||
params["trace_id"] = task.TraceId
|
|
||||||
}
|
|
||||||
if task.IMSI != "" {
|
|
||||||
params["imsi"] = task.IMSI
|
|
||||||
}
|
|
||||||
if task.MSISDN != "" {
|
|
||||||
params["msisdn"] = task.MSISDN
|
|
||||||
}
|
|
||||||
if task.StartTime != 0 {
|
|
||||||
params["start_time"] = task.StartTime
|
|
||||||
}
|
|
||||||
if task.EndTime != 0 {
|
|
||||||
params["end_time"] = task.EndTime
|
|
||||||
}
|
|
||||||
if task.Status != "" {
|
|
||||||
params["status"] = task.Status
|
|
||||||
}
|
|
||||||
if task.Msg != "" {
|
|
||||||
params["msg"] = task.Msg
|
|
||||||
}
|
|
||||||
if task.Remark != "" {
|
|
||||||
params["remark"] = task.Remark
|
|
||||||
}
|
|
||||||
if task.CreateBy != "" {
|
|
||||||
params["create_by"] = task.CreateBy
|
|
||||||
params["create_time"] = time.Now().UnixMilli()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建执行语句
|
|
||||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
|
||||||
sql := "insert into trace_task_hlr (" + 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update 修改信息
|
|
||||||
func (r *TraceTaskHlrImpl) Update(task model.TraceTaskHlr) int64 {
|
|
||||||
// 参数拼接
|
|
||||||
params := make(map[string]any)
|
|
||||||
if task.TraceId != "" {
|
|
||||||
params["trace_id"] = task.TraceId
|
|
||||||
}
|
|
||||||
if task.IMSI != "" {
|
|
||||||
params["imsi"] = task.IMSI
|
|
||||||
}
|
|
||||||
if task.MSISDN != "" {
|
|
||||||
params["msisdn"] = task.MSISDN
|
|
||||||
}
|
|
||||||
if task.StartTime != 0 {
|
|
||||||
params["start_time"] = task.StartTime
|
|
||||||
}
|
|
||||||
if task.EndTime != 0 {
|
|
||||||
params["end_time"] = task.EndTime
|
|
||||||
}
|
|
||||||
if task.Status != "" {
|
|
||||||
params["status"] = task.Status
|
|
||||||
}
|
|
||||||
if task.Msg != "" {
|
|
||||||
params["msg"] = task.Msg
|
|
||||||
}
|
|
||||||
if task.Remark != "" {
|
|
||||||
params["remark"] = task.Remark
|
|
||||||
}
|
|
||||||
if task.UpdateBy != "" {
|
|
||||||
params["update_by"] = task.UpdateBy
|
|
||||||
params["update_time"] = time.Now().UnixMilli()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建执行语句
|
|
||||||
keys, values := repo.KeyValueByUpdate(params)
|
|
||||||
sql := "update trace_task_hlr set " + strings.Join(keys, ",") + " where id = ?"
|
|
||||||
|
|
||||||
// 执行更新
|
|
||||||
values = append(values, task.ID)
|
|
||||||
rows, err := datasource.ExecDB("", sql, values)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("update row : %v", err.Error())
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return rows
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteByIds 批量删除信息
|
|
||||||
func (r *TraceTaskHlrImpl) DeleteByIds(ids []string) int64 {
|
|
||||||
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
|
||||||
sql := "delete from trace_task_hlr where id in (" + placeholder + ")"
|
|
||||||
parameters := repo.ConvertIdsSlice(ids)
|
|
||||||
results, err := datasource.ExecDB("", sql, parameters)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("delete err => %v", err)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
@@ -1,24 +1,205 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import "be.ems/src/modules/trace/model"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"be.ems/src/framework/utils/date"
|
||||||
|
"be.ems/src/framework/utils/generate"
|
||||||
|
"be.ems/src/framework/utils/ssh"
|
||||||
|
neFetchlink "be.ems/src/modules/network_element/fetch_link"
|
||||||
|
neModel "be.ems/src/modules/network_element/model"
|
||||||
|
neService "be.ems/src/modules/network_element/service"
|
||||||
|
"be.ems/src/modules/trace/model"
|
||||||
|
"be.ems/src/modules/trace/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 实例化数据层 TraceTaskHlr 结构体
|
||||||
|
var NewTraceTaskHlr = &TraceTaskHlr{
|
||||||
|
traceTaskHlrRepository: repository.NewTraceTaskHlr,
|
||||||
|
neInfoService: neService.NewNeInfoImpl,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TraceTaskHlr 跟踪_任务给HRL网元 服务层处理
|
||||||
|
type TraceTaskHlr struct {
|
||||||
|
// 跟踪_任务给HRL网元数据信息
|
||||||
|
traceTaskHlrRepository *repository.TraceTaskHlr
|
||||||
|
// 网元信息服务
|
||||||
|
neInfoService neService.INeInfo
|
||||||
|
}
|
||||||
|
|
||||||
// 跟踪_任务给HRL网元 服务层接口
|
|
||||||
type ITraceTaskHlr interface {
|
|
||||||
// SelectPage 根据条件分页查询
|
// SelectPage 根据条件分页查询
|
||||||
SelectPage(querys model.TraceTaskHlrQuery) map[string]any
|
func (r *TraceTaskHlr) SelectPage(querys model.TraceTaskHlrQuery) map[string]any {
|
||||||
|
return r.traceTaskHlrRepository.SelectPage(querys)
|
||||||
|
}
|
||||||
|
|
||||||
// SelectById 通过ID查询
|
// SelectById 通过ID查询
|
||||||
SelectById(id string) model.TraceTaskHlr
|
func (r *TraceTaskHlr) SelectById(id string) model.TraceTaskHlr {
|
||||||
|
tasks := r.traceTaskHlrRepository.SelectByIds([]string{id})
|
||||||
|
if len(tasks) > 0 {
|
||||||
|
return tasks[0]
|
||||||
|
}
|
||||||
|
return model.TraceTaskHlr{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert 新增信息
|
||||||
|
func (r *TraceTaskHlr) Insert(task model.TraceTaskHlr) string {
|
||||||
|
return r.traceTaskHlrRepository.Insert(task)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update 修改信息
|
||||||
|
func (r *TraceTaskHlr) Update(task model.TraceTaskHlr) int64 {
|
||||||
|
return r.traceTaskHlrRepository.Update(task)
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteByIds 批量删除信息
|
// DeleteByIds 批量删除信息
|
||||||
DeleteByIds(ids []string) (int64, error)
|
func (r *TraceTaskHlr) DeleteByIds(ids []string) (int64, error) {
|
||||||
|
// 检查是否存在
|
||||||
|
rows := r.traceTaskHlrRepository.SelectByIds(ids)
|
||||||
|
if len(rows) <= 0 {
|
||||||
|
return 0, fmt.Errorf("not data")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rows) == len(ids) {
|
||||||
|
// 停止任务
|
||||||
|
neInfos := r.neInfoService.SelectList(neModel.NeInfo{NeType: "UDM"}, false, false)
|
||||||
|
for _, r := range rows {
|
||||||
|
for _, v := range neInfos {
|
||||||
|
neFetchlink.HLRTraceStop(v.IP, map[string]any{
|
||||||
|
"traceIDArray": []string{r.TraceId},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
num := r.traceTaskHlrRepository.DeleteByIds(ids)
|
||||||
|
return num, nil
|
||||||
|
}
|
||||||
|
// 删除信息失败!
|
||||||
|
return 0, fmt.Errorf("delete fail")
|
||||||
|
}
|
||||||
|
|
||||||
// Start 创建任务
|
// Start 创建任务
|
||||||
Start(task model.TraceTaskHlr) (string, error)
|
func (r *TraceTaskHlr) Start(task model.TraceTaskHlr) (string, error) {
|
||||||
|
hlrList := []map[string]any{}
|
||||||
|
task.TraceId = generate.Code(6)
|
||||||
|
|
||||||
|
data := map[string]any{
|
||||||
|
"traceID": task.TraceId,
|
||||||
|
"imsi": task.IMSI,
|
||||||
|
"msisdn": task.MSISDN,
|
||||||
|
}
|
||||||
|
if task.StartTime > task.EndTime {
|
||||||
|
return "", fmt.Errorf("startTime must less than endTime")
|
||||||
|
}
|
||||||
|
if task.StartTime > 0 {
|
||||||
|
data["startTime"] = date.ParseDateToStr(task.StartTime, date.YYYY_MM_DDTHH_MM_SSZ)
|
||||||
|
}
|
||||||
|
if task.StartTime > 0 {
|
||||||
|
data["endTime"] = date.ParseDateToStr(task.EndTime, date.YYYY_MM_DDTHH_MM_SSZ)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送创建任务
|
||||||
|
neInfos := r.neInfoService.SelectList(neModel.NeInfo{NeType: "UDM"}, false, false)
|
||||||
|
for _, neInfo := range neInfos {
|
||||||
|
hlrItem := map[string]any{
|
||||||
|
"neType": neInfo.NeType,
|
||||||
|
"neId": neInfo.NeId,
|
||||||
|
"msg": "",
|
||||||
|
}
|
||||||
|
msg, err := neFetchlink.HLRTraceStart(neInfo.IP, data)
|
||||||
|
if err != nil {
|
||||||
|
hlrItem["err"] = err.Error()
|
||||||
|
} else {
|
||||||
|
hlrItem["err"] = msg
|
||||||
|
}
|
||||||
|
hlrList = append(hlrList, hlrItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, _ := json.Marshal(hlrList)
|
||||||
|
task.Msg = string(msg)
|
||||||
|
task.Status = "1"
|
||||||
|
id := r.traceTaskHlrRepository.Insert(task)
|
||||||
|
if id == "" {
|
||||||
|
return "", fmt.Errorf("start task fail")
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Stop 停止任务
|
// Stop 停止任务
|
||||||
Stop(task model.TraceTaskHlr) error
|
func (r *TraceTaskHlr) Stop(task model.TraceTaskHlr) error {
|
||||||
|
hlrList := []map[string]any{}
|
||||||
|
// 发送停止任务
|
||||||
|
neInfos := r.neInfoService.SelectList(neModel.NeInfo{NeType: "UDM"}, false, false)
|
||||||
|
for _, neInfo := range neInfos {
|
||||||
|
hlrItem := map[string]any{
|
||||||
|
"neType": neInfo.NeType,
|
||||||
|
"neId": neInfo.NeId,
|
||||||
|
"msg": "",
|
||||||
|
}
|
||||||
|
msg, err := neFetchlink.HLRTraceStop(neInfo.IP, map[string]any{
|
||||||
|
"traceIDArray": []string{task.TraceId},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
hlrItem["err"] = err.Error()
|
||||||
|
} else {
|
||||||
|
hlrItem["err"] = msg
|
||||||
|
}
|
||||||
|
hlrList = append(hlrList, hlrItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, _ := json.Marshal(hlrList)
|
||||||
|
task.Msg = string(msg)
|
||||||
|
task.Status = "0"
|
||||||
|
rows := r.traceTaskHlrRepository.Update(task)
|
||||||
|
if rows <= 0 {
|
||||||
|
return fmt.Errorf("stop task fail")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// File 任务文件
|
// File 任务文件
|
||||||
File(task model.TraceTaskHlr) ([]map[string]any, error)
|
func (r *TraceTaskHlr) File(traceId, dirPath string) ([]map[string]any, error) {
|
||||||
|
hlrList := []map[string]any{}
|
||||||
|
// 查询所有匹配的网元类型
|
||||||
|
neInfos := r.neInfoService.SelectList(neModel.NeInfo{NeType: "UDM"}, false, false)
|
||||||
|
if len(neInfos) == 0 {
|
||||||
|
return nil, fmt.Errorf("not found network element")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历多个网元主机获取文件
|
||||||
|
for _, neInfo := range neInfos {
|
||||||
|
hlrItem := map[string]any{
|
||||||
|
"neType": neInfo.NeType,
|
||||||
|
"neId": neInfo.NeId,
|
||||||
|
"err": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机的SSH客户端
|
||||||
|
sshClient, err := r.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
||||||
|
if err != nil {
|
||||||
|
hlrItem["err"] = "ssh link fail"
|
||||||
|
hlrList = append(hlrList, hlrItem)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
defer sshClient.Close()
|
||||||
|
|
||||||
|
// 获取文件列表
|
||||||
|
fileName := fmt.Sprintf("%s_%s_%s", neInfo.NeType, neInfo.NeId, traceId)
|
||||||
|
_, rows, err := ssh.FileList(sshClient, filepath.ToSlash(dirPath), fileName)
|
||||||
|
if err != nil {
|
||||||
|
hlrItem["err"] = "file not found"
|
||||||
|
hlrList = append(hlrList, hlrItem)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历组装
|
||||||
|
for _, v := range rows {
|
||||||
|
if v.FileType == "file" {
|
||||||
|
hlrItem["fileName"] = v.FileName
|
||||||
|
hlrItem["filePath"] = filepath.ToSlash(filepath.Join(dirPath, v.FileName))
|
||||||
|
hlrList = append(hlrList, hlrItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hlrList, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,150 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"be.ems/src/framework/utils/generate"
|
|
||||||
"be.ems/src/framework/utils/ssh"
|
|
||||||
neModel "be.ems/src/modules/network_element/model"
|
|
||||||
neService "be.ems/src/modules/network_element/service"
|
|
||||||
"be.ems/src/modules/trace/model"
|
|
||||||
"be.ems/src/modules/trace/repository"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 实例化数据层 TraceTaskHlrImpl 结构体
|
|
||||||
var NewTraceTaskHlrImpl = &TraceTaskHlrImpl{
|
|
||||||
traceTaskHlrRepository: repository.NewTraceTaskHlrImpl,
|
|
||||||
neInfoService: neService.NewNeInfoImpl,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TraceTaskHlrImpl 跟踪_任务给HRL网元 服务层处理
|
|
||||||
type TraceTaskHlrImpl struct {
|
|
||||||
// 跟踪_任务给HRL网元数据信息
|
|
||||||
traceTaskHlrRepository repository.ITraceTaskHlr
|
|
||||||
// 网元信息服务
|
|
||||||
neInfoService neService.INeInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
// SelectPage 根据条件分页查询
|
|
||||||
func (r *TraceTaskHlrImpl) SelectPage(querys model.TraceTaskHlrQuery) map[string]any {
|
|
||||||
return r.traceTaskHlrRepository.SelectPage(querys)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SelectById 通过ID查询
|
|
||||||
func (r *TraceTaskHlrImpl) SelectById(id string) model.TraceTaskHlr {
|
|
||||||
tasks := r.traceTaskHlrRepository.SelectByIds([]string{id})
|
|
||||||
if len(tasks) > 0 {
|
|
||||||
return tasks[0]
|
|
||||||
}
|
|
||||||
return model.TraceTaskHlr{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert 新增信息
|
|
||||||
func (r *TraceTaskHlrImpl) Insert(task model.TraceTaskHlr) string {
|
|
||||||
return r.traceTaskHlrRepository.Insert(task)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update 修改信息
|
|
||||||
func (r *TraceTaskHlrImpl) Update(task model.TraceTaskHlr) int64 {
|
|
||||||
return r.traceTaskHlrRepository.Update(task)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteByIds 批量删除信息
|
|
||||||
func (r *TraceTaskHlrImpl) DeleteByIds(ids []string) (int64, error) {
|
|
||||||
// 检查是否存在
|
|
||||||
rows := r.traceTaskHlrRepository.SelectByIds(ids)
|
|
||||||
if len(rows) <= 0 {
|
|
||||||
return 0, fmt.Errorf("not data")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(rows) == len(ids) {
|
|
||||||
rows := r.traceTaskHlrRepository.DeleteByIds(ids)
|
|
||||||
return rows, nil
|
|
||||||
}
|
|
||||||
// 删除信息失败!
|
|
||||||
return 0, fmt.Errorf("delete fail")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 创建任务
|
|
||||||
func (r *TraceTaskHlrImpl) Start(task model.TraceTaskHlr) (string, error) {
|
|
||||||
ingList := r.traceTaskHlrRepository.SelectList(model.TraceTaskHlr{Status: "1"})
|
|
||||||
if len(ingList) >= 10 {
|
|
||||||
return "", fmt.Errorf("maximum running tasks 10")
|
|
||||||
}
|
|
||||||
|
|
||||||
task.TraceId = generate.Code(6)
|
|
||||||
|
|
||||||
// 发送创建任务
|
|
||||||
task.Status = "1"
|
|
||||||
task.Msg = "111111"
|
|
||||||
|
|
||||||
id := r.traceTaskHlrRepository.Insert(task)
|
|
||||||
if id == "" {
|
|
||||||
return "", fmt.Errorf("start fail")
|
|
||||||
}
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop 停止任务
|
|
||||||
func (r *TraceTaskHlrImpl) Stop(task model.TraceTaskHlr) error {
|
|
||||||
|
|
||||||
// 发送停止任务
|
|
||||||
task.Status = "0"
|
|
||||||
task.Msg = "0000"
|
|
||||||
|
|
||||||
rows := r.traceTaskHlrRepository.Update(task)
|
|
||||||
if rows <= 0 {
|
|
||||||
return fmt.Errorf("stop fail")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// File 任务文件
|
|
||||||
func (r *TraceTaskHlrImpl) File(task model.TraceTaskHlr) ([]map[string]any, error) {
|
|
||||||
hlrList := []map[string]any{}
|
|
||||||
hlrTraceId := task.TraceId
|
|
||||||
hlrTraceDirPath := "/usr/local/etc/hlr/trace"
|
|
||||||
|
|
||||||
// task.TraceId
|
|
||||||
neInfos := r.neInfoService.SelectList(neModel.NeInfo{NeType: "HLR"}, false, false)
|
|
||||||
if len(neInfos) == 0 {
|
|
||||||
return nil, fmt.Errorf("not found ne for HLR")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历多个网元主机获取文件
|
|
||||||
for _, neInfo := range neInfos {
|
|
||||||
hlrItem := map[string]any{
|
|
||||||
"neType": neInfo.NeType,
|
|
||||||
"neId": neInfo.NeId,
|
|
||||||
"err": "",
|
|
||||||
}
|
|
||||||
|
|
||||||
// 网元主机的SSH客户端
|
|
||||||
sshClient, err := r.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
|
||||||
if err != nil {
|
|
||||||
hlrItem["err"] = "ssh link fail"
|
|
||||||
hlrList = append(hlrList, hlrItem)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
defer sshClient.Close()
|
|
||||||
|
|
||||||
// 获取文件列表
|
|
||||||
_, rows, err := ssh.FileList(sshClient, hlrTraceDirPath, hlrTraceId)
|
|
||||||
if err != nil {
|
|
||||||
hlrItem["err"] = "file not found"
|
|
||||||
hlrList = append(hlrList, hlrItem)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历组装
|
|
||||||
for _, v := range rows {
|
|
||||||
if v.FileType == "file" {
|
|
||||||
hlrItem["fileName"] = v.FileName
|
|
||||||
hlrItem["filePath"] = filepath.ToSlash(filepath.Join(hlrTraceDirPath, v.FileName))
|
|
||||||
hlrList = append(hlrList, hlrItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hlrList, nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user