def get_program_sessions_current( connection: Connection) -> Iterable[ProgramSession]: """Get the program sessions that are interesting to enforcement. This includes all sessions, opened and closed, since the start of the earlier of the start of the week or month as well as any open sessions. """ now = datetime.datetime.now() program_sessions_open = list(ProgramSession.list(connection, end=None)) month_start = datetime.datetime( year=now.year, month=now.month, day=1, hour=0, minute=0, second=0, ) week_start = now - datetime.timedelta(days=now.isoweekday() - 1) week_start = datetime.datetime( year=week_start.year, month=week_start.month, day=week_start.day, hour=0, minute=0, second=0, ) earliest = min(month_start, week_start) program_sessions_past = list( queries.program_session_list_since(connection, earliest)) return program_sessions_past + program_sessions_open
def program_session_close_except( connection: Connection, hostname: str, exempt_program_names: Iterable[str], ) -> None: "Close all program_sessions, except any for the named programs." now = datetime.datetime.now() programs = Program.list(connection) program_id_to_name = {program.id: program.name for program in programs} open_sessions = ProgramSession.list(connection, hostname=hostname, end=None) for program_session in open_sessions: program_name = program_id_to_name[program_session.program] if program_name in exempt_program_names: continue ProgramSession.update( connection, program_session.id, end=now, ) LOGGER.info("Ended program session %s", program_session.id)