Forráskód Böngészése

数据异常预警-比对最新数据与当前时间的时间间隔

huangyan 2 hónapja
szülő
commit
50fccab833
4 módosított fájl, 85 hozzáadás és 87 törlés
  1. 14 1
      config.yaml
  2. 1 2
      main.go
  3. 51 60
      service/datacaps.go
  4. 19 24
      util/create_code.go

+ 14 - 1
config.yaml

@@ -26,6 +26,19 @@ monitoring:
 subMail:
   appid: "97173"
   signature: "f639a60e41ee0554921d89884f5ff87e"
+server:
+  port: ":8080"
 phone:
   - "15186767321" #黄渰
-#  - "18777951277" #李哥
+#  - "18777951277" #李科慧
+#  - "14785419131" #冯志刚
+#  - "18927494331" #冯靖沣
+#  - "13765836337" #方芳
+#  - "18180861213" #任军
+#  - "13303842102" #胡继
+#  - "17384784046" #张杨
+#  - "15538026550" #乔世通
+#  - "15985650575" #杨建
+#  - "18086869080" #曾发萍
+#  - "17673255548" #熊国东
+  - "15117753016" #左浪浪

+ 1 - 2
main.go

@@ -11,6 +11,5 @@ func main() {
 	startTime := time.Now()
 	service.EquipmentMonitoring2()
 	elapsedTime := time.Since(startTime)
-	fmt.Printf("函数执行耗时: %v\n", elapsedTime)
-	//util.SendModel("15186767321", []string{"1", "2"})
+	fmt.Printf("程序执行耗时: %v\n", elapsedTime)
 }

+ 51 - 60
service/datacaps.go

@@ -24,7 +24,7 @@ func DataCaps(davice []Device) {
 	message := make([]string, 0)
 	//文件名
 	var row = 2
-	filename := fmt.Sprintf("data_caps_%s.xlsx", time.Now().Format("2006-01-02"))
+	filename := fmt.Sprintf("%s.xlsx", time.Now().Format("2006-01-02"))
 	file, err := excelize.OpenFile(filename)
 	if err != nil {
 		file = excelize.NewFile()
@@ -39,7 +39,7 @@ func DataCaps(davice []Device) {
 		file.SetCellValue(sheet, "E1", "数据缺失开始时间")
 	} else {
 		//获取最后一行
-		lastRow, err := getLastRow(file, "sheet")
+		lastRow, err := getLastRow(file, "sheet1")
 		if err != nil {
 			log.Println("获取Excel文件失败:", err)
 			return
@@ -49,6 +49,8 @@ func DataCaps(davice []Device) {
 
 	sqllist := make([]string, 0)
 	for _, v := range davice {
+		log.Println(v.Tsn)
+		//sql := fmt.Sprintf("select t_sn,t_id,t_pid,t_name from device_sensor where t_sn = %s")
 		sql := fmt.Sprintf("select t_sn,t_id,t_pid,t_name from device_sensor where t_sn = %s", v.Tsn)
 		sqllist = append(sqllist, sql)
 	}
@@ -65,78 +67,67 @@ func DataCaps(davice []Device) {
 	}
 	//查询最新的两条数据以及保存时间,比较最近两条时间是否有超过保存时间
 	for _, sensor := range deviceSensor {
-		t_time := make([]string, 0)
+		var t_time string
 		var t_save_t string
 		var companyname string
-		sql := fmt.Sprintf("SELECT t_time FROM z_device_data_%s ORDER BY t_time DESC LIMIT 2", sensor.Tsn)
+		var id string
+		sql := fmt.Sprintf("SELECT t_time FROM z_device_data_%s ORDER BY t_time DESC LIMIT 1", sensor.Tsn)
 		sqls := fmt.Sprintf("SELECT t_save_t FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sensor.Tsn)
-		company := fmt.Sprintf("SELECT t_name FROM company WHERE id='%s'", sensor.TPid)
+		company := fmt.Sprintf("SELECT t_name,id FROM company WHERE id='%s'", sensor.TPid)
 		databases.Db.QueryRow(sqls).Scan(&t_save_t)
-		databases.Db.QueryRow(company).Scan(&companyname)
-		query, err := databases.Db.Query(sql)
-		if err != nil {
-			log.Println("数据查询失败:", err)
+		databases.Db.QueryRow(company).Scan(&companyname, &id)
+		queryRow := databases.Db.QueryRow(sql)
+
+		if queryRow != nil {
+			queryRow.Scan(&t_time)
 		}
-		if query != nil {
-			for query.Next() {
-				var singleTime string
-				query.Scan(&singleTime)
-				t_time = append(t_time, singleTime)
+		// 解析时间字符串为time.Time对象
+		timeFormat := "2006-01-02 15:04:05" // 假设这是时间字符串的格式
+		//fmt.Println("时间不能为空")
+		if t_time != "" {
+			t1, err := time.Parse(timeFormat, t_time)
+			if err != nil {
+				log.Println("解析时间失败:", err)
+				continue
 			}
-			//fmt.Println("实际间隔时间:", t_time)
-			//fmt.Println("系统间隔时间:", t_save_t)
-			// 解析时间字符串为time.Time对象
-			timeFormat := "2006-01-02 15:04:05" // 假设这是时间字符串的格式
-			if len(t_time) >= 2 {
-				//fmt.Println("时间不能为空")
-				t1, err := time.Parse(timeFormat, t_time[0])
-				if err != nil {
-					log.Println("解析时间失败:", err)
-					continue
-				}
-				t2, err := time.Parse(timeFormat, t_time[1])
-				if err != nil {
-					log.Println("解析时间失败:", err)
-					continue
-				}
+			now := time.Now().Format(timeFormat)
+			nows, _ := time.Parse(timeFormat, now)
+			// 计算时间差
+			timeDiff := nows.Sub(t1).Seconds()
+			//timeDiff := t1.Sub(now).Seconds()
+			fmt.Println("最后一条数据时间:", t_time)
+			fmt.Println("系统当前时间:", now)
+			//比较时间差
+			fmt.Println("时间差:", timeDiff)
+			fmt.Println("系统设置时间差:", t_save_t)
 
-				// 计算时间差
-				timeDiff := t1.Sub(t2).Seconds()
-
-				//fmt.Printf("实际间隔时间(秒): %.2f\n", timeDiff)
-				//fmt.Println("系统间隔时间:", t_save_t)
-				// 比较时间差
-				// 获取今天的开始时间
-				todayStart := time.Now().Truncate(24 * time.Hour)
-				//fmt.Println("todayStart:", todayStart)
-				// 确保t1表示的时间为今天
-				if t1.After(todayStart) || t1.Equal(todayStart) {
-					float, _ := strconv.ParseFloat(t_save_t, 64)
-					if timeDiff > float {
-						//log.Printf("实际时间大于系统时间Sn:%s=>>%s=>>探头%s,上传时间%s", sensor.Tsn, companyname, sensor.TId, t_time[0])
-						message = append(message, fmt.Sprintf("Sn:%s==>%s==>设备:%s==>探头%s==>数据缺失上传时间:%s \n", sensor.Tsn, companyname, sensor.TName, sensor.TId, t_time[0]))
-						// 将相关信息记录到Excel文件中
-						file.SetCellValue("sheet1", fmt.Sprintf("A%d", row), sensor.Tsn)
-						file.SetCellValue("sheet1", fmt.Sprintf("B%d", row), companyname)
-						file.SetCellValue("sheet1", fmt.Sprintf("C%d", row), sensor.TName)
-						file.SetCellValue("sheet1", fmt.Sprintf("D%d", row), sensor.TId)
-						file.SetCellValue("sheet1", fmt.Sprintf("E%d", row), t_time[0]) // 数据缺失开始时间
-						row++
-
-					}
+			// 获取今天的开始时间
+			todayStart := time.Now().Truncate(24 * time.Hour)
+			// 确保t1表示的时间为今天
+			if t1.After(todayStart) || t1.Equal(todayStart) {
+				float, _ := strconv.ParseFloat(t_save_t, 64)
+				if timeDiff > float {
+					//log.Printf("实际时间大于系统时间Sn:%s=>>%s=>>探头%s,上传时间%s", sensor.Tsn, companyname, sensor.TId, t_time[0])
+					message = append(message, fmt.Sprintf("Sn:%s==>%s==>设备:%s==>探头%s==>数据缺失上传时间:%s \n", sensor.Tsn, companyname+"["+id+"]", sensor.TName, sensor.TId, t_time))
+					// 将相关信息记录到Excel文件中
+					file.SetCellValue("sheet1", fmt.Sprintf("A%d", row), sensor.Tsn)
+					file.SetCellValue("sheet1", fmt.Sprintf("B%d", row), companyname+"["+id+"]")
+					file.SetCellValue("sheet1", fmt.Sprintf("C%d", row), sensor.TName)
+					file.SetCellValue("sheet1", fmt.Sprintf("D%d", row), sensor.TId)
+					file.SetCellValue("sheet1", fmt.Sprintf("E%d", row), t_time) // 数据缺失开始时间
+					row++
 				}
 			}
-
-		}
-
-		if err := file.SaveAs(filename); err != nil {
-			log.Println("保存Excel文件失败:", err)
+			if err := file.SaveAs(filename); err != nil {
+				log.Println("保存Excel文件失败:", err)
+			}
 		}
 	}
 	fmt.Println(message)
+	lenght := len(message)
 	phones := config.Config.GetStringSlice("phone")
 	for _, phone := range phones {
-		err := util.SendModel(phone, message)
+		err := util.SendModel(phone, message, lenght)
 		if err != nil {
 			log.Println("发送失败:", err)
 		}

+ 19 - 24
util/create_code.go

@@ -3,33 +3,28 @@ package util
 import (
 	"fmt"
 	"go.uber.org/zap"
-	"math/rand"
-	"time"
+	"log"
 	"ydMonitoring/config"
 )
 
-func CreatCode() string {
-	const chars = "0123456789"
-	result := make([]byte, 6)
-	rand.Seed(time.Now().UnixNano())
-	for i := range result {
-		index := rand.Intn(len(chars))
-		result[i] = chars[index]
-	}
-	return string(result)
-}
-
-func SendModel(phone string, msg []string) error {
+func SendModel(phone string, msg []string, lenght int) error {
 	ss := NewSMS(config.Config.GetString("submail.appid"), config.Config.GetString("submail.signature"))
-
-	//code := CreatCode()
-	content := fmt.Sprintf("【数据预警提示】以下数据出现异常\n:%s", msg)
-	res, err := ss.Send(phone, content)
-	if err != nil || res.Status != SUCCESS {
-		fmt.Println("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
-		return err
-	} else {
-		//验证码装入redis并且设置过期时间
-		return nil
+	if lenght > 10 {
+		sprintf := fmt.Sprintf("【数据预警提示】当前出现%d条异常数据,请及时处理!", lenght)
+		res, err := ss.Send(phone, sprintf)
+		if err != nil || res.Status != SUCCESS {
+			log.Println("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
+			return err
+		}
+		log.Println("发送短信验证码成功", zap.Any("res", res))
+	} else if lenght > 0 {
+		content := fmt.Sprintf("【数据预警提示】以下数据出现异常\n:%s\n共有%d条异常数据", msg, lenght)
+		res, err := ss.Send(phone, content)
+		if err != nil || res.Status != SUCCESS {
+			log.Println("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
+			return err
+		}
+		log.Println("发送短信验证码成功", zap.Any("res", res))
 	}
+	return nil
 }