""" 同步小区价格相关信息至ES206 """ import logging import pandas as pd from goojia_utils import ElasticSearchClient, commutils, dateutils, areautils from elasticsearch import helpers log = logging.getLogger(__name__) es206_client = ElasticSearchClient(host='192.168.0.206') index_name = "community_summary" path = "/home/airflow/data/community" m_date = dateutils.iso_format() def build_doc(item): doc_dict = dict() if 'city_avg_price' in item and pd.notna(item['city_avg_price']): doc_dict['city_avg_price'] = int(item['city_avg_price']) if 'region_avg_price' in item and pd.notna(item['region_avg_price']): doc_dict['region_avg_price'] = int(item['region_avg_price']) if 'avg_price' in item and pd.notna(item['avg_price']): doc_dict['avg_price'] = int(item['avg_price']) if 'avg_price_region_ranking' in item and pd.notna(item['avg_price_region_ranking']): doc_dict['avg_price_region_ranking'] = int(item['avg_price_region_ranking']) if 'avg_price_ranking' in item and pd.notna(item['avg_price_ranking']): doc_dict['avg_price_ranking'] = int(item['avg_price_ranking']) if 'year_on_year' in item and pd.notna(item['year_on_year']): doc_dict['year_on_year'] = round(item['year_on_year'], 3) if 'half_on_year' in item and pd.notna(item['half_on_year']): doc_dict['half_on_year'] = round(item['half_on_year'], 3) if 'rent_price' in item and pd.notna(item['rent_price']): doc_dict['rent_price'] = int(item['rent_price']) if 'guide_price' in item and pd.notna(item['guide_price']): doc_dict['guide_price'] = int(item['guide_price']) return doc_dict def sync_by_city(city, total_seaweed_df): seaweed_df = total_seaweed_df[total_seaweed_df['city'] == city].copy() if len(seaweed_df) == 0: return seaweed_df = seaweed_df.reset_index(drop=True) # 小区所在城市均价 city_price_df = commutils.get_city_price(city=city) if len(city_price_df) > 0: city_price_df = city_price_df[['city', 'avg_price']] city_price_df = city_price_df.rename(columns={'avg_price': 'city_avg_price'}) seaweed_df = pd.merge(seaweed_df, city_price_df, how='left', on=['city']) # 小区所在区域均价 region_price_df = commutils.get_region_price(city=city) if len(region_price_df) > 0: 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(city=city) 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(cities=[city], 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(cities=[city], 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(city=city) if len(rent_price_df) > 0: 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']) # 政府指导价 gov_price_df = commutils.get_gov_guide_price(city=city) if len(gov_price_df) > 0: gov_price_df = gov_price_df[['city', 'region', 'name', 'avg_price']] gov_price_df = gov_price_df.rename(columns={'avg_price': 'guide_price'}) seaweed_df = pd.merge(seaweed_df, gov_price_df, how='left', on=['city', 'region', 'name']) actions = list() count = 0 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) == 5000: helpers.bulk(es206_client.elastic_client, actions) count += len(actions) log.info("更新:" + str(count)) actions.clear() if len(actions) > 0: helpers.bulk(es206_client.elastic_client, actions) actions.clear() def sync(city=None): log.info(f"开始同步价格信息至206 {index_name}") if pd.notna(city): cities = [city] else: cities = areautils.get_all_cities() seaweed_df = pd.read_csv(path + '/seaweed.csv', sep='@', encoding='UTF-8') for city in cities: log.info(f"开始同步{city}") sync_by_city(city, seaweed_df) log.info(f"完成同步{city}") log.info(f"完成同步价格信息至206 {index_name}") sync()