duck.etc.blueprints.dashboard.services

Dashboard service — reads real request metrics from MetricsStore.

All functions in this module pull live data collected by MetricsMiddleware. No simulation — every value reflects actual server activity since startup.

The only external dependency is the module-level store singleton from middleware.py. Swap the import target if you move to a persistent backend.

Module Contents

Functions

get_error_breakdown

Returns counts for each 4xx/5xx status code seen since startup.

get_full_snapshot

Returns a complete dashboard snapshot combining all metric sources.

get_latency_stats

Returns latency percentile metrics computed from the request history.

get_method_breakdown

Returns request counts broken down by HTTP method.

get_recent_logs

Returns recent server log entries captured by DashboardLogHandler.

get_request_stats

Returns aggregated request counts and success/failure breakdown.

get_server_state

Returns current server health and uptime metadata.

get_top_routes

Returns the most-hit routes with their request count and avg latency.

read_worker_count

Attempts to read the configured worker count from the Duck App instance.

Data

ERROR_STATUS_LABELS

API

duck.etc.blueprints.dashboard.services.ERROR_STATUS_LABELS

None

duck.etc.blueprints.dashboard.services.get_error_breakdown() list[dict][source]

Returns counts for each 4xx/5xx status code seen since startup.

Only status codes that have been observed at least once are included, supplemented with any codes listed in ERROR_STATUS_LABELS that have a zero count so the UI always shows a consistent set.

Returns:

code (int), label (str), count (int). Sorted by status code ascending.

Return type:

List of dicts with keys

duck.etc.blueprints.dashboard.services.get_full_snapshot() dict[source]

Returns a complete dashboard snapshot combining all metric sources.

Calls store.snapshot() once and distributes the data to each sub-function to avoid multiple lock acquisitions.

Returns:

server, requests, latency, errors, methods, routes, logs.

Return type:

Dict with keys

duck.etc.blueprints.dashboard.services.get_latency_stats() dict[source]

Returns latency percentile metrics computed from the request history.

Returns:

p50, p90, p95, p99, avg, history (list[int]). All latency values are in milliseconds.

Return type:

Dict with keys

duck.etc.blueprints.dashboard.services.get_method_breakdown() list[dict][source]

Returns request counts broken down by HTTP method.

Returns:

method (str), count (int), percent (float). Sorted by count descending.

Return type:

List of dicts with keys

duck.etc.blueprints.dashboard.services.get_recent_logs(limit: int = 40) list[dict][source]

Returns recent server log entries captured by DashboardLogHandler.

Parameters:

limit – Maximum number of log entries to return.

Returns:

level (str), message (str), ts (str), source (str). Most recent entries first.

Return type:

List of dicts with keys

duck.etc.blueprints.dashboard.services.get_request_stats() dict[source]

Returns aggregated request counts and success/failure breakdown.

Returns:

total, success, errors, per_minute, success_rate (float 0–100).

Return type:

Dict with keys

duck.etc.blueprints.dashboard.services.get_server_state() dict[source]

Returns current server health and uptime metadata.

Reads uptime from the store start time and system info from the platform module. Worker count is pulled from the Duck App instance when available.

Returns:

status, uptime, uptime_seconds, python_version, platform, worker_count, start_time.

Return type:

Dict with keys

duck.etc.blueprints.dashboard.services.get_top_routes(limit: int = 15) list[dict][source]

Returns the most-hit routes with their request count and avg latency.

Parameters:

limit – Maximum number of routes to return.

Returns:

path (str), method (str), hits (int), avg_ms (int), status (str — “ok” or “slow”). Sorted by hits descending.

Return type:

List of dicts with keys

duck.etc.blueprints.dashboard.services.read_worker_count() int[source]

Attempts to read the configured worker count from the Duck App instance.

Falls back to 1 if the App or server attribute is not accessible, which can happen when the dashboard is called before the server is fully initialised.

Returns:

Integer worker count, or 1 as a safe fallback.