Completed
Branch master (b389e4)
by Fox
01:43 queued 30s
created

reading()   B

Complexity

Conditions 5

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 5
c 5
b 1
f 0
dl 0
loc 36
rs 8.0894
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
    # flag to know if we have to update
23
    to_update = False
24
    count_new_data = 0
25
    # counting the new data to store to display them in the log
26
    # provider - the service that offer data
27
    provider_token = service.provider.token
28
    service_provider = default_provider.get_service(
29
        str(service.provider.name.name))
30
31
    # check if the service has already been triggered
32
    # if date_triggered is None, then it's the first run
33
    if service.date_triggered is None:
34
        logger.debug("first time {}".format(service))
35
        to_update = True
36
        # run run run
37
    else:
38
        # 1) get the data from the provider service
39
        # get a timestamp of the last triggered of the service
40
        kw = {'token': provider_token,
41
              'trigger_id': service.id,
42
              'date_triggered': service.date_triggered}
43
        getattr(service_provider, '__init__')(provider_token)
44
        data = getattr(service_provider, 'read_data')(**kw)
45
        # counting the new data to store to display them in the log
46
        count_new_data = len(data) if data else 0
47
        if count_new_data > 0:
48
            to_update = True
49
50
    if to_update:
51
        logger.info("{} - {} new data".format(service, count_new_data))
52