From b29c7b510e9c4a3b54560ab153315f666125e703 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 12 Oct 2019 17:10:21 +0200 Subject: [PATCH] views: export duration as numbers in JSON API (#36770) --- bijoe/visualization/views.py | 23 ++++++++++++++++++----- tests/fixtures/schema1/01_schema.json | 6 ++++++ tests/test_views.py | 27 ++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/bijoe/visualization/views.py b/bijoe/visualization/views.py index 2794b7c..35cfd04 100644 --- a/bijoe/visualization/views.py +++ b/bijoe/visualization/views.py @@ -272,7 +272,14 @@ class VisualizationJSONView(generics.GenericAPIView): permission_classes = () queryset = models.Visualization.objects.all() + def get(self, request, pk, format=None): + + def cell_value(cell): + if cell['type'] == 'duration': + return cell['value'].total_seconds() + return cell['value'] + instance = self.get_object() loop = [] all_visualizations = Visualization.from_json(instance.parameters, request=request) @@ -291,7 +298,7 @@ class VisualizationJSONView(generics.GenericAPIView): y_label = unicode(row[1]['value']) used_x_labels[x_label] = True used_y_labels[y_label] = True - grid[(x_label, y_label)] = row[2]['value'] + grid[(x_label, y_label)] = cell_value(row[2]) data = [] for y in used_y_labels.keys(): @@ -303,15 +310,18 @@ class VisualizationJSONView(generics.GenericAPIView): elif len(drilldowns) == 1: table = list(visualization.data()) axis_data = [force_text(x[0]['value'] or '').strip() for x in table] - data = [x[1]['value'] for x in table] + data = [cell_value(x[1]) for x in table] if visualization.drilldown_x: axis = {'x_labels': axis_data} else: axis = {'y_labels': axis_data} elif len(drilldowns) == 0: - data = list(list(visualization.data())[0])[0]['value'] + data = cell_value(list(list(visualization.data())[0])[0]) axis = {} - loop.append({'data': data, 'axis': axis}) + loop.append({ + 'data': data, + 'axis': axis + }) if not all_visualizations.loop: data = loop[0]['data'] @@ -321,11 +331,14 @@ class VisualizationJSONView(generics.GenericAPIView): axis['loop'] = [x.label for x in all_visualizations.loop.members] data = [x['data'] for x in loop] + unit = 'seconds' if all_visualizations.measure.type == 'duration' else None + return Response({ 'data': data, 'axis': axis, 'format': '1', - }) + 'unit': unit, + }) warehouse = WarehouseView.as_view() diff --git a/tests/fixtures/schema1/01_schema.json b/tests/fixtures/schema1/01_schema.json index 1cce1c5..0e31db1 100644 --- a/tests/fixtures/schema1/01_schema.json +++ b/tests/fixtures/schema1/01_schema.json @@ -190,6 +190,12 @@ "label": "pourcentage des demandes", "type": "percent", "expression": "case (select count({fact_table}.id) from {table_expression} where {where_conditions}) when 0 then null else count({fact_table}.id) * 100. / (select count({fact_table}.id) from {table_expression} where {where_conditions}) end" + }, + { + "name": "duration", + "label": "délai moyen depuis 2000", + "type": "duration", + "expression": "AVG(datetime - '2000-01-01')" } ] } diff --git a/tests/test_views.py b/tests/test_views.py index 88263be..89ee37f 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -63,5 +63,30 @@ def test_visualization_json_api(schema1, app, admin): assert resp.json == { 'axis': {'x_labels': ['01/2017', '02/2017', '03/2017', '04/2017', '05/2017', '06/2017', '07/2017', '08/2017']}, 'data': [10, 1, 1, 1, 1, 1, 1, 1], - 'format': '1' + 'format': '1', + 'unit': None, + } + + +def test_visualization_json_api_duration(schema1, app, admin): + visualization = Visualization( + name='test', + parameters={ + 'cube': 'facts1', + 'warehouse': 'schema1', + 'measure': 'duration', + 'representation': 'table', + 'loop': '', + 'filters': {}, + 'drilldown_x': 'date__yearmonth'}) + visualization.save() + + login(app, admin) + resp = app.get(reverse('visualization-json', kwargs={'pk': visualization.id})) + # values from test_schem1/test_yearmonth_drilldown + assert resp.json == { + 'axis': {'x_labels': ['01/2017', '02/2017', '03/2017', '04/2017', '05/2017', '06/2017', '07/2017', '08/2017']}, + 'data': [536968800.0, 539258400.0, 541677600.0, 544352400.0, 546944400.0, 549622800.0, 552214800.0, 554893200.0], + 'format': '1', + 'unit': 'seconds', } -- 2.23.0