def test_process_statistics_info_add_process_without_end_time(self): start_time = datetime.datetime.utcnow() #end_time = start_time + datetime.timedelta(seconds=60) psi = process_statistics.ProcessStatisticsInfo( p_username=USERNAME, p_reference_time=start_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) self.assertEqual(psi.active_processes, 0) pi = process_info.ProcessInfo(p_hostname=HOSTNAME, p_username=USERNAME, p_processhandler=None, p_processname=PROCESS_NAME, p_pid=PID, p_start_time=start_time, p_end_time=None) psi.add_process_start(p_process_info=pi, p_start_time=pi.start_time) self.assertEqual(psi.active_processes, 1) psi.add_process_end(p_process_info=pi, p_end_time=pi.end_time) self.assertEqual(psi.active_processes, 0)
def test_constructor_statistics_info(self): reference_time = datetime.datetime.utcnow() psi = process_statistics.ProcessStatisticsInfo( p_username=USERNAME, p_reference_time=reference_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) self.assertEqual(psi.username, USERNAME) self.assertEqual(psi.reference_time, reference_time) self.assertEqual(psi.min_activity_duration, MIN_ACTIVITY_DURATION) self.assertEqual(psi.max_lookback_in_days, MAX_LOOKBACK_IN_DAYS) self.assertEqual(psi.active_processes, 0) self.assertIsNone(psi.last_inactivity_start_time) self.assertIsNone(psi.current_activity) self.assertIsNone(psi.previous_activity) self.assertEqual(psi.accumulated_break_time, 0) self.assertIsNotNone(psi.day_statistics) self.assertEqual(len(psi.day_statistics), MAX_LOOKBACK_IN_DAYS + 1) self.assertIsInstance(psi.day_statistics[0], process_statistics.DayStatistics) self.assertIsNotNone(psi.currently_active_host_processes) self.assertEqual(len(psi.currently_active_host_processes), 0)
def test_process_statistics_info_str(self): start_time = datetime.datetime.utcnow() psi = process_statistics.ProcessStatisticsInfo( p_username=USERNAME, p_reference_time=start_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) self.assertIsNotNone(str(psi))
def test_max_session_duration_with_downtime(self): dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence( self._logger) a_rule_handler = self.create_dummy_rule_handler( p_persistence=dummy_persistence, p_create_complex_handlers=False) reference_time = datetime.datetime.utcnow() rule_set = TestRuleHandler.create_dummy_ruleset_config() activity_start = reference_time + datetime.timedelta(seconds=-1200) stat_info = process_statistics.ProcessStatisticsInfo( p_username=USERNAME, p_reference_time=reference_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) a_process_info = process_info.ProcessInfo(p_hostname=HOSTNAME, p_username=USERNAME, p_pid=PID, p_start_time=activity_start, p_downtime=300) stat_info.add_process_start(p_process_info=a_process_info, p_start_time=activity_start) # Check that playing is allowed up to the maximum session time rule_set.max_activity_duration = 899 rule_result_info = rule_handler.RuleResultInfo() a_rule_handler.check_activity_duration( p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual( rule_result_info.applying_rules & rule_handler.RULE_ACTIVITY_DURATION, rule_handler.RULE_ACTIVITY_DURATION) rule_set.max_activity_duration = 901 # Check that playing is allowed after the maximum session time rule_result_info = rule_handler.RuleResultInfo() a_rule_handler.check_activity_duration( p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual( rule_result_info.applying_rules & rule_handler.RULE_ACTIVITY_DURATION, 0)
def test_max_time_per_day_with_downtime_and_previous_activity(self): dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence(self._logger) a_rule_handler = self.create_dummy_rule_handler(p_persistence=dummy_persistence, p_create_complex_handlers=False) reference_time = datetime.datetime.utcnow() rule_set = TestRuleHandler.create_dummy_ruleset_config() activity_start = reference_time + datetime.timedelta(seconds=-2000) activity_end = reference_time + datetime.timedelta(seconds=-1700) stat_info = process_statistics.ProcessStatisticsInfo(p_username=USERNAME, p_reference_time=reference_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) a_process_info = process_info.ProcessInfo(p_hostname=HOSTNAME, p_username=USERNAME, p_pid=PID, p_start_time=activity_start, p_downtime=50) stat_info.add_process_start(p_process_info=a_process_info, p_start_time=activity_start) a_process_info.end_time = activity_end stat_info.add_process_end(p_process_info=a_process_info, p_end_time=activity_end) activity_start = reference_time + datetime.timedelta(seconds=-500) a_process_info = process_info.ProcessInfo(p_hostname=HOSTNAME, p_username=USERNAME, p_pid=PID, p_start_time=activity_start, p_downtime=100) stat_info.add_process_start(p_process_info=a_process_info, p_start_time=activity_start) # Check that playing is allowed up to the maximum session time rule_set.max_time_per_day = 649 rule_result_info = rule_handler.RuleResultInfo() a_rule_handler.check_time_per_day(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_TIME_PER_DAY, rule_handler.RULE_TIME_PER_DAY) # TODO: test error on CircleCI rule_set.max_time_per_day = 651 # Check that playing is allowed after the maximum session time rule_result_info = rule_handler.RuleResultInfo() a_rule_handler.check_time_per_day(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_TIME_PER_DAY, 0)
def test_max_time_per_day(self): dummy_persistence = test_persistence.TestPersistence.create_dummy_persistence( self._logger) a_rule_handler = self.create_dummy_rule_handler( p_persistence=dummy_persistence, p_create_complex_handlers=False) reference_time = datetime.datetime.utcnow() rule_set = TestRuleHandler.create_dummy_ruleset_config() activity_start = reference_time + datetime.timedelta(seconds=-1200) activity = process_statistics.Activity(p_start_time=activity_start) stat_info = process_statistics.ProcessStatisticsInfo( p_username=USERNAME, p_reference_time=reference_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) stat_info.current_activity = activity # Check that playing is allowed up to the maximum session time rule_set.max_time_per_day = 1199 rule_result_info = rule_handler.RuleResultInfo() a_rule_handler.check_time_per_day(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual( rule_result_info.applying_rules & rule_handler.RULE_TIME_PER_DAY, rule_handler.RULE_TIME_PER_DAY) rule_set.max_time_per_day = 1201 # Check that playing is allowed after the maximum session time rule_result_info = rule_handler.RuleResultInfo() a_rule_handler.check_time_per_day(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual( rule_result_info.applying_rules & rule_handler.RULE_TIME_PER_DAY, 0)
def test_min_break_time(self): reference_time = datetime.datetime.utcnow() rule_set = TestRuleHandler.create_dummy_ruleset_config() previous_activity_start = reference_time + datetime.timedelta(seconds=-1200) previous_activity_end = previous_activity_start + datetime.timedelta(seconds=600) previous_activity = process_statistics.Activity(p_start_time=previous_activity_start) previous_activity.set_end_time(previous_activity_end) previous_short_activity_end = previous_activity_start + datetime.timedelta(seconds=200) previous_short_activity = process_statistics.Activity(p_start_time=previous_activity_start) previous_short_activity.set_end_time(previous_short_activity_end) rule_set.max_activity_duration = 600 stat_info = process_statistics.ProcessStatisticsInfo(p_username=USERNAME, p_reference_time=reference_time, p_min_activity_duration=MIN_ACTIVITY_DURATION, p_max_lookback_in_days=MAX_LOOKBACK_IN_DAYS) stat_info.previous_activity = previous_activity # Check that playing is not allowed after full break minus one second rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-299) stat_info.last_inactivity_start_time = activity_start_time rule_set.min_break = 300 rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, rule_handler.RULE_MIN_BREAK) # Check that playing is allowed after full break minus one second but with free_play activated rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-299) stat_info.last_inactivity_start_time = activity_start_time rule_set.min_break = 300 rule_set.free_play = True rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, 0) # Check that playing is allowed after full break rule_set.free_play = False rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-300) stat_info.last_inactivity_start_time = activity_start_time rule_set.min_break = 300 rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, 0) # Check that playing is not allowed after break after short period (1/3 of max) minus one second stat_info.previous_activity = previous_short_activity rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-99) stat_info.last_inactivity_start_time = activity_start_time rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, rule_handler.RULE_MIN_BREAK) self.assertEqual(rule_result_info.args['break_minutes_left'], 0) # Check that playing is not allowed after break after short period (1/3 of max) minus one second # but with max_activity_duration=0 stat_info.previous_activity = previous_short_activity rule_result_info = rule_handler.RuleResultInfo() rule_set.max_activity_duration = 0 activity_start_time = reference_time + datetime.timedelta(seconds=-299) stat_info.last_inactivity_start_time = activity_start_time rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, rule_handler.RULE_MIN_BREAK) # Check that playing is allowed after break after short period (1/3 of max) # but with max_activity_duration=0 stat_info.previous_activity = previous_short_activity rule_result_info = rule_handler.RuleResultInfo() rule_set.max_activity_duration = 0 activity_start_time = reference_time + datetime.timedelta(seconds=-300) stat_info.last_inactivity_start_time = activity_start_time rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, 0) # Check that playing is allowed after break after short period (1/3 of max) stat_info.previous_activity = previous_short_activity rule_set.max_activity_duration = 600 rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-100) stat_info.last_inactivity_start_time = activity_start_time rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, 0) # Check that playing is not allowed after break after short period (1/3 of max) minus 31 second and # that the number of break minutes left = 1 stat_info.previous_activity = previous_short_activity rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-69) stat_info.last_inactivity_start_time = activity_start_time rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, rule_handler.RULE_MIN_BREAK) self.assertEqual(rule_result_info.args['break_minutes_left'], 1) # Check that playing is not allowed after break after short period (1/3 of max) minus 91 second and # that the number of break minutes left = 2 stat_info.previous_activity = previous_short_activity rule_result_info = rule_handler.RuleResultInfo() activity_start_time = reference_time + datetime.timedelta(seconds=-9) stat_info.last_inactivity_start_time = activity_start_time rule_handler.RuleHandler.check_min_break(p_rule_set=rule_set, p_stat_info=stat_info, p_rule_result_info=rule_result_info) self.assertEqual(rule_result_info.applying_rules & rule_handler.RULE_MIN_BREAK, rule_handler.RULE_MIN_BREAK) self.assertEqual(rule_result_info.args['break_minutes_left'], 2)