pase new snowflake log format

This commit is contained in:
Daniel Langbein 2022-09-29 15:09:39 +02:00
parent 24a4f9a375
commit fc4c2904e0

106
main.py
View File

@ -1,23 +1,29 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import annotations from __future__ import annotations
from typing import List
import re
from datetime import datetime from datetime import datetime
import sys import sys
import exec import exec
def main(): def test() -> None:
log = example_log()
parse_log(log)
def main() -> None:
if len(sys.argv) > 1: if len(sys.argv) > 1:
log = get_docker_log() log = get_docker_log()
else: else:
log = sys.stdin.read() log = sys.stdin.read()
parse_log(log)
filtered: List[str] = [line for line in log.splitlines()
if Throughput.PATTERN in line]
# filtered = filtered_example()
tps = [Throughput.from_str(line) for line in filtered] def parse_log(log: str) -> None:
tps = [Throughput.from_str(line) for line in log.splitlines()]
tps = [tp for tp in tps if tp]
if len(tps) > 0: if len(tps) > 0:
print(f'From {tps[0].dt} until {tps[-1].dt}:') print(f'From {tps[0].dt} until {tps[-1].dt}:')
@ -37,12 +43,15 @@ def main():
print(f'Total:\n{tps_total}') print(f'Total:\n{tps_total}')
def filtered_example() -> List[str]: def example_log() -> str:
return [ return '\n'.join(
'2022/04/04 15:08:10 Traffic throughput (up|down): 4 MB|259 KB -- (691 OnMessages, 3886 Sends, over 269 seconds)', [
'2022/04/04 16:00:06 Traffic throughput (up|down): 13 MB|15 MB -- (46326 OnMessages, 32325 Sends, over 36634 seconds)', '2022/09/27 02:02:26 In the last 1h0m0s, there were 1 connections. Traffic Relayed ↑ 708 KB, ↓ 328 KB.',
'2022/04/04 15:57:04 Traffic throughput (up|down): 61 KB|8 KB -- (69 OnMessages, 91 Sends, over 157 seconds)', '2022/09/28 02:02:26 In the last 1h0m0s, there were 0 connections. Traffic Relayed ↑ 0 B, ↓ 0 B.',
'2022/09/29 05:02:26 In the last 1h0m0s, there were 5 connections. Traffic Relayed ↑ 6 MB, ↓ 787 KB.',
'2022/09/29 11:02:26 In the last 1h0m0s, there were 26 connections. Traffic Relayed ↑ 16 MB, ↓ 10 MB.',
] ]
)
def get_docker_log() -> str: def get_docker_log() -> str:
@ -64,12 +73,10 @@ def docker_logs(container_name: str, ssh_host: str = None) -> str:
class Throughput: class Throughput:
FORMAT_EXAMPLE = '2022/04/06 10:37:42' # DATE_FORMAT_EXAMPLE = '2022/04/06 10:37:42'
FORMAT_STR = '%Y/%m/%d %H:%M:%S' DATE_FORMAT_STR = '%Y/%m/%d %H:%M:%S'
FORMAT_LENGTH = len(FORMAT_EXAMPLE)
PATTERN = ' Traffic throughput (up|down): '
# Units sorted from small to large.
_unit_dict = { _unit_dict = {
'B': 1, 'B': 1,
'KB': 10 ** 3, 'KB': 10 ** 3,
@ -79,40 +86,37 @@ class Throughput:
} }
@classmethod @classmethod
def from_str(cls, line: str) -> Throughput: def from_str(cls, line: str) -> Throughput | None:
dt_str = line[0:Throughput.FORMAT_LENGTH] pattern_str = r'(\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d)' \
dt = datetime.strptime(dt_str, Throughput.FORMAT_STR) r' In the last 1h0m0s, there were (\d+) connections\. ' \
r'Traffic Relayed ↑ (\d+ [A-Z]+), ↓ (\d+ [A-Z]+)\.'
pattern = re.compile(pattern_str)
match = pattern.match(line)
_, tail = line.split(Throughput.PATTERN) if not match:
up, tail = tail.split('|') print(f'No match for this line: {line}', file=sys.stderr)
down, tail = tail.split(' -- (') return None
on_messages, tail = tail.split(', ', maxsplit=1)
sends, tail = tail.split(', ')
seconds, tail = tail.split(')')
bytes_up = cls._split_to_bytes(up) dt = datetime.strptime(match.group(1), Throughput.DATE_FORMAT_STR)
bytes_down = cls._split_to_bytes(down) connections = int(match.group(2))
on_messages = int(on_messages.split(' ')[0]) bytes_up = cls._split_to_bytes(match.group(3))
sends = int(sends.split(' ')[0]) bytes_down = cls._split_to_bytes(match.group(4))
seconds = int(seconds.split(' ')[1])
return cls(dt, bytes_up, bytes_down, on_messages, sends, seconds) return cls(dt, bytes_up, bytes_down, connections)
@classmethod @classmethod
def from_args(cls, dt, bytes_up, bytes_down, on_messages, sends, seconds) -> Throughput: def from_args(cls, dt, bytes_up, bytes_down, connections) -> Throughput:
return cls(dt, bytes_up, bytes_down, on_messages, sends, seconds) return cls(dt, bytes_up, bytes_down, connections)
@classmethod @classmethod
def zero(cls) -> Throughput: def zero(cls) -> Throughput:
return cls(None, 0, 0, 0, 0, 0) return cls(None, 0, 0, 0)
def __init__(self, dt, bytes_up, bytes_down, on_messages, sends, seconds): def __init__(self, dt, bytes_up, bytes_down, connections):
self.dt = dt self.dt = dt
self.bytes_up = bytes_up self.bytes_up = bytes_up
self.bytes_down = bytes_down self.bytes_down = bytes_down
self.on_messages = on_messages self.connections = connections
self.sends = sends
self.seconds = seconds
def __add__(self, other): def __add__(self, other):
if not isinstance(other, Throughput): if not isinstance(other, Throughput):
@ -121,13 +125,13 @@ class Throughput:
self.dt, self.dt,
self.bytes_up + other.bytes_up, self.bytes_up + other.bytes_up,
self.bytes_down + other.bytes_down, self.bytes_down + other.bytes_down,
self.on_messages + other.on_messages, self.connections + other.connections,
self.sends + other.sends,
self.seconds + other.seconds,
) )
def __str__(self) -> str: def __str__(self) -> str:
return f'{Throughput._to_gb(self.bytes_up)} GB up, {Throughput._to_gb(self.bytes_down)} GB down ({self.on_messages} OnMessages, {self.sends} sends, {self.seconds} seconds)' up, up_unit = Throughput._to_unit_auto(self.bytes_up)
down, down_unit = Throughput._to_unit_auto(self.bytes_down)
return f'{up} {up_unit} up\t{down} {down_unit} down\t{self.connections} Connections'
@classmethod @classmethod
def _split_to_bytes(cls, num_unit: str) -> int: def _split_to_bytes(cls, num_unit: str) -> int:
@ -140,9 +144,23 @@ class Throughput:
return num * cls._unit_dict[unit] return num * cls._unit_dict[unit]
@classmethod @classmethod
def _to_gb(cls, num_bytes: int) -> int: def _to_unit(cls, num_bytes: int, unit: str = 'GB') -> int:
return round(num_bytes / cls._unit_dict['GB'], 1) return int(round(num_bytes / cls._unit_dict[unit], 1))
@classmethod
def _to_unit_auto(cls, num_bytes: int) -> (int, str):
converted, unit = -1, 'ERROR'
for unit, factor in cls._unit_dict.items():
converted = cls._to_unit(num_bytes, unit)
if converted < 9999:
return converted, unit
if unit == 'ERROR':
raise ValueError('Invalid state')
return converted, unit
if __name__ == '__main__': if __name__ == '__main__':
# test()
main() main()