From 9c2debab5792a4eaa3ac7c0edd395999123aa555 Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Fri, 9 Dec 2022 10:12:42 +0000
Subject: [PATCH] Add gzip support for log analyser

---
 CHANGELOG.rst                   |  2 ++
 backend/scripts/log_analyser.py | 14 +++++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index b9d5dc0..673ec7b 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,8 +1,10 @@
 Unreleased changes
 ------------------
+* `log_analyser.py` now also parses gzipped log files
 
 Version 2
 ---------
+
 2.2.3 / 2022-11-20
 ~~~~~~~~~~~~~~~~~~
 * Updates dependencies: km3pipe==9.13.5 which fixes the issue with the
diff --git a/backend/scripts/log_analyser.py b/backend/scripts/log_analyser.py
index f12dd6c..d6b88b7 100755
--- a/backend/scripts/log_analyser.py
+++ b/backend/scripts/log_analyser.py
@@ -4,6 +4,7 @@
 # Author: Tamas Gal <tgal@km3net.de>
 # vim: ts=4 sw=4 et
 from collections import defaultdict
+import gzip
 import sys
 import re
 import numpy as np
@@ -75,9 +76,16 @@ def process_log_file(log_file):
     """
     summary = defaultdict(lambda: defaultdict(int))
 
+    if log_file.endswith(".gz"):
+        opener = gzip.open
+        filemode = "rt"
+    else:
+        opener = open
+        filemode = "r"
+
     n_lines_parsed = 0
     n_lines_unparsed = 0
-    with open(log_file, "r") as fobj:
+    with opener(log_file, filemode) as fobj:
         lines_chunk = fobj.readlines(BUFFER_SIZE)
         while lines_chunk:
             for line in lines_chunk:
@@ -118,11 +126,11 @@ def process_log_file(log_file):
 def main():
 
     log_dir = "/logs/"
-    regexp = "^MSG_(.+)\.log$"
+    regexp = "^MSG_(.+)\.log"
 
     while True:
         for fname in os.listdir(log_dir):
-            plot_fpath = os.path.join(log_dir, os.path.splitext(fname)[0] + ".png")
+            plot_fpath = os.path.join(log_dir, os.path.splitext(fname.replace(".gz", ""))[0] + ".png")
             log_fpath = os.path.join(log_dir, fname)
             if re.match(regexp, fname) and not os.path.exists(plot_fpath):
                 print("-> Processing ", fname)
-- 
GitLab