def add_notification(self, mbox, trace_func=noop): """ :param mbox: (delivery_api_client.Model.mbox_response.MboxResponse) mbox :param trace_func: (callable) trace function """ display_tokens = [] for option in mbox.options: event_token = option.event_token event_key = "{}-{}".format(mbox.name, event_token) if event_token and event_key not in self.prev_event_keys: display_tokens.append(event_token) self.prev_event_keys.add(event_key) if not display_tokens: return notification_mbox = NotificationMbox(name=mbox.name) notification = Notification(id=create_uuid(), impression_id=create_uuid(), timestamp=get_epoch_time_milliseconds(), type=MetricType.DISPLAY, mbox=notification_mbox, tokens=display_tokens) if callable(trace_func): trace_func(notification) self.notifications.append(notification)
def time_start(self, _id, increment_timer=False): """Sets start time for ID :param _id: (str) metric name :param increment_timer: (bool) increment timer, defaults to False :return: (str) ID """ timing_id = self._get_unique_timing_id(_id) if increment_timer else _id if timing_id not in self.start_times: self.start_times[timing_id] = get_epoch_time_milliseconds() return timing_id
def create_target_cookie(cookies): """Serializes several individual cookies into a single Target cookie""" now = get_epoch_time_milliseconds() max_age = abs(get_max_expires(cookies) * MILLISECONDS_IN_SECOND - now) serialized_cookies = [serialize_cookie(cookie) for cookie in cookies] return { "name": TARGET_COOKIE, "value": "|".join(serialized_cookies), "maxAge": math.ceil(float(max_age) / MILLISECONDS_IN_SECOND) }
def time_end(self, _id, offset=0): """Sets timing for ID :param _id: (str) timing_id that must match the output from time_start :param offset: (int) timing offset, defaults to 0 :return: (int) timing """ if not self.start_times.get(_id): return -1 timing = get_epoch_time_milliseconds() - self.start_times.get(_id) - offset self.timings[_id] = timing return timing
def _get_all_mbox_notifications(mboxes): notifications = [] for mbox in mboxes: tokens = mbox.get("event_tokens") notifications.append(Notification(id=get_uuid(), impression_id=get_uuid(), mbox=NotificationMbox(name=mbox.get("name"), state=mbox.get("state")), type=MetricType.DISPLAY, timestamp=get_epoch_time_milliseconds(), tokens=tokens)) return notifications
def add_telemetry_entry(self, entry): """ :param entry: (delivery_api_client.Model.telemetry_entry.TelemetryEntry) telemetry entry """ if not self.telemetry_enabled: return entry.request_id = self.request_id entry.timestamp = get_epoch_time_milliseconds() entry.features = TelemetryFeatures( decisioning_method=DecisioningMethod.ON_DEVICE) self.telemetry_entries.append(entry)
def _get_all_view_notifications(views): notifications = [] for view in views: tokens = view.get("event_tokens") notifications.append(Notification(id=get_uuid(), impression_id=get_uuid(), view=NotificationView(key=view.get("key"), name=view.get("name"), state=view.get("state")), type=MetricType.DISPLAY, timestamp=get_epoch_time_milliseconds(), tokens=tokens)) return notifications
def _create_timing_context(): """Create timing context :return: (target_decisioning_engine.types.decisioning_context.TimingContext) Timing context """ now = datetime.datetime.utcnow() current_hours = two_digit_string(now.hour) current_minutes = two_digit_string(now.minute) _current_time = "{}{}".format(current_hours, current_minutes) # 24-hour time, UTC, HHmm # now.weekday() gives us Monday as 0 through Sunday as 6. We want to return Monday as 1 through Sunday as 7 _current_day = now.weekday() + 1 return TimingContext(current_timestamp=get_epoch_time_milliseconds(now), current_time=_current_time, current_day=_current_day)
def test_get_epoch_time_milliseconds_passed_datetime(self): with patch("target_tools.utils.datetime.datetime", Mock(wraps=datetime.datetime)) as mock_datetime: result = get_epoch_time_milliseconds(MOCK_DATE) self.assertEqual(result, 1616424781000) self.assertEqual(mock_datetime.utcnow.call_count, 0)
def test_get_epoch_time_milliseconds_now(self): with patch("target_tools.utils.datetime.datetime", Mock(wraps=datetime.datetime)) as mock_datetime: mock_datetime.utcnow.return_value = MOCK_DATE result = get_epoch_time_milliseconds() self.assertEqual(result, 1616424781000) self.assertEqual(mock_datetime.utcnow.call_count, 1)