cpa_agg.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2022/12/1 14:46
  3. # @Author : XuJiakai
  4. # @File : cpa_agg
  5. # @Software: PyCharm
  6. import json
  7. import time, queue
  8. from threading import Thread
  9. from utils.datetime_utils import datetime_format
  10. from log import get_log
  11. from sdk.WinhcAllClient import get_all_client
  12. from utils.datetime_utils import get_ds, get_now, datetime_format_transform
  13. from utils import map_2_json_str, json_path
  14. from utils.base_utils import tuple_max
  15. from utils.mysql_utils import insert_many
  16. from utils.xxl_queue import xxl_queue
  17. import re
  18. import sys
  19. import argparse
  20. from project_const import TOPIC_NAME, MONGODB_NAME
  21. date_part = re.compile('\\d{4}年\\d{2}月\\d{2}日')
  22. all_client = get_all_client()
  23. col = all_client.get_mongo_collection(MONGODB_NAME)
  24. del_col = all_client.get_mongo_collection(MONGODB_NAME + '_del')
  25. log = get_log('cpa_agg')
  26. holo_client = all_client.get_holo_client(db='winhc_biz')
  27. HOLO_TABLE_NAME = 'public.ads_waa_dim_info'
  28. def get_max_data(data: list, key: str, exclude_product_name: list = ['winhc']):
  29. max_data = None
  30. for i in data:
  31. tmp_v = json_path(i, key)
  32. if tmp_v is None:
  33. continue
  34. product_name = i['competitor_product_name']
  35. if product_name in exclude_product_name:
  36. continue
  37. pass
  38. if max_data is None:
  39. max_data = (tmp_v, product_name)
  40. else:
  41. max_data = tuple_max(max_data, (tmp_v, product_name))
  42. if max_data is None:
  43. return None, None
  44. return max_data
  45. def get_all_data_by_item(data: list, key):
  46. result_data = {}
  47. for i in data:
  48. result_data[i['competitor_product_name']] = json_path(i, key)
  49. return result_data
  50. def data_transform(data: list):
  51. log.info('input data: {}'.format(data))
  52. deleted_key = [i['_id'] for i in data][0]
  53. deleted_key = deleted_key[:deleted_key.rfind('_')]
  54. base_info = data[0]['base_info']
  55. ds = get_ds()
  56. key_set = set()
  57. winhc_data = None
  58. for i in data:
  59. key_set = key_set | set(i['summary'].keys())
  60. key_set = key_set | set(i['latest_date'].keys())
  61. if i['competitor_product_name'] == 'winhc':
  62. winhc_data = i
  63. pass
  64. pass
  65. if winhc_data is None:
  66. return
  67. li = []
  68. holo_keys = None
  69. for i in key_set:
  70. tmp_json = base_info.copy()
  71. summary_max, summary_max_p_name = get_max_data(data, "$.summary." + i)
  72. latest_date_max, latest_date_max_p_name = get_max_data(data, "$.latest_date." + i)
  73. winhc_dim_num = json_path(winhc_data, '$.summary.' + i)
  74. latest_date_max = datetime_format(latest_date_max)
  75. winhc_dim_date = json_path(winhc_data, '$.latest_date.' + i)
  76. if winhc_dim_date is not None and winhc_dim_date == '':
  77. winhc_dim_date = None
  78. winhc_dim_date = datetime_format(winhc_dim_date)
  79. if (latest_date_max is None or latest_date_max == '') and (
  80. summary_max is None or summary_max == 0) and winhc_dim_date is None and (
  81. winhc_dim_num is None or winhc_dim_num == 0):
  82. # print('这个维度为空...', i, )
  83. continue
  84. pass
  85. other_data = {
  86. "id": tmp_json['company_id'] + "_" + ds + "_" + i,
  87. "dim_name": i,
  88. "dim_max_num": summary_max,
  89. "dim_max_num_business_name": summary_max_p_name,
  90. "winhc_dim_num": winhc_dim_num,
  91. "dim_max_date": latest_date_max,
  92. "dim_max_date_business_name": latest_date_max_p_name,
  93. "winhc_dim_date": winhc_dim_date,
  94. "other_info": json.dumps({"summary": get_all_data_by_item(data, '$.summary.' + i),
  95. 'latest_date': get_all_data_by_item(data, '$.latest_date.' + i)}),
  96. "update_time": get_now(),
  97. "create_time": get_now(),
  98. "ds": ds,
  99. }
  100. tmp_json.update(other_data)
  101. li.append(tmp_json)
  102. if holo_keys is None:
  103. holo_keys = list(tmp_json.keys())
  104. pass
  105. log.info('output data: {}'.format(li))
  106. insert_many(li, holo_keys, HOLO_TABLE_NAME, holo_client)
  107. del_num = 0
  108. try:
  109. del_col.insert_many(data, ordered=False)
  110. del_num = col.delete_many({"_id": {"$regex": "^" + deleted_key}}).deleted_count
  111. except:
  112. pass
  113. log.info("deleted mongo _id: {} , deleted count: {}".format(deleted_key, del_num))
  114. return li
  115. q = queue.Queue(5000)
  116. class Work(Thread):
  117. def run(self):
  118. while True:
  119. data_transform(q.get())
  120. def main(max_round: int = 2, interval_of_sed: int = 300):
  121. thread_num = 10
  122. for i in range(thread_num):
  123. Work().start()
  124. pass
  125. round_num = 0
  126. while True:
  127. round_num += 1
  128. ds = get_ds()
  129. # ds = '20221205'
  130. log.info('{},第{}遍轮循...'.format(ds, round_num))
  131. xxl_q = xxl_queue(pop_threshold=2)
  132. for i in col.find({"_id": {"$regex": "^" + ds}}).batch_size(200):
  133. _id = i['_id']
  134. key = _id[:_id.rfind('_')]
  135. result = xxl_q.append(key=key, obj=i)
  136. if result:
  137. q.put(result)
  138. pass
  139. if round_num >= max_round:
  140. break
  141. try:
  142. log.info('{},第{}遍轮循结束.'.format(ds, round_num))
  143. time.sleep(interval_of_sed)
  144. pass
  145. except:
  146. pass
  147. pass
  148. pass
  149. # tmp_data = {
  150. # "_id": "20221214_0000b94de6aa5fba1f4daa0f2c353815_winhc",
  151. # "base_info": {
  152. # "cate_first": "租赁和商务服务业",
  153. # "cate_second": "商务服务业",
  154. # "cate_third": "旅行社及相关服务",
  155. # "city": "衢州市",
  156. # "company_id": "0000b94de6aa5fba1f4daa0f2c353815",
  157. # "company_name": "龙游县文化旅游发展有限公司",
  158. # "company_org_type": "有限责任公司(非自然人投资或控股的法人独资)",
  159. # "county": "龙游县",
  160. # "credit_code": "91330825573984254D",
  161. # "org_number": "573984254",
  162. # "province": "浙江省",
  163. # "reg_number": "330825000024620"
  164. # },
  165. # "competitor_product_name": "winhc",
  166. # "latest_date": {
  167. # "严重违法": None,
  168. # "公示催告": None,
  169. # "历史变更": "2022-04-15 00:00:00",
  170. # "双随机抽查": None,
  171. # "司法拍卖": None,
  172. # "土地公示": "2022-06-14 00:00:00",
  173. # "基本信息": "2021-12-17 00:00:00",
  174. # "失信信息": None,
  175. # "开庭公告": "2022-10-24 09:00:00",
  176. # "抽查检查": None,
  177. # "招投标": None,
  178. # "欠税公告": None,
  179. # "法院公告": None,
  180. # "环保处罚": None,
  181. # "税收违法": None,
  182. # "立案信息": "2019-08-21 00:00:00",
  183. # "终本案件": None,
  184. # "经营异常": None,
  185. # "行政处罚": None,
  186. # "行政许可": "2022-08-17 00:00:00",
  187. # "被执行人": None,
  188. # "裁判文书": "2020-05-29 00:00:00",
  189. # "诉前调解": None,
  190. # "询价评估": None,
  191. # "购地信息": "2022-06-27 00:00:00",
  192. # "送达公告": None,
  193. # "限制消费": None
  194. # },
  195. # "spider_date": "2022-12-14 10:09:43",
  196. # "summary": {
  197. # "主要成员": 2,
  198. # "企业年报": 9,
  199. # "历史主要成员": 1,
  200. # "历史变更": 32,
  201. # "历史对外投资": 0,
  202. # "历史股东信息": 0,
  203. # "商标": 97,
  204. # "土地公示": 11,
  205. # "对外投资": 21,
  206. # "股东信息": 1,
  207. # "行政许可": 10,
  208. # "裁判文书": 5,
  209. # "购地信息": 12
  210. # }
  211. # }
  212. #
  213. # tmp_data_2 = {
  214. # "_id": "20221215_0000b94de6aa5fba1f4daa0f2c353815_winhc",
  215. # "base_info": {
  216. # "cate_first": "租赁和商务服务业",
  217. # "cate_second": "商务服务业",
  218. # "cate_third": "旅行社及相关服务",
  219. # "city": "衢州市",
  220. # "company_id": "0000b94de6aa5fba1f4daa0f2c353815",
  221. # "company_name": "龙游县文化旅游发展有限公司",
  222. # "company_org_type": "有限责任公司(非自然人投资或控股的法人独资)",
  223. # "county": "龙游县",
  224. # "credit_code": "91330825573984254D",
  225. # "org_number": "573984254",
  226. # "province": "浙江省",
  227. # "reg_number": "330825000024620"
  228. # },
  229. # "competitor_product_name": "qcc",
  230. # "latest_date": {
  231. # "严重违法": None,
  232. # "公示催告": None,
  233. # "双随机抽查": None,
  234. # "司法拍卖": None,
  235. # "失信信息": None,
  236. # "抽查检查": None,
  237. # "招投标": None,
  238. # "欠税公告": None,
  239. # "法院公告": None,
  240. # "环保处罚": None,
  241. # "税收违法": None,
  242. # "终本案件": None,
  243. # "经营异常": None,
  244. # "行政处罚": None,
  245. # "被执行人": None,
  246. # "诉前调解": None,
  247. # "询价评估": None,
  248. # "送达公告": None,
  249. # "限制消费": None
  250. # },
  251. # "spider_date": "2022-12-14 10:09:43",
  252. # "summary": {
  253. # "主要成员": 0,
  254. # "企业年报": 0,
  255. # "历史主要成员": 0,
  256. # "历史变更": 0,
  257. # "历史对外投资": 0,
  258. # "历史股东信息": 0,
  259. # "商标": 0,
  260. # "土地公示": 0,
  261. # "对外投资": 0,
  262. # "股东信息": 0,
  263. # "行政许可": 0,
  264. # "裁判文书": 0,
  265. # "购地信息": None
  266. # }
  267. # }
  268. # def test():
  269. # ds = get_ds()
  270. # for i in col.find({"_id": {"$regex": "^" + ds}}).batch_size(200):
  271. # print(map_2_json_str(i))
  272. # break
  273. # pass
  274. #
  275. # data_transform([tmp_data,tmp_data_2])
  276. #
  277. # pass
  278. if __name__ == '__main__':
  279. # test()
  280. #
  281. log.info(f"input args: {sys.argv}")
  282. parser = argparse.ArgumentParser()
  283. parser.add_argument("-m", "--max-round", type=int, default=2, help='最大迭代轮次')
  284. parser.add_argument("-i", "--interval_of_sed", type=int, default=300, help='每轮间隔时间(秒)')
  285. args = parser.parse_args()
  286. main(max_round=args.max_round, interval_of_sed=args.interval_of_sed)
  287. pass