|
1 | 1 | # Import Libraries |
2 | 2 | from moesifapi.exceptions.api_exception import APIException |
3 | | -from moesifapi.moesif_api_client import MoesifAPIClient, Configuration |
4 | | -from .. import global_variables |
| 3 | +from .. import global_variables as gv |
| 4 | +from datetime import datetime, timedelta |
5 | 5 | import threading |
| 6 | +import random |
| 7 | +import math |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class SendMoesif(): |
9 | 11 | # Function to send event to Moesif |
10 | | - def send_event(self, application_id, event_model): |
11 | | - if global_variables.moesif_options.get('LOCAL_DEBUG', False): |
12 | | - Configuration.BASE_URI = global_variables.moesif_options.get('LOCAL_MOESIF_BASEURL', 'https://api.moesif.net') |
13 | | - |
14 | | - client = MoesifAPIClient(application_id) |
15 | | - api_client = client.api |
| 12 | + def send_event(self, event_model): |
16 | 13 | try: |
17 | | - if global_variables.DEBUG: |
| 14 | + if gv.DEBUG: |
18 | 15 | print('Calling API to create event') |
19 | | - api_client.create_event(event_model) |
20 | | - if global_variables.DEBUG: |
21 | | - print("sent done") |
| 16 | + event_api_response = gv.api_client.create_event(event_model) |
| 17 | + event_response_config_etag = event_api_response.get("X-Moesif-Config-ETag") |
| 18 | + |
| 19 | + if event_response_config_etag is not None \ |
| 20 | + and gv.config_etag is not None \ |
| 21 | + and gv.config_etag != event_response_config_etag \ |
| 22 | + and datetime.utcnow() > gv.last_updated_time + timedelta(minutes=5): |
| 23 | + try: |
| 24 | + gv.config = gv.app_config.get_config(gv.api_client, gv.DEBUG) |
| 25 | + self.config_etag, self.sampling_percentage, self.last_updated_time = gv.app_config.parse_configuration( |
| 26 | + gv.config, gv.DEBUG) |
| 27 | + except: |
| 28 | + if gv.DEBUG: |
| 29 | + print('Error while updating the application configuration') |
| 30 | + if gv.DEBUG: |
| 31 | + print("Event sent successfully") |
22 | 32 | except APIException as inst: |
23 | 33 | if 401 <= inst.response_code <= 403: |
24 | 34 | print("Unauthorized access sending event to Moesif. Please check your Appplication Id.") |
25 | | - if global_variables.DEBUG: |
| 35 | + if gv.DEBUG: |
26 | 36 | print("Error sending event to Moesif, with status code:") |
27 | 37 | print(inst.response_code) |
28 | 38 |
|
29 | 39 | # Function to send event async |
30 | | - def send_moesif_async(self, applicaiton_id, event_model): |
| 40 | + def send_moesif_async(self, event_model): |
31 | 41 | try: |
32 | | - mask_event_model = global_variables.moesif_options.get('MASK_EVENT_MODEL', None) |
| 42 | + mask_event_model = gv.moesif_options.get('MASK_EVENT_MODEL', None) |
33 | 43 | if mask_event_model is not None: |
34 | | - if global_variables.DEBUG: |
| 44 | + if gv.DEBUG: |
35 | 45 | print('Masking the event') |
36 | 46 | event_model = mask_event_model(event_model) |
37 | 47 | except: |
38 | | - if global_variables.DEBUG: |
| 48 | + if gv.DEBUG: |
39 | 49 | print("Can not execute MASK_EVENT_MODEL function. Please check moesif settings.") |
40 | 50 |
|
41 | | - sending_background_thread = threading.Thread(target=self.send_event, args=(applicaiton_id, event_model,)) |
42 | | - if global_variables.DEBUG: |
43 | | - print('Staring a new thread') |
44 | | - sending_background_thread.start() |
| 51 | + random_percentage = random.random() * 100 |
| 52 | + gv.sampling_percentage = gv.app_config.get_sampling_percentage(gv.config, event_model.user_id, event_model.company_id) |
| 53 | + |
| 54 | + if gv.sampling_percentage >= random_percentage: |
| 55 | + event_model.weight = 1 if gv.sampling_percentage == 0 else math.floor(100 / gv.sampling_percentage) |
| 56 | + sending_background_thread = threading.Thread(target=self.send_event, args=(event_model,)) |
| 57 | + if gv.DEBUG: |
| 58 | + print('Staring a new thread') |
| 59 | + sending_background_thread.start() |
| 60 | + else: |
| 61 | + if gv.DEBUG: |
| 62 | + print('Skipped Event due to sampling percentage: ' + str( |
| 63 | + gv.sampling_percentage) + ' and random percentage: ' + str(random_percentage)) |
0 commit comments