def setUp(self): super().setUp() self.programs = [ Program.insert( self.db, name="Minecraft", ), Program.insert( self.db, name="Terraria", ), ] self.processes = [ ProgramProcess.insert( self.db, name="net.minecraft.client.main.Main", program=self.programs[0], ), ProgramProcess.insert( self.db, name="minecraft-launcher", program=self.programs[0], ), ProgramProcess.insert( self.db, name="terraria.exe", program=self.programs[1], ), ]
def setUp(self): super().setUp() self.group_id = test_utilities.make_group(self.db) self.programs = [ Program.insert(self.db, name="Minecraft", program_group=self.group_id), Program.insert(self.db, name="Terraria", program_group=self.group_id), ]
def program_session_create_or_add(connection: Connection, hostname: str, username: str, elapsed_seconds: int, program_name: str, pids: Iterable[int]) -> int: "Either create a new program session or update an existing one." program = Program.search(connection, name=program_name) program_session = ProgramSession.search(connection, program=program.id, hostname=hostname, username=username, end=None) if program_session is None: program_session_id = ProgramSession.insert( connection, end=None, hostname=hostname, pids=",".join(sorted(pids)), program=program.id, start=datetime.datetime.now(), username=username, ) LOGGER.debug("Created new program session %s", program_session_id) else: program_session_id = program_session.id ProgramSession.update( connection, program_session_id, pids=",".join(sorted(pids)), ) LOGGER.debug("Updated program session %d to have pids %s", program_session_id, sorted(pids)) return program_session_id
def list_program_by_process(connection: Connection) -> Mapping[str, str]: "Get the mapping of processes to their program names." programs = Program.list(connection) program_by_id = {program.id: program.name for program in programs} processes = ProgramProcess.list(connection) return { process.name: program_by_id[process.program] for process in processes }
def user_to_usage(connection: Connection, program_group: ProgramGroup) -> Mapping[str, Status]: "Get a mapping of usernames to their current status." programs = list(Program.list(connection)) return { username: _user_to_status_for_program_group(connection, username, program_group, programs) for username in usernames(connection) }
def setUp(self): super().setUp() self.group_id = test_utilities.make_group(self.db) self.program_id = Program.insert(self.db, name="Minecraft", program_group=self.group_id) ProgramProcess.insert(self.db, name="net.minecraft.client.main.Main", program=self.program_id)
def user_to_status( connection: Connection) -> Mapping[str, Mapping[str, Status]]: """Get a mapping of usernames to their current status. The results map from a username to another mapping. That inner mapping maps from a group name to the minutes left. """ results = {} program_groups = list(ProgramGroup.list(connection)) programs = list(Program.list(connection)) for username in usernames(connection): results[username] = _user_to_status_for_program_groups( connection, username, program_groups, programs) return results
def actions_for_username_kills(connection: Connection, hostname: str, username: str) -> Iterable[Action]: program_groups = list(ProgramGroup.list(connection)) programs = list(Program.list(connection)) statuses = _user_to_status_for_program_groups(connection, username, program_groups, programs) pids = set() for status in statuses.values(): if status.minutes_remaining_today < 0: pids.update(status.pids) LOGGER.info("Killing pids %s", pids) return [Action( content=pid, type="kill", ) for pid in pids]
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)