Add monthly MVP snapshots and API
This commit is contained in:
@@ -249,7 +249,7 @@ def _is_effectively_empty_snapshot_payload(
|
||||
items = payload.get("items")
|
||||
return not isinstance(items, list) or len(items) == 0
|
||||
|
||||
if snapshot_type in {"weekly-leaderboard", "monthly-leaderboard"}:
|
||||
if snapshot_type in {"weekly-leaderboard", "monthly-leaderboard", "monthly-mvp"}:
|
||||
items = payload.get("items")
|
||||
return not isinstance(items, list) or len(items) == 0
|
||||
|
||||
@@ -287,6 +287,8 @@ def _build_snapshot_filename(*, snapshot_type: str, metric: str | None) -> str:
|
||||
if snapshot_type == "monthly-leaderboard":
|
||||
metric_suffix = "matches-over-100-kills" if metric == "matches_over_100_kills" else _slugify(metric or "unknown")
|
||||
return f"monthly-{metric_suffix}.json"
|
||||
if snapshot_type == "monthly-mvp":
|
||||
return "monthly-mvp.json"
|
||||
metric_suffix = _slugify(metric or "")
|
||||
base_name = _slugify(snapshot_type)
|
||||
return f"{base_name}-{metric_suffix}.json" if metric_suffix else f"{base_name}.json"
|
||||
|
||||
@@ -10,6 +10,7 @@ from .historical_storage import (
|
||||
list_historical_server_summaries,
|
||||
list_historical_servers,
|
||||
list_monthly_leaderboard,
|
||||
list_monthly_mvp_ranking,
|
||||
list_recent_historical_matches,
|
||||
list_weekly_leaderboard,
|
||||
)
|
||||
@@ -17,6 +18,7 @@ from .historical_storage import (
|
||||
SNAPSHOT_TYPE_SERVER_SUMMARY = "server-summary"
|
||||
SNAPSHOT_TYPE_WEEKLY_LEADERBOARD = "weekly-leaderboard"
|
||||
SNAPSHOT_TYPE_MONTHLY_LEADERBOARD = "monthly-leaderboard"
|
||||
SNAPSHOT_TYPE_MONTHLY_MVP = "monthly-mvp"
|
||||
SNAPSHOT_TYPE_RECENT_MATCHES = "recent-matches"
|
||||
|
||||
SUPPORTED_SNAPSHOT_TYPES = frozenset(
|
||||
@@ -24,6 +26,7 @@ SUPPORTED_SNAPSHOT_TYPES = frozenset(
|
||||
SNAPSHOT_TYPE_SERVER_SUMMARY,
|
||||
SNAPSHOT_TYPE_WEEKLY_LEADERBOARD,
|
||||
SNAPSHOT_TYPE_MONTHLY_LEADERBOARD,
|
||||
SNAPSHOT_TYPE_MONTHLY_MVP,
|
||||
SNAPSHOT_TYPE_RECENT_MATCHES,
|
||||
}
|
||||
)
|
||||
@@ -121,7 +124,16 @@ def build_historical_server_snapshots(
|
||||
limit=leaderboard_limit,
|
||||
db_path=db_path,
|
||||
)
|
||||
)
|
||||
|
||||
snapshots.append(
|
||||
_build_monthly_mvp_snapshot(
|
||||
server_key,
|
||||
generated_at_value,
|
||||
limit=leaderboard_limit,
|
||||
db_path=db_path,
|
||||
)
|
||||
)
|
||||
|
||||
snapshots.append(
|
||||
_build_recent_matches_snapshot(
|
||||
@@ -168,6 +180,14 @@ def build_priority_historical_snapshots(
|
||||
db_path=db_path,
|
||||
)
|
||||
)
|
||||
snapshots.append(
|
||||
_build_monthly_mvp_snapshot(
|
||||
server_key,
|
||||
generated_at_value,
|
||||
limit=leaderboard_limit,
|
||||
db_path=db_path,
|
||||
)
|
||||
)
|
||||
snapshots.append(
|
||||
_build_recent_matches_snapshot(
|
||||
server_key,
|
||||
@@ -400,6 +420,38 @@ def _build_recent_matches_snapshot(
|
||||
}
|
||||
|
||||
|
||||
def _build_monthly_mvp_snapshot(
|
||||
server_key: str,
|
||||
generated_at: datetime,
|
||||
*,
|
||||
limit: int,
|
||||
db_path: Path | None = None,
|
||||
) -> dict[str, object]:
|
||||
ranking_result = list_monthly_mvp_ranking(
|
||||
limit=limit,
|
||||
server_id=server_key,
|
||||
db_path=db_path,
|
||||
)
|
||||
month_key = str(ranking_result.get("window_start") or "")[:7] or None
|
||||
return {
|
||||
"server_key": server_key,
|
||||
"snapshot_type": SNAPSHOT_TYPE_MONTHLY_MVP,
|
||||
"metric": None,
|
||||
"window": DEFAULT_MONTHLY_SNAPSHOT_WINDOW,
|
||||
"generated_at": generated_at,
|
||||
"source_range_start": _parse_optional_timestamp(ranking_result.get("window_start")),
|
||||
"source_range_end": _parse_optional_timestamp(ranking_result.get("window_end")),
|
||||
"is_stale": False,
|
||||
"payload": {
|
||||
"server_key": server_key,
|
||||
"limit": limit,
|
||||
"month_key": month_key,
|
||||
"generated_at": _to_iso(generated_at),
|
||||
**ranking_result,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _resolve_snapshot_target_keys(
|
||||
*,
|
||||
server_key: str | None,
|
||||
|
||||
@@ -12,6 +12,7 @@ from .historical_snapshots import (
|
||||
DEFAULT_SNAPSHOT_WINDOW,
|
||||
DEFAULT_WEEKLY_SNAPSHOT_WINDOW,
|
||||
SNAPSHOT_TYPE_MONTHLY_LEADERBOARD,
|
||||
SNAPSHOT_TYPE_MONTHLY_MVP,
|
||||
SNAPSHOT_TYPE_RECENT_MATCHES,
|
||||
SNAPSHOT_TYPE_SERVER_SUMMARY,
|
||||
SNAPSHOT_TYPE_WEEKLY_LEADERBOARD,
|
||||
@@ -326,6 +327,31 @@ def build_recent_historical_matches_payload(
|
||||
}
|
||||
|
||||
|
||||
def build_monthly_mvp_payload(
|
||||
*,
|
||||
limit: int = 10,
|
||||
server_id: str | None = None,
|
||||
) -> dict[str, object]:
|
||||
"""Return the precomputed monthly MVP payload through the stable API surface."""
|
||||
snapshot_payload = build_monthly_mvp_snapshot_payload(
|
||||
limit=limit,
|
||||
server_id=server_id,
|
||||
)
|
||||
data = snapshot_payload["data"]
|
||||
return {
|
||||
"status": "ok",
|
||||
"data": {
|
||||
**data,
|
||||
"title": _build_monthly_mvp_title(
|
||||
is_all_servers=server_id == ALL_SERVERS_SLUG,
|
||||
snapshot=False,
|
||||
),
|
||||
"context": "historical-monthly-mvp",
|
||||
"source": "historical-precomputed-snapshots",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def build_historical_server_summary_snapshot_payload(
|
||||
*,
|
||||
server_slug: str | None = None,
|
||||
@@ -486,6 +512,62 @@ def build_recent_historical_matches_snapshot_payload(
|
||||
}
|
||||
|
||||
|
||||
def build_monthly_mvp_snapshot_payload(
|
||||
*,
|
||||
limit: int = 10,
|
||||
server_id: str | None = None,
|
||||
) -> dict[str, object]:
|
||||
"""Return one precomputed monthly MVP snapshot."""
|
||||
snapshot = _get_historical_snapshot_record(
|
||||
server_key=server_id,
|
||||
snapshot_type=SNAPSHOT_TYPE_MONTHLY_MVP,
|
||||
window=DEFAULT_MONTHLY_SNAPSHOT_WINDOW,
|
||||
)
|
||||
payload = snapshot.get("payload") if snapshot else {}
|
||||
items = payload.get("items") if isinstance(payload, dict) else None
|
||||
sliced_items = list(items[:limit]) if isinstance(items, list) else []
|
||||
return {
|
||||
"status": "ok",
|
||||
"data": {
|
||||
"title": _build_monthly_mvp_title(
|
||||
is_all_servers=server_id == ALL_SERVERS_SLUG,
|
||||
snapshot=True,
|
||||
),
|
||||
"context": "historical-monthly-mvp-snapshot",
|
||||
"source": "historical-precomputed-snapshots",
|
||||
"server_slug": server_id,
|
||||
"timeframe": "monthly",
|
||||
"metric": "mvp",
|
||||
"found": snapshot is not None,
|
||||
**_build_historical_snapshot_metadata(snapshot),
|
||||
"month_key": payload.get("month_key") if isinstance(payload, dict) else None,
|
||||
"window_days": payload.get("window_days") if isinstance(payload, dict) else None,
|
||||
"window_start": payload.get("window_start") if isinstance(payload, dict) else None,
|
||||
"window_end": payload.get("window_end") if isinstance(payload, dict) else None,
|
||||
"window_kind": payload.get("window_kind") if isinstance(payload, dict) else None,
|
||||
"window_label": payload.get("window_label") if isinstance(payload, dict) else None,
|
||||
"uses_fallback": bool(payload.get("uses_fallback")) if isinstance(payload, dict) else False,
|
||||
"selection_reason": payload.get("selection_reason") if isinstance(payload, dict) else None,
|
||||
"current_month_start": payload.get("current_month_start") if isinstance(payload, dict) else None,
|
||||
"current_month_closed_matches": (
|
||||
payload.get("current_month_closed_matches") if isinstance(payload, dict) else None
|
||||
),
|
||||
"previous_month_closed_matches": (
|
||||
payload.get("previous_month_closed_matches") if isinstance(payload, dict) else None
|
||||
),
|
||||
"sufficient_sample": payload.get("sufficient_sample") if isinstance(payload, dict) else None,
|
||||
"eligibility": payload.get("eligibility") if isinstance(payload, dict) else None,
|
||||
"ranking_version": payload.get("ranking_version") if isinstance(payload, dict) else None,
|
||||
"eligible_players_count": (
|
||||
payload.get("eligible_players_count") if isinstance(payload, dict) else 0
|
||||
),
|
||||
"snapshot_limit": payload.get("limit") if isinstance(payload, dict) else None,
|
||||
"limit": limit,
|
||||
"items": sliced_items,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def build_historical_server_summary_payload(
|
||||
*,
|
||||
server_slug: str | None = None,
|
||||
@@ -590,6 +672,12 @@ def _build_leaderboard_title(
|
||||
return title_by_metric.get(metric, fallback_label)
|
||||
|
||||
|
||||
def _build_monthly_mvp_title(*, is_all_servers: bool, snapshot: bool = False) -> str:
|
||||
prefix = "Snapshot " if snapshot else ""
|
||||
scope_label = "global" if is_all_servers else "por servidor"
|
||||
return f"{prefix}Top MVP mensual {scope_label}"
|
||||
|
||||
|
||||
def _enrich_server_items(items: list[dict[str, object]]) -> list[dict[str, object]]:
|
||||
target_index = {
|
||||
target.external_server_id: target
|
||||
|
||||
@@ -11,8 +11,10 @@ from .payloads import (
|
||||
build_error_payload,
|
||||
build_health_payload,
|
||||
build_historical_leaderboard_payload,
|
||||
build_monthly_mvp_payload,
|
||||
build_monthly_leaderboard_payload,
|
||||
build_monthly_leaderboard_snapshot_payload,
|
||||
build_monthly_mvp_snapshot_payload,
|
||||
build_historical_server_summary_snapshot_payload,
|
||||
build_historical_player_profile_payload,
|
||||
build_historical_server_summary_payload,
|
||||
@@ -107,6 +109,16 @@ def resolve_get_payload(path: str) -> tuple[HTTPStatus | None, dict[str, object]
|
||||
metric=metric,
|
||||
)
|
||||
|
||||
if parsed.path == "/api/historical/monthly-mvp":
|
||||
limit = _parse_limit(parsed.query)
|
||||
if limit is None:
|
||||
return HTTPStatus.BAD_REQUEST, build_error_payload("Invalid limit parameter")
|
||||
server_id = parse_qs(parsed.query).get("server", [None])[0]
|
||||
return HTTPStatus.OK, build_monthly_mvp_payload(
|
||||
limit=limit,
|
||||
server_id=server_id,
|
||||
)
|
||||
|
||||
if parsed.path == "/api/historical/snapshots/leaderboard":
|
||||
limit = _parse_limit(parsed.query)
|
||||
if limit is None:
|
||||
@@ -141,6 +153,16 @@ def resolve_get_payload(path: str) -> tuple[HTTPStatus | None, dict[str, object]
|
||||
metric=metric,
|
||||
)
|
||||
|
||||
if parsed.path == "/api/historical/snapshots/monthly-mvp":
|
||||
limit = _parse_limit(parsed.query)
|
||||
if limit is None:
|
||||
return HTTPStatus.BAD_REQUEST, build_error_payload("Invalid limit parameter")
|
||||
server_id = parse_qs(parsed.query).get("server", [None])[0]
|
||||
return HTTPStatus.OK, build_monthly_mvp_snapshot_payload(
|
||||
limit=limit,
|
||||
server_id=server_id,
|
||||
)
|
||||
|
||||
if parsed.path == "/api/historical/snapshots/weekly-leaderboard":
|
||||
limit = _parse_limit(parsed.query)
|
||||
if limit is None:
|
||||
|
||||
Reference in New Issue
Block a user