From 416a8a8ebdd16b0b1c7b6c9f5add8072aa3798c0 Mon Sep 17 00:00:00 2001 From: Jian Shen Date: Fri, 14 Oct 2022 14:29:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BB=B7=E6=A0=BC=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/community/community_summary.py | 2 + dags/business/business/community/price.py | 94 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 dags/business/business/community/price.py diff --git a/dags/business/business/community/community_summary.py b/dags/business/business/community/community_summary.py index 553484e..8914b48 100644 --- a/dags/business/business/community/community_summary.py +++ b/dags/business/business/community/community_summary.py @@ -1,6 +1,7 @@ import logging import sys import comm +import price log = logging.getLogger(__name__) @@ -10,4 +11,5 @@ log.info("同步类型:" + type) if type == 'all': comm.sync() + price.sync() pass \ No newline at end of file diff --git a/dags/business/business/community/price.py b/dags/business/business/community/price.py new file mode 100644 index 0000000..13ef831 --- /dev/null +++ b/dags/business/business/community/price.py @@ -0,0 +1,94 @@ +""" +同步小区价格相关信息至ES206 +""" +import logging +import pandas as pd +from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, commutils, dateutils, areautils +from elasticsearch import helpers + +log = logging.getLogger(__name__) +seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed') +es206_client = ElasticSearchClient(host='192.168.0.206') + +index_name = "community_summary" +m_date = dateutils.iso_format() + + +def build_doc(item): + doc_dict = dict() + if pd.notna(item['region_avg_price']): + doc_dict['region_avg_price'] = item['region_avg_price'] + if pd.notna(item['avg_price']): + doc_dict['avg_price'] = item['avg_price'] + doc_dict['avg_price_region_ranking'] = item['avg_price_region_ranking'] + doc_dict['avg_price_ranking'] = item['avg_price_ranking'] + if pd.notna(item['year_on_year']): + doc_dict['year_on_year'] = item['year_on_year'] + if pd.notna(item['half_on_year']): + doc_dict['half_on_year'] = item['half_on_year'] + if pd.notna(item['rent_price']): + doc_dict['rent_price'] = item['rent_price'] + return doc_dict + + +def sync(): + seaweed_df = pd.DataFrame( + list(seaweed_conn.find({'city': '上海', 'cat': 'district', 'status': 0}, {'city': 1, 'region': 1, 'name': 1}) + .batch_size(100000))) + + # 小区所在区域均价 + region_price_df = commutils.get_region_price() + region_price_df = region_price_df[['city', 'region', 'avg_price']] + region_price_df = region_price_df.rename(columns={'avg_price': 'region_avg_price'}) + seaweed_df = pd.merge(seaweed_df, region_price_df, how='left', on=['city', 'region']) + + # 小区均价 + avg_price_df = commutils.get_avg_price() + avg_price_df = avg_price_df[['city', 'district_name', 'region', 'avg_price']] + avg_price_df = avg_price_df.rename(columns={'district_name': 'name'}) + seaweed_df = pd.merge(seaweed_df, avg_price_df, how='left', on=['city', 'region', 'name']) + + # 小区均价区域排名 + seaweed_df['avg_price_region_ranking'] = seaweed_df.groupby(by=['city', 'region']).rank(method='first', na_option='bottom', ascending=False)['avg_price'] + # 小区均价城市排名 + seaweed_df['avg_price_ranking'] = seaweed_df.groupby(by=['city']).rank(method='first', na_option='bottom', ascending=False)['avg_price'] + + # 12个月均价涨跌幅 + avg_price_up_and_downs_df = commutils.get_avg_price_up_and_downs(months=-12) + avg_price_up_and_downs_df = avg_price_up_and_downs_df[['city', 'region', 'district_name', 'up_and_downs']] + avg_price_up_and_downs_df = avg_price_up_and_downs_df.rename(columns={'district_name': 'name', 'up_and_downs': 'year_on_year'}) + seaweed_df = pd.merge(seaweed_df, avg_price_up_and_downs_df, how='left', on=['city', 'region', 'name']) + + # 近半年均价涨跌幅 + avg_price_up_and_downs_df = commutils.get_avg_price_up_and_downs(months=-6) + avg_price_up_and_downs_df = avg_price_up_and_downs_df[['city', 'region', 'district_name', 'up_and_downs']] + avg_price_up_and_downs_df = avg_price_up_and_downs_df.rename( + columns={'district_name': 'name', 'up_and_downs': 'half_on_year'}) + seaweed_df = pd.merge(seaweed_df, avg_price_up_and_downs_df, how='left', on=['city', 'region', 'name']) + + # 平米租金 + rent_price_df = commutils.get_rent_price() + rent_price_df = rent_price_df[['city', 'region', 'district_name', 'avg_price']] + rent_price_df = rent_price_df.rename(columns={'district_name': 'name', 'avg_price': 'rent_price'}) + seaweed_df = pd.merge(seaweed_df, rent_price_df, how='left', on=['city', 'region', 'name']) + + actions = list() + for index, item in seaweed_df.iterrows(): + doc_info = build_doc(item) + + action = { + "_op_type": "update", + "_index": index_name, + "_id": item['id'], + "doc": doc_info + } + + actions.append(action) + if len(actions) == 1000: + helpers.bulk(es206_client.elastic_client, actions) + actions.clear() + if len(actions) > 0: + helpers.bulk(es206_client.elastic_client, actions) + actions.clear() + +sync() \ No newline at end of file