docs에 dashboard 관련 추가
This commit is contained in:
1 parent
6d09d25df7
commit
a0c352f567
4 files changed
+240
-13
No files matched your search
@@ -176,6 +176,9 @@ async def get_dashboard_stats(
|
||||
end_dt = today - timedelta(days=2)
|
||||
kpi_end_dt = end_dt
|
||||
start_dt = end_dt - timedelta(days=29)
|
||||
# 이전 30일 (YouTube API day_previous와 동일 기준)
|
||||
prev_start_dt = start_dt - timedelta(days=30)
|
||||
prev_kpi_end_dt = kpi_end_dt - timedelta(days=30)
|
||||
period_desc = "최근 30일"
|
||||
else: # mode == "month"
|
||||
# 월별 차트: dimensions=month API는 YYYY-MM-01 형식 필요
|
||||
@@ -191,6 +194,12 @@ async def get_dashboard_stats(
|
||||
else:
|
||||
start_year = end_dt.year
|
||||
start_dt = date(start_year, start_month, 1)
|
||||
# 이전 12개월 (YouTube API previous와 동일 기준 — 1년 전)
|
||||
prev_start_dt = start_dt.replace(year=start_dt.year - 1)
|
||||
try:
|
||||
prev_kpi_end_dt = kpi_end_dt.replace(year=kpi_end_dt.year - 1)
|
||||
except ValueError: # 윤년 2/29 → 이전 연도 2/28
|
||||
prev_kpi_end_dt = kpi_end_dt.replace(year=kpi_end_dt.year - 1, day=28)
|
||||
period_desc = "최근 12개월"
|
||||
|
||||
start_date = start_dt.strftime("%Y-%m-%d")
|
||||
@@ -263,7 +272,23 @@ async def get_dashboard_stats(
|
||||
)
|
||||
)
|
||||
period_video_count = count_result.scalar() or 0
|
||||
logger.debug(f"[3] 기간 내 업로드 영상 수 - count={period_video_count}")
|
||||
|
||||
# 이전 기간 업로드 영상 수 조회 (trend 계산용)
|
||||
prev_count_result = await session.execute(
|
||||
select(func.count())
|
||||
.select_from(Dashboard)
|
||||
.where(
|
||||
Dashboard.user_uuid == current_user.user_uuid,
|
||||
Dashboard.platform == "youtube",
|
||||
Dashboard.platform_user_id == social_account.platform_user_id,
|
||||
Dashboard.uploaded_at >= prev_start_dt,
|
||||
Dashboard.uploaded_at <= prev_kpi_end_dt,
|
||||
)
|
||||
)
|
||||
prev_period_video_count = prev_count_result.scalar() or 0
|
||||
logger.debug(
|
||||
f"[3] 기간 내 업로드 영상 수 - current={period_video_count}, prev={prev_period_video_count}"
|
||||
)
|
||||
|
||||
# 4. Redis 캐시 조회
|
||||
# platform_user_id 기준 캐시 키: 재연동해도 채널 ID는 불변 → 캐시 유지됨
|
||||
@@ -278,6 +303,9 @@ async def get_dashboard_stats(
|
||||
for metric in response.content_metrics:
|
||||
if metric.id == "uploaded-videos":
|
||||
metric.value = str(period_video_count)
|
||||
video_trend = float(period_video_count - prev_period_video_count)
|
||||
metric.trend = video_trend
|
||||
metric.trend_direction = "up" if video_trend > 0 else ("down" if video_trend < 0 else "-")
|
||||
break
|
||||
return response
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
@@ -404,10 +432,13 @@ async def get_dashboard_stats(
|
||||
else:
|
||||
logger.warning(f"[CACHE SET] 캐시 저장 실패 - key={cache_key}")
|
||||
|
||||
# 12. 업로드 영상 수 주입 (캐시 저장 후 — 항상 DB에서 직접 집계)
|
||||
# 12. 업로드 영상 수 및 trend 주입 (캐시 저장 후 — 항상 DB에서 직접 집계)
|
||||
for metric in dashboard_data.content_metrics:
|
||||
if metric.id == "uploaded-videos":
|
||||
metric.value = str(period_video_count)
|
||||
video_trend = float(period_video_count - prev_period_video_count)
|
||||
metric.trend = video_trend
|
||||
metric.trend_direction = "up" if video_trend > 0 else ("down" if video_trend < 0 else "-")
|
||||
break
|
||||
|
||||
logger.info(
|
||||
|
||||
@@ -195,9 +195,9 @@ class DataProcessor:
|
||||
|
||||
logger.info(
|
||||
f"[DataProcessor._build_content_metrics] SUCCESS - "
|
||||
f"views={views}({views_trend:+.1f}%), "
|
||||
f"watch_time={estimated_minutes_watched}min({watch_trend:+.1f}%), "
|
||||
f"subscribers={subscribers_gained}({subs_trend:+.1f}%)"
|
||||
f"views={views}({views_trend:+.1f}), "
|
||||
f"watch_time={estimated_minutes_watched}min({watch_trend:+.1f}), "
|
||||
f"subscribers={subscribers_gained}({subs_trend:+.1f})"
|
||||
)
|
||||
|
||||
return [
|
||||
@@ -206,7 +206,7 @@ class DataProcessor:
|
||||
label="조회수",
|
||||
label_en="Total Views",
|
||||
value=self._format_number(int(views)),
|
||||
trend=views_trend,
|
||||
trend=round(float(views_trend), 1),
|
||||
trend_direction=views_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -214,7 +214,7 @@ class DataProcessor:
|
||||
label="시청시간",
|
||||
label_en="Watch Time",
|
||||
value=f"{round(estimated_minutes_watched / 60, 1)}시간",
|
||||
trend=watch_trend,
|
||||
trend=round(watch_trend / 60, 1),
|
||||
trend_direction=watch_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -222,7 +222,7 @@ class DataProcessor:
|
||||
label="평균 시청시간",
|
||||
label_en="Avg. View Duration",
|
||||
value=f"{round(average_view_duration / 60)}분",
|
||||
trend=duration_trend,
|
||||
trend=round(duration_trend / 60, 1),
|
||||
trend_direction=duration_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -230,7 +230,7 @@ class DataProcessor:
|
||||
label="신규 구독자",
|
||||
label_en="New Subscribers",
|
||||
value=self._format_number(int(subscribers_gained)),
|
||||
trend=subs_trend,
|
||||
trend=round(float(subs_trend), 1),
|
||||
trend_direction=subs_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -238,7 +238,7 @@ class DataProcessor:
|
||||
label="좋아요",
|
||||
label_en="Likes",
|
||||
value=self._format_number(int(likes)),
|
||||
trend=likes_trend,
|
||||
trend=round(float(likes_trend), 1),
|
||||
trend_direction=likes_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -246,7 +246,7 @@ class DataProcessor:
|
||||
label="댓글",
|
||||
label_en="Comments",
|
||||
value=self._format_number(int(comments)),
|
||||
trend=comments_trend,
|
||||
trend=round(float(comments_trend), 1),
|
||||
trend_direction=comments_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -254,7 +254,7 @@ class DataProcessor:
|
||||
label="공유",
|
||||
label_en="Shares",
|
||||
value=self._format_number(int(shares)),
|
||||
trend=shares_trend,
|
||||
trend=round(float(shares_trend), 1),
|
||||
trend_direction=shares_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
@@ -263,7 +263,7 @@ class DataProcessor:
|
||||
label_en="Uploaded Videos",
|
||||
value=str(period_video_count),
|
||||
trend=0.0,
|
||||
trend_direction="up",
|
||||
trend_direction="-",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
Reference in new issue
Block a user