From 6a39a70b9fbdd29a03412282ffa8af49c61201d1 Mon Sep 17 00:00:00 2001 From: Rovis27 Date: Fri, 19 Jul 2024 14:06:21 -0500 Subject: [PATCH] Adding warning analyzer (#50) * Adding warning analyzer * Fixing formatting * Fixing formatting frfr :x * Fixing formatting yet again because @lassour keeps messing it up. * Finally actually fixing it --------- Co-authored-by: kschmader --- src/alogamous/__main__.py | 10 +++++++++- src/alogamous/warning_analyzer.py | 20 ++++++++++++++++++++ tests/warning_analyzer_test.py | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/alogamous/warning_analyzer.py create mode 100644 tests/warning_analyzer_test.py diff --git a/src/alogamous/__main__.py b/src/alogamous/__main__.py index c7c177c..e1c3992 100644 --- a/src/alogamous/__main__.py +++ b/src/alogamous/__main__.py @@ -1,4 +1,11 @@ -from alogamous import analyzer, directory_reader, echo_analyzer, error_counter_analyzer, line_count_analyzer +from alogamous import ( + analyzer, + directory_reader, + echo_analyzer, + error_counter_analyzer, + line_count_analyzer, + warning_analyzer, +) with open("../../data/test_output_file.txt", "a") as output_file: reader = directory_reader.DirectoryReader("../../data") @@ -7,6 +14,7 @@ echo_analyzer.EchoAnalyzer(), error_counter_analyzer.ErrorCounterAnalyzer(), line_count_analyzer.LineCountAnalyzer(), + warning_analyzer.WarningAnalyzer(), ], reader.read(), output_file, diff --git a/src/alogamous/warning_analyzer.py b/src/alogamous/warning_analyzer.py new file mode 100644 index 0000000..feb7b65 --- /dev/null +++ b/src/alogamous/warning_analyzer.py @@ -0,0 +1,20 @@ +from alogamous import analyzer + + +class WarningAnalyzer(analyzer.Analyzer): + def __init__(self): + self.count = 0 + + def read_log_line(self, line): + line_list = line.split(" ") + # List should have format [date, timestamp, -, root, -, log message type, -, first word of message...] + if line_list[5].lower() == "warning": + self.count += 1 + + def report(self, out_stream): + if self.count == 0: + out_stream.write("\n" + "No Warnings were detected.") + elif self.count == 1: + out_stream.write("\n" + "1 Warning was detected.") + else: + out_stream.write("\n" + str(self.count) + " Warnings were detected.") diff --git a/tests/warning_analyzer_test.py b/tests/warning_analyzer_test.py new file mode 100644 index 0000000..27898ad --- /dev/null +++ b/tests/warning_analyzer_test.py @@ -0,0 +1,17 @@ +import io + +from alogamous import analyzer, warning_analyzer + + +def test_warning_count(): + in_stream = io.StringIO("""2024-06-20 11:00:18,185 - root - INFO - Kafka reading from start of day 2024-06-20 05:00:00+00:00 on topic internal from kafka.servers:9092 +2024-06-20 11:00:19,328 - root - INFO - Kafka source starting for topic internal at current offset 7924032 end offset 7928950 on servers kafka.servers:9092 +2024-06-20 11:00:22,329 - root - INFO - Kafka topic internal is caught up at offset 7928949 +2024-06-20 11:00:22,329 - root - INFO - setting influx write rate to pre-market hours frequency +2024-06-20 11:00:22,329 - root - INFO - Tracking service is caught up +2024-06-20 11:40:43,527 - root - WARNING - instrument not found for sedol BYP321337 +2024-06-20 11:40:43,527 - root - WARNING - instrument not found for sedol BYP321337 +2024-06-20 17:25:08,029 - root - ERROR - Exception in message handler > TrackingService.on_order_change() missing 1 required positional argument: 'order_identifier'""") + out_stream = io.StringIO() + analyzer.analyze_log_stream([warning_analyzer.WarningAnalyzer()], in_stream, out_stream) + assert out_stream.getvalue() == "\n2 Warnings were detected."