task.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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("count --- %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. if result1["purchasinglist"] != nil {
  70. update["purchasinglist"] = result1["purchasinglist"]
  71. }
  72. if result1["procurementlist"] != nil {
  73. update["procurementlist"] = result1["procurementlist"]
  74. }
  75. }
  76. if len(update) > 0 {
  77. updatePool <- []map[string]interface{}{
  78. {"_id": mongodb.StringTOBsonId(id)},
  79. {"$set": update},
  80. }
  81. //updateEsPool <- []map[string]interface{}{{
  82. // "_id": id,
  83. //},
  84. // update,
  85. //}
  86. }
  87. }(tmp)
  88. tmp = map[string]interface{}{}
  89. }
  90. wg.Wait()
  91. log.Info("dispose over...", zap.Int("count:", count), zap.String("gtid:", gtid), zap.String("lteid:", lteid))
  92. NextNode(mapinfo)
  93. }
  94. // @Description procurementlist
  95. // @Author J 2022/8/31 15:28
  96. func taskA(tmp map[string]interface{}, gtid, lteid string) map[string]interface{} {
  97. toptype, _ := tmp["toptype"].(string)
  98. if toptype != "采购意向" {
  99. return nil
  100. }
  101. id := mongodb.BsonIdToSId(tmp["_id"])
  102. delete(tmp, "_id")
  103. delete(tmp, "detail")
  104. reqStr, err := json.Marshal(tmp)
  105. if err != nil {
  106. ErrorInfoCache <- map[string]interface{}{
  107. "err": "Json Marshal Error",
  108. "type": "采购意向",
  109. "id": id,
  110. "comeintime": time.Now().Unix(),
  111. "ok": false,
  112. "gtid": gtid,
  113. "letid": lteid,
  114. }
  115. return nil
  116. }
  117. //处理数据
  118. result, err := rpcGetFieldP(string(reqStr))
  119. if err != nil { //保存处理异常信息
  120. ErrorInfoCache <- map[string]interface{}{
  121. "err": err.Error(),
  122. "type": "采购意向",
  123. "id": id,
  124. "comeintime": time.Now().Unix(),
  125. "ok": false,
  126. "gtid": gtid,
  127. "letid": lteid,
  128. }
  129. }
  130. return result
  131. }
  132. // @Description review_experts
  133. // @Author J 2022/8/31 15:28
  134. func taskB(tmp map[string]interface{}, gtid, lteid string) map[string]interface{} {
  135. id := mongodb.BsonIdToSId(tmp["_id"])
  136. reqStr, err := json.Marshal(map[string]interface{}{id: tmp["detail"]})
  137. if err != nil {
  138. ErrorInfoCache <- map[string]interface{}{
  139. "err": "Json Marshal Error",
  140. "type": "评标专家字段",
  141. "id": id,
  142. "comeintime": time.Now().Unix(),
  143. "ok": false,
  144. "gtid": gtid,
  145. "letid": lteid,
  146. }
  147. return nil
  148. }
  149. //处理数据
  150. result, err := rpcGetFieldR(string(reqStr))
  151. if err != nil { //保存处理异常信息
  152. ErrorInfoCache <- map[string]interface{}{
  153. "err": err.Error(),
  154. "type": "评标专家字段",
  155. "id": id,
  156. "comeintime": time.Now().Unix(),
  157. "ok": false,
  158. "gtid": gtid,
  159. "letid": lteid,
  160. }
  161. }
  162. return result
  163. }
  164. func rpcGetFieldP(reqStr string) (map[string]interface{}, error) {
  165. defer util.Catch()
  166. //获取ip、port服务
  167. ipConn, ipErr := grpc.Dial(config.Conf.Serve.GrpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  168. if ipErr != nil {
  169. atomic.AddInt64(&IpDialErrNum, 1) //异常次数+1
  170. return nil, errors.New("Ip Grpc Dial Error")
  171. }
  172. atomic.StoreInt64(&IpDialErrNum, 0) //异常次数重置
  173. defer ipConn.Close()
  174. ipClient := service.NewServiceClient(ipConn)
  175. ip := ""
  176. port := -1
  177. //重试获取ip、port
  178. for i := 1; i <= 3; i++ {
  179. repl, err := ipClient.Apply(context.Background(), &service.ApplyReqData{Name: "goods_service", Balance: 0})
  180. if err != nil {
  181. continue
  182. } else {
  183. ip = repl.Ip
  184. port = int(repl.Port)
  185. break
  186. }
  187. }
  188. if ip == "" || port == -1 { //重试三次,回去ip、port失败
  189. atomic.AddInt64(&IpGetErrNum, 1) //异常次数+1
  190. return nil, errors.New("Get Ip Error")
  191. }
  192. atomic.StoreInt64(&IpGetErrNum, 0) //异常次数重置
  193. //处理数据
  194. addr := ip + ":" + fmt.Sprint(port)
  195. start := time.Now()
  196. conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  197. if err != nil {
  198. atomic.AddInt64(&ExtractDialErrNum, 1) //异常次数+1
  199. return nil, errors.New("Extract Grpc Dial Error,addr:" + addr)
  200. }
  201. atomic.StoreInt64(&ExtractDialErrNum, 0) //异常次数重置
  202. defer conn.Close()
  203. client := proto.NewGoodsExtractClient(conn)
  204. req := &proto.GoodsRequest{
  205. Contents: reqStr,
  206. }
  207. ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*1)
  208. defer cancel()
  209. resp, err := client.GoodsExtract(ctx, req)
  210. if err != nil {
  211. return nil, errors.New("Deal Data Error")
  212. }
  213. result := map[string]interface{}{}
  214. if json.Unmarshal([]byte(resp.Goods), &result) != nil {
  215. return nil, errors.New("Json Unmarshal Error")
  216. }
  217. if time.Since(start).Minutes() > 5 {
  218. // py接口字段识别超过5分钟
  219. log.Info("rpcGetFieldP 字段识别超过5min", zap.Any("serve", "goods_service"), zap.Any("reqStr", reqStr), zap.Any("ip+port", addr))
  220. }
  221. return result, nil
  222. }
  223. func rpcGetFieldR(reqStr string) (map[string]interface{}, error) {
  224. defer util.Catch()
  225. //获取ip、port服务
  226. ipConn, ipErr := grpc.Dial(config.Conf.Serve.GrpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  227. if ipErr != nil {
  228. atomic.AddInt64(&IpDialErrNum, 1) //异常次数+1
  229. return nil, errors.New("Ip Grpc Dial Error")
  230. }
  231. atomic.StoreInt64(&IpDialErrNum, 0) //异常次数重置
  232. defer ipConn.Close()
  233. ipClient := service.NewServiceClient(ipConn)
  234. ip := ""
  235. port := -1
  236. //重试获取ip、port
  237. for i := 1; i <= 3; i++ {
  238. repl, err := ipClient.Apply(context.Background(), &service.ApplyReqData{Name: "extract_expert_service", Balance: 0})
  239. if err != nil {
  240. continue
  241. } else {
  242. ip = repl.Ip
  243. port = int(repl.Port)
  244. break
  245. }
  246. }
  247. if ip == "" || port == -1 { //重试三次,回去ip、port失败
  248. atomic.AddInt64(&IpGetErrNum, 1) //异常次数+1
  249. return nil, errors.New("Get Ip Error")
  250. }
  251. atomic.StoreInt64(&IpGetErrNum, 0) //异常次数重置
  252. //处理数据
  253. addr := ip + ":" + fmt.Sprint(port)
  254. start := time.Now()
  255. conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  256. if err != nil {
  257. atomic.AddInt64(&ExtractDialErrNum, 1) //异常次数+1
  258. return nil, errors.New("Extract Grpc Dial Error,addr:" + addr)
  259. }
  260. atomic.StoreInt64(&ExtractDialErrNum, 0) //异常次数重置
  261. defer conn.Close()
  262. client := proto.NewExistsExpertClient(conn)
  263. req := &proto.ContentRequest{
  264. Contents: reqStr,
  265. }
  266. ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*1)
  267. defer cancel()
  268. resp, err := client.Extract(ctx, req)
  269. if err != nil {
  270. return nil, errors.New("Deal Data Error")
  271. }
  272. result := make(map[string]interface{})
  273. if json.Unmarshal([]byte(resp.Results), &result) != nil {
  274. return nil, errors.New("Json Unmarshal Error")
  275. }
  276. if time.Since(start).Minutes() > 5 {
  277. // py接口字段识别超过5分钟
  278. log.Info("rpcGetFieldR 字段识别超过5min", zap.Any("serve", "extract_expert_service"), zap.Any("reqStr", reqStr), zap.Any("ip+port", addr))
  279. }
  280. return result, nil
  281. }
  282. //定时检测异常数据量,发告警
  283. func CheckErrorNum() {
  284. defer util.Catch()
  285. for {
  286. warnText := ""
  287. //消息治理中心服务连接异常告警
  288. if IpDialErrNum > 3 {
  289. warnText += "消息治理中心连接失败次数:" + fmt.Sprint(IpDialErrNum) + ";\n"
  290. }
  291. //获取治理中心IP异常告警
  292. if IpDialErrNum > 5 {
  293. warnText += "获取IP异常次数:" + fmt.Sprint(IpDialErrNum) + ";\n"
  294. }
  295. //连接IP服务异常告警
  296. if IpDialErrNum > 5 {
  297. warnText += "IP连接异常次数:" + fmt.Sprint(IpDialErrNum) + ";\n"
  298. }
  299. if warnText != "" {
  300. SendWarnInfo(warnText)
  301. }
  302. time.Sleep(30 * time.Minute)
  303. }
  304. }
  305. var TextModel = `{
  306. "msgtype": "text",
  307. "text": {
  308. "content": "%s",
  309. "mentioned_mobile_list":[%s]
  310. }
  311. }`
  312. //告警信息
  313. func SendWarnInfo(content string) {
  314. defer util.Catch()
  315. toUserMsg := fmt.Sprintf(TextModel, content, "13373929153,15090279371") //李俊亮、王江含
  316. log.Info("SendWarnInfo", zap.String("toUserMsg", toUserMsg))
  317. resp1, err := http.Post(
  318. "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=45962efc-ca87-4996-9ffa-08bf6608ab7a",
  319. "application/json",
  320. bytes.NewBuffer([]byte(toUserMsg)),
  321. )
  322. if err != nil {
  323. fmt.Println("request error:", err)
  324. return
  325. }
  326. defer resp1.Body.Close()
  327. }
  328. var SP = make(chan bool, 5)
  329. var ErrorInfoCache = make(chan map[string]interface{}, 1000) //异常信息集合
  330. //批量保存异常信息
  331. func SaveErrorInfo() {
  332. log.Info("SaveErrorInfo 异常信息...")
  333. savearr := make([]map[string]interface{}, 200)
  334. indexh := 0
  335. for {
  336. select {
  337. case v := <-ErrorInfoCache:
  338. savearr[indexh] = v
  339. indexh++
  340. if indexh == 200 {
  341. SP <- true
  342. go func(savearr []map[string]interface{}) {
  343. defer func() {
  344. <-SP
  345. }()
  346. MgoB.SaveBulk("bidding_warn", savearr...)
  347. }(savearr)
  348. savearr = make([]map[string]interface{}, 200)
  349. indexh = 0
  350. }
  351. case <-time.After(1 * time.Minute):
  352. if indexh > 0 {
  353. SP <- true
  354. go func(savearr []map[string]interface{}) {
  355. defer func() {
  356. <-SP
  357. }()
  358. MgoB.SaveBulk("bidding_warn", savearr...)
  359. }(savearr[:indexh])
  360. savearr = make([]map[string]interface{}, 200)
  361. indexh = 0
  362. }
  363. }
  364. }
  365. }