perf(db): serve presses + downtime-reason texts from the shared reference cache

press_settings.presses and settings.downtime_reasons are re-read on every 12s
overview poll; route them through the kernel cached_select() TTL cache so they
stop consuming a shared-pool checkout each poll. Part of the gateway
connection-pool-saturation fix (see fastAPI/agent_docs/db-pool-relief.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-07-09 12:06:40 +02:00
parent 360b049af9
commit 3d52e0b79e
2 changed files with 10 additions and 5 deletions

4
_db.py
View File

@ -16,6 +16,6 @@ real connection opens only when a query actually executes.
""" """
from __future__ import annotations from __future__ import annotations
from app.db.mariadb import run_select_query # noqa: F401 (kernel SELECT-only proxy) from app.db.mariadb import run_select_query, cached_select # noqa: F401 (kernel read proxies)
__all__ = ["run_select_query"] __all__ = ["run_select_query", "cached_select"]

View File

@ -46,7 +46,7 @@ from typing import Any, Optional
from fastapi import APIRouter, Query from fastapi import APIRouter, Query
from .._db import run_select_query from .._db import run_select_query, cached_select
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -128,8 +128,11 @@ def _group_carts(rows: list[dict[str, Any]], colors: dict[str, int]) -> "dict[st
def _fetch_presses(plant: str) -> list[str]: def _fetch_presses(plant: str) -> list[str]:
# Reference data (admin-edited), re-read on every 12s poll — served from the shared
# reference cache so it stops consuming a pool checkout each poll.
try: try:
rows = run_select_query( rows = cached_select(
f"tov:presses:{plant}",
"SELECT psPress AS press FROM press_settings.presses WHERE psPlant = :plant " "SELECT psPress AS press FROM press_settings.presses WHERE psPlant = :plant "
"ORDER BY CAST(psPress AS UNSIGNED), psPress", "ORDER BY CAST(psPress AS UNSIGNED), psPress",
{"plant": plant}, {"plant": plant},
@ -239,8 +242,10 @@ def _fetch_downtime_reasons(plant: str) -> dict[str, int]:
def _fetch_reason_texts() -> dict[int, str]: def _fetch_reason_texts() -> dict[int, str]:
# Reference data (admin-edited), full-table read on every poll — served from cache.
try: try:
rows = run_select_query( rows = cached_select(
"tov:reason_texts",
"SELECT id, reason_DE AS de FROM settings.downtime_reasons", {} "SELECT id, reason_DE AS de FROM settings.downtime_reasons", {}
) )
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001