Răsfoiți Sursa

新增企业微信机器人提醒

maxiaoshan 3 ani în urmă
părinte
comite
5907e1430a
4 a modificat fișierele cu 140 adăugiri și 2 ștergeri
  1. 3 0
      src/main.go
  2. 3 2
      src/timetask/timetask.go
  3. 120 0
      src/timetask/wxworkwarn.go
  4. 14 0
      src/user.json

+ 3 - 0
src/main.go

@@ -34,6 +34,9 @@ func init() {
 	qu.ReadConfig(&util.Config)
 	qu.ReadConfig("autoimport.json", &front.AutoTpl)
 	qu.ReadConfig("transfercode.json", &front.Transfercode)
+	qu.ReadConfig("user.json", &timetask.LuaUserMap)
+	//组装lua开发人员信息
+	timetask.GetLuaUserInfo()
 	//redis
 	redis.InitRedis(util.Config.Redisservers)
 	//新建连接

+ 3 - 2
src/timetask/timetask.go

@@ -28,8 +28,9 @@ func TimeTask() {
 	c.Start()
 	c.AddFunc("0 20 9 ? * MON-FRI", CheckCreateTask)
 	c.AddFunc("0 0 */1 ? * *", CheckLuaMove)
-	c.AddFunc("0 30 23 * * *", UpdateSiteInfo) //定时更新站点信息
-	c.AddFunc("0 0 23 * * *", UpdateCodeHeart) //定时更新爬虫心跳信息
+	c.AddFunc("0 30 23 * * *", UpdateSiteInfo)       //定时更新站点信息
+	c.AddFunc("0 0 23 * * *", UpdateCodeHeart)       //定时更新爬虫心跳信息
+	c.AddFunc("0 0 9 ? * MON-FRI", SendInfoToWxWork) //定时查询未通过任务,心跳异常信息发送企业微信群
 }
 
 //监测爬虫由历史转增量时未成功的

+ 120 - 0
src/timetask/wxworkwarn.go

@@ -0,0 +1,120 @@
+package timetask
+
+import (
+	"bytes"
+	"fmt"
+	"math"
+	"net/http"
+	qu "qfw/util"
+	"time"
+	"util"
+)
+
+var LuaUserMap map[string]map[string]string
+var LuaUserInfoMap map[string]*LuaUserInfo
+
+type UserTextInfo struct {
+	Username             string
+	FailedTaskCount      int
+	FailedTaskOverdueDay int
+	HeartErrCount        int
+	NoCollectDataDay     int
+}
+
+type LuaUserInfo struct {
+	Username string
+	Mobile   string
+}
+
+//
+var TitleContentModel = `
+	截止目前,爬虫共有未通过任务<font color=\"warning\">%d个</font>,异常心跳爬虫<font color=\"warning\">%d个</font>。请及时处理!\n
+`
+var UserContentModel = `
+     >人员:<font color=\"warning\">%s</font>
+     >未通过任务:<font color=\"warning\">%d个</font><font color=\"info\">(最早任务已逾期%d天)</font>
+     >异常心跳爬虫:<font color=\"warning\">%d个</font><font color=\"info\">(已有爬虫%d天未采集数据)</font>\n
+`
+var MarkdownModel = `{
+    "msgtype": "markdown",
+    "markdown": {
+        "content": "%s",
+        "mentioned_mobile_list":["%s"]
+    }
+}`
+
+func GetLuaUserInfo() {
+	LuaUserInfoMap = map[string]*LuaUserInfo{}
+	for eu, info := range LuaUserMap {
+		LuaUserInfoMap[eu] = &LuaUserInfo{
+			Username: info["username"],
+			Mobile:   info["mobile"],
+		}
+	}
+}
+
+// SendInfoToWxWork
+func SendInfoToWxWork() {
+	defer qu.Catch()
+	failedTaskCount, heartCodeCount := 0, 0   //总未通过任务个数,总待处理心跳异常爬虫个数
+	userTextMap := map[string]*UserTextInfo{} //key:mobile
+	for eu, userInfo := range LuaUserInfoMap {
+		textInfo := &UserTextInfo{}
+		textInfo.Username = userInfo.Username
+		//1、未通过任务信息
+		list_task, _ := util.MgoE.Find("task",
+			map[string]interface{}{"s_modify": eu, "i_state": 5},
+			map[string]interface{}{"l_complete": 1},
+			map[string]interface{}{"l_complete": 1},
+			false, -1, -1)
+		taskLen := len(*list_task)
+		textInfo.FailedTaskCount = taskLen //个人未通过任务个数赋值
+		failedTaskCount += taskLen         //总未通过个数++
+		if taskLen > 0 {
+			complete := qu.Int64All((*list_task)[0]["l_complete"]) //未通过任务中最迟完成时间最早的任务
+			odDay := int(math.Floor(float64(time.Now().Unix()-complete) / float64(86400)))
+			textInfo.FailedTaskOverdueDay = odDay //个人未通过任务最早逾期天数赋值
+		}
+		//2、爬虫心跳信息
+		query := map[string]interface{}{
+			"list": map[string]interface{}{
+				"$lte": GetTime(0),
+			},
+			"modifyuser": eu,
+			"del":        false,
+		}
+		list_code, _ := util.MgoS.Find("spider_heart",
+			query,
+			map[string]interface{}{"list": 1},
+			map[string]interface{}{"list": 1},
+			false, -1, -1)
+		codeLen := len(*list_code)
+		textInfo.HeartErrCount = codeLen //个人异常心跳爬虫个数赋值
+		heartCodeCount += codeLen        //总异常心跳爬虫个数++
+		if codeLen > 0 {
+			listTime := qu.Int64All((*list_code)[0]["list"]) //未通过任务中最迟完成时间最早的任务
+			ncDay := int(math.Floor(float64(time.Now().Unix()-listTime) / float64(86400)))
+			textInfo.NoCollectDataDay = ncDay //个人未通过任务最早逾期天数赋值
+		}
+		userTextMap[userInfo.Mobile] = textInfo
+	}
+	//拼接content
+	resultContent := fmt.Sprintf(TitleContentModel, failedTaskCount, heartCodeCount)
+	mobileArr := []string{}
+	for mobile, t := range userTextMap {
+		mobileArr = append(mobileArr, mobile)
+		resultContent += fmt.Sprintf(UserContentModel, t.Username, t.FailedTaskCount, t.FailedTaskOverdueDay, t.HeartErrCount, t.NoCollectDataDay)
+	}
+	msg := fmt.Sprintf(MarkdownModel, resultContent, "@all")
+	qu.Debug(msg)
+	resp, err := http.Post(
+		"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
+		"application/json",
+		bytes.NewBuffer([]byte(msg)),
+	)
+	defer resp.Body.Close()
+	if err != nil {
+		fmt.Println("request error:", err)
+	}
+
+}

+ 14 - 0
src/user.json

@@ -0,0 +1,14 @@
+{
+	"ssc":{
+		"username": "施顺才",
+		"mobile": "13523457747"
+	},
+	"lyf":{
+		"username": "刘一帆",
+		"mobile": "15896901897"
+	},
+	"jiaoyubo":{
+		"username": "焦宇波",
+		"mobile": "15516197109"
+	}
+}