async def test_push_all_new_events(reader): event1 = Event(_type="nominate", time=timestamp.from_string("2020-01-01 01:00:00")) event2 = Event(_type="news", time=timestamp.from_string("2020-01-01 02:00:00")) event3 = Event(_type="nominate", time=timestamp.from_string("2020-01-01 03:00:00")) event4 = Event(_type="qualify", time=timestamp.from_string("2020-01-01 04:00:00")) event5 = Event(_type="news", time=timestamp.from_string("2020-01-01 05:00:00")) reader.database.insert_event(event1) reader.database.insert_event(event2) reader.database.insert_event(event3) reader.database.insert_event(event4) reader.database.insert_event(event5) mapset_scope = Scope("mapset", None) news_scope = Scope("news", None) timestamp.set_last( new_datetime=timestamp.from_string("2020-01-01 00:00:00"), _id=reader._Reader__time_id(mapset_scope)) timestamp.set_last( new_datetime=timestamp.from_string("2020-01-01 00:00:00"), _id=reader._Reader__time_id(news_scope)) await reader._Reader__push_all_new_events() assert received_events == [event1, event3, event4, event2, event5] assert timestamp.get_last(reader._Reader__time_id( mapset_scope)) == timestamp.from_string("2020-01-01 04:00:00") assert timestamp.get_last(reader._Reader__time_id( news_scope)) == timestamp.from_string("2020-01-01 05:00:00")
async def gather(async_event_generator, _id: str) -> None: """Iterates over new events since the last time, inserts them into the database, and then updates the last time if any were found.""" current_time = datetime.utcnow().replace(microsecond=0) last_time = timestamp.get_last(_id).replace(microsecond=0) last_checked_time = timestamp.get_last(_id + LAST_CHECKED_POSTFIX).replace(microsecond=0) if await push_events(async_event_generator, current_time, last_time, last_checked_time): last_updated(current_time, _id) last_updated(current_time, _id + LAST_CHECKED_POSTFIX)
async def gather_loop() -> None: """Gathers new events in an infinite loop.""" while (True): await gather_new_events() # We only need to check newsposts between exact hours, as this is when they're posted. if timestamp.get_last(_id="news").hour != datetime.utcnow().hour: await gather_news() # Group changes happen very rarely compared to other events, but people tend to want these updates quickly. if (datetime.utcnow() - timestamp.get_last(_id="groups")).total_seconds() > 300: await gather_group_changes()
async def gather_loop() -> None: """Gathers new events in an infinite loop.""" while(True): await gather_new_events() # We only need to check newsposts between exact hours, as this is when they're posted. if timestamp.get_last("news" + LAST_CHECKED_POSTFIX).hour != datetime.utcnow().hour: await gather_news() # Group changes happen very rarely compared to other events, but people tend to want these updates quickly. if (datetime.utcnow() - timestamp.get_last("groups" + LAST_CHECKED_POSTFIX)).total_seconds() > 300: await gather_group_changes() # SEV changes happen more commonly, but people can wait for these updates more than for other events. if (datetime.utcnow() - timestamp.get_last("sev" + LAST_CHECKED_POSTFIX)).total_seconds() > 300: await gather_sev_changes()
async def test_on_event_scope(reader): event1 = Event(_type="hello", time=timestamp.from_string("2020-01-01 05:00:00")) event2 = Event(_type="there", time=timestamp.from_string("2020-01-01 07:00:00")) reader.database.insert_event(event1) reader.database.insert_event(event2) _from = timestamp.from_string("2020-01-01 00:00:00") to = timestamp.from_string("2020-01-01 10:00:00") scope = Scope("greet", sql_target="type=\"hello\"") await reader._Reader__push_events_between(_from, to, scope) assert received_events == [event1] assert timestamp.get_last(reader._Reader__time_id(scope)) == timestamp.from_string("2020-01-01 05:00:00") assert reader.latest_event_time == timestamp.from_string("2020-01-01 05:00:00")
def test_get_missing_created(): time_id = "test_missing" expected_path = timestamp.get_path(time_id) with suppress(OSError): os.remove(expected_path) assert not timestamp.exists(time_id) assert not os.path.exists(expected_path) time_empty = timestamp.get_last(time_id) time_delta = datetime.utcnow() - time_empty assert timestamp.exists(time_id) assert os.path.exists(expected_path) os.remove(expected_path) assert not timestamp.exists(time_id) assert time_delta.total_seconds() < 1
async def __push_new_events(self, scope: Scope) -> None: """Triggers the on_event method for each new event since the last stored datetime for the given scope.""" last_time = timestamp.get_last(self.__time_id(scope)) await self.__push_events_between(last_time, datetime.utcnow(), scope)
def test_get_set(): new_time = datetime.utcnow() assert timestamp.get_last("test") < new_time timestamp.set_last(new_time, "test") assert abs((new_time - timestamp.get_last("test")).total_seconds()) < 1