Completed
Push — master ( 7a99b7...e28ec8 )
by Fox
01:25
created

Pub.provider()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
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, update_result
14
15
16
logger = getLogger('django_th.trigger_happy')
17
18
default_provider.load_services()
19
20
21
class Pub(object):
22
    """
23
        Publishing data to any service
24
    """
25
26
    def update_trigger(self, service):
27
        """
28
            update the date when occurs the trigger
29
            :param service: service object to update
30
        """
31
        now = arrow.utcnow().to(settings.TIME_ZONE).format(
32
            'YYYY-MM-DD HH:mm:ssZZ')
33
        TriggerService.objects.filter(id=service.id).update(date_triggered=now)
34
35
    def log_update(self, service, to_update, status, count):
36
        """
37
            lets log everything at the end
38
            :param service: service object
39
            :param to_update: boolean to check if we have to update
40
            :param status: is everything worked fine ?
41
            :param count: number of data to update
42
            :type service: service object
43
            :type to_update: boolean
44
            :type status: boolean
45
            :type count: interger
46
        """
47
        if to_update:
48
            if status:
49
                msg = "{} - {} new data".format(service, count)
50
                update_result(service.id, msg="OK")
51
                logger.info(msg)
52
            else:
53
                msg = "{} AN ERROR OCCURS ".format(service)
54
                update_result(service.id, msg=msg)
55
                logger.warn(msg)
56
        else:
57
            logger.debug("{} nothing new ".format(service))
58
59
    def provider(self, service):
60
        """
61
            get the data from (the cache of) the service provider
62
            :param service:
63
            :return: data
64
        """
65
        service_provider = default_provider.get_service(
66
            str(service.provider.name.name))
67
68
        # 1) get the data from the provider service
69
        module_name = 'th_' + \
70
                      service.provider.name.name.split('Service')[1].lower()
71
        kwargs = {'trigger_id': str(service.id), 'cache_stack': module_name}
72
        return getattr(service_provider, 'process_data')(**kwargs)
73
74
    def consumer(self, service, data, to_update, status):
75
        """
76
            call the consumer and handle the data
77
            :param service:
78
            :param data:
79
            :param to_update:
80
            :param status:
81
            :return: status
82
        """
83
        # consumer - the service which uses the data
84
        service_consumer = default_provider.get_service(
85
            str(service.consumer.name.name))
86
        kwargs = {'user': service.user}
87
        getattr(service_consumer, '__init__')(service.consumer.token,
88
                                              **kwargs)
89
        instance = getattr(service_consumer, 'save_data')
90
91
        # 2) for each one
92
        for d in data:
93
            d['userservice_id'] = service.consumer.id
94
            # the consumer will save the data and return if success or not
95
            status = instance(service.id, **d)
96
97
            to_update = True
98
99
        return to_update, status
100
101
    def publishing(self, service):
102
        """
103
            the purpose of this tasks is to get the data from the cache
104
            then publish them
105
            :param service: service object where we will publish
106
            :type service: object
107
        """
108
        # flag to know if we have to update
109
        to_update = False
110
        # flag to get the status of a service
111
        status = False
112
        # provider - the service that offer data
113
        # check if the service has already been triggered
114
        # if date_triggered is None, then it's the first run
115
        if service.date_triggered is None:
116
            logger.debug("first run {}".format(service))
117
            to_update = True
118
            status = True
119
        # run run run
120
        data = self.provider(service)
121
        count_new_data = len(data) if data else 0
122
        if count_new_data > 0:
123
            to_update, status = self.consumer(service, data, to_update, status)
124
            # let's log
125
        self.log_update(service, to_update, status, count_new_data)
126
        # let's update
127
        if to_update and status:
128
            self.update_trigger(service)
129