feat(office): auto-calculate mom and yoy for city, district, and office monthly stats

This commit is contained in:
王文艳
2026-10-19 11:54:35 +08:00
parent 0d65af4be7
commit 2d266a30c2
6 changed files with 206 additions and 33 deletions
+18 -4
View File
@@ -82,20 +82,34 @@ def calc_by_yearmonth(office_sell, office_rent, year_month, office_sell_stat_con
merge2_df = threads.process_dataframe_multithread(merge2_df, deal, 10)
merge2_df = merge2_df[merge2_df['sell_price'].notna()]
curr_date = int(year_month)
year = curr_date // 100
month = curr_date % 100
prev_date = (year - 1) * 100 + 12 if month == 1 else curr_date - 1
ly_date = curr_date - 100
proj = {'_id': 0, 'city': 1, 'district': 1, 'name': 1, 'avg_price': 1}
prev_map = {(d.get('city'), d.get('district'), d.get('name')): d.get('avg_price') for d in office_sell_stat_conn.find({'cat': 'office', 'date': prev_date}, proj) if d.get('avg_price')}
ly_map = {(d.get('city'), d.get('district'), d.get('name')): d.get('avg_price') for d in office_sell_stat_conn.find({'cat': 'office', 'date': ly_date}, proj) if d.get('avg_price')}
actions = list()
count = 0
for index, row in merge2_df.iterrows():
curr_city = row['city']
region = row['district']
name = row['name']
sell_price = row['sell_price']
sell_price = int(row['sell_price'])
estate_type2 = row['estate_type2']
bed_kind = row['bed_kind']
location = row['location']
action = UpdateOne({'cat': 'office', 'city': curr_city, 'district': region, 'name': name, 'date': year_month},
{'$set': {'city': curr_city, 'cat': 'office', 'date': year_month, 'district': region,
prev_p = prev_map.get((curr_city, region, name))
ly_p = ly_map.get((curr_city, region, name))
mom = round((sell_price - prev_p) / prev_p * 100, 2) if prev_p else None
yoy = round((sell_price - ly_p) / ly_p * 100, 2) if ly_p else None
action = UpdateOne({'cat': 'office', 'city': curr_city, 'district': region, 'name': name, 'date': curr_date},
{'$set': {'city': curr_city, 'cat': 'office', 'date': curr_date, 'district': region,
'name': name,
'avg_price': int(sell_price), 'estate_type2': estate_type2, 'bed_kind': bed_kind,
'avg_price': sell_price, 'mom': mom, 'yoy': yoy,
'estate_type2': estate_type2, 'bed_kind': bed_kind,
'location': location,
'calc_type': 5, 'updated_date': datetime.now()}},
upsert=True)