운영 안정화와 화면 회귀 검증을 반영
Some checks failed
API contract / OpenAPI type drift (push) Failing after 6m2s

This commit is contained in:
Yun Chan 2026-10-20 16:33:48 +09:00
parent 6e3123091a
commit aeddaa6972
69 changed files with 3178 additions and 424 deletions

View file

@ -3,6 +3,7 @@
from __future__ import annotations
from dataclasses import dataclass
import re
from ..db import acquire
@ -12,18 +13,31 @@ class AdminHealthMaintenanceResult:
applied: bool
rollup_days: int
retention_days: int
rollup_retention_days: int
rollup_event_count: int
rollup_bucket_count: int
upserted_rollups: int
prunable_event_count: int
deleted_events: int
# apply 전에 이미 존재한 만료 일별 rollup 수다. 이번 집계에서 새로 생긴 행은 포함하지 않는다.
prunable_rollup_count: int
deleted_rollups: int
def _validate_windows(*, rollup_days: int, retention_days: int) -> None:
def _validate_windows(
*,
rollup_days: int,
retention_days: int,
rollup_retention_days: int,
) -> None:
if rollup_days < 1:
raise ValueError("rollup_days must be 1 or greater")
if retention_days < rollup_days:
raise ValueError("retention_days must be greater than or equal to rollup_days")
if rollup_retention_days < retention_days:
raise ValueError(
"rollup_retention_days must be greater than or equal to retention_days"
)
def _deleted_count(command_tag: str) -> int:
@ -45,19 +59,74 @@ def _row_int(row, key: str) -> int:
return 0
_EXPECTED_ROLLUP_DELETE_QUAL = "(app.current_role_name()='admin')"
def _normalize_policy_expression(expression: object) -> str:
if not isinstance(expression, str):
return ""
return re.sub(r"\s+", "", expression).replace("::text", "")
def _is_exact_rollup_delete_policy(policy) -> bool:
try:
return (
policy["policy_name"] == "p_admin_health_daily_rollup_delete"
and policy["command"] == "d"
and policy["permissive"] is True
and policy["rls_enabled"] is True
and _normalize_policy_expression(policy["qual"])
== _EXPECTED_ROLLUP_DELETE_QUAL
)
except (KeyError, TypeError):
return False
async def _require_rollup_delete_policy(conn) -> None:
policies = await conn.fetch(
"""
SELECT
policy.polname AS policy_name,
policy.polcmd::text AS command,
policy.polpermissive AS permissive,
relation.relrowsecurity AS rls_enabled,
pg_get_expr(policy.polqual, policy.polrelid) AS qual
FROM pg_policy AS policy
JOIN pg_class AS relation ON relation.oid = policy.polrelid
JOIN pg_namespace AS namespace ON namespace.oid = relation.relnamespace
WHERE namespace.nspname = 'app'
AND relation.relname = 'admin_health_daily_rollup'
AND policy.polpermissive IS TRUE
AND policy.polcmd IN ('d', '*')
ORDER BY policy.polname
"""
)
if len(policies) != 1 or not _is_exact_rollup_delete_policy(policies[0]):
raise RuntimeError(
"admin health daily rollup DELETE policy is missing, invalid, or permissive; "
"owner must apply migration 25_admin_health_rollup_retention.sql"
)
async def maintain_admin_health_events(
*,
rollup_days: int,
retention_days: int,
retention_days: int = 90,
rollup_retention_days: int = 365,
apply: bool = False,
) -> AdminHealthMaintenanceResult:
"""Roll up old health samples and optionally prune retained raw rows.
"""오래된 관리자 헬스 표본을 일별로 집계하고 보존 기간이 지난 행을 정리한다.
Dry-run mode returns the affected event and bucket counts without mutating
data. Apply mode upserts daily service rollups first, then deletes only raw
events older than the retention window.
dry-run은 데이터를 바꾸지 않고 영향받는 표본과 버킷 수만 반환한다.
prunable_rollup_count는 apply 전에 존재한 만료 일별 rollup 수다. 이번 집계에서
새로 생긴 만료 rollup은 이 수에 포함하지 않지만, apply에서는 일별 rollup upsert 후
raw 표본과 만료된 일별 rollup을 같은 트랜잭션에서 순서대로 삭제한다.
"""
_validate_windows(rollup_days=rollup_days, retention_days=retention_days)
_validate_windows(
rollup_days=rollup_days,
retention_days=retention_days,
rollup_retention_days=rollup_retention_days,
)
async with acquire(role="admin") as conn:
rollup_stats = await conn.fetchrow(
"""
@ -78,9 +147,19 @@ async def maintain_admin_health_events(
""",
retention_days,
)
rollup_prune_stats = await conn.fetchrow(
"""
SELECT COUNT(*)::int AS rollup_count
FROM app.admin_health_daily_rollup
WHERE rollup_date < current_date - $1::int
""",
rollup_retention_days,
)
upserted_rollups = 0
deleted_events = 0
deleted_rollups = 0
if apply:
await _require_rollup_delete_policy(conn)
rows = await conn.fetch(
"""
WITH rolled AS (
@ -164,14 +243,26 @@ async def maintain_admin_health_events(
retention_days,
)
)
deleted_rollups = _deleted_count(
await conn.execute(
"""
DELETE FROM app.admin_health_daily_rollup
WHERE rollup_date < current_date - $1::int
""",
rollup_retention_days,
)
)
return AdminHealthMaintenanceResult(
applied=apply,
rollup_days=rollup_days,
retention_days=retention_days,
rollup_retention_days=rollup_retention_days,
rollup_event_count=_row_int(rollup_stats, "event_count"),
rollup_bucket_count=_row_int(rollup_stats, "bucket_count"),
upserted_rollups=upserted_rollups,
prunable_event_count=_row_int(prune_stats, "event_count"),
deleted_events=deleted_events,
prunable_rollup_count=_row_int(rollup_prune_stats, "rollup_count"),
deleted_rollups=deleted_rollups,
)