main.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. newCount++
  211. insert := map[string]interface{}{
  212. "bidding_id": k,
  213. "num": v,
  214. "createtime": time.Now().Unix(),
  215. "date": time.Now().Format("2006-01-02"),
  216. }
  217. err := MgoB.InsertOrUpdate(GF.MongoB.DB, "bidding_hots", insert)
  218. if err != nil {
  219. log.Error("getHot", zap.Error(err))
  220. }
  221. biddingData["hot_data"] = 1
  222. rea := TagBidding(biddingData)
  223. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  224. updatePool <- []map[string]interface{}{
  225. {"_id": mongodb.StringTOBsonId(k)},
  226. {"$set": bson.M{
  227. "nav_column": reb,
  228. }},
  229. }
  230. }
  231. }
  232. log.Info("getHot", zap.Int("over", newCount))
  233. }
  234. // dealProject 处理拟建项目数据标签
  235. func dealProject() {
  236. sess := MgoP.GetMgoConn()
  237. defer MgoP.DestoryMongoConn(sess)
  238. // 指定对应的时间格式
  239. //layout := "2006-01-02 15:04:05"
  240. // 获取当前时间
  241. now := time.Now()
  242. var q interface{}
  243. var startTime = GF.Cron.Start
  244. if startTime != 0 && GF.Cron.End != 0 {
  245. q = map[string]interface{}{
  246. "pici": map[string]interface{}{
  247. "$gt": GF.Cron.Start,
  248. "$lte": GF.Cron.End,
  249. },
  250. }
  251. } else if startTime != 0 {
  252. q = map[string]interface{}{
  253. "pici": map[string]interface{}{
  254. "$gt": startTime,
  255. },
  256. }
  257. } else if startTime == 0 && GF.Cron.End == 0 {
  258. //默认 取大于 昨天的数据
  259. q = map[string]interface{}{
  260. "pici": map[string]interface{}{
  261. "$gt": time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, now.Location()).Unix(),
  262. },
  263. }
  264. }
  265. log.Info("dealProject", zap.Any("q", q))
  266. query := sess.DB(GF.MongoP.DB).C(GF.MongoP.Coll).Find(q).Select(map[string]interface{}{
  267. "ids": 0, "list": 0}).Iter()
  268. count := 0
  269. ch := make(chan bool, 15)
  270. wg := &sync.WaitGroup{}
  271. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  272. if count%1000 == 0 {
  273. log.Info("dealProject", zap.Int("current", count))
  274. }
  275. ch <- true
  276. wg.Add(1)
  277. go func(tmp map[string]interface{}) {
  278. defer func() {
  279. <-ch
  280. wg.Done()
  281. }()
  282. rea := TagProject(tmp)
  283. if len(rea) > 0 {
  284. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  285. update := map[string]interface{}{
  286. "nav_column": reb,
  287. }
  288. where := map[string]interface{}{
  289. "_id": tmp["_id"],
  290. }
  291. MgoP.Update(GF.MongoP.Coll, where, map[string]interface{}{"$set": update}, true, false)
  292. }
  293. }(tmp)
  294. tmp = map[string]interface{}{}
  295. }
  296. wg.Wait()
  297. log.Info("dealProject", zap.Int("over ", count))
  298. //没有数据时,发送邮件
  299. if count == 0 {
  300. SendMail("网站导航 数据标签", "查询 拟建项目数据为空,请查收")
  301. }
  302. }
  303. // updateMethod 更新MongoDB
  304. func updateMethod() {
  305. updateSp := make(chan bool, 8)
  306. arru := make([][]map[string]interface{}, 200)
  307. indexu := 0
  308. for {
  309. select {
  310. case v := <-updatePool:
  311. arru[indexu] = v
  312. indexu++
  313. if indexu == 200 {
  314. updateSp <- true
  315. go func(arru [][]map[string]interface{}) {
  316. defer func() {
  317. <-updateSp
  318. }()
  319. MgoB.UpdateBulk(GF.MongoB.Coll, arru...)
  320. }(arru)
  321. arru = make([][]map[string]interface{}, 200)
  322. indexu = 0
  323. }
  324. case <-time.After(1000 * time.Millisecond):
  325. if indexu > 0 {
  326. updateSp <- true
  327. go func(arru [][]map[string]interface{}) {
  328. defer func() {
  329. <-updateSp
  330. }()
  331. MgoB.UpdateBulk(GF.MongoB.Coll, arru...)
  332. }(arru[:indexu])
  333. arru = make([][]map[string]interface{}, 200)
  334. indexu = 0
  335. }
  336. }
  337. }
  338. }
  339. // dealHotDataAll 处理存量热门数据标签
  340. func dealHotDataAll() {
  341. hotData, _ := MgoB.Find("bidding_hots", nil, nil, nil, false, -1, -1)
  342. for _, data := range *hotData {
  343. biddingID := util.ObjToString(data["bidding_id"])
  344. bidding, _ := MgoB.FindById("bidding", biddingID, nil)
  345. biddingData := *bidding
  346. if biddingData == nil {
  347. continue
  348. }
  349. biddingData["hot_data"] = 1
  350. rea := TagBidding(biddingData)
  351. reb := calculateFlag(rea, columns) //拿到十进制数字,标签栏目结果
  352. updatePool <- []map[string]interface{}{
  353. {"_id": mongodb.StringTOBsonId(biddingID)},
  354. {"$set": bson.M{
  355. "nav_column": reb,
  356. }},
  357. }
  358. }
  359. log.Info("dealHotDataAll", zap.Int("over", len(*hotData)))
  360. }