72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
import logging
|
|
import sys
|
|
from goojia_utils import ElasticSearchClient, commutils
|
|
import pandas as pd
|
|
import time
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
city_name = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(city_name):
|
|
log.info("同步城市:" + city_name)
|
|
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
|
|
def find_comm_by_grid(city, geo_shape):
|
|
comm_info_df = es206_client.scan_to_data_frame('community_summary', {
|
|
"_source": ["id", "city", "region", "name", "estate_type"],
|
|
"query": {
|
|
"bool": {
|
|
"filter": [
|
|
{
|
|
"term": {
|
|
"city": {
|
|
"value": city
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"geo_polygon": {
|
|
"location": {
|
|
"points": geo_shape
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
})
|
|
return comm_info_df
|
|
|
|
estate_type_list = ["普通住宅", "别墅", "商住"]
|
|
|
|
def sync(city):
|
|
sql = f"select * from grid "
|
|
if pd.notna(city):
|
|
sql += f" where city='{city}' "
|
|
origin_grid_df = es206_client.scan_by_sql_to_data_frame(sql)
|
|
cities = origin_grid_df['city'].unique()
|
|
|
|
for curr_city in cities:
|
|
grid_df = origin_grid_df[origin_grid_df['city'] == curr_city].copy()
|
|
avg_price_df = commutils.get_avg_price(curr_city)
|
|
avg_price_df = avg_price_df[['city', 'region', 'district_name', 'avg_price']]
|
|
avg_price_df = avg_price_df.rename(columns={'district_name': 'name'})
|
|
# 求解格子均价
|
|
actions = list()
|
|
for index, item in grid_df.iterrows():
|
|
comm_info_df = find_comm_by_grid(item['city'], item['fence']['coordinates'][0])
|
|
action = dict()
|
|
ret = dict()
|
|
for estate_type in estate_type_list:
|
|
# 计算不同物业类型的小区均价
|
|
comm_info_estate_type_df = comm_info_df[comm_info_df['estate_type'] == estate_type].copy()
|
|
if len(comm_info_estate_type_df) > 0:
|
|
comm_info_estate_type_df = pd.merge(comm_info_estate_type_df, avg_price_df, how='left', on=['city', 'region', 'name'])
|
|
comm_info_estate_type_df = comm_info_estate_type_df[comm_info_estate_type_df['avg_price'].notna()]
|
|
if len(comm_info_estate_type_df) > 0:
|
|
ret[estate_type] = int(comm_info_estate_type_df['avg_price'].mean())
|
|
action['district_price'] = {''}
|
|
sync(city_name)
|