0001-views-export-duration-as-numbers-in-JSON-API-36770.patch
bijoe/visualization/views.py | ||
---|---|---|
272 | 272 |
permission_classes = () |
273 | 273 |
queryset = models.Visualization.objects.all() |
274 | 274 | |
275 | ||
275 | 276 |
def get(self, request, pk, format=None): |
277 | ||
278 |
def cell_value(cell): |
|
279 |
if cell['type'] == 'duration' and cell['value'] is not None: |
|
280 |
return cell['value'].total_seconds() |
|
281 |
return cell['value'] |
|
282 | ||
276 | 283 |
instance = self.get_object() |
277 | 284 |
loop = [] |
278 | 285 |
all_visualizations = Visualization.from_json(instance.parameters, request=request) |
... | ... | |
291 | 298 |
y_label = unicode(row[1]['value']) |
292 | 299 |
used_x_labels[x_label] = True |
293 | 300 |
used_y_labels[y_label] = True |
294 |
grid[(x_label, y_label)] = row[2]['value']
|
|
301 |
grid[(x_label, y_label)] = cell_value(row[2])
|
|
295 | 302 | |
296 | 303 |
data = [] |
297 | 304 |
for y in used_y_labels.keys(): |
... | ... | |
303 | 310 |
elif len(drilldowns) == 1: |
304 | 311 |
table = list(visualization.data()) |
305 | 312 |
axis_data = [force_text(x[0]['value'] or '').strip() for x in table] |
306 |
data = [x[1]['value'] for x in table]
|
|
313 |
data = [cell_value(x[1]) for x in table]
|
|
307 | 314 |
if visualization.drilldown_x: |
308 | 315 |
axis = {'x_labels': axis_data} |
309 | 316 |
else: |
310 | 317 |
axis = {'y_labels': axis_data} |
311 | 318 |
elif len(drilldowns) == 0: |
312 |
data = list(list(visualization.data())[0])[0]['value']
|
|
319 |
data = cell_value(list(list(visualization.data())[0])[0])
|
|
313 | 320 |
axis = {} |
314 |
loop.append({'data': data, 'axis': axis}) |
|
321 |
loop.append({ |
|
322 |
'data': data, |
|
323 |
'axis': axis |
|
324 |
}) |
|
315 | 325 | |
316 | 326 |
if not all_visualizations.loop: |
317 | 327 |
data = loop[0]['data'] |
... | ... | |
321 | 331 |
axis['loop'] = [x.label for x in all_visualizations.loop.members] |
322 | 332 |
data = [x['data'] for x in loop] |
323 | 333 | |
334 |
unit = 'seconds' if all_visualizations.measure.type == 'duration' else None |
|
335 | ||
324 | 336 |
return Response({ |
325 | 337 |
'data': data, |
326 | 338 |
'axis': axis, |
327 | 339 |
'format': '1', |
328 |
}) |
|
340 |
'unit': unit, |
|
341 |
}) |
|
329 | 342 | |
330 | 343 | |
331 | 344 |
warehouse = WarehouseView.as_view() |
tests/fixtures/schema1/01_schema.json | ||
---|---|---|
190 | 190 |
"label": "pourcentage des demandes", |
191 | 191 |
"type": "percent", |
192 | 192 |
"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" |
193 |
}, |
|
194 |
{ |
|
195 |
"name": "duration", |
|
196 |
"label": "délai moyen depuis 2000", |
|
197 |
"type": "duration", |
|
198 |
"expression": "AVG(datetime - '2000-01-01')" |
|
193 | 199 |
} |
194 | 200 |
] |
195 | 201 |
} |
tests/test_views.py | ||
---|---|---|
63 | 63 |
assert resp.json == { |
64 | 64 |
'axis': {'x_labels': ['01/2017', '02/2017', '03/2017', '04/2017', '05/2017', '06/2017', '07/2017', '08/2017']}, |
65 | 65 |
'data': [10, 1, 1, 1, 1, 1, 1, 1], |
66 |
'format': '1' |
|
66 |
'format': '1', |
|
67 |
'unit': None, |
|
68 |
} |
|
69 | ||
70 | ||
71 |
def test_visualization_json_api_duration(schema1, app, admin): |
|
72 |
visualization = Visualization( |
|
73 |
name='test', |
|
74 |
parameters={ |
|
75 |
'cube': 'facts1', |
|
76 |
'warehouse': 'schema1', |
|
77 |
'measure': 'duration', |
|
78 |
'representation': 'table', |
|
79 |
'loop': '', |
|
80 |
'filters': {}, |
|
81 |
'drilldown_x': 'date__yearmonth'}) |
|
82 |
visualization.save() |
|
83 | ||
84 |
login(app, admin) |
|
85 |
resp = app.get(reverse('visualization-json', kwargs={'pk': visualization.id})) |
|
86 |
# values from test_schem1/test_yearmonth_drilldown |
|
87 |
assert resp.json == { |
|
88 |
'axis': {'x_labels': ['01/2017', '02/2017', '03/2017', '04/2017', '05/2017', '06/2017', '07/2017', '08/2017']}, |
|
89 |
'data': [536968800.0, 539258400.0, 541677600.0, 544352400.0, 546944400.0, 549622800.0, 552214800.0, 554893200.0], |
|
90 |
'format': '1', |
|
91 |
'unit': 'seconds', |
|
67 | 92 |
} |
68 |
- |