Exemplo n.º 1
0
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
Exemplo n.º 2
0
 def make_program_session(self,
                          end: Optional[datetime.datetime] = None,
                          start: Optional[datetime.datetime] = None) -> int:
     return ProgramSession.insert(
         self.db,
         end=end,
         hostname="testhost",
         pids="",
         program=self.program_id,
         start=start or datetime.datetime.now(),
         username="******",
     )
Exemplo n.º 3
0
	def test_week_split(self):
		"Can we capture time spent this week outside this month?"
		# program session for this week, but not this month
		ProgramSession.insert(
			self.db,
			end = datetime.datetime(2020, 2, 1, 10, 0, 0),
			hostname = "testhost",
			pids = "",
			program = self.programs[0],
			start = datetime.datetime(2020, 2, 1, 9, 10, 0),
			username = "******",
		)
		# program session last month, last week
		ProgramSession.insert(
			self.db,
			end = datetime.datetime(2020, 1, 25, 10, 0, 0),
			hostname = "testhost",
			pids = "",
			program = self.programs[1],
			start = datetime.datetime(2020, 1, 25, 9, 0, 0),
			username = "******",
		)
		# program session earlier today
		ProgramSession.insert(
			self.db,
			end = datetime.datetime(2020, 2, 2, 10, 0, 0),
			hostname = "testhost",
			pids = "",
			program = self.programs[1],
			start = datetime.datetime(2020, 2, 2, 9, 0, 0),
			username = "******",
		)
		with freezegun.freeze_time("2020-02-02 11:00:00"):
			result = enforcement.get_program_sessions_current(self.db)
		self.assertEqual(len(result), 2)