158 lines
4.7 KiB
Python
158 lines
4.7 KiB
Python
"""
|
|
相对价格排名计算同步至206
|
|
即周边一公里内小区价格排名0-1之间
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, commutils, dateutils, ClickHouseClient
|
|
from elasticsearch import helpers
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import sys
|
|
from threading import Lock
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
tmp_ck = ClickHouseClient(database='tmp')
|
|
|
|
index_name = "community_summary"
|
|
path = "/home/airflow/data/community"
|
|
m_date = dateutils.iso_format()
|
|
mutex = Lock()
|
|
|
|
|
|
def find_around_seaweed(city_now, lng, lat):
|
|
"""
|
|
查找周边均价
|
|
"""
|
|
if pd.isna(lng):
|
|
return None
|
|
ret_es = es206_client.scan_to_data_frame('community_summary', {
|
|
"_source": ["id", "avg_price"],
|
|
"query": {
|
|
"bool": {
|
|
"filter": [
|
|
{
|
|
"term": {
|
|
"city": {
|
|
"value": city_now
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"range": {
|
|
"avg_price": {
|
|
"gt": 0
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"geo_distance": {
|
|
"distance": 1000,
|
|
"location": [lng, lat]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
})
|
|
return ret_es
|
|
|
|
|
|
def build_doc(item):
|
|
doc_dict = dict()
|
|
if 'relative_price_ranking' in item and pd.notna(item['relative_price_ranking']):
|
|
doc_dict['relative_price_ranking'] = item['relative_price_ranking']
|
|
if 'credit_detail' in item:
|
|
doc_dict['credit_detail'] = item['credit_detail']
|
|
return doc_dict
|
|
|
|
|
|
find_count = 0
|
|
|
|
|
|
def find_around_price_item(seaweed):
|
|
global find_count
|
|
find_count += 1
|
|
if find_count % 1000 == 0:
|
|
log.info("已查找" + str(find_count))
|
|
if 'lng' not in seaweed:
|
|
return
|
|
lng = commutils.format_lat_lng(seaweed['lng'])
|
|
lat = commutils.format_lat_lng(seaweed['lat'])
|
|
if pd.notna(lng):
|
|
city_now = seaweed['city']
|
|
around_seaweed_df = find_around_seaweed(city_now, lng, lat)
|
|
mutex.acquire(timeout=60)
|
|
if len(around_seaweed_df) > 1:
|
|
around_seaweed_df['relative_price_ranking'] = around_seaweed_df['avg_price'].rank(pct=True)
|
|
seaweed['relative_price_ranking'] = around_seaweed_df[around_seaweed_df['id'] == seaweed['_id']].iloc[0][
|
|
'relative_price_ranking']
|
|
elif len(around_seaweed_df) == 1:
|
|
seaweed['relative_price_ranking'] = 0.5
|
|
|
|
around_community_num = len(around_seaweed_df)
|
|
if around_community_num >= 20:
|
|
relative_price_ranking_score = 10
|
|
elif 5 <= around_community_num < 20:
|
|
relative_price_ranking_score = 9
|
|
elif 1 < around_community_num < 5:
|
|
relative_price_ranking_score = 8
|
|
elif around_community_num == 1:
|
|
relative_price_ranking_score = 7
|
|
|
|
seaweed['credit_detail'] = {'around_community_num': around_community_num, 'relative_price_ranking_score': relative_price_ranking_score}
|
|
mutex.release()
|
|
|
|
|
|
def sync(city=None):
|
|
log.info("开始同步相对价格排名数据...")
|
|
|
|
tmp_sql = f"select * from community_summary "
|
|
if pd.notna(city):
|
|
tmp_sql += f" where city='{city}'"
|
|
seaweed_df = tmp_ck.read_sql_query(tmp_sql)
|
|
|
|
seaweed_list = list()
|
|
seaweed_df.apply(lambda x: seaweed_list.append(dict(x)), axis=1)
|
|
|
|
with ThreadPoolExecutor(max_workers=30) as pool:
|
|
for seaweed in seaweed_list:
|
|
pool.submit(find_around_price_item, seaweed)
|
|
|
|
# for seaweed in seaweed_list:
|
|
# find_around_price_item(seaweed)
|
|
|
|
seaweed_df = pd.DataFrame(seaweed_list)
|
|
|
|
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,
|
|
"_retry_on_conflict": 5
|
|
}
|
|
|
|
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:
|
|
try:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
except Exception as e:
|
|
log.error(e)
|
|
actions.clear()
|
|
|
|
log.info("完成同步相对价格排名数据...")
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
sync(sys_city)
|