Completed
Push — master ( cb6660...0be0a9 )
by Fox
01:29
created

reading()   C

Complexity

Conditions 7

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 7
c 4
b 1
f 0
dl 0
loc 38
rs 5.5
1
# coding: utf-8
2
from __future__ import unicode_literals
3
from __future__ import absolute_import
4
5
# django
6
from django.utils.log import getLogger
7
8
# trigger happy
9
from django_th.services import default_provider
10
11
logger = getLogger('django_th.trigger_happy')
12
13
default_provider.load_services()
14
15
16
def reading(service):
17
    """
18
       get the data from the service and put theme in cache
19
       :param service: service object to read
20
       :type service: object
21
    """
22
    # get only activated service
23
    if service.provider.name.status and service.consumer.name.status:
24
        # flag to know if we have to update
25
        to_update = False
26
        count_new_data = 0
27
        # counting the new data to store to display them in the log
28
        # provider - the service that offer data
29
        provider_token = service.provider.token
30
        service_provider = default_provider.get_service(
31
            str(service.provider.name.name))
32
33
        # check if the service has already been triggered
34
        # if date_triggered is None, then it's the first run
35
        if service.date_triggered is None:
36
            logger.debug("first time {}".format(service))
37
            to_update = True
38
            # run run run
39
        else:
40
            # 1) get the data from the provider service
41
            # get a timestamp of the last triggered of the service
42
            kw = {'token': provider_token,
43
                  'trigger_id': service.id,
44
                  'date_triggered': service.date_triggered}
45
            getattr(service_provider, '__init__')(provider_token)
46
            data = getattr(service_provider, 'read_data')(**kw)
47
            # counting the new data to store to display them in the log
48
            count_new_data = len(data) if data else 0
49
            if count_new_data > 0:
50
                to_update = True
51
52
        if to_update:
53
            logger.info("{} - {} new data".format(service, count_new_data))
54