Skip to content

fix: modularize admin dashboard statistics with reusable helpers #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tudoramariei
Copy link
Member

  • extract yearly stats logic into stats_helpers_yearly.py
  • extract metrics-related logic into stats_helper_metrics.py
  • extract chart-related logic into stats_helper_chart.py
  • refactor admin dashboard to use new helpers for stats and metrics

- extract yearly stats logic into `stats_helpers_yearly.py`
- extract metrics-related logic into `stats_helper_metrics.py`
- extract chart-related logic into `stats_helper_chart.py`
- refactor admin dashboard to use new helpers for stats and metrics
@tudoramariei tudoramariei requested review from danniel and Copilot July 15, 2025 09:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the admin dashboard statistics functionality by extracting logic into three reusable helper modules for better code organization and maintainability.

  • Modularizes yearly statistics logic into dedicated helper functions with improved caching
  • Extracts metrics calculation into a decorator-based caching system
  • Separates chart-related data processing into its own module

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 16 comments.

File Description
stats_helpers_yearly.py New module handling yearly statistics with cache validation and async updates
stats_helper_metrics.py New module providing cached metrics functions with decorator pattern
stats_helper_chart.py New module for monthly donor statistics used in chart generation
admin_dashboard.py Refactored to use new helper modules, removing inline statistics logic
Comments suppressed due to low confidence (1)

backend/donations/views/dashboard/stats_helper_metrics.py:22

  • Variable name CACHE_KEY should follow snake_case convention. It should be renamed to cache_key.
        CACHE_KEY = _cache_key_for_metric(func.__name__)

"month": month,
}

cache.set(cache_key, stat)
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing timeout parameter in cache.set() call. This should include a timeout value like the other cache operations in the codebase for consistency.

Suggested change
cache.set(cache_key, stat)
cache.set(cache_key, stat, timeout=300) # Cache expires after 5 minutes

Copilot uses AI. Check for mistakes.

"timestamp": now(),
}

cache.set(_cache_key_for_metric("current_year_redirections"), result)
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing timeout parameter in cache.set() call. This should include a timeout value for consistency with other cache operations.

Suggested change
cache.set(_cache_key_for_metric("current_year_redirections"), result)
cache.set(_cache_key_for_metric("current_year_redirections"), result, timeout=settings.TIMEOUT_CACHE_NORMAL)

Copilot uses AI. Check for mistakes.

"timestamp": now(),
}

cache.set(_cache_key_for_metric("all_redirections"), result)
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing timeout parameter in cache.set() call. This should include a timeout value for consistency with other cache operations.

Suggested change
cache.set(_cache_key_for_metric("all_redirections"), result)
cache.set(_cache_key_for_metric("all_redirections"), result, timeout=settings.TIMEOUT_CACHE_NORMAL)

Copilot uses AI. Check for mistakes.

"timestamp": now(),
}

cache.set(_cache_key_for_metric("all_active_ngos"), result)
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing timeout parameter in cache.set() call. This should include a timeout value for consistency with other cache operations.

Suggested change
cache.set(_cache_key_for_metric("all_active_ngos"), result)
cache.set(_cache_key_for_metric("all_active_ngos"), result, timeout=settings.TIMEOUT_CACHE_NORMAL)

Copilot uses AI. Check for mistakes.

"timestamp": now(),
}

cache.set(_cache_key_for_metric("ngos_active_in_current_year"), result)
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing timeout parameter in cache.set() call. This should include a timeout value for consistency with other cache operations.

Suggested change
cache.set(_cache_key_for_metric("ngos_active_in_current_year"), result)
cache.set(_cache_key_for_metric("ngos_active_in_current_year"), result, timeout=settings.TIMEOUT_CACHE_NORMAL)

Copilot uses AI. Check for mistakes.

"footer": _create_stat_link(
url=f'{reverse("admin:donations_ngo_changelist")}?is_active=1', text=_("View all")
),
"timestamp": now(),
"timestamp": all_active_ngos["timestamp"],
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function call parentheses. Should be all_active_ngos()["timestamp"] to actually invoke the function.

Suggested change
"timestamp": all_active_ngos["timestamp"],
"timestamp": all_active_ngos()["timestamp"],

Copilot uses AI. Check for mistakes.

Comment on lines +88 to +90
"metric": ngos_active_in_current_year["metric"],
"footer": _create_stat_link(url=f'{reverse("admin:donations_ngo_changelist")}', text=_("View all")),
"timestamp": now(),
"timestamp": ngos_active_in_current_year["timestamp"],
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function call parentheses. Should be ngos_active_in_current_year()["metric"] to actually invoke the function.

Copilot uses AI. Check for mistakes.

"footer": _create_stat_link(url=f'{reverse("admin:donations_ngo_changelist")}', text=_("View all")),
"timestamp": now(),
"timestamp": ngos_active_in_current_year["timestamp"],
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function call parentheses. Should be ngos_active_in_current_year()["timestamp"] to actually invoke the function.

Suggested change
"timestamp": ngos_active_in_current_year["timestamp"],
"timestamp": ngos_active_in_current_year()["timestamp"],

Copilot uses AI. Check for mistakes.

},
{
"title": _("NGOs from NGO Hub"),
"icon": "foundation",
"metric": Ngo.ngo_hub.count(),
"metric": ngos_with_ngo_hub["metric"],
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function call parentheses. Should be ngos_with_ngo_hub()["metric"] to actually invoke the function.

Suggested change
"metric": ngos_with_ngo_hub["metric"],
"metric": ngos_with_ngo_hub()["metric"],

Copilot uses AI. Check for mistakes.

"footer": _create_stat_link(
url=f'{reverse("admin:donations_ngo_changelist")}?is_active=1&has_ngohub=1', text=_("View all")
),
"timestamp": now(),
"timestamp": ngos_with_ngo_hub["timestamp"],
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function call parentheses. Should be ngos_with_ngo_hub()["timestamp"] to actually invoke the function.

Suggested change
"timestamp": ngos_with_ngo_hub["timestamp"],
"timestamp": ngos_with_ngo_hub()["timestamp"],

Copilot uses AI. Check for mistakes.

- add `cache_set` function in `stats_helpers.utils`
- replace direct `cache.set` calls with `cache_set` across stats helpers
- update imports to reflect the new utility module structure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants