wxworkwarn.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package timetask
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "net/http"
  7. qu "qfw/util"
  8. "strings"
  9. "time"
  10. "util"
  11. )
  12. var UserMap map[string]map[string]string
  13. var PythonModifyUserInfoMap map[string]*UserInfo //开发人员信息集合
  14. var LuaModifyUserInfoMap map[string]*UserInfo //开发人员信息集合
  15. var LuaAuditorInfoMap map[string]*UserInfo //审核人员信息集合
  16. //lua
  17. type LuaUserTextInfo struct {
  18. Username string
  19. FailedTaskCount int
  20. FailedTaskOverdueDay int
  21. HeartErrCount int
  22. NoCollectDataDay int
  23. }
  24. //python
  25. type PythonUserTextInfo struct {
  26. Username string
  27. ToBeCompleted int //待完成爬虫个数
  28. Failed int //未通过爬虫个数
  29. }
  30. type UserInfo struct {
  31. Username string
  32. Mobile string
  33. Auth string
  34. }
  35. //
  36. var LuaTitleContentModel = `
  37. 截止目前,爬虫共有未通过任务<font color=\"warning\">%d个</font>,异常心跳爬虫<font color=\"warning\">%d个</font>。请及时处理!\n
  38. `
  39. var PythonTitleContentModel = `
  40. 截止目前,共有待完成爬虫<font color=\"warning\">%d个</font>,未通过爬虫<font color=\"warning\">%d个</font>,待审核爬虫<font color=\"warning\">%d个</font>。请及时处理!\n
  41. `
  42. var LuaUserContentModel = `
  43. >人员:<font color=\"warning\">%s</font>
  44. >未通过任务:<font color=\"warning\">%d个</font><font color=\"info\">(最早任务已逾期%d天)</font>
  45. >异常心跳爬虫:<font color=\"warning\">%d个</font><font color=\"info\">(已有爬虫%d天未采集数据)</font>\n
  46. `
  47. var PythonUserContentModel = `
  48. >人员:<font color=\"warning\">%s</font>
  49. >待完成爬虫:<font color=\"warning\">%d个</font><font color=\"info\"></font>
  50. >未通过爬虫:<font color=\"warning\">%d个</font><font color=\"info\"></font>\n
  51. `
  52. var MarkdownModel = `{
  53. "msgtype": "markdown",
  54. "markdown": {
  55. "content": "%s"
  56. }
  57. }`
  58. var TextModel = `{
  59. "msgtype": "text",
  60. "text": {
  61. "content": "%s",
  62. "mentioned_mobile_list":[%s]
  63. }
  64. }`
  65. func GetLuaUserInfo() {
  66. LuaModifyUserInfoMap = map[string]*UserInfo{}
  67. LuaAuditorInfoMap = map[string]*UserInfo{}
  68. PythonModifyUserInfoMap = map[string]*UserInfo{}
  69. for eu, info := range UserMap {
  70. role := info["role"]
  71. auth := info["auth"]
  72. if role == "lua" {
  73. if auth == "1" { //开发人员
  74. LuaModifyUserInfoMap[eu] = &UserInfo{
  75. Username: info["username"],
  76. Mobile: info["mobile"],
  77. Auth: auth,
  78. }
  79. }
  80. if auth == "3" || eu == "ssc" { //审核人员
  81. LuaAuditorInfoMap[eu] = &UserInfo{
  82. Username: info["username"],
  83. Mobile: info["mobile"],
  84. Auth: auth,
  85. }
  86. }
  87. } else {
  88. if auth == "1" {
  89. PythonModifyUserInfoMap[eu] = &UserInfo{
  90. Username: info["username"],
  91. Mobile: info["mobile"],
  92. Auth: auth,
  93. }
  94. }
  95. }
  96. }
  97. qu.Debug(LuaModifyUserInfoMap)
  98. qu.Debug(LuaAuditorInfoMap)
  99. qu.Debug(PythonModifyUserInfoMap)
  100. }
  101. // 统计爬虫开发人员未完成爬虫和任务
  102. func SendInfoToWxWork_Tomodifyuser() {
  103. SendLuaInfo()
  104. SendPythonInfo()
  105. }
  106. func SendLuaInfo() {
  107. defer qu.Catch()
  108. qu.Debug("lua企业微信发送提示信息")
  109. failedTaskCount, heartCodeCount := 0, 0 //总未通过任务个数,总待处理心跳异常爬虫个数
  110. luaUserTextMap := map[string]*LuaUserTextInfo{} //key:mobile
  111. for user, userInfo := range LuaModifyUserInfoMap {
  112. textInfo := &LuaUserTextInfo{}
  113. textInfo.Username = userInfo.Username
  114. //1、未通过任务信息
  115. list_task, _ := util.MgoE.Find("task",
  116. map[string]interface{}{"s_modify": user, "i_state": 5},
  117. map[string]interface{}{"l_complete": 1},
  118. map[string]interface{}{"l_complete": 1},
  119. false, -1, -1)
  120. taskLen := len(*list_task)
  121. textInfo.FailedTaskCount = taskLen //个人未通过任务个数赋值
  122. failedTaskCount += taskLen //总未通过个数++
  123. if taskLen > 0 {
  124. complete := qu.Int64All((*list_task)[0]["l_complete"]) //未通过任务中最迟完成时间最早的任务
  125. odDay := int(math.Floor(float64(time.Now().Unix()-complete) / float64(86400)))
  126. textInfo.FailedTaskOverdueDay = odDay //个人未通过任务最早逾期天数赋值
  127. }
  128. //2、爬虫心跳信息
  129. query := map[string]interface{}{
  130. "modifyuser": user,
  131. "del": false,
  132. //"list": map[string]interface{}{
  133. // "$lte": util.GetTime(0),
  134. //},
  135. "$or": []interface{}{
  136. map[string]interface{}{
  137. "event": map[string]interface{}{
  138. "$ne": 7520,
  139. //"$nin": []int{7500, 7510},
  140. },
  141. "list": map[string]interface{}{
  142. "$lte": util.GetTime(0),
  143. },
  144. },
  145. map[string]interface{}{
  146. "event": 7520,
  147. //"event": map[string]interface{}{
  148. // "$in": []int{7500, 7510},
  149. //},
  150. "list": map[string]interface{}{
  151. "$lte": util.GetTime(-1),
  152. },
  153. },
  154. },
  155. }
  156. qu.Debug("heart query:", query)
  157. list_code, _ := util.MgoS.Find("spider_heart",
  158. query,
  159. map[string]interface{}{"list": 1},
  160. map[string]interface{}{"list": 1},
  161. false, -1, -1)
  162. codeLen := len(*list_code)
  163. textInfo.HeartErrCount = codeLen //个人异常心跳爬虫个数赋值
  164. heartCodeCount += codeLen //总异常心跳爬虫个数++
  165. if codeLen > 0 {
  166. listTime := qu.Int64All((*list_code)[0]["list"]) //未通过任务中最迟完成时间最早的任务
  167. ncDay := int(math.Floor(float64(time.Now().Unix()-listTime) / float64(86400)))
  168. textInfo.NoCollectDataDay = ncDay //个人未通过任务最早逾期天数赋值
  169. }
  170. luaUserTextMap[userInfo.Mobile] = textInfo
  171. }
  172. //拼接content
  173. resultContent := fmt.Sprintf(LuaTitleContentModel, failedTaskCount, heartCodeCount)
  174. mobileArr := []string{}
  175. for mobile, t := range luaUserTextMap {
  176. mobileArr = append(mobileArr, "\""+mobile+"\"")
  177. resultContent += fmt.Sprintf(LuaUserContentModel, t.Username, t.FailedTaskCount, t.FailedTaskOverdueDay, t.HeartErrCount, t.NoCollectDataDay)
  178. }
  179. msg := fmt.Sprintf(MarkdownModel, resultContent)
  180. qu.Debug("msg", msg)
  181. toUserMsg := fmt.Sprintf(TextModel, "", strings.Join(mobileArr, ","))
  182. qu.Debug("toUserMsg", toUserMsg)
  183. resp1, err := http.Post(
  184. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
  185. "application/json",
  186. bytes.NewBuffer([]byte(toUserMsg)),
  187. )
  188. if err != nil {
  189. fmt.Println("request error:", err)
  190. return
  191. }
  192. defer resp1.Body.Close()
  193. resp2, err := http.Post(
  194. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
  195. "application/json",
  196. bytes.NewBuffer([]byte(msg)),
  197. )
  198. if err != nil {
  199. fmt.Println("request error:", err)
  200. return
  201. }
  202. defer resp2.Body.Close()
  203. }
  204. func SendPythonInfo() {
  205. defer qu.Catch()
  206. qu.Debug("python企业微信发送提示信息")
  207. toBeCompletedAllCount, failedAllCount := 0, 0
  208. toBeReviewedAllCount := util.MgoEB.Count("luaconfig", map[string]interface{}{"state": 1, "platform": "python"})
  209. pythonUserTextMap := map[string]*PythonUserTextInfo{} //key:mobile
  210. for user, userInfo := range PythonModifyUserInfoMap {
  211. textInfo := &PythonUserTextInfo{}
  212. textInfo.Username = userInfo.Username
  213. //1、待完成爬虫个数
  214. textInfo.ToBeCompleted = util.MgoEB.Count("luaconfig", map[string]interface{}{"state": 0, "modifyuser": user})
  215. toBeCompletedAllCount += textInfo.ToBeCompleted
  216. //2、未通过爬虫个数
  217. textInfo.Failed = util.MgoEB.Count("luaconfig", map[string]interface{}{"state": 2, "modifyuser": user})
  218. failedAllCount += textInfo.Failed
  219. pythonUserTextMap[userInfo.Mobile] = textInfo
  220. }
  221. //拼接content
  222. resultContent := fmt.Sprintf(PythonTitleContentModel, toBeCompletedAllCount, failedAllCount, toBeReviewedAllCount)
  223. mobileArr := []string{}
  224. for mobile, t := range pythonUserTextMap {
  225. mobileArr = append(mobileArr, "\""+mobile+"\"")
  226. resultContent += fmt.Sprintf(PythonUserContentModel, t.Username, t.ToBeCompleted, t.Failed)
  227. }
  228. msg := fmt.Sprintf(MarkdownModel, resultContent)
  229. qu.Debug("msg", msg)
  230. toUserMsg := fmt.Sprintf(TextModel, "", strings.Join(mobileArr, ","))
  231. qu.Debug("toUserMsg", toUserMsg)
  232. resp1, err := http.Post(
  233. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
  234. "application/json",
  235. bytes.NewBuffer([]byte(toUserMsg)),
  236. )
  237. if err != nil {
  238. fmt.Println("request error:", err)
  239. return
  240. }
  241. defer resp1.Body.Close()
  242. resp2, err := http.Post(
  243. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
  244. "application/json",
  245. bytes.NewBuffer([]byte(msg)),
  246. )
  247. if err != nil {
  248. fmt.Println("request error:", err)
  249. return
  250. }
  251. defer resp2.Body.Close()
  252. }
  253. // 统计爬虫审核人员待审核爬虫
  254. func SendInfoToWxWork_ToAuditor() {
  255. defer qu.Catch()
  256. qu.Debug("企业微信发送提示信息")
  257. tmpContent := "当前"
  258. tmpModifyList := []string{}
  259. qu.Debug(LuaAuditorInfoMap)
  260. for eu, userInfo := range LuaAuditorInfoMap {
  261. query := map[string]interface{}{
  262. "state": 1,
  263. "platform": "golua平台",
  264. }
  265. if eu == "cjk" { //陈佳康审核的施顺才的爬虫
  266. query["modifyuser"] = map[string]interface{}{
  267. "$eq": "ssc",
  268. }
  269. } else {
  270. query["modifyuser"] = map[string]interface{}{
  271. "$ne": "ssc",
  272. }
  273. }
  274. count := util.MgoEB.Count("luaconfig", query)
  275. tmpContent += userInfo.Username + "需审核的爬虫有:" + fmt.Sprint(count) + "个;"
  276. tmpModifyList = append(tmpModifyList, userInfo.Mobile)
  277. }
  278. msg := fmt.Sprintf(TextModel, tmpContent+"请及时处理!", strings.Join(tmpModifyList, ","))
  279. qu.Debug("msg", msg)
  280. resp1, err := http.Post(
  281. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
  282. "application/json",
  283. bytes.NewBuffer([]byte(msg)),
  284. )
  285. if err != nil {
  286. fmt.Println("request error:", err)
  287. return
  288. }
  289. defer resp1.Body.Close()
  290. }