task.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. "bytes"
  7. "context"
  8. "encoding/json"
  9. "errors"
  10. "field-dispose/config"
  11. "field-dispose/proto"
  12. "fmt"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc"
  15. "google.golang.org/grpc/credentials/insecure"
  16. "net/http"
  17. "strings"
  18. "sync"
  19. "sync/atomic"
  20. "time"
  21. service "app.yhyue.com/BP/servicerd/proto"
  22. )
  23. var IpDialErrNum, IpGetErrNum, ExtractDialErrNum = int64(0), int64(0), int64(0)
  24. //@Description 处理字段procurementlist、review_experts
  25. // @Author J 2022/8/31 14:57
  26. func getIntention(gtid, lteid string, mapinfo map[string]interface{}) {
  27. defer util.Catch()
  28. sess := MgoB.GetMgoConn()
  29. defer MgoB.DestoryMongoConn(sess)
  30. ch := make(chan bool, 10)
  31. wg := &sync.WaitGroup{}
  32. query := map[string]interface{}{
  33. "_id": map[string]interface{}{
  34. "$gt": mongodb.StringTOBsonId(gtid),
  35. "$lte": mongodb.StringTOBsonId(lteid),
  36. },
  37. }
  38. field := map[string]interface{}{
  39. "toptype": 1,
  40. "attach_text": 1,
  41. "contenthtml": 1,
  42. "site": 1,
  43. "detail": 1,
  44. }
  45. log.Info(fmt.Sprintf("%d", MgoB.Count("bidding", query)))
  46. it := sess.DB(config.Conf.DB.Mongo.Dbname).C("bidding").Find(&query).Select(&field).Iter()
  47. count := 0
  48. for tmp := make(map[string]interface{}); it.Next(tmp); count++ {
  49. if count%20000 == 0 {
  50. log.Info("getIntention", zap.Int("current:", count))
  51. }
  52. ch <- true
  53. wg.Add(1)
  54. go func(tmp map[string]interface{}) {
  55. defer func() {
  56. <-ch
  57. wg.Done()
  58. }()
  59. id := mongodb.BsonIdToSId(tmp["_id"])
  60. update := make(map[string]interface{})
  61. result2 := taskB(tmp, gtid, lteid)
  62. //result1 := taskA(tmp, gtid, lteid)
  63. if r, ok := result2["result"].(map[string]interface{}); ok {
  64. if r[id] != nil && len(r[id].([]interface{})) > 0 {
  65. update["review_experts"] = strings.Join(util.ObjArrToStringArr(r[id].([]interface{})), ",")
  66. }
  67. }
  68. //if result1 != nil && len(result1) > 0 {
  69. // update = result1
  70. //}
  71. if len(update) > 0 {
  72. updatePool <- []map[string]interface{}{
  73. {"_id": tmp["_id"]},
  74. {"$set": update},
  75. }
  76. updateEsPool <- []map[string]interface{}{{
  77. "_id": id,
  78. },
  79. update,
  80. }
  81. }
  82. }(tmp)
  83. tmp = map[string]interface{}{}
  84. }
  85. wg.Wait()
  86. log.Info("dispose over...", zap.Int("count:", count), zap.String("gtid:", gtid), zap.String("lteid:", lteid))
  87. //NextNode(mapinfo)
  88. }
  89. // @Description procurementlist
  90. // @Author J 2022/8/31 15:28
  91. func taskA(tmp map[string]interface{}, gtid, lteid string) map[string]interface{} {
  92. toptype, _ := tmp["toptype"].(string)
  93. if toptype != "采购意向" {
  94. return nil
  95. }
  96. id := mongodb.BsonIdToSId(tmp["_id"])
  97. delete(tmp, "_id")
  98. delete(tmp, "detail")
  99. reqStr, err := json.Marshal(tmp)
  100. if err != nil {
  101. ErrorInfoCache <- map[string]interface{}{
  102. "err": "Json Marshal Error",
  103. "type": "采购意向",
  104. "id": id,
  105. "comeintime": time.Now().Unix(),
  106. "ok": false,
  107. "gtid": gtid,
  108. "letid": lteid,
  109. }
  110. return nil
  111. }
  112. //处理数据
  113. result, err := rpcGetFieldP(string(reqStr))
  114. if err != nil { //保存处理异常信息
  115. ErrorInfoCache <- map[string]interface{}{
  116. "err": err.Error(),
  117. "type": "采购意向",
  118. "id": id,
  119. "comeintime": time.Now().Unix(),
  120. "ok": false,
  121. "gtid": gtid,
  122. "letid": lteid,
  123. }
  124. }
  125. return result
  126. }
  127. // @Description review_experts
  128. // @Author J 2022/8/31 15:28
  129. func taskB(tmp map[string]interface{}, gtid, lteid string) map[string]interface{} {
  130. id := mongodb.BsonIdToSId(tmp["_id"])
  131. reqStr, err := json.Marshal(map[string]interface{}{id: tmp["detail"]})
  132. if err != nil {
  133. ErrorInfoCache <- map[string]interface{}{
  134. "err": "Json Marshal Error",
  135. "type": "评标专家字段",
  136. "id": id,
  137. "comeintime": time.Now().Unix(),
  138. "ok": false,
  139. "gtid": gtid,
  140. "letid": lteid,
  141. }
  142. return nil
  143. }
  144. //处理数据
  145. result, err := rpcGetFieldR(string(reqStr))
  146. if err != nil { //保存处理异常信息
  147. ErrorInfoCache <- map[string]interface{}{
  148. "err": err.Error(),
  149. "type": "评标专家字段",
  150. "id": id,
  151. "comeintime": time.Now().Unix(),
  152. "ok": false,
  153. "gtid": gtid,
  154. "letid": lteid,
  155. }
  156. }
  157. return result
  158. }
  159. func rpcGetFieldP(reqStr string) (map[string]interface{}, error) {
  160. defer util.Catch()
  161. //获取ip、port服务
  162. ipConn, ipErr := grpc.Dial(config.Conf.Serve.GrpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  163. if ipErr != nil {
  164. atomic.AddInt64(&IpDialErrNum, 1) //异常次数+1
  165. return nil, errors.New("Ip Grpc Dial Error")
  166. }
  167. atomic.StoreInt64(&IpDialErrNum, 0) //异常次数重置
  168. defer ipConn.Close()
  169. ipClient := service.NewServiceClient(ipConn)
  170. ip := ""
  171. port := -1
  172. //重试获取ip、port
  173. for i := 1; i <= 3; i++ {
  174. repl, err := ipClient.Apply(context.Background(), &service.ApplyReqData{Name: "goods_service", Balance: 0})
  175. if err != nil {
  176. continue
  177. } else {
  178. ip = repl.Ip
  179. port = int(repl.Port)
  180. break
  181. }
  182. }
  183. if ip == "" || port == -1 { //重试三次,回去ip、port失败
  184. atomic.AddInt64(&IpGetErrNum, 1) //异常次数+1
  185. return nil, errors.New("Get Ip Error")
  186. }
  187. atomic.StoreInt64(&IpGetErrNum, 0) //异常次数重置
  188. //处理数据
  189. addr := ip + ":" + fmt.Sprint(port)
  190. conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  191. if err != nil {
  192. atomic.AddInt64(&ExtractDialErrNum, 1) //异常次数+1
  193. return nil, errors.New("Extract Grpc Dial Error,addr:" + addr)
  194. }
  195. atomic.StoreInt64(&ExtractDialErrNum, 0) //异常次数重置
  196. defer conn.Close()
  197. client := proto.NewGoodsExtractClient(conn)
  198. req := &proto.GoodsRequest{
  199. Contents: reqStr,
  200. }
  201. resp, err := client.GoodsExtract(context.Background(), req)
  202. if err != nil {
  203. return nil, errors.New("Deal Data Error")
  204. }
  205. result := map[string]interface{}{}
  206. if json.Unmarshal([]byte(resp.Goods), &result) != nil {
  207. return nil, errors.New("Json Unmarshal Error")
  208. }
  209. return result, nil
  210. }
  211. func rpcGetFieldR(reqStr string) (map[string]interface{}, error) {
  212. defer util.Catch()
  213. //获取ip、port服务
  214. ipConn, ipErr := grpc.Dial(config.Conf.Serve.GrpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  215. if ipErr != nil {
  216. atomic.AddInt64(&IpDialErrNum, 1) //异常次数+1
  217. return nil, errors.New("Ip Grpc Dial Error")
  218. }
  219. atomic.StoreInt64(&IpDialErrNum, 0) //异常次数重置
  220. defer ipConn.Close()
  221. ipClient := service.NewServiceClient(ipConn)
  222. ip := ""
  223. port := -1
  224. //重试获取ip、port
  225. for i := 1; i <= 3; i++ {
  226. repl, err := ipClient.Apply(context.Background(), &service.ApplyReqData{Name: "extract_expert_service", Balance: 0})
  227. if err != nil {
  228. continue
  229. } else {
  230. ip = repl.Ip
  231. port = int(repl.Port)
  232. break
  233. }
  234. }
  235. if ip == "" || port == -1 { //重试三次,回去ip、port失败
  236. atomic.AddInt64(&IpGetErrNum, 1) //异常次数+1
  237. return nil, errors.New("Get Ip Error")
  238. }
  239. atomic.StoreInt64(&IpGetErrNum, 0) //异常次数重置
  240. //处理数据
  241. addr := ip + ":" + fmt.Sprint(port)
  242. conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  243. if err != nil {
  244. atomic.AddInt64(&ExtractDialErrNum, 1) //异常次数+1
  245. return nil, errors.New("Extract Grpc Dial Error,addr:" + addr)
  246. }
  247. atomic.StoreInt64(&ExtractDialErrNum, 0) //异常次数重置
  248. defer conn.Close()
  249. client := proto.NewExistsExpertClient(conn)
  250. req := &proto.ContentRequest{
  251. Contents: reqStr,
  252. }
  253. resp, err := client.Extract(context.Background(), req)
  254. if err != nil {
  255. return nil, errors.New("Deal Data Error")
  256. }
  257. result := make(map[string]interface{})
  258. if json.Unmarshal([]byte(resp.Results), &result) != nil {
  259. return nil, errors.New("Json Unmarshal Error")
  260. }
  261. return result, nil
  262. }
  263. //定时检测异常数据量,发告警
  264. func CheckErrorNum() {
  265. defer util.Catch()
  266. for {
  267. warnText := ""
  268. //消息治理中心服务连接异常告警
  269. if IpDialErrNum > 3 {
  270. warnText += "消息治理中心连接失败次数:" + fmt.Sprint(IpDialErrNum) + ";\n"
  271. }
  272. //获取治理中心IP异常告警
  273. if IpDialErrNum > 5 {
  274. warnText += "获取IP异常次数:" + fmt.Sprint(IpDialErrNum) + ";\n"
  275. }
  276. //连接IP服务异常告警
  277. if IpDialErrNum > 5 {
  278. warnText += "IP连接异常次数:" + fmt.Sprint(IpDialErrNum) + ";\n"
  279. }
  280. if warnText != "" {
  281. SendWarnInfo(warnText)
  282. }
  283. time.Sleep(30 * time.Minute)
  284. }
  285. }
  286. var TextModel = `{
  287. "msgtype": "text",
  288. "text": {
  289. "content": "%s",
  290. "mentioned_mobile_list":[%s]
  291. }
  292. }`
  293. //告警信息
  294. func SendWarnInfo(content string) {
  295. defer util.Catch()
  296. toUserMsg := fmt.Sprintf(TextModel, content, "13373929153,15090279371") //李俊亮、王江含
  297. log.Info("SendWarnInfo", zap.String("toUserMsg", toUserMsg))
  298. resp1, err := http.Post(
  299. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=45962efc-ca87-4996-9ffa-08bf6608ab7a",
  300. "application/json",
  301. bytes.NewBuffer([]byte(toUserMsg)),
  302. )
  303. if err != nil {
  304. fmt.Println("request error:", err)
  305. return
  306. }
  307. defer resp1.Body.Close()
  308. }
  309. var SP = make(chan bool, 5)
  310. var ErrorInfoCache = make(chan map[string]interface{}, 1000) //异常信息集合
  311. //批量保存异常信息
  312. func SaveErrorInfo() {
  313. log.Info("SaveErrorInfo 异常信息...")
  314. savearr := make([]map[string]interface{}, 200)
  315. indexh := 0
  316. for {
  317. select {
  318. case v := <-ErrorInfoCache:
  319. savearr[indexh] = v
  320. indexh++
  321. if indexh == 200 {
  322. SP <- true
  323. go func(savearr []map[string]interface{}) {
  324. defer func() {
  325. <-SP
  326. }()
  327. MgoB.SaveBulk("bidding_warn", savearr...)
  328. }(savearr)
  329. savearr = make([]map[string]interface{}, 200)
  330. indexh = 0
  331. }
  332. case <-time.After(1 * time.Minute):
  333. if indexh > 0 {
  334. SP <- true
  335. go func(savearr []map[string]interface{}) {
  336. defer func() {
  337. <-SP
  338. }()
  339. MgoB.SaveBulk("bidding_warn", savearr...)
  340. }(savearr[:indexh])
  341. savearr = make([]map[string]interface{}, 200)
  342. indexh = 0
  343. }
  344. }
  345. }
  346. }