Files
DECNET/decnet/web/router/logs/api_get_histogram.py
anti f2b3393669 chore: relicense to AGPL-3.0-or-later and add SPDX headers
Replaces LICENSE (GPLv3 -> AGPLv3) and prepends
`SPDX-License-Identifier: AGPL-3.0-or-later` to every source file
across decnet/, decnet_web/, tests/, scripts/, and tools/.

Rationale: closes the GPLv3 ASP loophole so any party operating a
modified DECNET as a network service must offer their modified
source. Personal copyright (Samuel Paschuan) + inbound=outbound
contributions make a future unilateral relicense infeasible.

- LICENSE: full AGPL-3.0 text (gnu.org/licenses/agpl-3.0.txt)
- COPYRIGHT: project copyright notice
- tools/add_spdx_headers.py: idempotent header injector
  (shebang- and PEP 263-aware)

Touches 1565 source files (.py, .ts, .tsx, .js, .jsx, .css, .sh).
No behavior change; comments only.
2026-05-22 21:04:16 -04:00

70 lines
2.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
import asyncio
import time
from typing import Any, Optional
from fastapi import APIRouter, Depends, Query
from decnet.telemetry import traced as _traced
from decnet.web.dependencies import require_viewer, repo
router = APIRouter()
# /logs/histogram aggregates over the full logs table — expensive and
# polled constantly by the UI. Cache only the unfiltered default call
# (which is what the UI and locust hit); any filter bypasses.
_HISTOGRAM_TTL = 5.0
_DEFAULT_INTERVAL = 15
_histogram_cache: tuple[Optional[list[dict[str, Any]]], float] = (None, 0.0)
_histogram_lock: Optional[asyncio.Lock] = None
def _reset_histogram_cache() -> None:
global _histogram_cache, _histogram_lock
_histogram_cache = (None, 0.0)
_histogram_lock = None
async def _get_histogram_cached() -> list[dict[str, Any]]:
global _histogram_cache, _histogram_lock
value, ts = _histogram_cache
now = time.monotonic()
if value is not None and now - ts < _HISTOGRAM_TTL:
return value
if _histogram_lock is None:
_histogram_lock = asyncio.Lock()
async with _histogram_lock:
value, ts = _histogram_cache
now = time.monotonic()
if value is not None and now - ts < _HISTOGRAM_TTL:
return value
value = await repo.get_log_histogram(
search=None, start_time=None, end_time=None, interval_minutes=_DEFAULT_INTERVAL,
)
_histogram_cache = (value, time.monotonic())
return value
@router.get("/logs/histogram", tags=["Logs"],
responses={401: {"description": "Could not validate credentials"}, 403: {"description": "Insufficient permissions"}, 422: {"description": "Validation error"}},)
@_traced("api.get_logs_histogram")
async def get_logs_histogram(
search: Optional[str] = None,
start_time: Optional[str] = Query(None),
end_time: Optional[str] = Query(None),
interval_minutes: int = Query(15, ge=1),
user: dict = Depends(require_viewer)
) -> list[dict[str, Any]]:
def _norm(v: Optional[str]) -> Optional[str]:
if v in (None, "null", "NULL", "undefined", ""):
return None
return v
s = _norm(search)
st = _norm(start_time)
et = _norm(end_time)
if s is None and st is None and et is None and interval_minutes == _DEFAULT_INTERVAL:
return await _get_histogram_cached()
return await repo.get_log_histogram(search=s, start_time=st, end_time=et, interval_minutes=interval_minutes)