def GetSortedEvents(self, time_range=None): """Retrieves the events in increasing chronological order. Args: time_range (Optional[TimeRange]): time range used to filter events that fall in a specific period. Returns: generator(EventObject): event generator. Raises: IOError: when the storage writer is closed. OSError: when the storage writer is closed. """ if not self._is_open: raise IOError('Unable to read from closed storage writer.') generator = self.GetAttributeContainers(self._CONTAINER_TYPE_EVENT) sorted_events = event_heap.EventHeap() for event_index, event in enumerate(generator): if (time_range and (event.timestamp < time_range.start_timestamp or event.timestamp > time_range.end_timestamp)): continue # The event index is used to ensure to sort events with the same date and # time and description in the order they were added to the store. sorted_events.PushEvent(event, event_index) return iter(sorted_events.PopEvents())
def testPushEvent(self): """Tests the PushEvent function.""" test_heap = event_heap.EventHeap() self.assertEqual(len(test_heap._heap), 0) event, _, _ = containers_test_lib.CreateEventFromValues( self._TEST_EVENTS[0]) test_heap.PushEvent(event, 0) self.assertEqual(len(test_heap._heap), 1)
def testPopEvents(self): """Tests the PopEvents function.""" test_heap = event_heap.EventHeap() self.assertEqual(len(test_heap._heap), 0) test_events = list(test_heap.PopEvents()) self.assertEqual(len(test_events), 0) event_index = 0 for event, _, _ in containers_test_lib.CreateEventsFromValues( self._TEST_EVENTS): test_heap.PushEvent(event, event_index) event_index += 1 self.assertEqual(len(test_heap._heap), 2) test_events = list(test_heap.PopEvents()) self.assertEqual(len(test_events), 2) self.assertEqual(len(test_heap._heap), 0)
def testNumberOfEvents(self): """Tests the number_of_events property.""" test_heap = event_heap.EventHeap() self.assertEqual(test_heap.number_of_events, 0)