|
@@ -0,0 +1,351 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "errors"
|
|
|
+ "eta/eta_task/models/data_manage"
|
|
|
+ "eta/eta_task/models/data_manage/edb_inspection"
|
|
|
+ "eta/eta_task/services/data"
|
|
|
+ "eta/eta_task/utils"
|
|
|
+ "fmt"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// GetInspectionConfigData
|
|
|
+// @Description: 获取需要巡检的指标配置列表
|
|
|
+// @param now time.Time
|
|
|
+// @return sourceEdbInfoListMap map[string][]*edb_inspection.EdbInspectionConfig
|
|
|
+// @return err error
|
|
|
+func GetInspectionConfigData(now time.Time) (configList []*edb_inspection.EdbInspectionConfig, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ currTimeStr := getPreviousHalfHour(now)
|
|
|
+
|
|
|
+ // 获取所有启用的巡检配置
|
|
|
+ list, err := edb_inspection.GetAllEnabledConfigs()
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("GetAllEnabledConfigs Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据时间筛选需要巡检的配置
|
|
|
+ for _, config := range list {
|
|
|
+ if config.DateType == 1 {
|
|
|
+ // 检查是否到达巡检时间
|
|
|
+ if !isInspectionTime(config, currTimeStr) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // start_time(从几点开始) 和interval_time(间隔N小时)
|
|
|
+ if config.StartTime != "" && config.StartTime > currTimeStr {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if config.IntervalTime > 0 {
|
|
|
+ // todo 检查是否到达巡检时间
|
|
|
+ // 得到所有时间列表,然后检查是否到达巡检时间
|
|
|
+ timeListMap := getIntervalTimeMap(config.StartTime, config.IntervalTime)
|
|
|
+ if _, ok := timeListMap[currTimeStr]; !ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ configList = append(configList, config)
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// isInspectionTime
|
|
|
+// @Description: 检查是否到达巡检时间
|
|
|
+// @param config *edb_inspection.EdbInspectionConfig
|
|
|
+// @param currTimeStr string
|
|
|
+// @return bool
|
|
|
+func isInspectionTime(config *edb_inspection.EdbInspectionConfig, currTimeStr string) bool {
|
|
|
+ // 查询所有的日期配置
|
|
|
+ dateConfigs, err := edb_inspection.GetEdbInspectionDateConfigListByConfigId(config.ConfigId)
|
|
|
+ if err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当前是周几
|
|
|
+ currWeekDay := time.Now().Weekday()
|
|
|
+ // 将周日转换为7,其他保持不变
|
|
|
+ if currWeekDay == time.Sunday {
|
|
|
+ currWeekDay = 7
|
|
|
+ } else {
|
|
|
+ currWeekDay = currWeekDay + 1 // 将周一转换为1,周二转换为2,以此类推
|
|
|
+ }
|
|
|
+
|
|
|
+ // 定义中文周几的映射
|
|
|
+ weekdayMap := map[int]string{
|
|
|
+ 1: "周一",
|
|
|
+ 2: "周二",
|
|
|
+ 3: "周三",
|
|
|
+ 4: "周四",
|
|
|
+ 5: "周五",
|
|
|
+ 6: "周六",
|
|
|
+ 7: "周日",
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, dateConfig := range dateConfigs {
|
|
|
+ if dateConfig.InspectionTime != currTimeStr {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ switch dateConfig.InspectionFrequency {
|
|
|
+ case "每自然日":
|
|
|
+ return true
|
|
|
+ case "每交易日":
|
|
|
+ // 周六日不处理
|
|
|
+ if currWeekDay == 6 || currWeekDay == 7 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ case "每周":
|
|
|
+ // 检查当前是周几
|
|
|
+ if weekdayMap[int(currWeekDay)] == dateConfig.InspectionDate {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+// AddEdbInspectionRecord
|
|
|
+// @Description: 添加巡检记录
|
|
|
+// @param cont context.Context
|
|
|
+// @return err error
|
|
|
+func AddEdbInspectionRecord() (err error) {
|
|
|
+ now := time.Now()
|
|
|
+
|
|
|
+ // 获取需要巡检的配置
|
|
|
+ configList, err := GetInspectionConfigData(now)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ errMsgList := make([]string, 0)
|
|
|
+ defer func() {
|
|
|
+ if len(errMsgList) > 0 {
|
|
|
+ utils.FileLog.Error("巡检失败原因:%s", errMsgList)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 遍历每个数据源和终端的配置
|
|
|
+ for _, config := range configList {
|
|
|
+ edbInfo, tmpErr := data_manage.GetEdbInfoBySourceAndTerminalCode(config.Source, config.TerminalCode)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("获取指标信息失败: source=%s, terminalCode=%s, err=%v",
|
|
|
+ config.Source, config.TerminalCode, tmpErr))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 调用指标刷新接口
|
|
|
+ startDate := ""
|
|
|
+ dataRefreshNum := 1
|
|
|
+ edbEndDate, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
|
|
|
+ if err != nil {
|
|
|
+ errMsgList = append(errMsgList, edbInfo.EdbCode+"ParseEndDate Err:"+err.Error())
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if edbInfo.Frequency == "日度" {
|
|
|
+ startDate = edbEndDate.AddDate(0, 0, -dataRefreshNum).Format(utils.FormatDate)
|
|
|
+ } else if edbInfo.Frequency == "周度" {
|
|
|
+ startDate = edbEndDate.AddDate(0, 0, -(dataRefreshNum * 7)).Format(utils.FormatDate)
|
|
|
+ } else {
|
|
|
+ startDate = edbEndDate.AddDate(0, 0, -utils.DATA_REFRESH).Format(utils.FormatDate)
|
|
|
+ }
|
|
|
+
|
|
|
+ refreshResult := false
|
|
|
+ refreshErrMsg := "状态异常:服务异常"
|
|
|
+
|
|
|
+ // 数据更新
|
|
|
+ resp, tmpErr := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, startDate)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, edbInfo.EdbCode+"RefreshEdbData Err:"+tmpErr.Error())
|
|
|
+ } else {
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ errMsgList = append(errMsgList, edbInfo.EdbCode+";RefreshEdbData Err:"+resp.Msg+";ErrMsg:"+resp.ErrMsg)
|
|
|
+ } else {
|
|
|
+ // 查询刷新记录
|
|
|
+ refreshRecord, tmpErr := data_manage.GetEdbInfoUpdateLogByCreateTime(edbInfo.EdbInfoId, now)
|
|
|
+ if tmpErr == nil {
|
|
|
+ if refreshRecord.DataUpdateResult == 1 {
|
|
|
+ refreshResult = true
|
|
|
+ refreshErrMsg = "状态正常"
|
|
|
+ } else {
|
|
|
+ refreshErrMsg = "状态异常:"+refreshRecord.DataUpdateFailedReason
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 创建巡检记录
|
|
|
+ record := &edb_inspection.EdbInspectionRecord{
|
|
|
+ EdbInfoId: edbInfo.EdbInfoId,
|
|
|
+ Source: config.Source,
|
|
|
+ TerminalCode: config.TerminalCode,
|
|
|
+ InspectionTime: now,
|
|
|
+ InspectionResult: 0,
|
|
|
+ CreateTime: now,
|
|
|
+ ModifyTime: now,
|
|
|
+ }
|
|
|
+
|
|
|
+ if refreshResult {
|
|
|
+ record.InspectionResult = 1 // 成功
|
|
|
+ } else {
|
|
|
+ record.InspectionResult = 2 // 失败
|
|
|
+ record.ErrorReason = refreshErrMsg
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 保存巡检记录
|
|
|
+ err = record.Add()
|
|
|
+ if err != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("保存巡检记录失败: edbInfoId=%d, err=%v",
|
|
|
+ edbInfo.EdbInfoId, err))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 更新巡检看板
|
|
|
+ // 判断该终端的巡检看板是否已存在,如果存在则更新看板,否则创建看板
|
|
|
+ dashboard, tempErr := edb_inspection.GetDashboardBySourceAndTerminalCode(config.Source, config.TerminalCode)
|
|
|
+ if tempErr != nil && !utils.IsErrNoRow(tempErr) {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("获取巡检看板失败: source=%d, terminalCode=%s, err=%v",
|
|
|
+ config.Source, config.TerminalCode, err))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if dashboard == nil {
|
|
|
+ dashboard = &edb_inspection.EdbInspectionDashboard{
|
|
|
+ Source: config.Source,
|
|
|
+ TerminalCode: config.TerminalCode,
|
|
|
+ InspectionRecordId: record.InspectionRecordId,
|
|
|
+ InspectionTime: now,
|
|
|
+ InspectionResult: record.InspectionResult,
|
|
|
+ ErrorReason: record.ErrorReason,
|
|
|
+ CreateTime: now,
|
|
|
+ ModifyTime: now,
|
|
|
+ }
|
|
|
+ err = dashboard.Add()
|
|
|
+ if err != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("更新巡检看板失败: recordId=%d, err=%v",
|
|
|
+ record.InspectionRecordId, err))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ dashboard.InspectionRecordId = record.InspectionRecordId
|
|
|
+ dashboard.InspectionTime = now
|
|
|
+ dashboard.InspectionResult = record.InspectionResult
|
|
|
+ dashboard.ErrorReason = record.ErrorReason
|
|
|
+ err = dashboard.Update([]string{"InspectionRecordId", "InspectionTime", "InspectionResult", "ErrorReason"})
|
|
|
+ if err != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("更新巡检看板失败: recordId=%d, err=%v",
|
|
|
+ record.InspectionRecordId, err))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 如果巡检失败,添加消息记录
|
|
|
+ if record.InspectionResult == 2 {
|
|
|
+ msg := fmt.Sprintf("指标[%s]巡检结果异常: %s", edbInfo.EdbCode, record.ErrorReason)
|
|
|
+ adminList := strings.Split(config.NotifyUsers, ",")
|
|
|
+ for _, adminIdStr := range adminList {
|
|
|
+ adminId, _ := strconv.ParseInt(adminIdStr, 10, 64)
|
|
|
+ message := &edb_inspection.EdbInspectionMessage{
|
|
|
+ InspectionRecordId: record.InspectionRecordId,
|
|
|
+ AdminId: adminId,
|
|
|
+ Message: msg,
|
|
|
+ IsRead: 0,
|
|
|
+ CreateTime: now,
|
|
|
+ ModifyTime: now,
|
|
|
+ }
|
|
|
+ err = message.Add()
|
|
|
+ if err != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("添加消息记录失败: recordId=%d, adminId=%d, err=%v",
|
|
|
+ record.InspectionRecordId, adminId, err))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetEdbInspectionConfig
|
|
|
+// @Description: 获取巡检配置
|
|
|
+// @param cont context.Context
|
|
|
+// @return err error
|
|
|
+func GetEdbInspectionConfig(cont context.Context) (err error) {
|
|
|
+ // 获取所有巡检配置
|
|
|
+ list, err := edb_inspection.GetAllEnabledConfigs()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取每个配置的详细信息
|
|
|
+ for _, config := range list {
|
|
|
+ // 获取日期配置
|
|
|
+ dateConfigs, err := edb_inspection.GetEdbInspectionDateConfigListByConfigId(config.ConfigId)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to get date configs for config %d: %v\n", config.ConfigId, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取终端信息
|
|
|
+ terminalInfo, err := edb_inspection.GetConfigListBySourceAndTerminalCode(config.Source, config.TerminalCode)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to get terminal info for config %d: %v\n", config.ConfigId, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: 处理配置信息
|
|
|
+ fmt.Printf("Config %d: %+v\n", config.ConfigId, config)
|
|
|
+ fmt.Printf("Date configs: %+v\n", dateConfigs)
|
|
|
+ fmt.Printf("Terminal info: %+v\n", terminalInfo)
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getIntervalTimeMap(startTime string, intervalNum int) map[string]struct{} {
|
|
|
+ timeListMap := make(map[string]struct{})
|
|
|
+
|
|
|
+ // 解析开始时间startTime为09:00,转成时间格式
|
|
|
+ start, err := time.Parse("15:04", startTime)
|
|
|
+ if err != nil {
|
|
|
+ // 如果解析失败,尝试添加秒数
|
|
|
+ start, err = time.Parse("15:04:05", startTime)
|
|
|
+ if err != nil {
|
|
|
+ return timeListMap
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成24小时内的时间点
|
|
|
+ current := start
|
|
|
+ // 记录已经生成的时间点数量,防止死循环
|
|
|
+ generatedCount := 0
|
|
|
+ maxPoints := 24 / intervalNum + 1 // 最多生成的时间点数量
|
|
|
+
|
|
|
+ for generatedCount < maxPoints {
|
|
|
+ // 将时间点添加到map中,格式化为15:04
|
|
|
+ timeListMap[current.Format("15:04")] = struct{}{}
|
|
|
+ generatedCount++
|
|
|
+
|
|
|
+ // 增加间隔时间
|
|
|
+ current = current.Add(time.Duration(intervalNum) * time.Hour)
|
|
|
+
|
|
|
+ // 如果超过24小时,则停止
|
|
|
+ if current.Hour() > 23 || current.Hour() < start.Hour() {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return timeListMap
|
|
|
+}
|