cpa_agg.py 9.7 KB

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