From a1c0244561d31d9685a90e660007bce2cf037e69 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 18 Jan 2022 10:59:41 +0100 Subject: [PATCH 3/5] dataviz: change time interval aggregation internals (#60547) --- combo/apps/dataviz/forms.py | 10 ++--- .../migrations/0020_auto_20220118_1103.py | 27 +++++++++++++ combo/apps/dataviz/models.py | 39 +++++++++++++------ tests/test_dataviz.py | 20 +++++----- 4 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 combo/apps/dataviz/migrations/0020_auto_20220118_1103.py diff --git a/combo/apps/dataviz/forms.py b/combo/apps/dataviz/forms.py index 1c44173a..88b549d7 100644 --- a/combo/apps/dataviz/forms.py +++ b/combo/apps/dataviz/forms.py @@ -57,10 +57,10 @@ def trigger_statistics_list_refresh(): class ChartNgForm(forms.ModelForm): blank_choice = ('', '---------') time_intervals = ( - ('_week', _('Week')), - ('_month', _('Month')), - ('_year', _('Year')), - ('_weekday', _('Week day')), + ('week', _('Week')), + ('month', _('Month')), + ('year', _('Year')), + ('weekday', _('Week day')), ) class Meta: @@ -146,7 +146,7 @@ class ChartNgForm(forms.ModelForm): if 'day' not in choice_ids: return for choice in self.time_intervals: - if choice[0].strip('_') not in choice_ids: + if choice[0] not in choice_ids: self.fields['time_interval'].choices.append(choice) def clean(self): diff --git a/combo/apps/dataviz/migrations/0020_auto_20220118_1103.py b/combo/apps/dataviz/migrations/0020_auto_20220118_1103.py new file mode 100644 index 00000000..e07e50a1 --- /dev/null +++ b/combo/apps/dataviz/migrations/0020_auto_20220118_1103.py @@ -0,0 +1,27 @@ +# Generated by Django 2.2.19 on 2022-01-18 10:03 + +from django.db import migrations + + +def update_time_intervals(apps, schema_editor): + ChartNgCell = apps.get_model('dataviz', 'ChartNgCell') + + for cell in ChartNgCell.objects.all(): + save = False + for param in cell.filter_params: + if param == 'time_interval' and cell.filter_params[param].startswith('_'): + cell.filter_params[param] = cell.filter_params[param][1:] + save = True + if save: + cell.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('dataviz', '0019_auto_20211006_1525'), + ] + + operations = [ + migrations.RunPython(update_time_intervals, migrations.RunPython.noop), + ] diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 1206e828..3a8b2f5a 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -148,6 +148,15 @@ class Statistic(models.Model): def natural_key(self): return (self.slug, self.site_slug, self.service_slug) + def has_native_support_for_interval(self, time_interval): + # pylint: disable=not-an-iterable + return any( + time_interval == x['id'] + for filter_ in self.filters + for x in filter_['options'] + if filter_['id'] == 'time_interval' + ) + TIME_FILTERS = ( ('previous-year', _('Previous year')), @@ -350,7 +359,11 @@ class ChartNgCell(CellBase): data = response['data'] interval = self.filter_params.get('time_interval', '') - if data['x_labels'] and (interval == 'day' or interval.startswith('_')): + if ( + data['x_labels'] + and interval + and (interval == 'day' or not self.statistic.has_native_support_for_interval(interval)) + ): self.aggregate_data(data, interval) chart.x_labels = data['x_labels'] @@ -421,7 +434,9 @@ class ChartNgCell(CellBase): params['end'] = Template('{{ %s|date:"Y-m-d" }}' % self.time_range_end_template).render( Context(context) ) - if 'time_interval' in params and params['time_interval'].startswith('_'): + if 'time_interval' in params and not self.statistic.has_native_support_for_interval( + params['time_interval'] + ): params['time_interval'] = 'day' return params @@ -625,33 +640,33 @@ class ChartNgCell(CellBase): # "W" and "o" are interpreted by Django's date filter and should be left as is. First W is # backslash escaped to prevent it from being interpreted, translators should refer to Django's # documentation in order to know if the new letter resulting of translation should be escaped or not. - '_week': gettext(r'\WW-o'), - '_month': 'm-Y', - '_year': 'Y', - '_weekday': 'l', + 'week': gettext(r'\WW-o'), + 'month': 'm-Y', + 'year': 'Y', + 'weekday': 'l', } if interval == 'day': x_labels = [ format_date(min_date + timedelta(days=i), date_formats['day']) for i in range((max_date - min_date).days + 1) ] - elif interval == '_month': + elif interval == 'month': month_difference = max_date.month - min_date.month + (max_date.year - min_date.year) * 12 x_labels = [ - format_date(min_date + relativedelta(months=i), date_formats['_month']) + format_date(min_date + relativedelta(months=i), date_formats['month']) for i in range(month_difference + 1) ] - elif interval == '_year': + elif interval == 'year': x_labels = [str(year) for year in range(min_date.year, max_date.year + 1)] - elif interval == '_weekday': + elif interval == 'weekday': x_labels = [str(label) for label in WEEKDAYS.values()] - elif interval == '_week': + elif interval == 'week': x_labels = [] date, last_date = min_date, max_date if min_date.weekday() > max_date.weekday(): last_date += relativedelta(weeks=1) while date <= last_date: - x_labels.append(format_date(date, date_formats['_week'])) + x_labels.append(format_date(date, date_formats['week'])) date += relativedelta(weeks=1) aggregates = OrderedDict((label, [0] * len(series_data)) for label in x_labels) diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 06696d89..d5d02c6a 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -1211,8 +1211,8 @@ def test_chartng_cell_manager_new_api(app, admin_user, new_api_statistics): ('day', False, 'Day'), ('month', True, 'Month'), ('year', False, 'Year'), - ('_week', False, 'Week'), - ('_weekday', False, 'Week day'), + ('week', False, 'Week'), + ('weekday', False, 'Week day'), ] ou_field = resp.form[field_prefix + 'ou'] @@ -1667,10 +1667,10 @@ def test_chartng_cell_new_api_aggregation(new_api_statistics, app, admin_user, n assert time_interval_field.value == 'day' assert time_interval_field.options == [ ('day', True, 'Day'), - ('_week', False, 'Week'), - ('_month', False, 'Month'), - ('_year', False, 'Year'), - ('_weekday', False, 'Week day'), + ('week', False, 'Week'), + ('month', False, 'Month'), + ('year', False, 'Year'), + ('weekday', False, 'Week day'), ] resp.form.submit() cell.refresh_from_db() @@ -1682,7 +1682,7 @@ def test_chartng_cell_new_api_aggregation(new_api_statistics, app, admin_user, n assert chart.raw_series[0][0][:8] == [0, 0, 0, 0, 0, 0, 0, 1] assert chart.raw_series[1][0][:8] == [2, 0, 0, 0, 0, 0, 0, 2] - time_interval_field.value = '_month' + time_interval_field.value = 'month' resp.form.submit() cell.refresh_from_db() @@ -1696,7 +1696,7 @@ def test_chartng_cell_new_api_aggregation(new_api_statistics, app, admin_user, n ([4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], {'title': 'Serie 2'}), ] - time_interval_field.value = '_year' + time_interval_field.value = 'year' resp.form.submit() cell.refresh_from_db() @@ -1708,7 +1708,7 @@ def test_chartng_cell_new_api_aggregation(new_api_statistics, app, admin_user, n ([5, 0, 0], {'title': 'Serie 2'}), ] - time_interval_field.value = '_weekday' + time_interval_field.value = 'weekday' resp.form.submit() cell.refresh_from_db() @@ -1720,7 +1720,7 @@ def test_chartng_cell_new_api_aggregation(new_api_statistics, app, admin_user, n ([1, 4, 0, 0, 0, 0, 0], {'title': 'Serie 2'}), ] - time_interval_field.value = '_week' + time_interval_field.value = 'week' resp.form.submit() cell.refresh_from_db() -- 2.30.2