新增价格信息

This commit is contained in:
Jian Shen
2022-10-14 14:29:10 +08:00
parent 0f98e1e935
commit 416a8a8ebd
2 changed files with 96 additions and 0 deletions
@@ -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
+94
View File
@@ -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()