main.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. package main
  2. /**
  3. 招标信息判重
  4. **/
  5. import (
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "github.com/cron"
  10. "log"
  11. mu "mfw/util"
  12. "net"
  13. "os"
  14. "qfw/util"
  15. "regexp"
  16. "strings"
  17. "sync"
  18. "time"
  19. "gopkg.in/mgo.v2/bson"
  20. )
  21. var (
  22. Sysconfig map[string]interface{} //配置文件
  23. mconf map[string]interface{} //mongodb配置信息
  24. mgo *MongodbSim //mongodb操作对象
  25. extract string
  26. extract_back string
  27. udpclient mu.UdpClient //udp对象
  28. nextNode []map[string]interface{} //下节点数组
  29. dupdays = 5 //初始化判重范围
  30. DM *datamap //
  31. //正则筛选相关
  32. FilterRegTitle = regexp.MustCompile("^_$")
  33. FilterRegTitle_0 = regexp.MustCompile("^_$")
  34. FilterRegTitle_1 = regexp.MustCompile("^_$")
  35. FilterRegTitle_2 = regexp.MustCompile("^_$")
  36. isMerger bool //是否合并
  37. Is_Sort bool //是否排序
  38. threadNum int //线程数量
  39. SiteMap map[string]map[string]interface{} //站点map
  40. LowHeavy bool //低质量数据判重
  41. TimingTask bool //是否定时任务
  42. timingSpanDay int64 //时间跨度
  43. timingPubScope int64 //发布时间周期
  44. sid,eid,lastid string //测试人员判重使用
  45. IdType bool //默认object类型
  46. )
  47. func init() {
  48. flag.StringVar(&lastid, "id", "", "最后加载id") //以小于等于此id开始加载最近几天的数据
  49. flag.StringVar(&sid, "sid", "", "开始id")
  50. flag.StringVar(&eid, "eid", "", "结束id")
  51. flag.Parse()
  52. //172.17.145.163:27080
  53. util.ReadConfig(&Sysconfig)
  54. nextNode = util.ObjArrToMapArr(Sysconfig["nextNode"].([]interface{}))
  55. mconf = Sysconfig["mongodb"].(map[string]interface{})
  56. mgo = &MongodbSim{
  57. MongodbAddr: mconf["addr"].(string),
  58. DbName: mconf["db"].(string),
  59. Size: util.IntAllDef(mconf["pool"], 10),
  60. }
  61. mgo.InitPool()
  62. extract = mconf["extract"].(string)
  63. extract_back = mconf["extract_back"].(string)
  64. dupdays = util.IntAllDef(Sysconfig["dupdays"], 3)
  65. //加载数据
  66. DM = NewDatamap(dupdays, lastid)
  67. FilterRegTitle = regexp.MustCompile(util.ObjToString(Sysconfig["specialwords"]))
  68. FilterRegTitle_0 = regexp.MustCompile(util.ObjToString(Sysconfig["specialtitle_0"]))
  69. FilterRegTitle_1 = regexp.MustCompile(util.ObjToString(Sysconfig["specialtitle_1"]))
  70. FilterRegTitle_2 = regexp.MustCompile(util.ObjToString(Sysconfig["specialtitle_2"]))
  71. isMerger = Sysconfig["isMerger"].(bool)
  72. Is_Sort = Sysconfig["isSort"].(bool)
  73. threadNum = util.IntAllDef(Sysconfig["threads"], 1)
  74. LowHeavy = Sysconfig["lowHeavy"].(bool)
  75. TimingTask = Sysconfig["timingTask"].(bool)
  76. timingSpanDay = util.Int64All(Sysconfig["timingSpanDay"])
  77. timingPubScope = util.Int64All(Sysconfig["timingPubScope"])
  78. //站点配置
  79. site := mconf["site"].(map[string]interface{})
  80. SiteMap = make(map[string]map[string]interface{}, 0)
  81. start := int(time.Now().Unix())
  82. sess_site := mgo.GetMgoConn()
  83. defer mgo.DestoryMongoConn(sess_site)
  84. res_site := sess_site.DB(site["dbname"].(string)).C(site["coll"].(string)).Find(map[string]interface{}{}).Sort("_id").Iter()
  85. for site_dict := make(map[string]interface{}); res_site.Next(&site_dict); {
  86. data_map := map[string]interface{}{
  87. "area": util.ObjToString(site_dict["area"]),
  88. "city": util.ObjToString(site_dict["city"]),
  89. "district": util.ObjToString(site_dict["district"]),
  90. "sitetype": util.ObjToString(site_dict["sitetype"]),
  91. "level": util.ObjToString(site_dict["level"]),
  92. "weight": util.ObjToString(site_dict["weight"]),
  93. }
  94. SiteMap[util.ObjToString(site_dict["site"])] = data_map
  95. }
  96. log.Printf("new站点加载用时:%d秒,%d个\n", int(time.Now().Unix())-start, len(SiteMap))
  97. }
  98. func main() {
  99. go checkMapJob()
  100. updport := Sysconfig["udpport"].(string)
  101. udpclient = mu.UdpClient{Local: updport, BufSize: 1024}
  102. udpclient.Listen(processUdpMsg)
  103. log.Println("Udp服务监听", updport)
  104. if TimingTask {
  105. go timedTaskDay()
  106. }
  107. time.Sleep(99999 * time.Hour)
  108. }
  109. //测试组人员使用
  110. func mainT() {
  111. if TimingTask {
  112. log.Println("定时任务测试开始")
  113. go timedTaskDay()
  114. time.Sleep(99999 * time.Hour)
  115. } else {
  116. //2019年8月1日-8月17日 712646
  117. /*
  118. sid = "5d55031fa5cb26b9b7f57570"
  119. eid = "5e8c02b150b5ea296eed4509"
  120. 5e933b1a50b5ea296ef0e839
  121. */
  122. //IdType = true
  123. sid = "5ee1d3d59e628c599167adf1"
  124. eid = "5eea4291801f744d045c3169"
  125. log.Println("正常判重测试开始")
  126. log.Println(sid, "---", eid)
  127. mapinfo := map[string]interface{}{}
  128. if sid == "" || eid == "" {
  129. log.Println("sid,eid参数不能为空")
  130. os.Exit(0)
  131. }
  132. mapinfo["gtid"] = sid
  133. mapinfo["lteid"] = eid
  134. mapinfo["stop"] = "true"
  135. task([]byte{}, mapinfo)
  136. time.Sleep(99999 * time.Hour)
  137. }
  138. }
  139. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  140. fmt.Println("接受的段数据")
  141. switch act {
  142. case mu.OP_TYPE_DATA: //上个节点的数据
  143. //从表中开始处理
  144. var mapInfo map[string]interface{}
  145. err := json.Unmarshal(data, &mapInfo)
  146. log.Println("err:", err, "mapInfo:", mapInfo)
  147. if err != nil {
  148. udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  149. } else if mapInfo != nil {
  150. taskType := util.ObjToString(mapInfo["stype"])
  151. if taskType == "normalTask" {
  152. //判重流程
  153. go task(data, mapInfo)
  154. } else {
  155. //其他
  156. go task(data, mapInfo)
  157. }
  158. key, _ := mapInfo["key"].(string)
  159. if key == "" {
  160. key = "udpok"
  161. }
  162. udpclient.WriteUdp([]byte(key), mu.OP_NOOP, ra)
  163. }
  164. case mu.OP_NOOP: //下个节点回应
  165. ok := string(data)
  166. if ok != "" {
  167. log.Println("ok:", ok)
  168. udptaskmap.Delete(ok)
  169. }
  170. }
  171. }
  172. //开始判重程序
  173. func task(data []byte, mapInfo map[string]interface{}) {
  174. log.Println("开始数据判重")
  175. defer util.Catch()
  176. //区间id
  177. q := map[string]interface{}{
  178. "_id": map[string]interface{}{
  179. "$gt": StringTOBsonId(mapInfo["gtid"].(string)),
  180. "$lte": StringTOBsonId(mapInfo["lteid"].(string)),
  181. },
  182. }
  183. if IdType {
  184. q = map[string]interface{}{
  185. "_id": map[string]interface{}{
  186. "$gt": mapInfo["gtid"].(string),
  187. "$lte": mapInfo["lteid"].(string),
  188. },
  189. }
  190. }
  191. log.Println(mgo.DbName, extract, q)
  192. sess := mgo.GetMgoConn()
  193. defer mgo.DestoryMongoConn(sess)
  194. //是否排序
  195. sortName :="_id"
  196. if Is_Sort {
  197. sortName = "publishtime"
  198. log.Println("排序")
  199. }
  200. it := sess.DB(mgo.DbName).C(extract).Find(&q).Sort(sortName).Iter()
  201. updateExtract := [][]map[string]interface{}{}
  202. ids:=[]string{}
  203. log.Println("线程数:", threadNum)
  204. pool := make(chan bool, threadNum)
  205. wg := &sync.WaitGroup{}
  206. n, repeateN := 0, 0
  207. for tmp := make(map[string]interface{}); it.Next(&tmp); n++ {
  208. if n%10000 == 0 {
  209. log.Println("current:", n, tmp["_id"], "repeateN:", repeateN)
  210. }
  211. source := util.ObjToMap(tmp["jsondata"])
  212. if util.IntAll((*source)["sourcewebsite"]) == 1 {
  213. repeateN++
  214. updateExtract = append(updateExtract, []map[string]interface{}{
  215. map[string]interface{}{
  216. "_id": tmp["_id"],
  217. },
  218. map[string]interface{}{
  219. "$set": map[string]interface{}{
  220. "repeat": 1,
  221. "repeat_reason": "sourcewebsite为1,重复",
  222. },
  223. },
  224. })
  225. if len(updateExtract) >= 200 {
  226. mgo.UpSertBulk(extract, updateExtract...)
  227. updateExtract = [][]map[string]interface{}{}
  228. }
  229. tmp = make(map[string]interface{})
  230. continue
  231. }
  232. if util.IntAll(tmp["repeat"]) == 1 || util.IntAll(tmp["repeat"]) == -1||
  233. util.IntAll(tmp["dataging"]) == 1 {
  234. if util.IntAll(tmp["repeat"]) == 1 {
  235. repeateN++
  236. }
  237. tmp = make(map[string]interface{})
  238. continue
  239. }
  240. pool <- true
  241. wg.Add(1)
  242. go func(tmp map[string]interface{}) {
  243. defer func() {
  244. <-pool
  245. wg.Done()
  246. }()
  247. info := NewInfo(tmp)
  248. if !LowHeavy { //是否进行低质量数据判重
  249. if invalidData(info.buyer, info.projectname, info.projectcode, info.contractnumber) {
  250. updateExtract = append(updateExtract, []map[string]interface{}{
  251. map[string]interface{}{
  252. "_id": tmp["_id"],
  253. },
  254. map[string]interface{}{
  255. "$set": map[string]interface{}{
  256. "repeat": -1, //无效数据标签
  257. },
  258. },
  259. })
  260. if len(updateExtract) >= 200 {
  261. mgo.UpSertBulk(extract, updateExtract...)
  262. updateExtract = [][]map[string]interface{}{}
  263. }
  264. return
  265. }
  266. }
  267. b, source, reason := DM.check(info)
  268. if b { //有重复,生成更新语句,更新抽取和更新招标
  269. repeateN++
  270. var is_replace = false
  271. var mergeArr = []int64{} //更改合并数组记录
  272. var newData = &Info{} //更换新的数据池数据
  273. var repeat_idMap = map[string]interface{}{} //记录判重的
  274. var merge_idMap = map[string]interface{}{} //记录合并的
  275. repeat_idMap["_id"] = StringTOBsonId(info.id)
  276. if IdType {
  277. repeat_idMap["_id"] = info.id
  278. }
  279. merge_idMap["_id"] = StringTOBsonId(source.id)
  280. repeat_id := source.id //初始化一个数据
  281. if isMerger { //合并相关
  282. basic_bool := basicDataScore(source, info)
  283. if basic_bool {
  284. //已原始数据为标准 - 对比数据打判重标签-
  285. newData, mergeArr, is_replace = mergeDataFields(source, info)
  286. DM.replaceSourceData(newData, source) //替换
  287. //对比数据打重复标签的id,原始数据id的记录
  288. repeat_idMap["_id"] = StringTOBsonId(info.id)
  289. merge_idMap["_id"] = StringTOBsonId(source.id)
  290. if IdType {
  291. repeat_idMap["_id"] = info.id
  292. merge_idMap["_id"] = source.id
  293. }
  294. repeat_id = source.id
  295. } else {
  296. //已对比数据为标准 ,数据池的数据打判重标签
  297. newData, mergeArr, is_replace = mergeDataFields(info, source)
  298. DM.replaceSourceData(newData, source) //替换
  299. //原始数据打重复标签的id, 对比数据id的记录
  300. repeat_idMap["_id"] = StringTOBsonId(source.id)
  301. merge_idMap["_id"] = StringTOBsonId(info.id)
  302. if IdType {
  303. repeat_idMap["_id"] = source.id
  304. merge_idMap["_id"] = info.id
  305. }
  306. repeat_id = info.id
  307. }
  308. merge_map := make(map[string]interface{}, 0)
  309. if is_replace { //有过合并-更新数据
  310. merge_map = map[string]interface{}{
  311. "$set": map[string]interface{}{
  312. "merge": newData.mergemap,
  313. },
  314. }
  315. //更新合并后的数据
  316. for _, value := range mergeArr {
  317. if value == 0 {
  318. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  319. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  320. } else if value == 1 {
  321. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  322. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  323. } else if value == 2 {
  324. merge_map["$set"].(map[string]interface{})["projectname"] = newData.projectname
  325. } else if value == 3 {
  326. merge_map["$set"].(map[string]interface{})["projectcode"] = newData.projectcode
  327. } else if value == 4 {
  328. merge_map["$set"].(map[string]interface{})["buyer"] = newData.buyer
  329. } else if value == 5 {
  330. merge_map["$set"].(map[string]interface{})["budget"] = newData.budget
  331. } else if value == 6 {
  332. merge_map["$set"].(map[string]interface{})["winner"] = newData.winner
  333. } else if value == 7 {
  334. merge_map["$set"].(map[string]interface{})["bidamount"] = newData.bidamount
  335. } else if value == 8 {
  336. merge_map["$set"].(map[string]interface{})["bidopentime"] = newData.bidopentime
  337. } else if value == 9 {
  338. merge_map["$set"].(map[string]interface{})["contractnumber"] = newData.contractnumber
  339. } else if value == 10 {
  340. merge_map["$set"].(map[string]interface{})["publishtime"] = newData.publishtime
  341. } else if value == 11 {
  342. merge_map["$set"].(map[string]interface{})["agency"] = newData.agency
  343. } else {
  344. }
  345. }
  346. //模板数据更新
  347. updateExtract = append(updateExtract, []map[string]interface{}{
  348. merge_idMap,
  349. merge_map,
  350. })
  351. }
  352. } else { //高质量数据
  353. basic_bool := basicDataScore(source, info)
  354. if !basic_bool {
  355. DM.replaceSourceData(info, source) //替换
  356. repeat_idMap["_id"] = StringTOBsonId(source.id)
  357. if IdType {
  358. repeat_idMap["_id"] = source.id
  359. }
  360. repeat_id = info.id
  361. if len(ids)>=9 {
  362. ids=append(ids,source.id)
  363. for _, to := range nextNode {
  364. key := source.id + "-" + source.id + "-" + util.ObjToString(to["stype"])
  365. by, _ := json.Marshal(map[string]interface{}{
  366. "gtid": source.id,
  367. "lteid": source.id,
  368. "stype": util.ObjToString(to["stype"]),
  369. "key": key,
  370. "ids": strings.Join(ids, ","),
  371. })
  372. addr := &net.UDPAddr{
  373. IP: net.ParseIP(to["addr"].(string)),
  374. Port: util.IntAll(to["port"]),
  375. }
  376. node := &udpNode{by, addr, time.Now().Unix(), 0}
  377. udptaskmap.Store(key, node)
  378. udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  379. }
  380. //
  381. ids = []string{}
  382. }else {
  383. ids=append(ids,source.id)
  384. }
  385. }
  386. }
  387. //if repeateN%150==0&&repeateN>0 {
  388. // fmt.Println("最终结果","目标id:",repeat_idMap["_id"])
  389. //}
  390. //重复数据打标签
  391. updateExtract = append(updateExtract, []map[string]interface{}{
  392. repeat_idMap,
  393. map[string]interface{}{
  394. "$set": map[string]interface{}{
  395. "repeat": 1,
  396. "repeat_reason": reason,
  397. "repeat_id": repeat_id,
  398. },
  399. },
  400. })
  401. }
  402. }(tmp)
  403. if len(updateExtract) >= 200 {
  404. mgo.UpSertBulk(extract, updateExtract...)
  405. updateExtract = [][]map[string]interface{}{}
  406. }
  407. tmp = make(map[string]interface{})
  408. }
  409. wg.Wait()
  410. if len(updateExtract) > 0 {
  411. mgo.UpSertBulk(extract, updateExtract...)
  412. }
  413. log.Println("this task over.", n, "repeateN:", repeateN, mapInfo["stop"])
  414. //任务完成,开始发送广播通知下面节点
  415. if n > repeateN && mapInfo["stop"] == nil {
  416. log.Println("判重任务完成发送udp")
  417. for _, to := range nextNode {
  418. sid, _ := mapInfo["gtid"].(string)
  419. eid, _ := mapInfo["lteid"].(string)
  420. key := sid + "-" + eid + "-" + util.ObjToString(to["stype"])
  421. by, _ := json.Marshal(map[string]interface{}{
  422. "gtid": sid,
  423. "lteid": eid,
  424. "stype": util.ObjToString(to["stype"]),
  425. "key": key,
  426. "ids": strings.Join(ids, ","),
  427. })
  428. addr := &net.UDPAddr{
  429. IP: net.ParseIP(to["addr"].(string)),
  430. Port: util.IntAll(to["port"]),
  431. }
  432. node := &udpNode{by, addr, time.Now().Unix(), 0}
  433. udptaskmap.Store(key, node)
  434. udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  435. }
  436. }
  437. }
  438. //定时任务
  439. func timedTaskDay() {
  440. log.Println("部署定时任务")
  441. c := cron.New()
  442. //c.AddFunc("0 0 */4 * * ?", func() { movedata() })
  443. c.AddFunc("0 0 */4 * * ?", func() { timedTaskOnce() })
  444. c.Start()
  445. }
  446. func timedTaskOnce() {
  447. defer util.Catch()
  448. log.Println("开始一次迁移任务")
  449. movedata()
  450. log.Println("开始一次任务判重")
  451. //当前时间-8 -4 小时
  452. now := time.Now()
  453. log.Println(now)
  454. preTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-8, 0, 0, 0, time.Local)
  455. curTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-4, 0, 0, 0, time.Local)
  456. log.Println(preTime,curTime)
  457. task_sid := util.BsonIdToSId(bson.NewObjectIdWithTime(preTime))
  458. task_eid := util.BsonIdToSId(bson.NewObjectIdWithTime(curTime))
  459. between_time := curTime.Unix() - (86400 * timingPubScope)
  460. log.Println("id区间:",task_sid, task_eid,"时间:", between_time)
  461. //区间id
  462. q_start := map[string]interface{}{
  463. "_id": map[string]interface{}{
  464. "$gte": StringTOBsonId(task_sid),
  465. "$lte": StringTOBsonId(task_eid),
  466. },
  467. }
  468. sess := mgo.GetMgoConn()
  469. defer mgo.DestoryMongoConn(sess)
  470. it_start := sess.DB(mgo.DbName).C(extract).Find(&q_start).Sort("publishtime").Iter()
  471. num,oknum, deterTime:= int64(0),int64(0),int64(0) //计数
  472. updateExtract := [][]map[string]interface{}{}//批量更新mongo数组
  473. pendAllArr:=[][]map[string]interface{}{}//待处理数组
  474. dayArr := []map[string]interface{}{}
  475. for tmp := make(map[string]interface{}); it_start.Next(&tmp); num++ {
  476. if num%10000 == 0 {
  477. log.Println("正序遍历:", num)
  478. }
  479. source := util.ObjToMap(tmp["jsondata"])
  480. if util.IntAll((*source)["sourcewebsite"]) == 1 {
  481. updateExtract = append(updateExtract, []map[string]interface{}{
  482. map[string]interface{}{
  483. "_id": tmp["_id"],
  484. },
  485. map[string]interface{}{
  486. "$set": map[string]interface{}{
  487. "repeat": 1,
  488. "dataging": 0,
  489. "repeat_reason": "sourcewebsite为1 重复",
  490. },
  491. },
  492. })
  493. if len(updateExtract) > 50 {
  494. mgo.UpSertBulk(extract, updateExtract...)
  495. updateExtract = [][]map[string]interface{}{}
  496. }
  497. tmp = make(map[string]interface{})
  498. continue
  499. }
  500. //取-符合-发布时间X年内的数据
  501. if util.IntAll(tmp["dataging"]) == 1 {
  502. pubtime := util.Int64All(tmp["publishtime"])
  503. if pubtime > 0 && pubtime >= between_time {
  504. oknum++
  505. if deterTime==0 {
  506. log.Println("找到第一条符合条件的数据")
  507. deterTime = util.Int64All(tmp["publishtime"])
  508. dayArr = append(dayArr,tmp)
  509. }else {
  510. if pubtime-deterTime >timingSpanDay*86400 {
  511. //新数组重新构建,当前组数据加到全部组数据
  512. pendAllArr = append(pendAllArr,dayArr)
  513. dayArr = []map[string]interface{}{}
  514. deterTime = util.Int64All(tmp["publishtime"])
  515. dayArr = append(dayArr,tmp)
  516. }else {
  517. dayArr = append(dayArr,tmp)
  518. }
  519. }
  520. }else {
  521. //不在两年内的也清标记
  522. updateExtract = append(updateExtract, []map[string]interface{}{
  523. map[string]interface{}{
  524. "_id": tmp["_id"],
  525. },
  526. map[string]interface{}{
  527. "$set": map[string]interface{}{
  528. "dataging": 0,
  529. },
  530. },
  531. })
  532. if len(updateExtract) > 50 {
  533. mgo.UpSertBulk(extract, updateExtract...)
  534. updateExtract = [][]map[string]interface{}{}
  535. }
  536. }
  537. }
  538. tmp = make(map[string]interface{})
  539. }
  540. //批量更新标记
  541. if len(updateExtract) > 0 {
  542. mgo.UpSertBulk(extract, updateExtract...)
  543. updateExtract = [][]map[string]interface{}{}
  544. }
  545. if len(dayArr)>0 {
  546. pendAllArr = append(pendAllArr,dayArr)
  547. dayArr = []map[string]interface{}{}
  548. }
  549. log.Println("查询数量:",num,"符合条件:",oknum)
  550. if len(pendAllArr) <= 0 {
  551. log.Println("没找到dataging==1的数据")
  552. return
  553. }
  554. //测试分组数量是否正确
  555. testNum:=0
  556. for k,v:=range pendAllArr {
  557. log.Println("第",k,"组--","数量:",len(v))
  558. testNum = testNum+len(v)
  559. }
  560. log.Println("本地构建分组完成:",len(pendAllArr),"组","测试-总计数量:",testNum)
  561. n, repeateN := 0, 0
  562. for k,v:=range pendAllArr { //每组结束更新一波数据
  563. //构建当前组的数据池
  564. log.Println("构建第",k,"组---(数据池)")
  565. //当前组的第一个发布时间
  566. first_pt :=util.Int64All(v[0]["publishtime"])
  567. coll :=extract_back
  568. DM = TimedTaskDatamap(dupdays, first_pt,coll)
  569. log.Println("开始遍历判重第",k,"组 共计数量:",len(v))
  570. n = n+len(v)
  571. log.Println("统计目前总数量:",n,"重复数量:",repeateN)
  572. for _,tmp:=range v {
  573. info := NewInfo(tmp)
  574. if !LowHeavy { //是否进行低质量数据判重
  575. if invalidData(info.buyer, info.projectname, info.projectcode, info.contractnumber) {
  576. log.Println("无效数据")
  577. updateExtract = append(updateExtract, []map[string]interface{}{
  578. map[string]interface{}{
  579. "_id": tmp["_id"],
  580. },
  581. map[string]interface{}{
  582. "$set": map[string]interface{}{
  583. "repeat": -1, //无效数据标签
  584. "dataging": 0,
  585. },
  586. },
  587. })
  588. if len(updateExtract) > 50 {
  589. mgo.UpSertBulk(extract, updateExtract...)
  590. updateExtract = [][]map[string]interface{}{}
  591. }
  592. continue
  593. }
  594. }
  595. b, source, reason := DM.check(info)
  596. if b { //有重复,生成更新语句,更新抽取和更新招标
  597. log.Println("判重结果", b, reason,"目标id",info.id)
  598. repeateN++
  599. //重复数据打标签
  600. updateExtract = append(updateExtract, []map[string]interface{}{
  601. map[string]interface{}{
  602. "_id": tmp["_id"],
  603. },
  604. map[string]interface{}{
  605. "$set": map[string]interface{}{
  606. "repeat": 1,
  607. "repeat_reason": reason,
  608. "repeat_id": source.id,
  609. "dataging": 0,
  610. },
  611. },
  612. })
  613. }else {
  614. updateExtract = append(updateExtract, []map[string]interface{}{
  615. map[string]interface{}{
  616. "_id": tmp["_id"],
  617. },
  618. map[string]interface{}{
  619. "$set": map[string]interface{}{
  620. "dataging": 0,//符合条件的都为dataging==0
  621. },
  622. },
  623. })
  624. }
  625. if len(updateExtract) > 50 {
  626. mgo.UpSertBulk(extract, updateExtract...)
  627. updateExtract = [][]map[string]interface{}{}
  628. }
  629. }
  630. //每组数据结束-更新数据
  631. if len(updateExtract) > 0 {
  632. mgo.UpSertBulk(extract, updateExtract...)
  633. updateExtract = [][]map[string]interface{}{}
  634. }
  635. }
  636. if len(updateExtract) > 0 {
  637. mgo.UpSertBulk(extract, updateExtract...)
  638. updateExtract = [][]map[string]interface{}{}
  639. }
  640. log.Println("this timeTask over.", n, "repeateN:", repeateN)
  641. //任务完成,开始发送广播通知下面节点 发udp 去升索引待定 + 合并
  642. if n > repeateN {
  643. for _, to := range nextNode {
  644. next_sid := util.BsonIdToSId(task_sid)
  645. next_eid := util.BsonIdToSId(task_eid)
  646. key := next_sid + "-" + next_eid + "-" + util.ObjToString(to["stype"])
  647. by, _ := json.Marshal(map[string]interface{}{
  648. "gtid": next_sid,
  649. "lteid": next_eid,
  650. "stype": util.ObjToString(to["stype"]),
  651. "key": key,
  652. })
  653. addr := &net.UDPAddr{
  654. IP: net.ParseIP(to["addr"].(string)),
  655. Port: util.IntAll(to["port"]),
  656. }
  657. node := &udpNode{by, addr, time.Now().Unix(), 0}
  658. udptaskmap.Store(key, node)
  659. udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  660. }
  661. }
  662. }
  663. //判断是否在周期天内
  664. func isTaskTimeCycle(pt int64) bool {
  665. year, month, day := time.Now().Date()
  666. predur_pt:=time.Date(year, month, day, 0, 0, 0, 0, time.Local).Add(-time.Duration(dupdays) * 24 * time.Hour).Unix()
  667. log.Println(predur_pt)
  668. if pt >= predur_pt {
  669. return true
  670. }else {
  671. return false
  672. }
  673. }
  674. //合并字段-并更新merge字段的值
  675. func mergeDataFields(source *Info, info *Info) (*Info, []int64, bool) {
  676. merge_recordMap := make(map[string]interface{}, 0)
  677. mergeArr := make([]int64, 0)
  678. //是否替换数据了-记录原始的数据
  679. is_replace := false
  680. //1、城市
  681. if source.area == "" || source.area == "全国" {
  682. //为空
  683. if info.area != "全国" && info.area != "" {
  684. merge_recordMap["area"] = source.area
  685. merge_recordMap["city"] = source.city
  686. source.area = info.area
  687. source.city = info.city
  688. mergeArr = append(mergeArr, 1)
  689. is_replace = true
  690. }
  691. } else {
  692. //不为空-查看站点相关-有值必替换
  693. if source.is_site {
  694. //是站点替换的城市
  695. merge_recordMap["site_area"] = source.area
  696. merge_recordMap["site_city"] = source.city
  697. mergeArr = append(mergeArr, 0)
  698. is_replace = true
  699. source.is_site = false
  700. }
  701. }
  702. //2、项目名称
  703. if source.projectname == "" && info.projectname != "" {
  704. merge_recordMap["projectname"] = source.projectname
  705. source.projectname = info.projectname
  706. mergeArr = append(mergeArr, 2)
  707. is_replace = true
  708. }
  709. //3、项目编号
  710. if source.projectcode == "" && info.projectcode != "" {
  711. merge_recordMap["projectcode"] = source.projectcode
  712. source.projectcode = info.projectcode
  713. mergeArr = append(mergeArr, 3)
  714. is_replace = true
  715. }
  716. //4、采购单位
  717. if source.buyer == "" && info.buyer != "" {
  718. merge_recordMap["buyer"] = source.buyer
  719. source.buyer = info.buyer
  720. mergeArr = append(mergeArr, 4)
  721. is_replace = true
  722. }
  723. //5、预算
  724. if source.budget == 0 && info.budget != 0 {
  725. merge_recordMap["budget"] = source.budget
  726. source.budget = info.budget
  727. mergeArr = append(mergeArr, 5)
  728. is_replace = true
  729. }
  730. //6、中标单位
  731. if source.winner == "" && info.winner != "" {
  732. merge_recordMap["winner"] = source.winner
  733. source.winner = info.winner
  734. mergeArr = append(mergeArr, 6)
  735. is_replace = true
  736. }
  737. //7、中标金额
  738. if source.bidamount == 0 && info.bidamount != 0 {
  739. merge_recordMap["bidamount"] = source.bidamount
  740. source.bidamount = info.bidamount
  741. mergeArr = append(mergeArr, 7)
  742. is_replace = true
  743. }
  744. //8、开标时间-地点
  745. if source.bidopentime == 0 && info.bidopentime != 0 {
  746. merge_recordMap["bidopentime"] = source.bidopentime
  747. source.bidopentime = info.bidopentime
  748. mergeArr = append(mergeArr, 8)
  749. is_replace = true
  750. }
  751. //9、合同编号
  752. if source.contractnumber == "" && info.contractnumber != "" {
  753. merge_recordMap["contractnumber"] = source.contractnumber
  754. source.contractnumber = info.contractnumber
  755. mergeArr = append(mergeArr, 9)
  756. is_replace = true
  757. }
  758. //10、发布时间
  759. if source.publishtime == 0 && info.publishtime != 0 {
  760. merge_recordMap["publishtime"] = source.publishtime
  761. source.publishtime = info.publishtime
  762. mergeArr = append(mergeArr, 10)
  763. is_replace = true
  764. }
  765. //11、代理机构
  766. if source.agency == "" && info.agency != "" {
  767. merge_recordMap["agency"] = source.agency
  768. source.agency = info.agency
  769. mergeArr = append(mergeArr, 11)
  770. is_replace = true
  771. }
  772. if is_replace { //有过替换更新
  773. //总次数+1
  774. source.mergemap["total_num"] = util.Int64All(source.mergemap["total_num"]) + 1
  775. merge_recordMap["num"] = util.Int64All(source.mergemap["total_num"])
  776. //和哪一个数据id进行非空替换的-记录
  777. key := info.id
  778. source.mergemap[key] = merge_recordMap
  779. }
  780. //待进一步优化
  781. return source, mergeArr, is_replace
  782. }
  783. //权重评估
  784. func basicDataScore(v *Info, info *Info) bool {
  785. /*
  786. 权重评估
  787. 网站优先级判定规则:
  788. 1、国家>省级>市级>县区
  789. 2、政府采购>公共资源>官方网站|政府门户>社会公共招标平台|企业招标平台
  790. 3、同sitetype-分析weight
  791. 4、要素打分-分析
  792. */
  793. v_score, info_score := -1, -1
  794. dict_v := SiteMap[v.site]
  795. dict_info := SiteMap[info.site]
  796. //先判断level
  797. if dict_v != nil {
  798. v_level := util.ObjToString(dict_v["level"])
  799. if v_level == "国家" {
  800. v_score = 4
  801. } else if v_level == "省级" {
  802. v_score = 3
  803. } else if v_level == "市级" {
  804. v_score = 2
  805. } else if v_level == "县区" {
  806. v_score = 1
  807. } else if v_level == "" {
  808. } else {
  809. v_score = 0
  810. }
  811. }
  812. if dict_info != nil {
  813. info_level := util.ObjToString(dict_info["level"])
  814. if info_level == "国家" {
  815. info_score = 4
  816. } else if info_level == "省级" {
  817. info_score = 3
  818. } else if info_level == "市级" {
  819. info_score = 2
  820. } else if info_level == "县区" {
  821. info_score = 1
  822. } else if info_level == "" {
  823. } else {
  824. v_score = 0
  825. }
  826. }
  827. if v_score > info_score {
  828. return true
  829. }
  830. if v_score < info_score {
  831. return false
  832. }
  833. //判断sitetype
  834. if dict_v != nil {
  835. v_sitetype := util.ObjToString(dict_v["sitetype"])
  836. if v_sitetype == "政府采购" {
  837. v_score = 4
  838. } else if v_sitetype == "公共资源" {
  839. v_score = 3
  840. } else if v_sitetype == "官方网站"|| v_sitetype == "政府门户" {
  841. v_score = 2
  842. } else if v_sitetype == "社会公共招标平台" || v_sitetype == "企业招标平台" {
  843. v_score = 1
  844. } else if v_sitetype == "" {
  845. } else {
  846. v_score = 0
  847. }
  848. }
  849. if dict_info != nil {
  850. info_sitetype := util.ObjToString(dict_info["sitetype"])
  851. if info_sitetype == "政府采购" {
  852. info_score = 4
  853. } else if info_sitetype == "公共资源" {
  854. info_score = 3
  855. } else if info_sitetype == "官方网站"|| info_sitetype == "政府门户" {
  856. info_score = 2
  857. } else if info_sitetype == "社会公共招标平台" || info_sitetype == "企业招标平台" {
  858. info_score = 1
  859. } else if info_sitetype == "" {
  860. } else {
  861. info_score = 0
  862. }
  863. }
  864. if v_score > info_score {
  865. return true
  866. }
  867. if v_score < info_score {
  868. return false
  869. }
  870. if v_score == info_score {//同sitetype 情况下 分析weight
  871. v_weight := util.IntAll(dict_v["weight"])
  872. info_weight := util.IntAll(dict_info["weight"])
  873. if v_weight>info_weight {
  874. return true
  875. }
  876. if info_weight>v_weight {
  877. return false
  878. }
  879. }
  880. //网站评估
  881. m, n := 0, 0
  882. if v.projectname != "" {
  883. m++
  884. }
  885. if v.buyer != "" {
  886. m++
  887. }
  888. if v.projectcode != "" || v.contractnumber != "" {
  889. m++
  890. }
  891. if v.budget != 0 {
  892. m++
  893. }
  894. if v.bidamount != 0 {
  895. m++
  896. }
  897. if v.winner != "" {
  898. m++
  899. }
  900. if v.bidopentime != 0 {
  901. m++
  902. }
  903. if v.bidopenaddress != "" {
  904. m++
  905. }
  906. if v.agency != "" {
  907. m = m + 2
  908. }
  909. if v.city != "" {
  910. m = m + 2
  911. }
  912. if info.projectname != "" {
  913. n++
  914. }
  915. if info.buyer != "" {
  916. n++
  917. }
  918. if info.projectcode != "" || info.contractnumber != "" {
  919. n++
  920. }
  921. if info.budget != 0 {
  922. n++
  923. }
  924. if info.bidamount != 0 {
  925. n++
  926. }
  927. if info.winner != "" {
  928. n++
  929. }
  930. if info.bidopentime != 0 {
  931. n++
  932. }
  933. if info.bidopenaddress != "" {
  934. n++
  935. }
  936. if info.agency != "" {
  937. n = n + 2
  938. }
  939. if info.city != "" {
  940. n = n + 2
  941. }
  942. if m > n {
  943. return true
  944. } else if m == n {
  945. if v.publishtime >= info.publishtime {
  946. return true
  947. } else {
  948. return false
  949. }
  950. } else {
  951. return false
  952. }
  953. }
  954. //无效数据
  955. func invalidData(d1 string, d2 string, d3 string, d4 string) bool {
  956. var n int
  957. if d1 != "" {
  958. n++
  959. }
  960. if d2 != "" {
  961. n++
  962. }
  963. if d3 != "" {
  964. n++
  965. }
  966. if d4 != "" {
  967. n++
  968. }
  969. if n == 0 {
  970. return true
  971. }
  972. return false
  973. }
  974. //迁移数据dupdays+5之前的数据
  975. func movedata() {
  976. sess := mgo.GetMgoConn()
  977. defer mgo.DestoryMongoConn(sess)
  978. year, month, day := time.Now().Date()
  979. now:=time.Now()
  980. move_time := time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-8, 0, 0, 0, time.Local).Unix()
  981. q := map[string]interface{}{
  982. "comeintime": map[string]interface{}{
  983. "$lt": move_time,
  984. },
  985. }
  986. log.Println(q)
  987. it := sess.DB(mgo.DbName).C(extract).Find(&q).Iter()
  988. index := 0
  989. for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
  990. mgo.Save(extract_back, tmp)
  991. tmp = map[string]interface{}{}
  992. if index%1000 == 0 {
  993. log.Println("index", index)
  994. }
  995. }
  996. log.Println("save to", extract_back, " ok index", index)
  997. qv := map[string]interface{}{
  998. "comeintime": map[string]interface{}{
  999. "$lt": time.Date(year, month, day, 0, 0, 0, 0, time.Local).Add(-time.Duration(dupdays) * 24 * time.Hour*2).Unix(),
  1000. },
  1001. }
  1002. delnum := mgo.Delete(extract, qv)
  1003. log.Println("remove from ", extract, delnum)
  1004. }