main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/gclient"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/robfig/cron/v3"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "go.uber.org/zap"
  11. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  13. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. var (
  19. MgoB *mongodb.MongodbSim
  20. MgoP *mongodb.MongodbSim
  21. columns = make([]map[string]interface{}, 0) //存储配置 栏目
  22. // 更新mongo
  23. updatePool = make(chan []map[string]interface{}, 5000)
  24. )
  25. func main() {
  26. go updateMethod()
  27. local, _ := time.LoadLocation("Asia/Shanghai")
  28. c := cron.New(cron.WithLocation(local), cron.WithSeconds())
  29. eid, err := c.AddFunc(GF.Cron.Spec, dealData)
  30. if err != nil {
  31. log.Info("main", zap.Any("AddFunc err", err))
  32. }
  33. log.Info("main", zap.Any("eid", eid))
  34. c.Start()
  35. defer c.Stop()
  36. //
  37. select {}
  38. }
  39. func dealData() {
  40. go dealBidding()
  41. go dealProject()
  42. }
  43. // dealBidding 处理标讯数据
  44. func dealBidding() {
  45. sess := MgoB.GetMgoConn()
  46. defer MgoB.DestoryMongoConn(sess)
  47. var q interface{}
  48. var startTime = GF.Cron.Start
  49. now := time.Now()
  50. ctx, _ := context.WithTimeout(context.Background(), 99999*time.Hour)
  51. coll := sess.M.C.Database(GF.MongoB.DB).Collection(GF.MongoB.Coll)
  52. find := options.Find().SetBatchSize(1000).SetSort(bson.D{bson.E{"comeintime", 1}}).SetProjection(bson.M{"comeintime": 1, "toptype": 1, "subtype": 1, "buyerclass": 1, "title": 1, "detail": 1, "package": 1, "funds": 1, "spidercode": 1, "area": 1, "city": 1, "publishtime": 1})
  53. if startTime != 0 && GF.Cron.End != 0 {
  54. q = map[string]interface{}{
  55. "comeintime": map[string]interface{}{
  56. "$gt": GF.Cron.Start,
  57. "$lte": GF.Cron.End,
  58. },
  59. }
  60. } else if startTime != 0 {
  61. q = map[string]interface{}{
  62. "comeintime": map[string]interface{}{
  63. "$gt": startTime,
  64. },
  65. }
  66. } else if startTime == 0 && GF.Cron.End == 0 {
  67. //默认 取大于 昨天的数据
  68. q = map[string]interface{}{
  69. "comeintime": map[string]interface{}{
  70. "$gt": time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, now.Location()).Unix(),
  71. },
  72. }
  73. }
  74. cur, err := coll.Find(ctx, q, find)
  75. if err != nil {
  76. log.Error("dealBidding,coll.Find ", zap.Error(err))
  77. }
  78. log.Info("dealBidding", zap.Any("q", q))
  79. //query := sess.DB(GF.MongoB.DB).C(GF.MongoB.Coll).Find(q).Select(map[string]interface{}{
  80. // "contenthtml": 0}).Iter()
  81. count := 0
  82. ch := make(chan bool, 15)
  83. wg := &sync.WaitGroup{}
  84. //for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  85. for tmp := make(map[string]interface{}); cur.Next(ctx); count++ {
  86. if cur != nil {
  87. cur.Decode(&tmp)
  88. }
  89. startTime = util.IntAll(tmp["comeintime"])
  90. if count%1000 == 0 {
  91. log.Info("dealBidding", zap.Int("current", count), zap.Any("comeintime", tmp["comeintime"]))
  92. }
  93. ch <- true
  94. wg.Add(1)
  95. go func(tmp map[string]interface{}) {
  96. defer func() {
  97. <-ch
  98. wg.Done()
  99. }()
  100. rea := TagBidding(tmp)
  101. if len(rea) > 0 {
  102. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  103. //update := map[string]interface{}{
  104. // "nav_column": reb,
  105. //}
  106. //where := map[string]interface{}{
  107. // "_id": tmp["_id"],
  108. //}
  109. updatePool <- []map[string]interface{}{
  110. {"_id": tmp["_id"]},
  111. {"$set": bson.M{
  112. "nav_column": reb,
  113. }},
  114. }
  115. // 改动栏目的数据,存储临时表;刷存量数据使用
  116. //if util.IntAll(rea["项目分包"]) == 1 || util.IntAll(rea["省级项目"]) == 1 {
  117. // insert := map[string]interface{}{
  118. // "bid_id": mongodb.BsonIdToSId(tmp["_id"]),
  119. // "area": tmp["area"],
  120. // "city": tmp["city"],
  121. // "publishtime": time.Unix(util.Int64All(tmp["publishtime"]), 0).Format("2006-01-02 15:04:05"),
  122. // "nav_column": reb,
  123. // }
  124. // MgoB.InsertOrUpdate(MgoB.DbName, "wcc_column_tmp", insert)
  125. //}
  126. //MgoB.Update(GF.MongoB.Coll, where, map[string]interface{}{"$set": update}, true, false)
  127. }
  128. }(tmp)
  129. tmp = map[string]interface{}{}
  130. }
  131. wg.Wait()
  132. log.Info("dealBidding", zap.Int("over ", count))
  133. //没有数据时,发送邮件
  134. if count == 0 {
  135. SendMail("每日数据监控", "查询数据为空,请处理")
  136. }
  137. //处理热门标讯数据/热门项目
  138. getHot()
  139. //dealHotDataAll()
  140. log.Info("dealBidding", zap.Int("over ", count))
  141. }
  142. // getHot 获取热门数据
  143. func getHot() {
  144. var existsMap = make(map[string]bool) //bidding_hots 已经存在的ID
  145. //获取已有热门数据
  146. hots, _ := MgoB.Find("bidding_hots", nil, nil, map[string]interface{}{"bidding_id": 1}, false, -1, -1)
  147. if len(*hots) > 0 {
  148. for _, v := range *hots {
  149. existsMap[util.ObjToString(v["bidding_id"])] = true
  150. }
  151. }
  152. var hotMap = make(map[string]int)
  153. //1.获取昨日数据
  154. file := gtime.Now().AddDate(0, 0, -1).Format("Y-m-d")
  155. res := gclient.New().GetContent(context.Background(), "http://172.17.145.164:18880/jyartvisit/"+file+".res")
  156. arrs := strings.Split(res, "\n")
  157. log.Info("getHot", zap.Int("res", len(arrs)))
  158. for _, va := range arrs {
  159. vs := strings.Split(va, " ")
  160. if len(vs) == 2 {
  161. insert := map[string]interface{}{
  162. "bidding_id": vs[0],
  163. "num": g.NewVar(vs[1]).Int(),
  164. "date": file,
  165. }
  166. err := MgoB.InsertOrUpdate(GF.MongoB.DB, "bidding_hot_data", insert)
  167. if err != nil {
  168. log.Error("getHot", zap.Error(err))
  169. }
  170. }
  171. }
  172. //2. 汇总15天内,符合条件热门数据
  173. where := map[string]interface{}{
  174. "date": map[string]interface{}{
  175. "$gte": gtime.Now().AddDate(0, 0, -GF.Cron.HotDay).Format("Y-m-d"),
  176. },
  177. }
  178. hotData, _ := MgoB.Find("bidding_hot_data", where, nil, nil, false, -1, -1)
  179. for _, data := range *hotData {
  180. biddingID := util.ObjToString(data["bidding_id"])
  181. // 将字符串转换为 ObjectId
  182. objectID := mongodb.StringTOBsonId(biddingID)
  183. // 从 ObjectId 中提取时间戳
  184. timestamp := objectID.Timestamp()
  185. // 判断时间是否在最近一年内
  186. oneYearAgo := time.Now().AddDate(-1, 0, 0)
  187. isWithinOneYear := timestamp.After(oneYearAgo)
  188. if !isWithinOneYear {
  189. continue
  190. }
  191. num := util.IntAll(data["num"])
  192. if da, ok := hotMap[biddingID]; ok {
  193. hotMap[biddingID] = num + da
  194. } else {
  195. hotMap[biddingID] = num
  196. }
  197. }
  198. newCount := 0
  199. for k, v := range hotMap {
  200. //之前存在过的数据,不再入 bidding_hots
  201. if existsMap[k] {
  202. continue
  203. }
  204. if v > GF.Cron.HotNum {
  205. bidding, _ := MgoB.FindById("bidding", k, nil)
  206. biddingData := *bidding
  207. if biddingData == nil {
  208. continue
  209. }
  210. toptype := util.ObjToString(biddingData["toptype"])
  211. //拟建和采购意向数据过滤
  212. if toptype == "拟建" || toptype == "采购意向" {
  213. continue
  214. }
  215. newCount++
  216. insert := map[string]interface{}{
  217. "bidding_id": k,
  218. "num": v,
  219. "createtime": time.Now().Unix(),
  220. "date": time.Now().Format("2006-01-02"),
  221. }
  222. err := MgoB.InsertOrUpdate(GF.MongoB.DB, "bidding_hots", insert)
  223. if err != nil {
  224. log.Error("getHot", zap.Error(err))
  225. }
  226. biddingData["hot_data"] = 1
  227. rea := TagBidding(biddingData)
  228. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  229. updatePool <- []map[string]interface{}{
  230. {"_id": mongodb.StringTOBsonId(k)},
  231. {"$set": bson.M{
  232. "nav_column": reb,
  233. }},
  234. }
  235. }
  236. }
  237. log.Info("getHot", zap.Int("over", newCount))
  238. }
  239. // dealProject 处理拟建项目数据标签
  240. func dealProject() {
  241. sess := MgoP.GetMgoConn()
  242. defer MgoP.DestoryMongoConn(sess)
  243. // 指定对应的时间格式
  244. //layout := "2006-01-02 15:04:05"
  245. // 获取当前时间
  246. now := time.Now()
  247. var q interface{}
  248. var startTime = GF.Cron.Start
  249. if startTime != 0 && GF.Cron.End != 0 {
  250. q = map[string]interface{}{
  251. "pici": map[string]interface{}{
  252. "$gt": GF.Cron.Start,
  253. "$lte": GF.Cron.End,
  254. },
  255. }
  256. } else if startTime != 0 {
  257. q = map[string]interface{}{
  258. "pici": map[string]interface{}{
  259. "$gt": startTime,
  260. },
  261. }
  262. } else if startTime == 0 && GF.Cron.End == 0 {
  263. //默认 取大于 昨天的数据
  264. q = map[string]interface{}{
  265. "pici": map[string]interface{}{
  266. "$gt": time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, now.Location()).Unix(),
  267. },
  268. }
  269. }
  270. log.Info("dealProject", zap.Any("q", q))
  271. query := sess.DB(GF.MongoP.DB).C(GF.MongoP.Coll).Find(q).Select(nil).Iter()
  272. count := 0
  273. ch := make(chan bool, 15)
  274. wg := &sync.WaitGroup{}
  275. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  276. if count%1000 == 0 {
  277. log.Info("dealProject", zap.Int("current", count))
  278. }
  279. ch <- true
  280. wg.Add(1)
  281. go func(tmp map[string]interface{}) {
  282. defer func() {
  283. <-ch
  284. wg.Done()
  285. }()
  286. rea := TagProject(tmp)
  287. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  288. update := map[string]interface{}{
  289. "nav_column": reb,
  290. }
  291. where := map[string]interface{}{
  292. "_id": tmp["_id"],
  293. }
  294. MgoP.Update(GF.MongoP.Coll, where, map[string]interface{}{"$set": update}, true, false)
  295. }(tmp)
  296. tmp = map[string]interface{}{}
  297. }
  298. wg.Wait()
  299. log.Info("dealProject", zap.Int("over ", count))
  300. //没有数据时,发送邮件
  301. if count == 0 {
  302. SendMail("网站导航 数据标签", "查询 拟建项目数据为空,请查收")
  303. }
  304. }
  305. // updateMethod 更新MongoDB
  306. func updateMethod() {
  307. updateSp := make(chan bool, 8)
  308. arru := make([][]map[string]interface{}, 200)
  309. indexu := 0
  310. for {
  311. select {
  312. case v := <-updatePool:
  313. arru[indexu] = v
  314. indexu++
  315. if indexu == 200 {
  316. updateSp <- true
  317. go func(arru [][]map[string]interface{}) {
  318. defer func() {
  319. <-updateSp
  320. }()
  321. MgoB.UpdateBulk(GF.MongoB.Coll, arru...)
  322. }(arru)
  323. arru = make([][]map[string]interface{}, 200)
  324. indexu = 0
  325. }
  326. case <-time.After(1000 * time.Millisecond):
  327. if indexu > 0 {
  328. updateSp <- true
  329. go func(arru [][]map[string]interface{}) {
  330. defer func() {
  331. <-updateSp
  332. }()
  333. MgoB.UpdateBulk(GF.MongoB.Coll, arru...)
  334. }(arru[:indexu])
  335. arru = make([][]map[string]interface{}, 200)
  336. indexu = 0
  337. }
  338. }
  339. }
  340. }
  341. // dealHotDataAll 处理存量热门数据标签
  342. func dealHotDataAll() {
  343. hotData, _ := MgoB.Find("bidding_hots", nil, nil, nil, false, -1, -1)
  344. for _, data := range *hotData {
  345. biddingID := util.ObjToString(data["bidding_id"])
  346. bidding, _ := MgoB.FindById("bidding", biddingID, nil)
  347. biddingData := *bidding
  348. if biddingData == nil {
  349. continue
  350. }
  351. biddingData["hot_data"] = 1
  352. rea := TagBidding(biddingData)
  353. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  354. updatePool <- []map[string]interface{}{
  355. {"_id": mongodb.StringTOBsonId(biddingID)},
  356. {"$set": bson.M{
  357. "nav_column": reb,
  358. }},
  359. }
  360. }
  361. log.Info("dealHotDataAll", zap.Int("over", len(*hotData)))
  362. }