cpa_agg.py 10.0 KB

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