76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
"""
|
|
修正相对价格排名
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, dateutils, data_utils
|
|
from elasticsearch import helpers
|
|
import sys
|
|
from threading import Lock
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.89', username='elastic', password='elasticsearch')
|
|
|
|
index_name = "community_summary"
|
|
path = "/home/airflow/data/community"
|
|
m_date = dateutils.iso_format()
|
|
mutex = Lock()
|
|
|
|
|
|
def sync(city=None):
|
|
log.info("开始调整相对价格排名数据...")
|
|
|
|
query_filter = []
|
|
if pd.notna(city):
|
|
query_filter.append({"term": {"city": city}})
|
|
|
|
query = {
|
|
"_source": ["city", "region", "relative_price_ranking", "credit_detail"],
|
|
"query": {
|
|
"bool": {
|
|
"filter": query_filter
|
|
}
|
|
}
|
|
}
|
|
community_summary_origin_df = es206_client.scan_to_data_frame("community_summary", query)
|
|
community_summary_df = community_summary_origin_df[community_summary_origin_df['credit_detail'].notna()].reset_index(drop=True)
|
|
community_summary_df = data_utils.flatten_json(community_summary_df, 'credit_detail')
|
|
community_summary_df['around_community_max_num'] = community_summary_df.groupby(by=['city'])['around_community_num'].transform('max')
|
|
community_summary_df['relative_price_ranking_fix'] = community_summary_df['around_community_num'] / community_summary_df['around_community_max_num'] * community_summary_df['relative_price_ranking']
|
|
|
|
actions = list()
|
|
count = 0
|
|
for index, item in community_summary_df[community_summary_df['relative_price_ranking_fix'].notna()].iterrows():
|
|
doc_info = {'relative_price_ranking_fix': round(item['relative_price_ranking_fix'], 6)}
|
|
|
|
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:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
log.info("更新:" + str(count))
|
|
actions.clear()
|
|
|
|
log.info("完成调整相对价格排名数据...")
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(sys_city) and "," in sys_city:
|
|
cities = sys_city.split(",")
|
|
for city in cities:
|
|
sync(city)
|
|
else:
|
|
sync(sys_city)
|