feat: refine web analytics and history views
This commit is contained in:
@@ -46,6 +46,9 @@ class PortalRepository(Protocol):
|
||||
) -> tuple[List[Dict[str, Any]], int]:
|
||||
...
|
||||
|
||||
def list_history_month_counts(self) -> List[Dict[str, Any]]:
|
||||
...
|
||||
|
||||
def list_months(self) -> List[Dict[str, Any]]:
|
||||
...
|
||||
|
||||
@@ -94,6 +97,9 @@ class UnavailablePortalRepository:
|
||||
) -> tuple[List[Dict[str, Any]], int]:
|
||||
self._raise()
|
||||
|
||||
def list_history_month_counts(self) -> List[Dict[str, Any]]:
|
||||
self._raise()
|
||||
|
||||
def list_months(self) -> List[Dict[str, Any]]:
|
||||
self._raise()
|
||||
|
||||
@@ -374,6 +380,37 @@ WHERE run.period_start = %s
|
||||
""".strip()
|
||||
|
||||
|
||||
HISTORY_MONTH_COUNTS_SQL = """
|
||||
WITH daily AS (
|
||||
SELECT
|
||||
CASE
|
||||
WHEN run.business_date IS NOT NULL
|
||||
THEN date_trunc('month', run.business_date)::date
|
||||
ELSE date_trunc(
|
||||
'month',
|
||||
run.created_at AT TIME ZONE 'Asia/Bangkok'
|
||||
)::date
|
||||
END AS period_start,
|
||||
count(*) AS record_count
|
||||
FROM ingestion.processing_runs AS run
|
||||
WHERE run.pipeline_type = 'opera_daily'
|
||||
GROUP BY 1
|
||||
),
|
||||
monthly AS (
|
||||
SELECT run.period_start, count(*) AS record_count
|
||||
FROM reporting.monthly_runs AS run
|
||||
GROUP BY run.period_start
|
||||
)
|
||||
SELECT
|
||||
coalesce(daily.period_start, monthly.period_start) AS period_start,
|
||||
coalesce(daily.record_count, 0) AS daily_count,
|
||||
coalesce(monthly.record_count, 0) AS monthly_count
|
||||
FROM daily
|
||||
FULL OUTER JOIN monthly USING (period_start)
|
||||
ORDER BY period_start DESC
|
||||
""".strip()
|
||||
|
||||
|
||||
DAILY_DOWNLOAD_SQL = """
|
||||
SELECT
|
||||
artifact.artifact_kind,
|
||||
@@ -708,6 +745,32 @@ class PostgresPortalRepository:
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def list_history_month_counts(self) -> List[Dict[str, Any]]:
|
||||
connection = self._open()
|
||||
try:
|
||||
with connection.transaction():
|
||||
with connection.cursor() as cursor:
|
||||
self._begin(cursor)
|
||||
cursor.execute(HISTORY_MONTH_COUNTS_SQL)
|
||||
rows = list(cursor.fetchall())
|
||||
return [
|
||||
{
|
||||
"month_key": row[0].strftime("%Y-%m"),
|
||||
"daily_count": int(row[1] or 0),
|
||||
"monthly_count": int(row[2] or 0),
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
except PortalDataError:
|
||||
raise
|
||||
except Exception:
|
||||
raise PortalDataError(
|
||||
"DATABASE_QUERY_FAILED",
|
||||
"历史月份查询失败",
|
||||
) from None
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def list_months(self) -> List[Dict[str, Any]]:
|
||||
try:
|
||||
return self._analytics.list_months()
|
||||
|
||||
Reference in New Issue
Block a user