7
7
from django .utils .timezone import now
8
8
from django .utils .translation import gettext_lazy as _
9
9
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
12
19
13
- from ...models .donors import Donor
14
20
from .helpers import (
15
21
generate_donations_per_month_chart ,
16
22
get_current_year_range ,
@@ -27,7 +33,6 @@ def callback(request, context) -> Dict:
27
33
return context
28
34
29
35
30
- @cache_decorator (timeout = settings .TIMEOUT_CACHE_NORMAL , cache_key = ADMIN_DASHBOARD_STATS_CACHE_KEY )
31
36
def _get_admin_stats () -> Dict :
32
37
today = now ()
33
38
years_range_ascending = get_current_year_range ()
@@ -55,53 +60,54 @@ def _get_header_stats(today) -> List[List[Dict[str, Union[str, int | datetime]]]
55
60
{
56
61
"title" : _ ("Donations this year" ),
57
62
"icon" : "edit_document" ,
58
- "metric" : Donor . available . filter ( date_created__year = current_year ). count () ,
63
+ "metric" : current_year_redirections [ "metric" ] ,
59
64
"footer" : _create_stat_link (
60
65
url = f'{ reverse ("admin:donations_donor_changelist" )} ?{ current_year_range } ' , text = _ ("View all" )
61
66
),
62
- "timestamp" : now () ,
67
+ "timestamp" : current_year_redirections [ "timestamp" ] ,
63
68
},
64
69
{
65
70
"title" : _ ("Donations all-time" ),
66
71
"icon" : "edit_document" ,
67
- "metric" : Donor . available . count () ,
72
+ "metric" : all_redirections [ "metric" ] ,
68
73
"footer" : _create_stat_link (url = reverse ("admin:donations_donor_changelist" ), text = _ ("View all" )),
69
- "timestamp" : now () ,
74
+ "timestamp" : all_redirections [ "timestamp" ] ,
70
75
},
71
76
{
72
77
"title" : _ ("NGOs registered" ),
73
78
"icon" : "foundation" ,
74
- "metric" : Ngo . active . count () ,
79
+ "metric" : all_active_ngos [ "metric" ] ,
75
80
"footer" : _create_stat_link (
76
81
url = f'{ reverse ("admin:donations_ngo_changelist" )} ?is_active=1' , text = _ ("View all" )
77
82
),
78
- "timestamp" : now () ,
83
+ "timestamp" : all_active_ngos [ "timestamp" ] ,
79
84
},
80
85
{
81
86
"title" : _ ("Functioning NGOs" ),
82
87
"icon" : "foundation" ,
83
- "metric" : Ngo . with_forms_this_year . count () ,
88
+ "metric" : ngos_active_in_current_year [ "metric" ] ,
84
89
"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" ] ,
86
91
},
87
92
{
88
93
"title" : _ ("NGOs from NGO Hub" ),
89
94
"icon" : "foundation" ,
90
- "metric" : Ngo . ngo_hub . count () ,
95
+ "metric" : ngos_with_ngo_hub [ "metric" ] ,
91
96
"footer" : _create_stat_link (
92
97
url = f'{ reverse ("admin:donations_ngo_changelist" )} ?is_active=1&has_ngohub=1' , text = _ ("View all" )
93
98
),
94
- "timestamp" : now () ,
99
+ "timestamp" : ngos_with_ngo_hub [ "timestamp" ] ,
95
100
},
96
101
]
97
102
]
98
103
99
104
100
105
def _create_chart_statistics () -> Dict [str , str ]:
101
106
default_border_width : int = 3
107
+ current_year = now ().year
102
108
103
109
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 )
105
111
]
106
112
107
113
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]:
110
116
111
117
112
118
def _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 ]
114
120
115
121
for index , statistic in enumerate (statistics ):
116
122
if index == 0 :
@@ -129,24 +135,6 @@ def _get_yearly_stats(years_range_ascending) -> List[Dict[str, Union[int, List[D
129
135
return sorted (final_statistics , key = lambda x : x ["year" ], reverse = True )
130
136
131
137
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
-
150
138
def _format_yearly_stats (statistics ) -> List [Dict [str , Union [int , List [Dict ]]]]:
151
139
return [
152
140
{
0 commit comments