From 3baf1d5468b09be9885dc9796a09689e448af5fa Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 7 Dec 2022 10:38:39 +0100 Subject: [PATCH] dataviz: add formatting for series in seconds (#72131) --- .../migrations/0025_statistic_data_type.py | 19 ++++++++++ combo/apps/dataviz/models.py | 7 +++- combo/apps/dataviz/utils.py | 3 +- tests/test_dataviz.py | 38 +++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 combo/apps/dataviz/migrations/0025_statistic_data_type.py diff --git a/combo/apps/dataviz/migrations/0025_statistic_data_type.py b/combo/apps/dataviz/migrations/0025_statistic_data_type.py new file mode 100644 index 00000000..87a758f2 --- /dev/null +++ b/combo/apps/dataviz/migrations/0025_statistic_data_type.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.26 on 2022-12-06 17:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dataviz', '0024_display_condition'), + ] + + operations = [ + migrations.AddField( + model_name='statistic', + name='data_type', + field=models.CharField(default='', max_length=32), + preserve_default=False, + ), + ] diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index b22f6280..00075718 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -142,6 +142,7 @@ class Statistic(models.Model): url = models.URLField(_('Data URL')) filters = JSONField(default=list) has_future_data = models.BooleanField(default=False) + data_type = models.CharField(max_length=32) available = models.BooleanField(_('Available data'), default=True) last_update = models.DateTimeField(_('Last update'), null=True, auto_now=True) @@ -372,6 +373,10 @@ class ChartNgCell(CellBase): chart.axis_count = min(len(data['series']), 2) self.prepare_chart(chart, width, height) + if self.statistic.data_type: + chart.config.value_formatter = self.get_value_formatter(self.statistic.data_type) + chart.compute_sum = False + if chart.axis_count == 1: data['series'][0]['data'] = self.process_one_dimensional_data( chart, data['series'][0]['data'] @@ -630,7 +635,7 @@ class ChartNgCell(CellBase): chart.add(label, value) @staticmethod - def get_value_formatter(unit, measure): + def get_value_formatter(unit, measure='duration'): if unit == 'seconds' or measure == 'duration': def format_duration(value): diff --git a/combo/apps/dataviz/utils.py b/combo/apps/dataviz/utils.py index 113ecaa5..3d13cce1 100644 --- a/combo/apps/dataviz/utils.py +++ b/combo/apps/dataviz/utils.py @@ -46,11 +46,12 @@ def update_available_statistics(): site_title=site_dict.get('title', ''), filters=stat.get('filters', []), has_future_data=stat.get('future_data', False), + data_type=stat.get('data_type', ''), available=True, ) ) - update_fields = ('label', 'url', 'site_title', 'filters', 'available', 'has_future_data') + update_fields = ('label', 'url', 'site_title', 'filters', 'available', 'has_future_data', 'data_type') all_statistics = {stat.natural_key(): stat for stat in Statistic.objects.all()} statistics_to_create = [] statistics_to_update = {} diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 5fcf2cbe..010a6f23 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -525,6 +525,12 @@ STATISTICS_LIST = { }, ], }, + { + 'url': 'https://authentic.example.com/api/statistics/duration-in-seconds/', + 'name': 'Duration in seconds', + 'id': 'duration-in-seconds', + 'data_type': 'seconds', + }, ] } @@ -621,6 +627,14 @@ def new_api_mock(url, request): }, } return {'content': json.dumps(response), 'request': request, 'status_code': 200} + if url.path == '/api/statistics/duration-in-seconds/': + response = { + 'data': { + 'series': [{'data': [140, 3600 * 3, 86400 * 4, 86400 * 100], 'label': 'Serie 1'}], + 'x_labels': ['Minutes', 'Hours', 'Days', 'Months'], + }, + } + return {'content': json.dumps(response), 'request': request, 'status_code': 200} @pytest.fixture @@ -1304,6 +1318,30 @@ def test_chartng_cell_view_new_api(app, normal_user, new_api_statistics): resp = app.get(location, status=404) +@with_httmock(new_api_mock) +def test_chartng_cell_view_new_api_duration(app, normal_user, new_api_statistics): + page = Page.objects.create(title='One', slug='index') + cell = ChartNgCell(page=page, order=1, placeholder='content') + cell.statistic = Statistic.objects.get(slug='duration-in-seconds') + cell.save() + + resp = app.get('/') + location = '/api/dataviz/graph/%s/' % cell.id + resp = app.get(location + '?width=400', status=200) + assert '>Less than an hour<' in resp.text + assert '>3 hours<' in resp.text + assert '>4 days<' in resp.text + assert '>100 days<' in resp.text + + cell.chart_type = 'table' + cell.save() + resp = app.get(location, status=200) + assert 'Less than an hour' in resp.text + assert '3 hours' in resp.text + assert '4 days' in resp.text + assert '100 days' in resp.text + + @with_httmock(bijoe_mock) def test_chartng_cell_manager(app, admin_user, statistics): page = Page(title='One', slug='index') -- 2.35.1