main.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/robfig/cron/v3"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "go.uber.org/zap"
  7. utils "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  8. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  10. "time"
  11. )
  12. var (
  13. MgoB *mongodb.MongodbSim
  14. MgoC *mongodb.MongodbSim
  15. Rest = make(map[string]interface{}, 0) //存储配置 栏目
  16. //// 更新mongo
  17. //updatePool = make(chan []map[string]interface{}, 5000)
  18. //千里马对应的招标 channel
  19. columns = []string{"招标公告", "重新招标", "意见征集", "招标预告", "信息变更", "答疑公告", "废标公告", "流标公告",
  20. "开标公示", "候选人公示", "中标通知", "合同公告", "验收合同", "违规公告", "其他公告"}
  21. )
  22. func main() {
  23. local, _ := time.LoadLocation("Asia/Shanghai")
  24. c := cron.New(cron.WithLocation(local), cron.WithSeconds())
  25. _, err := c.AddFunc(GF.Cron.Spec, getIndicators)
  26. if err != nil {
  27. log.Error("main", zap.Error(err))
  28. }
  29. log.Info("main", zap.String("spec", GF.Cron.Spec))
  30. c.Start()
  31. defer c.Stop()
  32. select {}
  33. }
  34. //获取数据指标数据
  35. func getIndicators() {
  36. // 获取昨天零点和今天零点的时间戳
  37. now := time.Now()
  38. yesterday := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
  39. today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  40. //1.数据日采集量
  41. whereBidding := map[string]interface{}{
  42. "comeintime": map[string]interface{}{
  43. "$gt": yesterday.Unix(),
  44. "$lte": today.Unix(),
  45. },
  46. }
  47. biddingCount := MgoB.Count("bidding", whereBidding)
  48. if biddingCount == 0 {
  49. SendMail("数据昨日采为0", "请检查相关流程")
  50. }
  51. Rest["date"] = yesterday.Format("2006-01-02")
  52. Rest["数据日采集量"] = biddingCount
  53. log.Info("getIndicators", zap.Int("数据日采集量", biddingCount))
  54. //2. 统计爬虫总量
  55. whereT := map[string]interface{}{
  56. "state": map[string]interface{}{
  57. "$ne": []interface{}{4, 10},
  58. },
  59. }
  60. collectAll := MgoC.Count("luaconfig", whereT)
  61. Rest["爬虫总量"] = collectAll
  62. log.Info("getIndicators", zap.Int("爬虫总量", collectAll))
  63. //3. 爬虫异常数量
  64. whereCollectErr := map[string]interface{}{
  65. "l_comeintime": map[string]interface{}{
  66. "$gt": yesterday.Unix(),
  67. "$lte": today.Unix(),
  68. },
  69. }
  70. collectErrCount := MgoC.Count("task", whereCollectErr)
  71. Rest["爬虫日异常量"] = collectErrCount
  72. errPercentage := (float64(collectErrCount) / float64(collectAll)) * 100.0
  73. Rest["爬虫日异常量比例"] = fmt.Sprintf("%.2f%%", errPercentage)
  74. log.Info("getIndicators", zap.Int("爬虫日异常量", collectErrCount))
  75. //4.爬虫上架时效(小时)
  76. // 获取星期几
  77. dayOfWeek := now.Weekday()
  78. // 判断是否为周一,每周日 统计一次 上周 周一到周日 爬虫上架时效
  79. if dayOfWeek == time.Sunday {
  80. lastSunday := time.Date(now.Year(), now.Month(), now.Day()-13, 0, 0, 0, 0, time.Local)
  81. lastMonday := time.Date(now.Year(), now.Month(), now.Day()-6, 0, 0, 0, 0, time.Local)
  82. //fmt.Println(lastMonday)
  83. whereShelves := map[string]interface{}{
  84. "comeintime": map[string]interface{}{
  85. "$gt": lastSunday.Unix(),
  86. "$lte": lastMonday.Unix(),
  87. },
  88. }
  89. shelves, _ := MgoC.Find("luaconfig", whereShelves, nil, map[string]interface{}{"code": 1, "comeintime": 1}, false, -1, -1)
  90. if len(*shelves) > 0 {
  91. shelvesCount := int64(0)
  92. shelvesTime := int64(0)
  93. for _, v := range *shelves {
  94. code := utils.ObjToString(v["code"])
  95. shelveNew, _ := MgoC.FindOne("lua_logs_auditor_new", map[string]interface{}{"code": code, "types": "审核"})
  96. fmt.Println(shelveNew)
  97. if shelveNew == nil {
  98. continue
  99. } else {
  100. shelvesCount++
  101. comeintimeNew := utils.Int64All((*shelveNew)["comeintime"])
  102. comeintime := utils.Int64All(v["comeintime"])
  103. shelvesTime = shelvesTime + comeintimeNew - comeintime
  104. }
  105. }
  106. Rest["爬虫上架时效"] = (shelvesTime / shelvesCount) / 3600
  107. log.Info("getIndicators", zap.Any("爬虫上架时效", (shelvesTime/shelvesCount)/3600))
  108. }
  109. }
  110. //5.竞品覆盖率,每周4统计上周的数据
  111. sessC := MgoC.GetMgoConn()
  112. defer MgoC.DestoryMongoConn(sessC)
  113. if dayOfWeek == time.Thursday {
  114. //获取上周四,千里马的招标数据;然后获取标讯前后个3天,一共7天的所有数据,对比看标题或者项目名称是否存在
  115. lastWednesday := time.Date(now.Year(), now.Month(), now.Day()-8, 0, 0, 0, 0, time.Local)
  116. //lastThursday := time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local)
  117. whereQlm := map[string]interface{}{
  118. "publishtime": lastWednesday.Format("2006-01-02"),
  119. "site": "千里马",
  120. }
  121. query := sessC.DB("qlm").C("data_merge").Find(whereQlm).Select(map[string]interface{}{"title": 1, "projectname": 1}).Iter()
  122. count := 0
  123. qlmDatas := make([]map[string]interface{}, 0)
  124. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  125. data := map[string]interface{}{
  126. "title": tmp["title"],
  127. "projectname": tmp["projectname"],
  128. }
  129. qlmDatas = append(qlmDatas, data)
  130. }
  131. log.Info("getIndicators", zap.Int("千里马上周三总数", count))
  132. biddingWhere := map[string]interface{}{
  133. "publishtime": map[string]interface{}{
  134. "$gt": lastWednesday.AddDate(0, 0, -3).Unix(),
  135. "$lte": lastWednesday.AddDate(0, 0, 4).Unix(),
  136. },
  137. }
  138. biddingDatas, _ := MgoB.Find("bidding", biddingWhere, nil, map[string]interface{}{"title": 1, "projectname": 1}, false, -1, -1)
  139. log.Info("getIndicators", zap.Int("标讯一周总数", len(*biddingDatas)))
  140. // 将切片B中的标题和项目名称分别存储在哈希表中
  141. titlesInB, projectsInB := getUniqueFields(*biddingDatas)
  142. matchs := countMatches(qlmDatas, titlesInB, projectsInB)
  143. Rest["竞品覆盖率-详情"] = map[string]interface{}{
  144. "date": lastWednesday.Format("2006-01-02"),
  145. "count": count,
  146. "matchs": matchs,
  147. }
  148. Rest["竞品覆盖率"] = fmt.Sprintf("%.2f%%", float64(matchs)/float64(count)*100)
  149. log.Info("getIndicators", zap.String("竞品覆盖率", fmt.Sprintf("%.2f%%", float64(matchs)/float64(count)*100)))
  150. }
  151. //6.数据整体流程均耗时(分钟)
  152. sessB := MgoB.GetMgoConn()
  153. defer MgoB.DestoryMongoConn(sessB)
  154. fd := bson.M{"extracttype": 1, "sensitive": 1, "dataging": 1, "site": 1, "infoformat": 1, "comeintime": 1, "pici": 1, "publishtime": 1, "competehref": 1, "attach_text": 1}
  155. query := sessB.DB("qfw").C("bidding").Find(whereBidding).Select(fd).Iter()
  156. biddingRealCount := 0
  157. comein_publish_totaltime := int64(0) //comeintime 和 生索引 publish 时间 差值的总和
  158. pici_comein_totaltime := int64(0) //publishtime 和 生索引 pici 时间 差值的总和
  159. for tmp := make(map[string]interface{}); query.Next(tmp); {
  160. if utils.IntAll(tmp["extracttype"]) != -1 && utils.ObjToString(tmp["sensitive"]) != "测试" && utils.IntAll(tmp["dataging"]) != 1 && utils.Float64All(tmp["infoformat"]) != 3 {
  161. comeintime := utils.Int64All(tmp["comeintime"])
  162. publishtime := utils.Int64All(tmp["publishtime"])
  163. pici := utils.Int64All(tmp["pici"])
  164. if (comeintime-publishtime) < 12*60*60 && pici > 0 {
  165. biddingRealCount++
  166. diff1 := comeintime - publishtime
  167. diff2 := pici - comeintime
  168. comein_publish_totaltime += diff1
  169. pici_comein_totaltime += diff2
  170. }
  171. }
  172. }
  173. if biddingRealCount > 0 {
  174. comein_publish_avgtime := comein_publish_totaltime / int64(biddingRealCount)
  175. pici_comein_avgtime := pici_comein_totaltime / int64(biddingRealCount)
  176. Rest["数据整体流程均耗时(分钟)"] = comein_publish_avgtime / 60
  177. Rest["数据处理均耗时(分钟)"] = pici_comein_avgtime / 60
  178. log.Info("getIndicators", zap.Any("数据整体流程均耗时(分钟)", comein_publish_avgtime/60))
  179. log.Info("getIndicators", zap.Any("数据处理均耗时(分钟)", pici_comein_avgtime/60))
  180. }
  181. //7.数据行质量合格率,暂时写死
  182. Rest["数据行质量合格率"] = "90%"
  183. MgoB.Save("bidding_zhibiao", Rest)
  184. }