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
+44
View File
@@ -186,6 +186,49 @@ def calculate_mom_yoy_for_office(city, district, office_name):
office_rent_stat_conn.bulk_write(actions)
def calculate_mom_yoy_by_month(year_month, city=None):
"""
批量计算某月所有写字楼的 mom (环比) 和 yoy (同比) 并更新
"""
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
base_query = {'cat': 'office'}
if pd.notna(city) and city != '':
base_query['city'] = city
proj = {'_id': 1, 'city': 1, 'district': 1, 'name': 1, 'avg_price': 1}
curr_docs = list(office_rent_stat_conn.find({**base_query, 'date': curr_date}, proj))
if not curr_docs:
return
prev_docs = list(office_rent_stat_conn.find({**base_query, 'date': prev_date}, proj))
ly_docs = list(office_rent_stat_conn.find({**base_query, 'date': ly_date}, proj))
prev_map = {(d.get('city'), d.get('district'), d.get('name')): d.get('avg_price') for d in prev_docs if d.get('avg_price')}
ly_map = {(d.get('city'), d.get('district'), d.get('name')): d.get('avg_price') for d in ly_docs if d.get('avg_price')}
actions = []
for doc in curr_docs:
curr_price = doc.get('avg_price')
if not curr_price:
continue
key = (doc.get('city'), doc.get('district'), doc.get('name'))
prev_price = prev_map.get(key)
ly_price = ly_map.get(key)
mom = round((curr_price - prev_price) / prev_price * 100, 2) if prev_price else None
yoy = round((curr_price - ly_price) / ly_price * 100, 2) if ly_price else None
actions.append(UpdateOne({'_id': doc['_id']}, {'$set': {'mom': mom, 'yoy': yoy}}))
if len(actions) == 5000:
office_rent_stat_conn.bulk_write(actions)
actions.clear()
if actions:
office_rent_stat_conn.bulk_write(actions)
def calcByCity(city, year_month):
"""
写字楼基准价
@@ -212,6 +255,7 @@ def calcByCity(city, year_month):
office = getOffice(city)
update_office(city, office, sample, year_month)
calculate_mom_yoy_by_month(year_month, city)
def update_office(city, office, sample, year_month):
+36 -16
View File
@@ -37,25 +37,36 @@ def calc(city, year_month):
region_median_count_prices = {'median': {}, 'count': {}}
actions = list()
year_month = int(year_month)
last_month = int(
dateutils.format_date(dateutils.add_months(dateutils.parse_date(str(year_month), '%Y%m'), months=-1), '%Y%m'))
ly_month = year_month - 100
prev_city_stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'city', 'date': {'$lte': int(last_month)}, 'avg_price': {'$lte': 15, '$gt': 0}},
sort=[('date', -1)]
)
if pd.isna(city_price):
# 处理均价为空情况,用上月均价替代
stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'city', 'date': {'$lte': int(last_month)}, 'avg_price': {'$lte': 15, '$gt': 0}},
sort=[('date', -1)]
)
if pd.isna(stat):
if pd.isna(prev_city_stat):
return False
last_month_city_price = stat['avg_price']
last_month_city_price = prev_city_stat['avg_price']
if pd.isna(last_month_city_price):
return False
city_price = last_month_city_price
ly_city_stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'city', 'date': int(ly_month), 'avg_price': {'$lte': 15, '$gt': 0}}
)
city_mom = round((city_price - prev_city_stat['avg_price']) / prev_city_stat['avg_price'] * 100, 2) if (
prev_city_stat and prev_city_stat.get('avg_price')) else None
city_yoy = round((city_price - ly_city_stat['avg_price']) / ly_city_stat['avg_price'] * 100, 2) if (
ly_city_stat and ly_city_stat.get('avg_price')) else None
# 写入MONGODB
action = UpdateOne({'city': city, 'cat': 'city', 'date': int(year_month)},
{'$set': {'city': city, 'cat': 'city', 'date': int(year_month), 'avg_price': city_price,
'mom': city_mom, 'yoy': city_yoy,
'updated_date': datetime.now()}},
upsert=True)
@@ -63,24 +74,33 @@ def calc(city, year_month):
for region in areautils.get_all_districts(city):
region_avg_prices = region_median_count_prices['median']
region_count = region_median_count_prices['count']
prev_district_stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'district', 'date': {'$lte': int(last_month)}, 'district': region, 'avg_price': {'$lte': 15, '$gt': 0}},
sort=[('date', -1)]
)
if region in region_avg_prices and region_count[region] >= 10:
avg_price = region_avg_prices[region]
avg_price = round(region_avg_prices[region], 2)
else:
# 处理均价为空情况,用上月均价替代
district_stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'district', 'date': {'$lte': int(last_month)}, 'district': region, 'avg_price': {'$lte': 15, '$gt': 0}},
sort=[('date', -1)]
)
if pd.isna(district_stat):
if pd.isna(prev_district_stat):
continue
last_month_region_price = district_stat['avg_price']
last_month_region_price = prev_district_stat['avg_price']
if pd.isna(last_month_region_price):
continue
avg_price = last_month_region_price
avg_price = round(last_month_region_price, 2)
ly_district_stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'district', 'date': int(ly_month), 'district': region, 'avg_price': {'$lte': 15, '$gt': 0}}
)
region_mom = round((avg_price - prev_district_stat['avg_price']) / prev_district_stat['avg_price'] * 100, 2) if (
prev_district_stat and prev_district_stat.get('avg_price')) else None
region_yoy = round((avg_price - ly_district_stat['avg_price']) / ly_district_stat['avg_price'] * 100, 2) if (
ly_district_stat and ly_district_stat.get('avg_price')) else None
action = UpdateOne({'city': city, 'cat': 'district', 'date': int(year_month), 'district': region},
{'$set': {'city': city, 'cat': 'district', 'date': int(year_month), 'district': region,
'avg_price': round(avg_price, 2), 'updated_date': datetime.now()}},
'avg_price': avg_price, 'mom': region_mom, 'yoy': region_yoy,
'updated_date': datetime.now()}},
upsert=True)
actions.append(action)
office_rent_stat_conn.bulk_write(actions)