views: export duration as numbers in JSON API (#36770)
This commit is contained in:
parent
7a235b8aa8
commit
09c52ca6c3
|
@ -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' and cell['value'] is not None:
|
||||
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()
|
||||
|
|
|
@ -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')"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue