Projet

Général

Profil

0001-dataviz-format-durations-in-graphs-36874.patch

Frédéric Péters, 13 octobre 2019 15:41

Télécharger (5,17 ko)

Voir les différences:

Subject: [PATCH] dataviz: format durations in graphs (#36874)

 combo/apps/dataviz/models.py | 27 ++++++++++++++++++++++-
 tests/test_dataviz.py        | 42 +++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)
combo/apps/dataviz/models.py
19 19

  
20 20
from django.core.urlresolvers import reverse
21 21
from django.db import models
22
from django.utils.translation import ugettext_lazy as _
22
from django.utils.encoding import force_text
23
from django.utils.translation import ugettext_lazy as _, ungettext
23 24
from django.conf import settings
24 25

  
25 26
from jsonfield import JSONField
......
259 260
                chart.add(label, value)
260 261
            chart.show_legend = True
261 262

  
263
        if response.get('unit') == 'seconds':
264
            def format_duration(value):
265
                if value is None:
266
                    return '-'
267
                days = value // 86400
268
                hours = (value % 86400) // 3600
269
                if days:
270
                    days_string = ungettext('%d day', '%d days', days) % days
271
                if hours:
272
                    hours_string = ungettext('%d hour', '%d hours', hours) % hours
273
                if days and hours:
274
                    value = _('%(days_string)s and %(hours_string)s') % {
275
                            'days_string': days_string,
276
                            'hours_string': hours_string,
277
                    }
278
                elif days:
279
                    value = days_string
280
                elif hours:
281
                    value = hours_string
282
                else:
283
                    value = _('Less than an hour')
284
                return force_text(value)
285
            chart.config.value_formatter = format_duration
286

  
262 287
        return chart
tests/test_dataviz.py
88 88
        'name': 'seventh visualization (loop/X/Y)',
89 89
        'slug': 'seventh',
90 90
    },
91

  
91
    {
92
        'data-url': 'https://bijoe.example.com/visualization/8/json/',
93
        'path': 'https://bijoe.example.com/visualization/8/iframe/?signature=123',
94
        'name': 'eighth visualization (duration)',
95
        'slug': 'eighth',
96
    },
92 97
]
93 98

  
94 99

  
......
175 180
            }
176 181
        }
177 182
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
183
    if url.path == '/visualization/8/json/':
184
        response = {
185
            'format': '1',
186
            'data': [1000, 123000, 8600, 86400],
187
            'axis': {
188
                'y_labels': ['web', 'mail', 'email', 'fax']
189
            },
190
            'unit': 'seconds',
191
        }
192
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
178 193

  
179 194

  
180 195
def test_chartng_cell(app):
......
273 288
            with pytest.raises(UnsupportedDataSet):
274 289
                chart = cell.get_chart()
275 290

  
291
            # duration
292
            cell.data_reference = 'plop:eighth'
293
            cell.save()
294
            chart = cell.get_chart()
295

  
276 296

  
277 297
def test_chartng_cell_view(app, normal_user):
278 298
    page = Page(title='One', slug='index')
......
331 351
            resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
332 352
            assert 'Unsupported dataset' in resp.text
333 353

  
354
            # durations
355
            cell.data_reference = 'plop:eighth'
356
            cell.chart_type = 'table'
357
            cell.save()
358
            resp = app.get('/api/dataviz/graph/1/')  # get data in cache
359
            resp = app.get('/')
360
            assert '<td>Less than an hour</td>' in resp.text
361
            assert '<td>1 day and 10 hours</td>' in resp.text
362
            assert '<td>2 hours</td>' in resp.text
363
            assert '<td>1 day</td>' in resp.text
364

  
365
            cell.chart_type = 'bar'
366
            cell.save()
367
            resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
368
            assert '>Less than an hour<' in resp.text
369
            assert '>1 day and 10 hours<' in resp.text
370
            assert '>2 hours<' in resp.text
371
            assert '>1 day<' in resp.text
372

  
334 373

  
335 374
def test_chartng_cell_manager(app, admin_user):
336 375
    page = Page(title='One', slug='index')
......
347 386
            cell.save()
348 387
            resp = app.get('/manage/pages/%s/' % page.id)
349 388
            assert resp.form['cdataviz_chartngcell-%s-data_reference' % cell.id].options == [
389
                (u'plop:eighth', False, u'eighth visualization (duration)'),
350 390
                (u'plop:example', True, u'example visualization (X)'),
351 391
                (u'plop:fifth', False, u'fifth visualization (loop/X)'),
352 392
                (u'plop:fourth', False, u'fourth visualization (no axis)'),
353
-