Completed
Push — master ( 0be0a9...f0e1ac )
by Fox
01:24
created

publishing()   D

Complexity

Conditions 9

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 9
c 6
b 1
f 0
dl 0
loc 50
rs 4
1
# coding: utf-8
2
from __future__ import unicode_literals
3
from __future__ import absolute_import
4
5
import arrow
6
7
# django
8
from django.conf import settings
9
from django.utils.log import getLogger
10
11
# trigger happy
12
from django_th.services import default_provider
13
from django_th.models import TriggerService
14
15
logger = getLogger('django_th.trigger_happy')
16
17
default_provider.load_services()
18
19
20
def update_trigger(service):
21
    """
22
        update the date when occurs the trigger
23
        :param service: service object to update
24
    """
25
    now = arrow.utcnow().to(settings.TIME_ZONE).format(
26
        'YYYY-MM-DD HH:mm:ssZZ')
27
    TriggerService.objects.filter(id=service.id).update(date_triggered=now)
28
29
30
def log_update(service, to_update, status, count):
31
    """
32
        lets log everything at the end
33
        :param service: service object
34
        :param to_update: boolean to check if we have to update
35
        :param status: is everything worked fine ?
36
        :param count: number of data to update
37
        :type service: service object
38
        :type to_update: boolean
39
        :type status: boolean
40
        :type count: interger
41
    """
42
    if to_update:
43
        if status:
44
            logger.info("{} - {} new data".format(service, count))
45
        else:
46
            logger.warn("{} AN ERROR OCCURS ".format(service))
47
    else:
48
        logger.debug("{} nothing new ".format(service))
49
50
51
def publishing(service):
52
    """
53
        the purpose of this tasks is to get the data from the cache
54
        then publish them
55
        :param service: service object where we will publish
56
        :type service: object
57
    """
58
    if service.provider.name.status and service.consumer.name.status:
59
        # flag to know if we have to update
60
        to_update = False
61
        # flag to get the status of a service
62
        status = False
63
        # provider - the service that offer data
64
        # check if the service has already been triggered
65
        # if date_triggered is None, then it's the first run
66
        if service.date_triggered is None:
67
            logger.debug("first run {}".format(service))
68
            to_update = True
69
            status = True
70
        # run run run
71
        service_provider = default_provider.get_service(
72
            str(service.provider.name.name))
73
74
        # 1) get the data from the provider service
75
        module_name = 'th_' + \
76
                      service.provider.name.name.split('Service')[1].lower()
77
        kw = {'trigger_id': str(service.id), 'cache_stack': module_name}
78
        data = getattr(service_provider, 'process_data')(**kw)
79
        count_new_data = len(data) if data else 0
80
        if count_new_data > 0:
81
            # consumer - the service which uses the data
82
            service_consumer = default_provider.get_service(
83
                        str(service.consumer.name.name))
84
            kwargs = {'user': service.user}
85
            getattr(service_consumer, '__init__')(service.consumer.token,
86
                                                  **kwargs)
87
            consumer = getattr(service_consumer, 'save_data')
88
89
            # 2) for each one
90
            for d in data:
91
                d['userservice_id'] = service.consumer.id
92
                # the consumer will save the data and return if success or not
93
                status = consumer(service.id, **d)
94
95
                to_update = True
96
            # let's log
97
        log_update(service, to_update, status, count_new_data)
98
        # let's update
99
        if to_update and status:
100
            update_trigger(service)
101