[loggi-klf] DRY stats endpoint boilerplate with a shared helper #117

Closed
opened 2026-02-21 19:43:48 +01:00 by matthias · 0 comments
Owner

Bead ID: loggi-klf
Type: task
Priority: P2
Status: closed
Close reason: Extracted _get_entry_and_base_query() helper in stats.py that handles entry lookup, access check (404/403), and date-filtered base query construction. All 6 stats endpoints now use this instead of repeating ~10 lines of boilerplate each. Cleaned up unused imports. All 171 tests pass.


Why

All 6 stats endpoints in stats.py repeat the same 10-line preamble: get entry, check not found (404), check access (403), parse from/to params, build base query with date filters. That's ~60 lines of pure duplication and a maintenance risk.

What

Extract a helper function like _stats_base_query(entry_id) that:

  1. Looks up the entry (or returns 404 JSON response)
  2. Checks entry_access (or returns 403 JSON response)
  3. Parses from/to query params
  4. Builds and returns the filtered base query + entry object

Each endpoint replaces its boilerplate with a single call to this helper. On error, the helper returns a Flask response tuple; on success, it returns (entry, base_query).

Acceptance criteria

  • All 171 tests still pass
  • stats.py shrinks by ~50 lines
  • No functional changes — pure DRY refactor

Dependencies:

  • blocks: loggi-2ze
**Bead ID:** `loggi-klf` **Type:** task **Priority:** P2 **Status:** closed **Close reason:** Extracted _get_entry_and_base_query() helper in stats.py that handles entry lookup, access check (404/403), and date-filtered base query construction. All 6 stats endpoints now use this instead of repeating ~10 lines of boilerplate each. Cleaned up unused imports. All 171 tests pass. --- ## Why All 6 stats endpoints in stats.py repeat the same 10-line preamble: get entry, check not found (404), check access (403), parse from/to params, build base query with date filters. That's ~60 lines of pure duplication and a maintenance risk. ## What Extract a helper function like _stats_base_query(entry_id) that: 1. Looks up the entry (or returns 404 JSON response) 2. Checks entry_access (or returns 403 JSON response) 3. Parses from/to query params 4. Builds and returns the filtered base query + entry object Each endpoint replaces its boilerplate with a single call to this helper. On error, the helper returns a Flask response tuple; on success, it returns (entry, base_query). ## Acceptance criteria - All 171 tests still pass - stats.py shrinks by ~50 lines - No functional changes — pure DRY refactor **Dependencies:** - blocks: `loggi-2ze`
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
matthias/loggi#117
No description provided.