Skip to content

Commit 894f5b2

Browse files
authored
Merge pull request #441 from Amdrel/T396683
Fix graph date range showing irrelevant dates
2 parents 6e7a3d9 + 26e0f33 commit 894f5b2

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

extlinks/organisations/views.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
PageProjectAggregate,
1919
UserAggregate,
2020
)
21+
from extlinks.aggregates.storage import (
22+
find_unique,
23+
calculate_totals,
24+
download_aggregates,
25+
)
2126
from extlinks.common.forms import FilterForm
2227
from extlinks.common.helpers import (
2328
get_linksearchtotal_data_by_time,
@@ -192,10 +197,16 @@ def _fill_chart_context(self, context, queryset_filter):
192197
existing_link_aggregates = {}
193198
eventstream_dates = []
194199
eventstream_net_change = []
195-
current_date = date.today()
196200
filtered_link_aggregate = LinkAggregate.objects.filter(queryset_filter)
197201
to_date = None
198202

203+
# Figure out what date the graph should end on.
204+
date_cursor = self.request.GET.get("end_date")
205+
if date_cursor:
206+
date_cursor = datetime.strptime(date_cursor, "%Y-%m-%d").date()
207+
else:
208+
date_cursor = date.today()
209+
199210
if filtered_link_aggregate.exists():
200211
earliest_link_date = filtered_link_aggregate.earliest("full_date").full_date
201212

@@ -207,7 +218,21 @@ def _fill_chart_context(self, context, queryset_filter):
207218
else:
208219
# No link information from that collection, so setting earliest_link_date
209220
# to the first of the current month
210-
earliest_link_date = current_date.replace(day=1)
221+
earliest_link_date = date_cursor.replace(day=1)
222+
223+
links_aggregated_date = []
224+
225+
# Download aggregates from object storage and calculate totals grouped
226+
# by year and month.
227+
totals = calculate_totals(
228+
download_aggregates(
229+
prefix="aggregates_linkaggregate",
230+
queryset_filter=queryset_filter,
231+
to_date=to_date,
232+
),
233+
group_by=lambda record: (record["year"], record["month"]),
234+
)
235+
links_aggregated_date.extend(totals)
211236

212237
links_aggregated_date = []
213238

@@ -222,7 +247,7 @@ def _fill_chart_context(self, context, queryset_filter):
222247
group_by=lambda record: (record["year"], record["month"]),
223248
)
224249
links_aggregated_date.extend(totals)
225-
250+
226251
# Fetch remaining aggregates that are present in the database and
227252
# append them after the aggregates from the archives.
228253
links_aggregated_date.extend(
@@ -242,10 +267,10 @@ def _fill_chart_context(self, context, queryset_filter):
242267
earliest_link_date = full_date
243268

244269
# Filling an array of dates that should be in the chart
245-
while current_date >= earliest_link_date:
246-
dates.append(current_date.strftime("%Y-%m"))
270+
while date_cursor >= earliest_link_date:
271+
dates.append(date_cursor.strftime("%Y-%m"))
247272
# Figure out what the last month is regardless of today's date
248-
current_date = current_date.replace(day=1) - timedelta(days=1)
273+
date_cursor = date_cursor.replace(day=1) - timedelta(days=1)
249274

250275
dates = dates[::-1]
251276

extlinks/programs/views.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import json
2+
import json
3+
4+
from datetime import date, timedelta, datetime
5+
from logging import getLogger
26

37
from datetime import date, timedelta
48
from logging import getLogger
@@ -125,7 +129,13 @@ def _fill_chart_context(self, context, queryset_filter):
125129
existing_link_aggregates = {}
126130
eventstream_dates = []
127131
eventstream_net_change = []
128-
current_date = date.today()
132+
133+
# Figure out what date the graph should end on.
134+
date_cursor = self.request.GET.get("end_date")
135+
if date_cursor:
136+
date_cursor = datetime.strptime(date_cursor, "%Y-%m-%d").date()
137+
else:
138+
date_cursor = date.today()
129139

130140
# Query program-level totals from top organisations since it is the
131141
# smallest totals table that's available.
@@ -136,7 +146,7 @@ def _fill_chart_context(self, context, queryset_filter):
136146
else:
137147
# No link information from that collection, so setting earliest_link_date
138148
# to the first of the current month
139-
earliest_total_date = current_date.replace(day=1)
149+
earliest_total_date = date_cursor.replace(day=1)
140150

141151
# We can GROUP BY 'full_date' as if it was just year and month as
142152
# program totals always set the day to the last day of the month.
@@ -149,10 +159,10 @@ def _fill_chart_context(self, context, queryset_filter):
149159
)
150160

151161
# Filling an array of dates that should be in the chart
152-
while current_date >= earliest_total_date:
153-
dates.append(current_date.strftime("%Y-%m"))
162+
while date_cursor >= earliest_total_date:
163+
dates.append(date_cursor.strftime("%Y-%m"))
154164
# Figure out what the last month is regardless of today's date
155-
current_date = current_date.replace(day=1) - timedelta(days=1)
165+
date_cursor = date_cursor.replace(day=1) - timedelta(days=1)
156166

157167
dates = dates[::-1]
158168

0 commit comments

Comments
 (0)