|
| 1 | +from typing import Dict, Any |
| 2 | + |
1 | 3 | from django.conf import settings
|
2 |
| -from django.core.cache import cache |
3 | 4 | from django.utils.timezone import now
|
4 |
| -from django_q.tasks import async_task |
5 | 5 |
|
6 | 6 | from donations.models import Donor, Ngo
|
7 | 7 | from donations.views.dashboard.stats_helpers.utils import cache_set
|
| 8 | +from redirectioneaza.common.async_cache import async_cache_decorator |
| 9 | + |
| 10 | +# Cache key prefix for metrics |
| 11 | +METRIC_CACHE_PREFIX = "METRIC_" |
8 | 12 |
|
9 | 13 |
|
10 | 14 | def _cache_key_for_metric(metric_name: str) -> str:
|
11 | 15 | """
|
12 | 16 | Generates a cache key for the given metric name.
|
13 | 17 | """
|
14 |
| - return f"METRIC_{metric_name.upper()}" |
| 18 | + return f"{METRIC_CACHE_PREFIX}{metric_name.upper()}" |
15 | 19 |
|
16 | 20 |
|
17 |
| -def metrics_cache_decorator(func): |
18 |
| - """ |
19 |
| - Decorator to cache the metrics functions. |
| 21 | +@async_cache_decorator( |
| 22 | + cache_key=_cache_key_for_metric("current_year_redirections"), |
| 23 | + timeout=settings.TIMEOUT_CACHE_NORMAL, |
| 24 | +) |
| 25 | +def current_year_redirections() -> Dict[str, Any]: |
20 | 26 | """
|
| 27 | + Returns the number of redirections (donations) for the current year. |
21 | 28 |
|
22 |
| - def wrapper(*args, **kwargs): |
23 |
| - CACHE_KEY = _cache_key_for_metric(func.__name__) |
24 |
| - |
25 |
| - if cached_result := cache.get(CACHE_KEY): |
26 |
| - # if the cache has expired (the timestamp is older than TIMEOUT_CACHE_NORMAL), |
27 |
| - # we delete the cache entry, trigger an async task to recalculate the metric, |
28 |
| - # and return the cached result; the updated metric will be available in the next request |
29 |
| - cache_timestamp = cached_result.get("timestamp") |
30 |
| - if cache_timestamp and (now() - cache_timestamp).total_seconds() > settings.TIMEOUT_CACHE_NORMAL: |
31 |
| - cache.delete(CACHE_KEY) |
32 |
| - async_task(func.__name__, *args, **kwargs) |
33 |
| - |
34 |
| - return cached_result |
35 |
| - default_result = { |
36 |
| - "metric": -2, |
37 |
| - "timestamp": now(), |
38 |
| - } |
39 |
| - |
40 |
| - async_task(func.__name__, *args, **kwargs) |
| 29 | + Returns: |
| 30 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 31 | + """ |
| 32 | + return _update_current_year_redirections() |
41 | 33 |
|
42 |
| - return default_result |
43 | 34 |
|
44 |
| - return wrapper |
| 35 | +def _update_current_year_redirections(_cache_key: str = None, _timeout: int = None) -> Dict[str, Any]: |
| 36 | + """ |
| 37 | + Updates the number of redirections for the current year and caches the result. |
45 | 38 |
|
| 39 | + Parameters: |
| 40 | + _cache_key (str, optional): The cache key to use when storing the result. |
| 41 | + This is passed by the async_cache_decorator. |
| 42 | + _timeout (int, optional): The cache timeout in seconds. |
| 43 | + This is passed by the async_cache_decorator. |
46 | 44 |
|
47 |
| -@metrics_cache_decorator |
48 |
| -def current_year_redirections(): |
| 45 | + Returns: |
| 46 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 47 | + """ |
49 | 48 | result = {
|
50 | 49 | "metric": Donor.available.filter(date_created__year=now().year).count(),
|
51 | 50 | "timestamp": now(),
|
52 | 51 | }
|
53 | 52 |
|
54 |
| - cache_set(_cache_key_for_metric("current_year_redirections"), result) |
| 53 | + if _cache_key and _timeout is not None: |
| 54 | + cache_set(_cache_key, result, timeout=_timeout) |
55 | 55 |
|
56 | 56 | return result
|
57 | 57 |
|
58 | 58 |
|
59 |
| -@metrics_cache_decorator |
60 |
| -def all_redirections(): |
| 59 | +@async_cache_decorator( |
| 60 | + cache_key=_cache_key_for_metric("all_redirections"), |
| 61 | + timeout=settings.TIMEOUT_CACHE_NORMAL, |
| 62 | +) |
| 63 | +def all_redirections() -> Dict[str, Any]: |
| 64 | + """ |
| 65 | + Returns the total number of redirections (donations) across all years. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 69 | + """ |
| 70 | + return _update_all_redirections() |
| 71 | + |
| 72 | + |
| 73 | +def _update_all_redirections(_cache_key: str = None, _timeout: int = None) -> Dict[str, Any]: |
| 74 | + """ |
| 75 | + Updates the total number of redirections and caches the result. |
| 76 | +
|
| 77 | + Parameters: |
| 78 | + _cache_key (str, optional): The cache key to use when storing the result. |
| 79 | + This is passed by the async_cache_decorator. |
| 80 | + _timeout (int, optional): The cache timeout in seconds. |
| 81 | + This is passed by the async_cache_decorator. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 85 | + """ |
61 | 86 | result = {
|
62 | 87 | "metric": Donor.available.count(),
|
63 | 88 | "timestamp": now(),
|
64 | 89 | }
|
65 | 90 |
|
66 |
| - cache_set(_cache_key_for_metric("all_redirections"), result) |
| 91 | + if _cache_key and _timeout is not None: |
| 92 | + cache_set(_cache_key, result, timeout=_timeout) |
67 | 93 |
|
68 | 94 | return result
|
69 | 95 |
|
70 | 96 |
|
71 |
| -@metrics_cache_decorator |
72 |
| -def all_active_ngos(): |
| 97 | +@async_cache_decorator( |
| 98 | + cache_key=_cache_key_for_metric("all_active_ngos"), |
| 99 | + timeout=settings.TIMEOUT_CACHE_NORMAL, |
| 100 | +) |
| 101 | +def all_active_ngos() -> Dict[str, Any]: |
| 102 | + """ |
| 103 | + Returns the total number of active NGOs. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 107 | + """ |
| 108 | + return _update_all_active_ngos() |
| 109 | + |
| 110 | + |
| 111 | +def _update_all_active_ngos(_cache_key: str = None, _timeout: int = None) -> Dict[str, Any]: |
| 112 | + """ |
| 113 | + Updates the total number of active NGOs and caches the result. |
| 114 | +
|
| 115 | + Parameters: |
| 116 | + _cache_key (str, optional): The cache key to use when storing the result. |
| 117 | + This is passed by the async_cache_decorator. |
| 118 | + _timeout (int, optional): The cache timeout in seconds. |
| 119 | + This is passed by the async_cache_decorator. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 123 | + """ |
73 | 124 | result = {
|
74 | 125 | "metric": Ngo.active.count(),
|
75 | 126 | "timestamp": now(),
|
76 | 127 | }
|
77 | 128 |
|
78 |
| - cache_set(_cache_key_for_metric("all_active_ngos"), result) |
| 129 | + if _cache_key and _timeout is not None: |
| 130 | + cache_set(_cache_key, result, timeout=_timeout) |
79 | 131 |
|
80 | 132 | return result
|
81 | 133 |
|
82 | 134 |
|
83 |
| -@metrics_cache_decorator |
84 |
| -def ngos_active_in_current_year(): |
| 135 | +@async_cache_decorator( |
| 136 | + cache_key=_cache_key_for_metric("ngos_active_in_current_year"), |
| 137 | + timeout=settings.TIMEOUT_CACHE_NORMAL, |
| 138 | +) |
| 139 | +def ngos_active_in_current_year() -> Dict[str, Any]: |
| 140 | + """ |
| 141 | + Returns the number of NGOs that are active in the current year. |
| 142 | +
|
| 143 | + Returns: |
| 144 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 145 | + """ |
| 146 | + return _update_ngos_active_in_current_year() |
| 147 | + |
| 148 | + |
| 149 | +def _update_ngos_active_in_current_year(_cache_key: str = None, _timeout: int = None) -> Dict[str, Any]: |
| 150 | + """ |
| 151 | + Updates the number of NGOs active in the current year and caches the result. |
| 152 | +
|
| 153 | + Parameters: |
| 154 | + _cache_key (str, optional): The cache key to use when storing the result. |
| 155 | + This is passed by the async_cache_decorator. |
| 156 | + _timeout (int, optional): The cache timeout in seconds. |
| 157 | + This is passed by the async_cache_decorator. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 161 | + """ |
85 | 162 | result = {
|
86 | 163 | "metric": Ngo.with_forms_this_year.count(),
|
87 | 164 | "timestamp": now(),
|
88 | 165 | }
|
89 | 166 |
|
90 |
| - cache_set(_cache_key_for_metric("ngos_active_in_current_year"), result) |
| 167 | + if _cache_key and _timeout is not None: |
| 168 | + cache_set(_cache_key, result, timeout=_timeout) |
91 | 169 |
|
92 | 170 | return result
|
93 | 171 |
|
94 | 172 |
|
95 |
| -@metrics_cache_decorator |
96 |
| -def ngos_with_ngo_hub(): |
| 173 | +@async_cache_decorator( |
| 174 | + cache_key=_cache_key_for_metric("ngos_with_ngo_hub"), |
| 175 | + timeout=settings.TIMEOUT_CACHE_NORMAL, |
| 176 | +) |
| 177 | +def ngos_with_ngo_hub() -> Dict[str, Any]: |
| 178 | + """ |
| 179 | + Returns the number of NGOs that are part of the NGO Hub. |
| 180 | +
|
| 181 | + Returns: |
| 182 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 183 | + """ |
| 184 | + return _update_ngos_with_ngo_hub() |
| 185 | + |
| 186 | + |
| 187 | +def _update_ngos_with_ngo_hub(_cache_key: str = None, _timeout: int = None) -> Dict[str, Any]: |
| 188 | + """ |
| 189 | + Updates the number of NGOs with NGO Hub and caches the result. |
| 190 | +
|
| 191 | + Parameters: |
| 192 | + _cache_key (str, optional): The cache key to use when storing the result. |
| 193 | + This is passed by the async_cache_decorator. |
| 194 | + _timeout (int, optional): The cache timeout in seconds. |
| 195 | + This is passed by the async_cache_decorator. |
| 196 | +
|
| 197 | + Returns: |
| 198 | + Dict[str, Any]: A dictionary containing the metric and timestamp. |
| 199 | + """ |
97 | 200 | result = {
|
98 | 201 | "metric": Ngo.ngo_hub.count(),
|
99 | 202 | "timestamp": now(),
|
100 | 203 | }
|
101 | 204 |
|
102 |
| - cache_set(_cache_key_for_metric("ngos_with_ngo_hub"), result) |
| 205 | + if _cache_key and _timeout is not None: |
| 206 | + cache_set(_cache_key, result, timeout=_timeout) |
103 | 207 |
|
104 | 208 | return result
|
0 commit comments