main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/elastic"
  5. "app.yhyue.com/data_processing/common_utils/mongodb"
  6. "encoding/json"
  7. "fmt"
  8. es "github.com/olivere/elastic/v7"
  9. "github.com/robfig/cron/v3"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. "log"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. //定时任务,去统计bidding索引,排查问题、预警
  19. type T struct {
  20. Cron string
  21. Name string
  22. Min int
  23. Max int
  24. Type string
  25. Tjscope string
  26. Mgo string
  27. }
  28. //es数据最报警
  29. var (
  30. config map[string]interface{}
  31. to string
  32. api string
  33. esAddr, esAddr2, esAddr3 string
  34. esIndex, esIndex2, esIndex3 string
  35. username, password string
  36. username3, password3 string
  37. Ts = []*T{}
  38. esQ = `{"query": {"range": {"id": {"gte": "%s","lt": "%s"}}}}`
  39. esQ1 = `{"query": {"bool": {"must": [{"range": {"id": {"gte": "%s","lt": "%s"}}},{"terms": {"bidding.site": ["元博网(采购与招标网)","中国招标与采购网"]}}]}}}`
  40. )
  41. func init() {
  42. util.ReadConfig(&config)
  43. jkmail := config["jkmail"].(map[string]interface{})
  44. to, _ = jkmail["to"].(string)
  45. api, _ = jkmail["api"].(string)
  46. //esAddr, _ = config["esAddr"].(string)
  47. esIndex, _ = config["esIndex"].(string)
  48. esAddr2, _ = config["esAddr2"].(string)
  49. esIndex2, _ = config["esIndex2"].(string) //
  50. if _, ok := config["esAddr3"]; ok {
  51. esAddr3, _ = config["esAddr3"].(string)
  52. esIndex3, _ = config["esIndex3"].(string) //
  53. username3, _ = config["username3"].(string)
  54. password3, _ = config["password3"].(string)
  55. }
  56. username, _ = config["username"].(string)
  57. password, _ = config["password"].(string)
  58. tasks, _ := config["task"].([]interface{})
  59. for _, t := range tasks {
  60. bs, _ := json.Marshal(t)
  61. var v *T
  62. json.Unmarshal(bs, &v)
  63. if v != nil {
  64. Ts = append(Ts, v)
  65. }
  66. }
  67. }
  68. func main() {
  69. log.Println("start..")
  70. if len(Ts) > 0 {
  71. local, _ := time.LoadLocation("Asia/Shanghai")
  72. c := cron.New(cron.WithLocation(local), cron.WithSeconds())
  73. for _, v := range Ts {
  74. c.AddFunc(v.Cron, v.task)
  75. }
  76. c.Start()
  77. defer c.Stop()
  78. select {}
  79. }
  80. log.Println("end..")
  81. }
  82. func (t *T) task() {
  83. //初始化语句
  84. qt := strings.Split(t.Tjscope, ",")
  85. if len(qt) != 2 {
  86. return
  87. }
  88. st, et := int64(0), int64(0)
  89. now := time.Now()
  90. switch qt[1] {
  91. case "h":
  92. et = now.Unix()
  93. st = et + util.Int64All(qt[0])*3600
  94. case "d":
  95. st = util.GetDayStartSecond(util.IntAll(qt[0]))
  96. et = util.GetDayStartSecond(0)
  97. }
  98. st1 := fmt.Sprintf("%x0000000000000000", st)
  99. et1 := fmt.Sprintf("%x0000000000000000", et)
  100. eq := fmt.Sprintf(esQ, st1, et1)
  101. fmt.Println("eq", eq)
  102. eq1 := fmt.Sprintf(esQ1, st1, et1)
  103. fmt.Println("eq1", eq1)
  104. rangeQuery := es.NewRangeQuery("id").Gte(st1).Lt(et1)
  105. termsQuery := es.NewTermsQuery("site", "元博网(采购与招标网)", "中国招标与采购网")
  106. countQuery := es.NewBoolQuery().
  107. Must(rangeQuery)
  108. count1Query := es.NewBoolQuery().Must(rangeQuery).Filter(termsQuery)
  109. es2 := elastic.Elastic{S_esurl: esAddr2, I_size: 2, Username: username, Password: password}
  110. es2.InitElasticSize()
  111. es3 := elastic.Elastic{S_esurl: esAddr3, I_size: 5, Username: username3, Password: password3}
  112. es3.InitElasticSize()
  113. count := int(es2.Count(esIndex2, esIndex2, countQuery)) //公司 es集群 数量统计
  114. count1 := int(es2.Count(esIndex, esIndex, count1Query)) //竞品网站 数量统计
  115. countNew := int(es3.Count(esIndex3, esIndex3, countQuery)) //华为云 新集群
  116. fmt.Println("count", count)
  117. fmt.Println("count1", count1)
  118. fmt.Println("countNew", countNew)
  119. switch t.Type {
  120. case "alert":
  121. if count < t.Min || count > t.Max || countNew < t.Min || countNew > t.Max {
  122. report := fmt.Sprintf("告警%s,最小%d,最大%d,统计结果:%d", t.Name, t.Min, t.Max, count)
  123. t.SendMail(report)
  124. }
  125. case "report":
  126. report := fmt.Sprintf("统计报告%s,【统计结果】,es库数量:%d", t.Name, count)
  127. if len(t.Mgo) > 5 {
  128. fs := strings.Split(t.Mgo, "|")
  129. fmgo := mongodb.NewMgoWithUser(fs[0], fs[3], fs[1], fs[2], 1)
  130. id1 := mongodb.StringTOBsonId(st1)
  131. id2 := mongodb.StringTOBsonId(et1)
  132. mq := bson.M{"_id": bson.M{"$gte": id1, "$lt": id2}} //一天时间内的id段
  133. fd := bson.M{"extracttype": 1, "sensitive": 1, "dataging": 1, "site": 1, "infoformat": 1, "comeintime": 1, "pici": 1, "publishtime": 1, "competehref": 1, "attach_text": 1}
  134. sess := fmgo.GetMgoConn()
  135. defer fmgo.DestoryMongoConn(sess)
  136. /**
  137. count 一天内,es 中 数据总量
  138. count1 一天内,es 中 竞品总量
  139. count2 一天内,mgo 总入库量
  140. count3 一天内,mgo 有效数据 总数
  141. count4 一天内,mgo 中 竞品数据总量
  142. count5 一天内,mgo 有效数据中,竞品的数量
  143. countNew 一天内,es3新集群 中 数据总量
  144. timeCount pici-comeintime 时差在12小时内的数据
  145. */
  146. count2, count3 := 0, 0 //
  147. count4, count5 := 0, 0 //竟品
  148. es_comeintime_totaltime := int64(0) //comeintime 和 生索引 pici 时间 差值的总和
  149. es_publishtime_totaltime := int64(0) //publishtime 和 生索引 pici 时间 差值的总和
  150. es_comeintime_avgtime := int64(0) //comeintime 和 生索引 pici 时间 差值的平均值
  151. es_publishtime_avgtime := int64(0) //publishtime 和 生索引 pici 时间 差值的平均值
  152. file_totaltime := int64(0)
  153. no_file_totaltime := int64(0)
  154. file_avgltime := int64(0)
  155. no_file_avgltime := int64(0)
  156. timeCount := 0 //12小时 统计时间差的数据量
  157. fileCount := 0
  158. noFileCount := 0
  159. //统计pici -comeintime 时间差,1、3、5、10、15、30、30+ 分钟
  160. var pc_diff1 int64
  161. var pc_diff3 int64
  162. var pc_diff5 int64
  163. var pc_diff10 int64
  164. var pc_diff15 int64
  165. var pc_diff30 int64
  166. var pc_diff301 int64
  167. query := sess.DB(fs[3]).C(fs[4]).Find(mq).Select(fd).Iter()
  168. for tmp := make(map[string]interface{}); query.Next(tmp); count2++ {
  169. if util.ObjToString(tmp["site"]) == "元博网(采购与招标网)" || util.ObjToString(tmp["site"]) == "中国招标与采购网" {
  170. count4++
  171. }
  172. if util.IntAll(tmp["extracttype"]) != -1 && util.ObjToString(tmp["sensitive"]) != "测试" && util.IntAll(tmp["dataging"]) != 1 && util.Float64All(tmp["infoformat"]) != 3 {
  173. count3++
  174. comeintime := util.Int64All(tmp["comeintime"])
  175. publishtime := util.Int64All(tmp["publishtime"])
  176. pici := util.Int64All(tmp["pici"])
  177. tp := time.Unix(publishtime, 0)
  178. isZero := false
  179. if (tp.Hour() == 0 && tp.Minute() == 0) && tp.Second() == 0 {
  180. isZero = true
  181. }
  182. if (comeintime-publishtime) < 12*60*60 && !isZero {
  183. if pici > 0 {
  184. diff1 := pici - comeintime
  185. diff2 := pici - publishtime
  186. if diff1 < 0 {
  187. fmt.Println("diff1", diff1, tmp["_id"])
  188. } else if diff1 <= 60 {
  189. pc_diff1++
  190. } else if diff1 <= 3*60 {
  191. pc_diff3++
  192. } else if diff1 <= 5*60 {
  193. pc_diff5++
  194. } else if diff1 <= 10*60 {
  195. pc_diff10++
  196. } else if diff1 <= 15*60 {
  197. pc_diff15++
  198. } else if diff1 <= 30*60 {
  199. pc_diff30++
  200. } else {
  201. pc_diff301++
  202. }
  203. if diff2 < 0 {
  204. fmt.Println("diff2", diff2, tmp["_id"])
  205. }
  206. es_comeintime_totaltime += diff1
  207. es_publishtime_totaltime += diff2
  208. timeCount++
  209. if _, ok := tmp["attach_text"]; ok {
  210. curtime := tmp["_id"].(primitive.ObjectID).Timestamp().Unix()
  211. diff3 := curtime - comeintime
  212. if diff3 >= 0 {
  213. file_totaltime += diff3
  214. }
  215. fileCount++
  216. } else {
  217. curtime := tmp["_id"].(primitive.ObjectID).Timestamp().Unix()
  218. diff4 := curtime - comeintime
  219. if diff4 >= 0 {
  220. no_file_totaltime += diff4
  221. }
  222. noFileCount++
  223. }
  224. }
  225. }
  226. if util.ObjToString(tmp["site"]) == "元博网(采购与招标网)" || util.ObjToString(tmp["site"]) == "中国招标与采购网" {
  227. count5++
  228. }
  229. }
  230. }
  231. if timeCount > 0 {
  232. es_comeintime_avgtime = es_comeintime_totaltime / int64(timeCount)
  233. es_publishtime_avgtime = es_publishtime_totaltime / int64(timeCount)
  234. }
  235. if fileCount > 0 {
  236. file_avgltime = file_totaltime / int64(fileCount)
  237. }
  238. if noFileCount > 0 {
  239. no_file_avgltime = no_file_totaltime / int64(noFileCount)
  240. }
  241. report += ",mgo统计:" + fmt.Sprint(count3) + ",差值:" + fmt.Sprint(count3-count) + ",mgo总入库量:" + fmt.Sprint(count2)
  242. report += "<br>" + "【竟品统计结果】:" + strconv.Itoa(count1) + ",mgo统计:" + fmt.Sprint(count5) + ",差值:" + fmt.Sprint(count5-count1) + ",mgo总入库量" + fmt.Sprint(count4) + "<br>【新集群统计结果】es数量:" + strconv.Itoa(countNew) + ",mgo统计:" + fmt.Sprint(count3) + ",差值:" + fmt.Sprint(count3-countNew)
  243. //存入数据库
  244. yesterday := now.AddDate(0, 0, -1)
  245. insert := map[string]interface{}{
  246. "es_count": count, //
  247. "es3_count": countNew, //
  248. "mgo_count": count3,
  249. "es_mgo_diff": count3 - count,
  250. "mgo_total": count2,
  251. "competitor_es_count": count1, //竞品网站es 数量
  252. "competitor_mgo_count": count5,
  253. "competitor_diff": count5 - count1,
  254. "competitor_mgo_total": count4,
  255. "date": yesterday.Format("2006-01-02"),
  256. "es_comeintime_totaltime": es_comeintime_totaltime,
  257. "es_publishtime_totaltime": es_publishtime_totaltime,
  258. "es_comeintime_avgtime": es_comeintime_avgtime,
  259. "es_publishtime_avgtime": es_publishtime_avgtime,
  260. "file_avgltime": file_avgltime,
  261. "no_file_avgltime": no_file_avgltime,
  262. "file_totaltime": file_totaltime,
  263. "no_file_totaltime": no_file_totaltime,
  264. "file_count": fileCount,
  265. "no_file_count": noFileCount,
  266. "pc_diff1": pc_diff1,
  267. "pc_diff3": pc_diff3,
  268. "pc_diff5": pc_diff5,
  269. "pc_diff10": pc_diff10,
  270. "pc_diff15": pc_diff15,
  271. "pc_diff30": pc_diff30,
  272. "pc_diff301": pc_diff301,
  273. "timeCount": timeCount,
  274. }
  275. fmgo.Save("bidding_ribao", insert)
  276. }
  277. t.SendMail(report)
  278. //t.SendMail("【竟品统计结果】")
  279. //fmt.Println(report)
  280. }
  281. log.Println("task over:", t.Name, eq, count)
  282. }
  283. func (t *T) SendMail(report string) {
  284. url := fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, to, t.Name, report)
  285. fmt.Println("url=>", url)
  286. res, err := http.Get(url)
  287. if err != nil {
  288. fmt.Println("SendMail err ", err)
  289. } else {
  290. fmt.Println("SendMail res ", res)
  291. }
  292. }