def check_surrounding_events(self, threshold_ids): """Checks that the events surrounding the first event in a new session are allocated correctly. Args: threshold_ids: a list of IDs of the first events in the sessions. """ ds = MockDataStore('test', 0) session_no = 1 last_id = threshold_ids[-1] for threshold_id in threshold_ids: if threshold_id != 0: # check previous event is in the previous session event = (ds.get_event('test_index', str(threshold_id - 1), stored_events=True)) self.assertEqual(event['_source']['session_number'], session_no) if threshold_id != last_id: # check next event is in the same session (as the event with # threshold id) session_no += 1 event = (ds.get_event('test_index', str(threshold_id + 1), stored_events=True)) self.assertEqual(event['_source']['session_number'], session_no)
def test_edge_time_diff(self): """Test events with the edge time difference between them are allocated correctly.""" with mock.patch.object( self.analyzer_class, 'event_stream', return_value=_create_mock_event( 0, 2, time_diffs=[self.analyzer_class.max_time_diff_micros])): index = 'test_index' sketch_id = 1 analyzer = self.analyzer_class(index, sketch_id) message = analyzer.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 1') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_id'], {analyzer.session_type: 1}) event2 = (ds.get_event('test_index', '101', stored_events=True)) self.assertEqual(event2['_source']['session_id'], {analyzer.session_type: 1})
def test_diff_session(self): """Test multiple events in different sessions are allocated correctly.""" with mock.patch.object( self.analyzer_class, 'event_stream', return_value=_create_mock_event( 0, 2, time_diffs=[ self.analyzer_class.max_time_diff_micros + (self.analyzer_class.max_time_diff_micros / 2) ])): index = 'test_index' sketch_id = 1 analyzer = self.analyzer_class(index, sketch_id) message = analyzer.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 2') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_id'], {analyzer.session_type: 1}) event2 = (ds.get_event('test_index', '101', stored_events=True)) self.assertEqual(event2['_source']['session_id'], {analyzer.session_type: 2}) self._check_surrounding_events([101], analyzer.session_type)
def test_same_session(self): """Test multiple events in the same session are allocated correctly.""" with mock.patch.object( self.analyzer_class, 'event_stream', return_value=_create_mock_event( 0, 2, time_diffs=[self.analyzer_class.max_time_diff_micros / 2 ])): index = 'test_index' sketch_id = 1 analyzer = self.analyzer_class(index, sketch_id) message = analyzer.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 1') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_id'], {analyzer.session_type: 1}) # checking event with id '101' as 100 events have been inserted # as 'padding' (see _create_mock_event()) event2 = (ds.get_event('test_index', '101', stored_events=True)) self.assertEqual(event2['_source']['session_id'], {analyzer.session_type: 1})
def test_zero_time_diff(self): """Test events with no time difference between them are allocated correctly.""" with mock.patch.object(SessionizerSketchPlugin, 'event_stream', return_value=_create_mock_event(0, 2, time_diffs=[0 ])): index = 'test_index' sketch_id = 1 analyser = SessionizerSketchPlugin(index, sketch_id) message = analyser.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 1') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_id'], {'all_events': 1}) event1 = (ds.get_event('test_index', '100', stored_events=True)) self.assertEqual(event1['_source']['session_id'], {'all_events': 1}) event2 = (ds.get_event('test_index', '101', stored_events=True)) self.assertEqual(event2['_source']['session_id'], {'all_events': 1})
def test_multiple_sessions(self): """Test multiple events, two of which are in the same session and one in a different session.""" with mock.patch.object(SessionizerSketchPlugin, 'event_stream', return_value=_create_mock_event( 0, 3, time_diffs=[3000, 400000000])): index = 'test_index' sketch_id = 1 analyser = SessionizerSketchPlugin(index, sketch_id) message = analyser.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 2') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_id'], {'all_events': 1}) event2 = (ds.get_event('test_index', '101', stored_events=True)) self.assertEqual(event2['_source']['session_id'], {'all_events': 1}) event3 = (ds.get_event('test_index', '202', stored_events=True)) self.assertEqual(event3['_source']['session_id'], {'all_events': 2}) self._check_surrounding_events([202], 'all_events')
def _create_mock_event(event_id, quantity, time_diffs=None, source_attrs=None): """ Returns an instance of Event, based on the MockDataStore event_dict example. Args: event_id: Desired ID for the Event. quantity: The number of Events to be generated. time_diffs: A list of time differences between the generated Events. source_attrs: Dictionary of attributes to add to the source of the generated events. Returns: A generator of Event objects. """ if not time_diffs: time_diffs = [0] if quantity < 0: quantity = abs(quantity) # If the list of time differences is too small to be compatible # with the quantity of events, then extend the list with the last # value for as many items as necessary. if quantity - len(time_diffs) > 0: time_diffs.extend([time_diffs[len(time_diffs) - 1]] * (quantity - len(time_diffs))) # Setup for Event object initialisation ds = MockDataStore('test', 0) user = User('test_user') sketch = Sketch('test_sketch', 'description', user) label = sketch.Label(label='Test label', user=user) sketch.labels.append(label) event_timestamp = 1410895419859714 event_template = ds.get_event('test', 'test') for i in range(quantity): eventObj = _create_eventObj(ds, sketch, event_template, event_id, event_timestamp, source_attrs) yield eventObj # adding extra events after every requested event for better # simulation of real timeline data i.e. working with a larger # dataset for _ in range(100): event_timestamp += 1 event_id += 1 eventObj = _create_eventObj(ds, sketch, event_template, event_id, event_timestamp, source_attrs) yield eventObj event_timestamp += abs(time_diffs[i]) event_id += 1
def test_diff_session(self): """Test multiple events in different sessions are allocated correctly.""" with mock.patch.object(SessionizerSketchPlugin, 'event_stream', return_value=_create_mock_event( 0, 2, time_diffs=[400000000])): index = 'test_index' sketch_id = 1 analyser = sessionizer.SessionizerSketchPlugin(index, sketch_id) message = analyser.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 2') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_number'], 1) event2 = (ds.get_event('test_index', '101', stored_events=True)) self.assertEqual(event2['_source']['session_number'], 2) self.check_surrounding_events([101])
def test_one_event(self): """Test the behaviour of the sessionizer when given one event.""" with mock.patch.object(SessionizerSketchPlugin, 'event_stream', return_value=_create_mock_event(0, 1)): index = 'test_index' sketch_id = 1 analyser = sessionizer.SessionizerSketchPlugin(index, sketch_id) message = analyser.run() self.assertEqual( message, 'Sessionizing completed, number of session created: 1') ds = MockDataStore('test', 0) event1 = (ds.get_event('test_index', '0', stored_events=True)) self.assertEqual(event1['_source']['session_number'], 1)