178 lines
7.9 KiB
Python
178 lines
7.9 KiB
Python
"""
|
|
户均总价根据房源面积补充至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, dateutils
|
|
from elasticsearch import helpers
|
|
import sys
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from threading import Lock
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
index_name = "community_summary"
|
|
path = "/opt/airflow/dags/repo/dags/business/business/community/data"
|
|
m_date = dateutils.iso_format()
|
|
|
|
mutex = Lock()
|
|
|
|
def build_doc(item):
|
|
doc_dict = dict()
|
|
if 'total_price' in item and pd.notna(item['total_price']):
|
|
doc_dict['total_price'] = int(item['total_price'])
|
|
if 'total_price_score' in item and pd.notna(item['total_price_score']):
|
|
doc_dict['credit_detail'] = {'total_price_score': item['total_price_score']}
|
|
return doc_dict
|
|
|
|
|
|
def save(community_factor_df):
|
|
community_factor_df = community_factor_df[community_factor_df['total_price'].notna()]
|
|
community_factor_df['total_price'] = community_factor_df['total_price'].astype(int)
|
|
actions = list()
|
|
count = 0
|
|
for index, item in community_factor_df.iterrows():
|
|
doc_info = build_doc(item)
|
|
|
|
action = {
|
|
"_op_type": "update",
|
|
"_index": index_name,
|
|
"_id": item['_id'],
|
|
"doc": doc_info,
|
|
"_retry_on_conflict": 5
|
|
}
|
|
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
logging.info("已补全:" + str(count))
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
|
|
|
|
def complete_by_build_year_area(community_factor_df):
|
|
build_year_area_df = pd.read_excel(path + '/全国各区各建筑年代户均面积已去极值.xlsx')
|
|
build_year_area_dict = dict()
|
|
for index, build_year_area in build_year_area_df.iterrows():
|
|
build_year_area_dict[
|
|
build_year_area['city'] + '|' + build_year_area['region'] + "|" + build_year_area['buildyear']] = build_year_area['factor']
|
|
# 修复按城市跑批数据字段可能不存在问题
|
|
if 'finish_building_date' not in community_factor_df.columns or 'total_price' not in community_factor_df.columns:
|
|
return False
|
|
for index, item in community_factor_df[
|
|
(community_factor_df['total_price'].isna()) & (community_factor_df['finish_building_date'].notna())].iterrows():
|
|
finish_building_date = item['finish_building_date']
|
|
if finish_building_date < 1980:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|1980以前"] if item['city'] + "|" + item[
|
|
'region'] + "|1980以前" in build_year_area_dict else None
|
|
elif 1980 <= finish_building_date < 1990:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[1980-1990)"] if item['city'] + "|" + item[
|
|
'region'] + "|[1980-1990)" in build_year_area_dict else None
|
|
elif 1990 <= finish_building_date < 2000:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[1990-2000)"] if item['city'] + "|" + item[
|
|
'region'] + "|[1990-2000)" in build_year_area_dict else None
|
|
elif 2000 <= finish_building_date < 2010:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[2000-2010)"] if item['city'] + "|" + item[
|
|
'region'] + "|[2000-2010)" in build_year_area_dict else None
|
|
elif 2010 <= finish_building_date < 2020:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[2010-2020)"] if item['city'] + "|" + item[
|
|
'region'] + "|[2010-2020)" in build_year_area_dict else None
|
|
elif finish_building_date >= 2020:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|2020及以后"] if item['city'] + "|" + item[
|
|
'region'] + "|2020及以后" in build_year_area_dict else None
|
|
community_factor_df['total_price'] = community_factor_df['avg_price'] * community_factor_df['factor']
|
|
# 按各区各建筑年代户均面积补充, 得6分
|
|
community_factor_df.loc[community_factor_df['total_price'].notna(), 'total_price_score'] = 6
|
|
|
|
|
|
def complete_by_region_area(community_factor_df):
|
|
region_area_df = pd.read_excel(path + '/全国各区户均面积已去极值.xlsx')
|
|
region_area_dict = dict()
|
|
for index, region_area in region_area_df.iterrows():
|
|
region_area_dict[region_area['city'] + '|' + region_area['region']] = region_area['factor']
|
|
for index, item in community_factor_df[community_factor_df['total_price'].isna()].iterrows():
|
|
community_factor_df.loc[index, 'factor'] = region_area_dict[item['city'] + "|" + item['region']] if item['city'] + "|" + item['region'] in region_area_dict else None
|
|
community_factor_df['total_price'] = community_factor_df['avg_price'] * community_factor_df['factor']
|
|
|
|
|
|
def find_around_avg_area(city_now, region, lng, lat):
|
|
ret_es = es206_client.elastic_client.search({
|
|
"query": {
|
|
"bool": {
|
|
"filter": [
|
|
{"term": {"city": {"value": city_now}}},
|
|
{"term": {"region": {"value": region}}},
|
|
{"geo_distance": {"distance": 3000, "location": [lng, lat]}},
|
|
{"range": {"house_avg_area": {"gt": 0}}}
|
|
]
|
|
}
|
|
},
|
|
"size": 0,
|
|
"aggs": {
|
|
"house_avg_area": {
|
|
"avg": {
|
|
"field": "house_avg_area"
|
|
}
|
|
}
|
|
}
|
|
}, index='community_summary')
|
|
return ret_es['aggregations']['house_avg_area']['value']
|
|
|
|
|
|
def complete_by_round_area(community_factor_df):
|
|
"""
|
|
按周边三公里小区户均面积均值补充
|
|
"""
|
|
def deal_complete_by_round_area(index, item):
|
|
avg_price = item['avg_price']
|
|
house_avg_area = find_around_avg_area(item['city'], item['region'], item['location']['lon'],
|
|
item['location']['lat'])
|
|
mutex.acquire(timeout=60)
|
|
community_factor_df.loc[index, 'factor'] = house_avg_area
|
|
if pd.notna(avg_price) and pd.notna(house_avg_area):
|
|
community_factor_df.loc[index, 'total_price'] = avg_price * house_avg_area
|
|
# 按周边三公里小区户均面积均值补充,得5分
|
|
community_factor_df.loc[index, 'total_price_score'] = 5
|
|
mutex.release()
|
|
|
|
# for index, item in community_factor_df[community_factor_df['total_price'].isna()].iterrows():
|
|
# deal_complete_by_round_area(index, item)
|
|
with ThreadPoolExecutor(max_workers=30) as pool:
|
|
for index, item in community_factor_df[community_factor_df['total_price'].isna()].iterrows():
|
|
pool.submit(deal_complete_by_round_area, index, item)
|
|
|
|
|
|
|
|
|
|
def sync(city=None):
|
|
log.info(f"开始补全户均总价信息至206 {index_name}")
|
|
|
|
sql = "select city, region, name, avg_price, finish_building_date, location from community_summary where avg_price is not null and total_price is null "
|
|
if pd.notna(city):
|
|
sql += " and city='" + city + "'"
|
|
community_df = es206_client.scan_by_sql_to_data_frame(sql)
|
|
|
|
if len(community_df) > 0:
|
|
community_df['total_price'] = None
|
|
community_df['total_price_score'] = None
|
|
complete_by_build_year_area(community_df)
|
|
#complete_by_region_area(community_df)
|
|
complete_by_round_area(community_df)
|
|
save(community_df)
|
|
|
|
log.info(f"完成补全户均总价信息至206 {index_name}")
|
|
|
|
|
|
city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
sync(city)
|