77from django .utils .timezone import now
88from django .utils .translation import gettext_lazy as _
99
10- from donations .models .ngos import Ngo
11- from redirectioneaza .common .cache import cache_decorator
10+ from .stats_helper_chart import donors_for_month
11+ from .stats_helper_metrics import (
12+ all_active_ngos ,
13+ all_redirections ,
14+ current_year_redirections ,
15+ ngos_active_in_current_year ,
16+ ngos_with_ngo_hub ,
17+ )
18+ from .stats_helpers_yearly import get_stats_for_year
1219
13- from ...models .donors import Donor
1420from .helpers import (
1521 generate_donations_per_month_chart ,
1622 get_current_year_range ,
@@ -27,7 +33,6 @@ def callback(request, context) -> Dict:
2733 return context
2834
2935
30- @cache_decorator (timeout = settings .TIMEOUT_CACHE_NORMAL , cache_key = ADMIN_DASHBOARD_STATS_CACHE_KEY )
3136def _get_admin_stats () -> Dict :
3237 today = now ()
3338 years_range_ascending = get_current_year_range ()
@@ -55,53 +60,54 @@ def _get_header_stats(today) -> List[List[Dict[str, Union[str, int | datetime]]]
5560 {
5661 "title" : _ ("Donations this year" ),
5762 "icon" : "edit_document" ,
58- "metric" : Donor . available . filter ( date_created__year = current_year ). count () ,
63+ "metric" : current_year_redirections [ "metric" ] ,
5964 "footer" : _create_stat_link (
6065 url = f'{ reverse ("admin:donations_donor_changelist" )} ?{ current_year_range } ' , text = _ ("View all" )
6166 ),
62- "timestamp" : now () ,
67+ "timestamp" : current_year_redirections [ "timestamp" ] ,
6368 },
6469 {
6570 "title" : _ ("Donations all-time" ),
6671 "icon" : "edit_document" ,
67- "metric" : Donor . available . count () ,
72+ "metric" : all_redirections [ "metric" ] ,
6873 "footer" : _create_stat_link (url = reverse ("admin:donations_donor_changelist" ), text = _ ("View all" )),
69- "timestamp" : now () ,
74+ "timestamp" : all_redirections [ "timestamp" ] ,
7075 },
7176 {
7277 "title" : _ ("NGOs registered" ),
7378 "icon" : "foundation" ,
74- "metric" : Ngo . active . count () ,
79+ "metric" : all_active_ngos [ "metric" ] ,
7580 "footer" : _create_stat_link (
7681 url = f'{ reverse ("admin:donations_ngo_changelist" )} ?is_active=1' , text = _ ("View all" )
7782 ),
78- "timestamp" : now () ,
83+ "timestamp" : all_active_ngos [ "timestamp" ] ,
7984 },
8085 {
8186 "title" : _ ("Functioning NGOs" ),
8287 "icon" : "foundation" ,
83- "metric" : Ngo . with_forms_this_year . count () ,
88+ "metric" : ngos_active_in_current_year [ "metric" ] ,
8489 "footer" : _create_stat_link (url = f'{ reverse ("admin:donations_ngo_changelist" )} ' , text = _ ("View all" )),
85- "timestamp" : now () ,
90+ "timestamp" : ngos_active_in_current_year [ "timestamp" ] ,
8691 },
8792 {
8893 "title" : _ ("NGOs from NGO Hub" ),
8994 "icon" : "foundation" ,
90- "metric" : Ngo . ngo_hub . count () ,
95+ "metric" : ngos_with_ngo_hub [ "metric" ] ,
9196 "footer" : _create_stat_link (
9297 url = f'{ reverse ("admin:donations_ngo_changelist" )} ?is_active=1&has_ngohub=1' , text = _ ("View all" )
9398 ),
94- "timestamp" : now () ,
99+ "timestamp" : ngos_with_ngo_hub [ "timestamp" ] ,
95100 },
96101 ]
97102 ]
98103
99104
100105def _create_chart_statistics () -> Dict [str , str ]:
101106 default_border_width : int = 3
107+ current_year = now ().year
102108
103109 donations_per_month_queryset = [
104- Donor . available . filter ( date_created__month = month ) for month in range (1 , settings .DONATIONS_LIMIT .month + 1 )
110+ donors_for_month ( month , current_year )[ "metric" ] for month in range (1 , settings .DONATIONS_LIMIT .month + 1 )
105111 ]
106112
107113 forms_per_month_chart = generate_donations_per_month_chart (default_border_width , donations_per_month_queryset )
@@ -110,7 +116,7 @@ def _create_chart_statistics() -> Dict[str, str]:
110116
111117
112118def _get_yearly_stats (years_range_ascending ) -> List [Dict [str , Union [int , List [Dict ]]]]:
113- statistics = [_get_stats_for_year (year ) for year in years_range_ascending ]
119+ statistics = [get_stats_for_year (year ) for year in years_range_ascending ]
114120
115121 for index , statistic in enumerate (statistics ):
116122 if index == 0 :
@@ -129,24 +135,6 @@ def _get_yearly_stats(years_range_ascending) -> List[Dict[str, Union[int, List[D
129135 return sorted (final_statistics , key = lambda x : x ["year" ], reverse = True )
130136
131137
132- # TODO: This cache seems useless because we already cache the entire dashboard stats
133- @cache_decorator (timeout = settings .TIMEOUT_CACHE_NORMAL , cache_key_prefix = ADMIN_DASHBOARD_CACHE_KEY )
134- def _get_stats_for_year (year : int ) -> Dict [str , int | datetime ]:
135- donations : int = Donor .available .filter (date_created__year = year ).count ()
136- ngos_registered : int = Ngo .objects .filter (date_created__year = year ).count ()
137- ngos_with_forms : int = Donor .available .filter (date_created__year = year ).values ("ngo_id" ).distinct ().count ()
138-
139- statistic = {
140- "year" : year ,
141- "donations" : donations ,
142- "ngos_registered" : ngos_registered ,
143- "ngos_with_forms" : ngos_with_forms ,
144- "timestamp" : now (),
145- }
146-
147- return statistic
148-
149-
150138def _format_yearly_stats (statistics ) -> List [Dict [str , Union [int , List [Dict ]]]]:
151139 return [
152140 {
0 commit comments