|
@@ -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"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+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 {
|
|
|
|
+
|
|
|
|
+ if config.StartTime != "" && config.StartTime > currTimeStr {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ if config.IntervalTime > 0 {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ timeListMap := getIntervalTimeMap(config.StartTime, config.IntervalTime)
|
|
|
|
+ if _, ok := timeListMap[currTimeStr]; !ok {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ configList = append(configList, config)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+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()
|
|
|
|
+
|
|
|
|
+ if currWeekDay == time.Sunday {
|
|
|
|
+ currWeekDay = 7
|
|
|
|
+ } else {
|
|
|
|
+ currWeekDay = currWeekDay + 1
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func AddEdbInspectionRecord(cont context.Context) (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
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ err = record.Add()
|
|
|
|
+ if err != nil {
|
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprintf("保存巡检记录失败: edbInfoId=%d, err=%v",
|
|
|
|
+ edbInfo.EdbInfoId, err))
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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{})
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ start, err := time.Parse("15:04", startTime)
|
|
|
|
+ if err != nil {
|
|
|
|
+
|
|
|
|
+ start, err = time.Parse("15:04:05", startTime)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return timeListMap
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ current := start
|
|
|
|
+
|
|
|
|
+ generatedCount := 0
|
|
|
|
+ maxPoints := 24 / intervalNum + 1
|
|
|
|
+
|
|
|
|
+ for generatedCount < maxPoints {
|
|
|
|
+
|
|
|
|
+ timeListMap[current.Format("15:04")] = struct{}{}
|
|
|
|
+ generatedCount++
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ current = current.Add(time.Duration(intervalNum) * time.Hour)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if current.Hour() > 23 || current.Hour() < start.Hour() {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return timeListMap
|
|
|
|
+}
|