proTask.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/log"
  5. "app.yhyue.com/data_processing/common_utils/mongodb"
  6. "encoding/json"
  7. "fmt"
  8. "go.uber.org/zap"
  9. "proposed_project/config"
  10. "sync"
  11. "time"
  12. )
  13. func updateAllQueue() {
  14. arru := make([][]map[string]interface{}, saveSize)
  15. indexu := 0
  16. for {
  17. select {
  18. case v := <-updatePool:
  19. arru[indexu] = v
  20. indexu++
  21. if indexu == saveSize {
  22. updateSp <- true
  23. go func(arru [][]map[string]interface{}) {
  24. defer func() {
  25. <-updateSp
  26. }()
  27. MgoPro.UpSertBulk(config.Conf.Serve.ProColl, arru...)
  28. }(arru)
  29. arru = make([][]map[string]interface{}, saveSize)
  30. indexu = 0
  31. }
  32. case <-time.After(1 * time.Second):
  33. if indexu > 0 {
  34. updateSp <- true
  35. go func(arru [][]map[string]interface{}) {
  36. defer func() {
  37. <-updateSp
  38. }()
  39. MgoPro.UpSertBulk(config.Conf.Serve.ProColl, arru...)
  40. }(arru[:indexu])
  41. arru = make([][]map[string]interface{}, saveSize)
  42. indexu = 0
  43. }
  44. }
  45. }
  46. }
  47. func doTask(gtid, lteid string) {
  48. defer util.Catch()
  49. sess := MgoBid.GetMgoConn()
  50. defer MgoBid.DestoryMongoConn(sess)
  51. ch := make(chan bool, config.Conf.Serve.Thread)
  52. wg := &sync.WaitGroup{}
  53. q := map[string]interface{}{"_id": map[string]interface{}{
  54. "$gt": mongodb.StringTOBsonId(gtid),
  55. "$lte": mongodb.StringTOBsonId(lteid)}}
  56. f := map[string]interface{}{
  57. "detail": 0,
  58. "contenthtml": 0,
  59. "field_source": 0,
  60. "nj_record": 0,
  61. "kvtext": 0,
  62. }
  63. log.Info("doTask", zap.Any("q", q))
  64. query := sess.DB(config.Conf.DB.MongoB.Dbname).C(config.Conf.DB.MongoB.Coll).Find(q).Select(f).Sort("publishtime").Iter()
  65. count := 0
  66. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  67. if count%2000 == 0 {
  68. log.Info(fmt.Sprintf("current --- %d", count))
  69. }
  70. if util.ObjToString(tmp["toptype"]) != "拟建" {
  71. continue
  72. }
  73. ch <- true
  74. wg.Add(1)
  75. go func(temp map[string]interface{}) {
  76. defer func() {
  77. <-ch
  78. wg.Done()
  79. }()
  80. taskTag(temp) // 增量数据打标签
  81. info := ParseInfo(temp)
  82. if info.ProjectName != "" {
  83. MergeTask(temp, info)
  84. } else {
  85. log.Error("MergeTask", zap.Any("infoid", temp["_id"]))
  86. }
  87. }(tmp)
  88. tmp = make(map[string]interface{})
  89. }
  90. wg.Wait()
  91. log.Info(fmt.Sprintf("over --- %d", count))
  92. TaskSingle = true
  93. }
  94. func MergeTask(tmp map[string]interface{}, info *Info) {
  95. if info.ApproveCode != "" && info.ApproveCode != "无" && info.ApproveCode != "暂无项目代码" {
  96. AllCodeMapLock.Lock()
  97. pid := AllCodeMap[info.ApproveCode]
  98. AllCodeMapLock.Unlock()
  99. if pid != "" {
  100. AllPidMapLock.Lock()
  101. res := AllPidMap[pid]
  102. AllPidMapLock.Unlock()
  103. if res != nil {
  104. comparePro := res.P
  105. updateProject(tmp, *info, comparePro)
  106. } else {
  107. startProjectMerge(tmp, info)
  108. }
  109. } else {
  110. pid, pinfo := newProject(tmp, *info)
  111. AllPidMapLock.Lock()
  112. AllPidMap[info.Id] = &ID{P: pinfo}
  113. AllPidMapLock.Unlock()
  114. AllPnMapLock.Lock()
  115. res := AllPnMap[info.ProjectName]
  116. if res != nil {
  117. res.Id[info.Id] = info.ProjectName
  118. } else {
  119. res = &Pname{
  120. Id: map[string]string{info.Id: info.ProjectName},
  121. }
  122. }
  123. AllPnMapLock.Unlock()
  124. AllCodeMapLock.Lock()
  125. AllCodeMap[info.ApproveCode] = pid
  126. AllCodeMapLock.Unlock()
  127. }
  128. } else {
  129. startProjectMerge(tmp, info)
  130. }
  131. }
  132. func ParseInfo(tmp map[string]interface{}) (info *Info) {
  133. bys, _ := json.Marshal(tmp)
  134. var thisinfo *Info
  135. _ = json.Unmarshal(bys, &thisinfo)
  136. if thisinfo == nil {
  137. return nil
  138. }
  139. if len(thisinfo.Topscopeclass) == 0 {
  140. thisinfo.Topscopeclass = []string{}
  141. }
  142. if len(thisinfo.Subscopeclass) == 0 {
  143. thisinfo.Subscopeclass = []string{}
  144. }
  145. return thisinfo
  146. }
  147. func taskTag(tmp map[string]interface{}) {
  148. tag := taskFuc(tmp)
  149. if tag["nature"] != "" {
  150. tmp["nature_code"] = tag["nature"]
  151. } else {
  152. tmp["nature_code"] = "00"
  153. }
  154. if tag["project_stage"] != "" {
  155. tmp["project_stage_code"] = tag["project_stage"]
  156. } else {
  157. tmp["project_stage_code"] = "00"
  158. }
  159. if tag["owner"] != "" {
  160. tmp["ownerclass_code"] = tag["owner"]
  161. } else {
  162. tmp["ownerclass_code"] = "00"
  163. }
  164. if tag["sub_category"] != "" {
  165. tmp["category_code"] = tag["sub_category"]
  166. } else {
  167. if tag["top_category"] != "" {
  168. tmp["category_code"] = tag["top_category"]
  169. }
  170. }
  171. if util.ObjToString(tmp["category_code"]) == "" {
  172. tmp["category_code"] = "04"
  173. }
  174. }
  175. func doTask1(gtid, lteid string) {
  176. defer util.Catch()
  177. sess := MgoBid.GetMgoConn()
  178. defer MgoBid.DestoryMongoConn(sess)
  179. ch := make(chan bool, config.Conf.Serve.Thread)
  180. wg := &sync.WaitGroup{}
  181. q := map[string]interface{}{"_id": map[string]interface{}{
  182. "$gt": mongodb.StringTOBsonId(gtid),
  183. "$lte": mongodb.StringTOBsonId(lteid)}}
  184. log.Info("doTask", zap.Any("q", q))
  185. query := sess.DB(config.Conf.DB.MongoB.Dbname).C(config.Conf.DB.MongoB.Coll).Find(q).Select(nil).Iter()
  186. count := 0
  187. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  188. if count%20000 == 0 {
  189. log.Info(fmt.Sprintf("current --- %d", count))
  190. }
  191. if util.ObjToString(tmp["toptype"]) != "拟建" {
  192. continue
  193. }
  194. ch <- true
  195. wg.Add(1)
  196. go func(temp map[string]interface{}) {
  197. defer func() {
  198. <-ch
  199. wg.Done()
  200. }()
  201. savePool <- temp
  202. }(tmp)
  203. tmp = make(map[string]interface{})
  204. }
  205. wg.Wait()
  206. log.Info(fmt.Sprintf("over --- %d", count))
  207. TaskSingle = true
  208. }