Skip to content

refactor: reduce double-query patterns in dashboard_widgets.py #142

Description

@dholbach

Problem

Two efficiency issues in utils/dashboard_widgets.py:

1. InvoiceActionsWidgetBuilder — overdue/unpaid double queries

_get_overdue_invoices queries Invoice with status="sent" and invoice_date__lt=cutoff — a subset of _get_unpaid_invoices which queries the same with no cutoff. Both querysets are evaluated twice each (once for [:5], once for .count()). That's 6 DB hits for what could be 2.

Fix: evaluate unpaid invoices once into a list, derive overdue by filtering in Python ([inv for inv in unpaid if inv.invoice_date < cutoff]), use len() for counts.

2. ClientAttentionWidgetBuilder._get_tagged_clients — 12 sequential queries for 4 tags

For each of 4 hardcoded attention tags: ClientTag.objects.get(name=tag_name) (1 query), then clients.exists() (1 query) and clients.count() (1 query) — up to 12 sequential queries.

Fix: fetch all relevant tags in one query (ClientTag.objects.filter(name__in=attention_tags)), then one Client.objects.filter(..., tags__name__in=attention_tags).prefetch_related("tags") and group by tag in Python.

Identified in simplify review of P-117 chunk 4 (PR #140).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions