Projet

Général

Profil

0001-esirius_swi-compute-a-fancy-status-60958.patch

Nicolas Roche, 21 janvier 2022 19:31

Télécharger (8,19 ko)

Voir les différences:

Subject: [PATCH] esirius_swi: compute a fancy status (#60958)

 passerelle/contrib/esirius_swi/models.py |  4 +-
 passerelle/contrib/esirius_swi/utils.py  | 62 ++++++++++++++++++++++++
 tests/test_esirius_swi.py                | 62 +++++++++++++++++++-----
 3 files changed, 116 insertions(+), 12 deletions(-)
 create mode 100644 passerelle/contrib/esirius_swi/utils.py
passerelle/contrib/esirius_swi/models.py
19 19
import zeep
20 20
from django.db import models
21 21
from django.utils.translation import ugettext_lazy as _
22 22

  
23 23
from passerelle.base.models import BaseResource
24 24
from passerelle.utils.api import endpoint
25 25
from passerelle.utils.jsonresponse import APIError
26 26

  
27
from . import utils
28

  
27 29

  
28 30
class ESiriusSwi(BaseResource):
29 31
    base_url = models.URLField(_('Base API URL'), help_text=_('with trailing slash'))
30 32
    verify_cert = models.BooleanField(default=True, verbose_name=_('Check HTTPS Certificate validity'))
31 33

  
32 34
    category = _('Business Process Connectors')
33 35

  
34 36
    class Meta:
......
74 76
                'realMaxWaitingTime',
75 77
                'resourceNumber',
76 78
                'serviceWaitingIndicatorWSList',
77 79
                'siteCode',
78 80
                'waitingVisitorNumber',
79 81
                'weekHoursWS',
80 82
            ]:
81 83
                item_dest[key] = item_src[key]
82
            data.append(item_dest)
84
            data.append(utils.compute_status(item_dest))
83 85
        return {'err': 0, 'data': data}
passerelle/contrib/esirius_swi/utils.py
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17

  
18
def compute_status(raw_data):
19
    data = {
20
        'name': raw_data['name'],
21
        'status': 'closed',
22
        'waiting_visitors': raw_data['cumulatedWaitingVisitorNumber'],
23
        'focus': {
24
            'value': None,
25
            'unit': None,
26
            'html_color': '#ff1800',
27
        },
28
        'raw_data': raw_data,
29
    }
30

  
31
    # closed
32
    if not raw_data['resourceNumber']:
33
        return data
34
    try:
35
        hours, minutes, seconds = (int(x) for x in raw_data['estimatedAvgWaitingTime'].split(':'))
36
    except ValueError:
37
        return data
38

  
39
    data['status'] = 'open'
40
    unit = ''
41
    if hours:
42
        data['focus']['value'] = hours
43
        unit = 'heure'
44
    elif minutes:
45
        data['focus']['value'] = minutes
46
        unit = 'minute'
47
    else:
48
        data['focus']['value'] = seconds
49
        unit = 'seconde'
50
    if data['focus']['value'] > 1:
51
        unit = unit + 's'
52
    data['focus']['unit'] = unit
53
    for color in [
54
        {'time_limit': '00:05:00', 'html_color': '#c3f000'},
55
        {'time_limit': '00:10:00', 'html_color': '#ffff00'},
56
        {'time_limit': '00:20:00', 'html_color': '#ffd100'},
57
        {'time_limit': '00:40:00', 'html_color': '#ff5e00'},
58
    ]:
59
        if raw_data['estimatedAvgWaitingTime'] < color['time_limit']:
60
            data['focus']['html_color'] = color['html_color']
61
            break
62
    return data
tests/test_esirius_swi.py
16 16
import json
17 17
import os
18 18

  
19 19
import mock
20 20
import pytest
21 21
import utils
22 22

  
23 23
from passerelle.contrib.esirius_swi.models import ESiriusSwi
24
from passerelle.contrib.esirius_swi.utils import compute_status
24 25

  
25 26
pytestmark = pytest.mark.django_db
26 27

  
27 28
TEST_BASE_DIR = os.path.join(os.path.dirname(__file__), 'data', 'esirius_swi')
28 29

  
29 30

  
30 31
def get_xml_file(filename):
31 32
    with open(os.path.join(TEST_BASE_DIR, filename), 'rb') as desc:
......
60 61
        status_code=200,
61 62
        headers={'Content-Type': 'text/xml'},
62 63
    )
63 64
    resp = app.get('/esirius-swi/test/get_all_indicators')
64 65
    assert resp.json['err'] == 1
65 66
    assert resp.json['err_desc'] == "'NoneType' object has no attribute 'getroottree'"
66 67

  
67 68

  
69
@pytest.mark.parametrize(
70
    'resource, time, status, value, unit, color',
71
    [
72
        (0, '00:01:39', 'closed', None, None, '#ff1800'),
73
        (1, '-', 'closed', None, None, '#ff1800'),
74
        (1, '00:00:00', 'open', 0, 'seconde', '#c3f000'),
75
        (1, '00:00:03', 'open', 3, 'secondes', '#c3f000'),
76
        (1, '00:01:39', 'open', 1, 'minute', '#c3f000'),
77
        (1, '00:05:00', 'open', 5, 'minutes', '#ffff00'),
78
        (1, '00:10:00', 'open', 10, 'minutes', '#ffd100'),
79
        (1, '00:17:00', 'open', 17, 'minutes', '#ffd100'),
80
        (1, '00:20:00', 'open', 20, 'minutes', '#ff5e00'),
81
        (2, '00:40:00', 'open', 40, 'minutes', '#ff1800'),
82
        (2, '00:42:00', 'open', 42, 'minutes', '#ff1800'),
83
        (3, '01:22:22', 'open', 1, 'heure', '#ff1800'),
84
    ],
85
)
86
def test_status(resource, time, status, value, unit, color):
87
    raw_data = {
88
        'name': "Braine-l'Alleud",
89
        'cumulatedWaitingVisitorNumber': 6,
90
        'resourceNumber': resource,
91
        'estimatedAvgWaitingTime': time,
92
    }
93
    assert compute_status(raw_data) == {
94
        'name': "Braine-l'Alleud",
95
        'status': status,
96
        'waiting_visitors': 6,
97
        'focus': {'value': value, 'unit': unit, 'html_color': color},
98
        'raw_data': raw_data,
99
    }
100

  
101

  
68 102
@mock.patch('passerelle.utils.Request.get')
69 103
@mock.patch('passerelle.utils.Request.post')
70 104
def test_get_all_indicator(mocked_post, mocked_get, v1_service_wsdl, connector, app):
71 105
    mocked_get.return_value = mock.Mock(content=v1_service_wsdl)
72 106
    mocked_post.return_value = mock.Mock(
73 107
        content=get_xml_file('get_all_indicators.xml'),
74 108
        status_code=200,
75 109
        headers={'Content-Type': 'text/xml'},
76 110
    )
77 111
    resp = app.get('/esirius-swi/test/get_all_indicators')
78 112
    assert resp.json['err'] == 0
79 113
    assert resp.json['data'] == [
80 114
        {
81
            'cumulatedWaitingVisitorNumber': 6,
82
            'estimatedAvgWaitingTime': '00:03:29',
83
            'estimatedMaxWaitingTime': '-',
84
            'estimatedWaitingTimeForNextTicket': '-',
85 115
            'name': "Braine-l'Alleud",
86
            'realAvgWaitingTime': '00:03:51',
87
            'realMaxWaitingTime': '00:06:42',
88
            'resourceNumber': 9.000003,
89
            'serviceWaitingIndicatorWSList': None,
90
            'siteCode': 'site1',
91
            'waitingVisitorNumber': None,
92
            'weekHoursWS': None,
116
            'status': 'open',
117
            'waiting_visitors': 6,
118
            'focus': {'value': 3, 'unit': 'minutes', 'html_color': '#c3f000'},
119
            'raw_data': {
120
                'cumulatedWaitingVisitorNumber': 6,
121
                'estimatedAvgWaitingTime': '00:03:29',
122
                'estimatedMaxWaitingTime': '-',
123
                'estimatedWaitingTimeForNextTicket': '-',
124
                'name': "Braine-l'Alleud",
125
                'realAvgWaitingTime': '00:03:51',
126
                'realMaxWaitingTime': '00:06:42',
127
                'resourceNumber': 9.000003,
128
                'serviceWaitingIndicatorWSList': None,
129
                'siteCode': 'site1',
130
                'waitingVisitorNumber': None,
131
                'weekHoursWS': None,
132
            },
93 133
        }
94 134
    ]
95
-