Esempio n. 1
0
def logtime_split(block, seconds):
    if not block:
        return []
    result = []
    event = TrafficEventBlock(0, epoch_time(block[0]["datetime"]))
    for entry in block:
        new_time = epoch_time(entry["datetime"])
        if not event.add_if_in_range(new_time, seconds):
            result.append(event)
            event = TrafficEventBlock(0, new_time)
    result.append(event)
    return result
Esempio n. 2
0
 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))
Esempio n. 3
0
 def test_one_update(self):
     watcher = TrafficWatcher(DEFAULT_WINDOW, DEFAULT_RATE)
     count = DEFAULT_RATE
     start_time = DEFAULT_START_TIME
     end_time = start_time + TIME_PER_BLOCK
     assert watcher.update(
         TrafficEventBlock(count=count,
                           start_time=start_time,
                           end_time=end_time)) == []
Esempio n. 4
0
 def update_real_time_watcher(self, report_time, block_summary):
     start_time = report_time - self.sleep_interval
     alerts = self.real_time_watcher.update(
         TrafficEventBlock(start_time=start_time,
                           end_time=report_time,
                           count=block_summary.traffic()))
     if not alerts:
         return []
     return alert_report("Real-time", alerts)
Esempio n. 5
0
 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
Esempio n. 6
0
 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))
Esempio n. 7
0
 def test_bad_traffic_event_block(self):
     with pytest.raises(ValueError):
         TrafficEventBlock(count=10,
                           start_time=END_OF_TIME,
                           end_time=BEGINNING_OF_TIME)
Esempio n. 8
0
 def test_good_traffic_event_block(self):
     assert TrafficEventBlock(count=10,
                              start_time=BEGINNING_OF_TIME,
                              end_time=END_OF_TIME).end_time == END_OF_TIME