171 lines
5.2 KiB
Python
171 lines
5.2 KiB
Python
"""
|
|
小区周边消费同步至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, commutils, dateutils, areautils
|
|
from elasticsearch import helpers
|
|
import numpy as np
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import sys
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
index_name = "community_summary"
|
|
m_date = dateutils.iso_format()
|
|
path = "/home/airflow/data/community"
|
|
|
|
|
|
def find_around_price(lng, lat):
|
|
"""
|
|
查找周边均价
|
|
"""
|
|
if pd.isna(lng):
|
|
return None
|
|
ret_es = es206_client.elastic_client.search({
|
|
"query": {
|
|
"bool": {
|
|
"filter": [
|
|
{
|
|
"term": {
|
|
"status": {
|
|
"value": 0
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"range": {
|
|
"price": {
|
|
"gt": 0
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"terms": {
|
|
"tag": [
|
|
"美食;中餐厅",
|
|
"美食;外国餐厅"
|
|
]
|
|
}
|
|
},
|
|
{
|
|
"geo_distance": {
|
|
"distance": 1000,
|
|
"location": [lng, lat]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"size": 0,
|
|
"aggs": {
|
|
"price": {
|
|
"avg": {
|
|
"field": "price"
|
|
}
|
|
}
|
|
}
|
|
}, index='bd_poi')
|
|
return ret_es['aggregations']['price']['value']
|
|
|
|
|
|
def build_doc(item):
|
|
doc_dict = dict()
|
|
if 'avg_cost' in item and pd.notna(item['avg_cost']):
|
|
doc_dict['avg_cost'] = item['avg_cost']
|
|
return doc_dict
|
|
|
|
|
|
def update_status(new_df):
|
|
actions = list()
|
|
|
|
def deal(item):
|
|
action_doc = {
|
|
"_op_type": "update",
|
|
"_index": "bd_poi",
|
|
"_id": item['_id'],
|
|
"doc": {'status': 0}
|
|
}
|
|
actions.append(action_doc)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
return item
|
|
|
|
new_df.apply(lambda x: deal(x), axis=1)
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
|
|
|
|
def sync(city=None):
|
|
log.info("开始同步消费数据...")
|
|
# 暂时注释
|
|
# query_sql = "select id, city, tag, type, price from bd_poi where price > 0 and tag in('美食;中餐厅', '美食;外国餐厅') "
|
|
# if pd.notna(city):
|
|
# query_sql += " and city='" + areautils.get_city_display(city) + "'"
|
|
# df = es206_client.scan_by_sql_to_data_frame(query_sql)
|
|
#
|
|
# # 按百分位5-95筛选数据
|
|
# cities = df['city'].unique()
|
|
# for city_now in cities:
|
|
# df.loc[df['city'] == city_now, 'minPrice5'] = np.percentile(a=df[df['city'] == city_now]['price'], q=5, axis=0)
|
|
# df.loc[df['city'] == city_now, 'minPrice95'] = np.percentile(a=df[df['city'] == city_now]['price'], q=95, axis=0)
|
|
#
|
|
# new_df = df[(df['price'] >= df['minPrice5']) & (df['price'] <= df['minPrice95'])]
|
|
# new_df['new_mean'] = new_df.groupby(by=['city'])['price'].transform(np.mean)
|
|
#
|
|
# # 按最新的百分位更新库中价格状态
|
|
# update_status(new_df)
|
|
|
|
seaweed_df = pd.read_csv(path + '/seaweed.csv', sep='@', encoding='UTF-8')
|
|
if pd.notna(city):
|
|
seaweed_df = seaweed_df[seaweed_df['city'] == city].copy()
|
|
seaweed_df = seaweed_df.reset_index(drop=True)
|
|
|
|
def find_around_price_item(index, item):
|
|
if index % 1000 == 0:
|
|
log.info("已查找" + str(index))
|
|
if 'lng' not in item:
|
|
return
|
|
lng = commutils.format_lat_lng(item['lng'])
|
|
lat = commutils.format_lat_lng(item['lat'])
|
|
price = find_around_price(lng, lat)
|
|
if pd.notna(price):
|
|
seaweed_df.loc[index, 'avg_cost'] = price
|
|
else:
|
|
seaweed_df.loc[index, 'avg_cost'] = None
|
|
with ThreadPoolExecutor(max_workers=80) as pool:
|
|
for index, item in seaweed_df.iterrows():
|
|
pool.submit(find_around_price_item, index, item)
|
|
# for index, item in seaweed_df.iterrows():
|
|
# find_around_price_item(index, item)
|
|
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:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
|
|
log.info("完成同步消费数据...")
|
|
|
|
|
|
city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
sync(city) |