class EventBuffer(object): def __init__(self, capacity): self._capacity = capacity self._events = [] self._summarizer = EventSummarizer() self._exceeded_capacity = False def add_event(self, event): if len(self._events) >= self._capacity: if not self._exceeded_capacity: log.warning("Event queue is full-- dropped an event") self._exceeded_capacity = True else: self._events.append(event) self._exceeded_capacity = False def add_to_summary(self, event): self._summarizer.summarize_event(event) def get_payload(self): return FlushPayload(self._events, self._summarizer.snapshot()) def clear(self): self._events = [] self._summarizer.clear()
class EventBuffer: def __init__(self, capacity): self._capacity = capacity self._events = [] self._summarizer = EventSummarizer() self._exceeded_capacity = False self._dropped_events = 0 def add_event(self, event): if len(self._events) >= self._capacity: self._dropped_events += 1 if not self._exceeded_capacity: log.warning( "Exceeded event queue capacity. Increase capacity to avoid dropping events." ) self._exceeded_capacity = True else: self._events.append(event) self._exceeded_capacity = False def add_to_summary(self, event): self._summarizer.summarize_event(event) def get_and_clear_dropped_count(self): dropped_count = self._dropped_events self._dropped_events = 0 return dropped_count def get_payload(self): return FlushPayload(self._events, self._summarizer.snapshot()) def clear(self): self._events = [] self._summarizer.clear()
def test_summarize_event_increments_counters(): es = EventSummarizer() event1 = { 'kind': 'feature', 'creationDate': 1000, 'key': 'flag1', 'user': user, 'version': 11, 'variation': 1, 'value': 'value1', 'default': 'default1' } event2 = { 'kind': 'feature', 'creationDate': 1000, 'key': 'flag1', 'user': user, 'version': 11, 'variation': 2, 'value': 'value2', 'default': 'default1' } event3 = { 'kind': 'feature', 'creationDate': 1000, 'key': 'flag2', 'user': user, 'version': 22, 'variation': 1, 'value': 'value99', 'default': 'default2' } event4 = { 'kind': 'feature', 'creationDate': 1000, 'key': 'flag1', 'user': user, 'version': 11, 'variation': 1, 'value': 'value1', 'default': 'default1' } event5 = { 'kind': 'feature', 'creationDate': 1000, 'key': 'badkey', 'user': user, 'version': None, 'variation': None, 'value': 'default3', 'default': 'default3' } es.summarize_event(event1) es.summarize_event(event2) es.summarize_event(event3) es.summarize_event(event4) es.summarize_event(event5) data = es.snapshot() expected = { ('flag1', 1, 11): { 'count': 2, 'value': 'value1', 'default': 'default1' }, ('flag1', 2, 11): { 'count': 1, 'value': 'value2', 'default': 'default1' }, ('flag2', 1, 22): { 'count': 1, 'value': 'value99', 'default': 'default2' }, ('badkey', None, None): { 'count': 1, 'value': 'default3', 'default': 'default3' } } assert data.counters == expected
def test_summarize_event_does_nothing_for_identify_event(): es = EventSummarizer() snapshot = es.snapshot() es.summarize_event({ 'kind': 'identify', 'creationDate': 1000, 'user': user }) assert es.snapshot() == snapshot
def test_summarize_event_does_nothing_for_custom_event(): es = EventSummarizer() snapshot = es.snapshot() es.summarize_event({ 'kind': 'custom', 'creationDate': 1000, 'key': 'eventkey', 'user': user }) assert es.snapshot() == snapshot
def test_summarize_event_sets_start_and_end_dates(): es = EventSummarizer() event1 = { 'kind': 'feature', 'creationDate': 2000, 'key': 'flag', 'user': user, 'version': 1, 'variation': 0, 'value': '', 'default': None } event2 = { 'kind': 'feature', 'creationDate': 1000, 'key': 'flag', 'user': user, 'version': 1, 'variation': 0, 'value': '', 'default': None } event3 = { 'kind': 'feature', 'creationDate': 1500, 'key': 'flag', 'user': user, 'version': 1, 'variation': 0, 'value': '', 'default': None } es.summarize_event(event1) es.summarize_event(event2) es.summarize_event(event3) data = es.snapshot() assert data.start_date == 1000 assert data.end_date == 2000
def __init__(self, capacity): self._capacity = capacity self._events = [] self._summarizer = EventSummarizer() self._exceeded_capacity = False