feat(office): auto-calculate mom and yoy for city, district, and office monthly stats
This commit is contained in:
@@ -59,11 +59,25 @@ def calc(city, year_month):
|
||||
merge2_df = threads.process_dataframe_multithread(merge2_df, deal_city, 10)
|
||||
merge2_df = merge2_df[merge2_df['sell_price'].notna()]
|
||||
|
||||
year = year_month // 100
|
||||
month = year_month % 100
|
||||
prev_date = (year - 1) * 100 + 12 if month == 1 else year_month - 1
|
||||
ly_date = year_month - 100
|
||||
|
||||
prev_city_map = {d.get('city'): d.get('avg_price') for d in office_sell_stat_conn.find({'date': prev_date, 'cat': 'city'}) if d.get('avg_price')}
|
||||
ly_city_map = {d.get('city'): d.get('avg_price') for d in office_sell_stat_conn.find({'date': ly_date, 'cat': 'city'}) if d.get('avg_price')}
|
||||
|
||||
actions = list()
|
||||
# 写入MONGODB
|
||||
for index, row in merge2_df.iterrows():
|
||||
sell_p = int(row['sell_price'])
|
||||
prev_p = prev_city_map.get(row['city'])
|
||||
ly_p = ly_city_map.get(row['city'])
|
||||
mom = round((sell_p - prev_p) / prev_p * 100, 2) if prev_p else None
|
||||
yoy = round((sell_p - ly_p) / ly_p * 100, 2) if ly_p else None
|
||||
action = UpdateOne({'city': row['city'], 'cat': 'city', 'date': year_month},
|
||||
{'$set': {'city': row['city'], 'cat': 'city', 'date': year_month, 'calc_type': 2, 'avg_price': int(row['sell_price']),
|
||||
{'$set': {'city': row['city'], 'cat': 'city', 'date': year_month, 'calc_type': 2, 'avg_price': sell_p,
|
||||
'mom': mom, 'yoy': yoy,
|
||||
'updated_date': datetime.now()}},
|
||||
upsert=True)
|
||||
actions.append(action)
|
||||
@@ -89,10 +103,19 @@ def calc(city, year_month):
|
||||
merge2_df = threads.process_dataframe_multithread(merge2_df, deal_district, 10)
|
||||
merge2_df = merge2_df[merge2_df['sell_price'].notna()]
|
||||
|
||||
prev_dist_map = {(d.get('city'), d.get('district')): d.get('avg_price') for d in office_sell_stat_conn.find({'date': prev_date, 'cat': 'district'}) if d.get('avg_price')}
|
||||
ly_dist_map = {(d.get('city'), d.get('district')): d.get('avg_price') for d in office_sell_stat_conn.find({'date': ly_date, 'cat': 'district'}) if d.get('avg_price')}
|
||||
|
||||
# 写入MONGODB
|
||||
for index, row in merge2_df.iterrows():
|
||||
sell_p = int(row['sell_price'])
|
||||
prev_p = prev_dist_map.get((row['city'], row['district']))
|
||||
ly_p = ly_dist_map.get((row['city'], row['district']))
|
||||
mom = round((sell_p - prev_p) / prev_p * 100, 2) if prev_p else None
|
||||
yoy = round((sell_p - ly_p) / ly_p * 100, 2) if ly_p else None
|
||||
action = UpdateOne({'city': row['city'], 'district': row['district'], 'cat': 'district', 'date': year_month},
|
||||
{'$set': {'city': row['city'], 'district': row['district'], 'cat': 'district', 'date': year_month, 'calc_type': 2, 'avg_price': int(row['sell_price']),
|
||||
{'$set': {'city': row['city'], 'district': row['district'], 'cat': 'district', 'date': year_month, 'calc_type': 2, 'avg_price': sell_p,
|
||||
'mom': mom, 'yoy': yoy,
|
||||
'updated_date': datetime.now()}},
|
||||
upsert=True)
|
||||
actions.append(action)
|
||||
|
||||
Reference in New Issue
Block a user