def check_block(self, event_block): if self.event_blocks == []: return end_time = self.event_blocks[0].end_time if (end_time > event_block.start_time): self.alerts.append( ALERT_TIMEWARP.format(end_time=display_time(end_time), start_time=display_time( event_block.start_time))) self.event_blocks = []
def test_timewarp(self): watcher = TrafficWatcher(DEFAULT_WINDOW, DEFAULT_RATE) count = DEFAULT_RATE start_time = DEFAULT_START_TIME end_time = start_time + TIME_PER_BLOCK watcher.update( TrafficEventBlock(count=count, start_time=start_time, end_time=end_time)) alert = ALERT_TIMEWARP.format(start_time=display_time(start_time), end_time=display_time(end_time)) # Note: start_time has not changed assert alert in watcher.update( TrafficEventBlock(count=count, start_time=start_time, end_time=end_time))
def summarize(self, block): report = [] report_time = time() report.append("\nDate: {}".format(display_time(report_time))) block_summary = BlockSummary(block) report += block_summary.summarize() report += self.update_whole(report_time, block_summary) report += self.summarize_whole() return report
def test_alert_recovery(self): watcher = TrafficWatcher(DEFAULT_WINDOW, DEFAULT_RATE) count = DEFAULT_WINDOW * DEFAULT_RATE + 1 start_time = DEFAULT_START_TIME end_time = start_time + TIME_PER_BLOCK alert = ALERT_HIGH_TRAFFIC.format(count=count, time=display_time(end_time)) assert alert in watcher.update( TrafficEventBlock(count=count, start_time=start_time, end_time=end_time)) for i in range(int(DEFAULT_WINDOW / DEFAULT_RATE) + 1): start_time = end_time end_time = end_time + TIME_PER_BLOCK result = watcher.update( TrafficEventBlock(count=0, start_time=start_time, end_time=end_time)) alert = RECOVER_HIGH_TRAFFIC.format(time=display_time(end_time)) assert alert in result
def test_alert_triggered(self): watcher = TrafficWatcher(DEFAULT_WINDOW, DEFAULT_RATE) count = DEFAULT_WINDOW * DEFAULT_RATE + 1 start_time = DEFAULT_START_TIME end_time = start_time + TIME_PER_BLOCK alert = ALERT_HIGH_TRAFFIC.format(count=count, time=display_time(end_time)) assert alert in watcher.update( TrafficEventBlock(count=count, start_time=start_time, end_time=end_time))
def test_display_time(self): assert "1970" in display_time(0)
def cancel_alert(self): if self.alert_mode: self.alert_mode = False self.alerts.append( RECOVER_HIGH_TRAFFIC.format( time=display_time(self.last_end_time())))
def alert_triggered(self): self.alert_mode = True self.alerts.append( ALERT_HIGH_TRAFFIC.format(count=self.event_count(), time=display_time(self.last_end_time())))