Made an outage into a datastructure.

* Code-correctness has not been veriefied yet.
This commit is contained in:
Kristian Krsnik 2023-10-05 09:32:15 +02:00
parent c317b7a137
commit 3cebcf2ea4
Signed by: Kristian
GPG Key ID: FD1330AC9F909E85

View File

@ -17,6 +17,14 @@ class LogEntry:
return f"[{self.date}][{self.host}][{'OK' if not self.failed else 'FAIL'}]"
class Outage:
'''Class describing an outage.'''
def __init__(self, start: LogEntry, end: LogEntry):
self.start = start
self.end = end
def printOutages(filepath: str, time: int) -> None:
'''
Print outages from a log file
@ -46,28 +54,26 @@ def printOutages(filepath: str, time: int) -> None:
# We consider the time of subsequent failed connection attempts to the same host an outage.
outages: list = []
last_failed = False
outage_start = None
for entries in log_by_host.values():
for entry in entries:
# Continue an outage.
if entry.failed and last_failed:
outages[-1].append(entry)
# Start a new outage.
elif entry.failed and not last_failed:
outages.append([entry])
if entry.failed and not last_failed:
outage_start = entry
last_failed = True
# End an outage.
elif not entry.failed and last_failed:
outages.append(Outage(outage_start, entry))
last_failed = False
# Print the outages.
for outage_start, outage_end in [(outage[0], outage[-1]) for outage in outages]:
for outage in outages:
# Get duration of outage in hours and minutes.
outage_duration = round(
(outage_end.date - outage_start.date).seconds / 60
(outage.end.date - outage.start.date).seconds / 60
)
# Skip printing if outage is shorter than the minimum duration.
@ -77,10 +83,10 @@ def printOutages(filepath: str, time: int) -> None:
# Get hours and minutes for printing.
hours = outage_duration // 60
minutes = outage_duration - (hours * 60)
host = outage_start.host
host = outage.start.host
# Outputs outages in the form of "Outage at: 2023-19-01 06:29:01 lasting for 2 Hours and 39 Minutes".
print(f"[{outage_start.date}][{host}] lasting for {'{} Hours and '.format(hours) if hours >= 1 else ''}{minutes} Minutes")
print(f"[{outage.start.date}][{host}] lasting for {'{} Hours and '.format(hours) if hours >= 1 else ''}{minutes} Minutes")
def isOnline(host: str, timeout: int) -> bool: