Projet

Général

Profil

0001-dataviz-add-formatting-for-series-in-seconds-72131.patch

Valentin Deniaud, 07 décembre 2022 13:37

Télécharger (5,78 ko)

Voir les différences:

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
combo/apps/dataviz/migrations/0025_statistic_data_type.py
1
# Generated by Django 2.2.26 on 2022-12-06 17:03
2

  
3
from django.db import migrations, models
4

  
5

  
6
class Migration(migrations.Migration):
7

  
8
    dependencies = [
9
        ('dataviz', '0024_display_condition'),
10
    ]
11

  
12
    operations = [
13
        migrations.AddField(
14
            model_name='statistic',
15
            name='data_type',
16
            field=models.CharField(default='', max_length=32),
17
            preserve_default=False,
18
        ),
19
    ]
combo/apps/dataviz/models.py
142 142
    url = models.URLField(_('Data URL'))
143 143
    filters = JSONField(default=list)
144 144
    has_future_data = models.BooleanField(default=False)
145
    data_type = models.CharField(max_length=32)
145 146
    available = models.BooleanField(_('Available data'), default=True)
146 147
    last_update = models.DateTimeField(_('Last update'), null=True, auto_now=True)
147 148

  
......
372 373
            chart.axis_count = min(len(data['series']), 2)
373 374
            self.prepare_chart(chart, width, height)
374 375

  
376
            if self.statistic.data_type:
377
                chart.config.value_formatter = self.get_value_formatter(self.statistic.data_type)
378
                chart.compute_sum = False
379

  
375 380
            if chart.axis_count == 1:
376 381
                data['series'][0]['data'] = self.process_one_dimensional_data(
377 382
                    chart, data['series'][0]['data']
......
630 635
                chart.add(label, value)
631 636

  
632 637
    @staticmethod
633
    def get_value_formatter(unit, measure):
638
    def get_value_formatter(unit, measure='duration'):
634 639
        if unit == 'seconds' or measure == 'duration':
635 640

  
636 641
            def format_duration(value):
combo/apps/dataviz/utils.py
46 46
                        site_title=site_dict.get('title', ''),
47 47
                        filters=stat.get('filters', []),
48 48
                        has_future_data=stat.get('future_data', False),
49
                        data_type=stat.get('data_type', ''),
49 50
                        available=True,
50 51
                    )
51 52
                )
52 53

  
53
    update_fields = ('label', 'url', 'site_title', 'filters', 'available', 'has_future_data')
54
    update_fields = ('label', 'url', 'site_title', 'filters', 'available', 'has_future_data', 'data_type')
54 55
    all_statistics = {stat.natural_key(): stat for stat in Statistic.objects.all()}
55 56
    statistics_to_create = []
56 57
    statistics_to_update = {}
tests/test_dataviz.py
525 525
                },
526 526
            ],
527 527
        },
528
        {
529
            'url': 'https://authentic.example.com/api/statistics/duration-in-seconds/',
530
            'name': 'Duration in seconds',
531
            'id': 'duration-in-seconds',
532
            'data_type': 'seconds',
533
        },
528 534
    ]
529 535
}
530 536

  
......
621 627
            },
622 628
        }
623 629
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
630
    if url.path == '/api/statistics/duration-in-seconds/':
631
        response = {
632
            'data': {
633
                'series': [{'data': [140, 3600 * 3, 86400 * 4, 86400 * 100], 'label': 'Serie 1'}],
634
                'x_labels': ['Minutes', 'Hours', 'Days', 'Months'],
635
            },
636
        }
637
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
624 638

  
625 639

  
626 640
@pytest.fixture
......
1304 1318
    resp = app.get(location, status=404)
1305 1319

  
1306 1320

  
1321
@with_httmock(new_api_mock)
1322
def test_chartng_cell_view_new_api_duration(app, normal_user, new_api_statistics):
1323
    page = Page.objects.create(title='One', slug='index')
1324
    cell = ChartNgCell(page=page, order=1, placeholder='content')
1325
    cell.statistic = Statistic.objects.get(slug='duration-in-seconds')
1326
    cell.save()
1327

  
1328
    resp = app.get('/')
1329
    location = '/api/dataviz/graph/%s/' % cell.id
1330
    resp = app.get(location + '?width=400', status=200)
1331
    assert '>Less than an hour<' in resp.text
1332
    assert '>3 hours<' in resp.text
1333
    assert '>4 days<' in resp.text
1334
    assert '>100 days<' in resp.text
1335

  
1336
    cell.chart_type = 'table'
1337
    cell.save()
1338
    resp = app.get(location, status=200)
1339
    assert '<td>Less than an hour</td>' in resp.text
1340
    assert '<td>3 hours</td>' in resp.text
1341
    assert '<td>4 days</td>' in resp.text
1342
    assert '<td>100 days</td>' in resp.text
1343

  
1344

  
1307 1345
@with_httmock(bijoe_mock)
1308 1346
def test_chartng_cell_manager(app, admin_user, statistics):
1309 1347
    page = Page(title='One', slug='index')
1310
-