feat(office): auto-calculate mom and yoy for city, district, and office monthly stats
This commit is contained in:
+36
-16
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user