-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathimpression.py
More file actions
191 lines (153 loc) · 6.62 KB
/
Copy pathimpression.py
File metadata and controls
191 lines (153 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import logging
import queue
from splitio.api import APIException
from splitio.optional.loaders import asyncio
_LOGGER = logging.getLogger(__name__)
class ImpressionSynchronizer(object):
"""Impressions synchronizer class."""
def __init__(self, impressions_api, storage, bulk_size):
"""
Class constructor.
:param impressions_api: Impressions Api object to send data to the backend
:type impressions_api: splitio.api.impressions.ImpressionsAPI
:param storage: Impressions Storage
:type storage: splitio.storage.ImpressionsStorage
:param bulk_size: How many impressions to send per push.
:type bulk_size: int
"""
self._api = impressions_api
self._impression_storage = storage
self._bulk_size = bulk_size
self._failed = queue.Queue()
def _get_failed(self):
"""Return up to <BULK_SIZE> impressions stored in the failed impressions queue."""
imps = []
count = 0
while count < self._bulk_size:
try:
imps.append(self._failed.get(False))
count += 1
except queue.Empty:
# If no more items in queue, break the loop
break
return imps
def _add_to_failed_queue(self, imps):
"""
Add impressions that were about to be sent to a secondary queue for failed sends.
:param imps: List of impressions that failed to be pushed.
:type imps: list
"""
for impression in imps:
self._failed.put(impression, False)
def synchronize_impressions(self):
"""Send impressions from both the failed and new queues."""
to_send = self._get_failed()
if len(to_send) < self._bulk_size:
# If the amount of previously failed items is less than the bulk
# size, try to complete with new impressions from storage
to_send.extend(self._impression_storage.pop_many(self._bulk_size - len(to_send)))
if not to_send:
return
try:
self._api.flush_impressions(to_send)
except APIException:
_LOGGER.error('Exception raised while reporting impressions')
_LOGGER.debug('Exception information: ', exc_info=True)
self._add_to_failed_queue(to_send)
class ImpressionsCountSynchronizer(object):
def __init__(self, impressions_api, imp_counter):
"""
Class constructor.
:param impressions_api: Impressions Api object to send data to the backend
:type impressions_api: splitio.api.impressions.ImpressionsAPI
:param impressions_manager: Impressions manager instance
:type impressions_manager: splitio.engine.impressions.Manager
"""
self._impressions_api = impressions_api
self._impressions_counter = imp_counter
def synchronize_counters(self):
"""Send impressions from both the failed and new queues."""
if self._impressions_counter == None:
return
to_send = self._impressions_counter.pop_all()
if not to_send:
return
try:
self._impressions_api.flush_counters(to_send)
except APIException:
_LOGGER.error('Exception raised while reporting impression counts')
_LOGGER.debug('Exception information: ', exc_info=True)
class ImpressionSynchronizerAsync(object):
"""Impressions async synchronizer class."""
def __init__(self, impressions_api, storage, bulk_size):
"""
Class constructor.
:param impressions_api: Impressions Api object to send data to the backend
:type impressions_api: splitio.api.impressions.ImpressionsAPI
:param storage: Impressions Storage
:type storage: splitio.storage.ImpressionsStorage
:param bulk_size: How many impressions to send per push.
:type bulk_size: int
"""
self._api = impressions_api
self._impression_storage = storage
self._bulk_size = bulk_size
self._failed = asyncio.Queue()
async def _get_failed(self):
"""Return up to <BULK_SIZE> impressions stored in the failed impressions queue."""
imps = []
count = 0
while count < self._bulk_size and self._failed.qsize() > 0:
try:
imps.append(await self._failed.get())
count += 1
except asyncio.QueueEmpty:
# If no more items in queue, break the loop
break
return imps
async def _add_to_failed_queue(self, imps):
"""
Add impressions that were about to be sent to a secondary queue for failed sends.
:param imps: List of impressions that failed to be pushed.
:type imps: list
"""
for impression in imps:
await self._failed.put(impression)
async def synchronize_impressions(self):
"""Send impressions from both the failed and new queues."""
to_send = await self._get_failed()
if len(to_send) < self._bulk_size:
# If the amount of previously failed items is less than the bulk
# size, try to complete with new impressions from storage
to_send.extend(await self._impression_storage.pop_many(self._bulk_size - len(to_send)))
if not to_send:
return
try:
await self._api.flush_impressions(to_send)
except APIException:
_LOGGER.error('Exception raised while reporting impressions')
_LOGGER.debug('Exception information: ', exc_info=True)
await self._add_to_failed_queue(to_send)
class ImpressionsCountSynchronizerAsync(object):
def __init__(self, impressions_api, imp_counter):
"""
Class constructor.
:param impressions_api: Impressions Api object to send data to the backend
:type impressions_api: splitio.api.impressions.ImpressionsAPI
:param impressions_manager: Impressions manager instance
:type impressions_manager: splitio.engine.impressions.Manager
"""
self._impressions_api = impressions_api
self._impressions_counter = imp_counter
async def synchronize_counters(self):
"""Send impressions from both the failed and new queues."""
if self._impressions_counter == None:
return
to_send = self._impressions_counter.pop_all()
if not to_send:
return
try:
await self._impressions_api.flush_counters(to_send)
except APIException:
_LOGGER.error('Exception raised while reporting impression counts')
_LOGGER.debug('Exception information: ', exc_info=True)