From 246a3e25b34a6ddd5bda332b9798c3dd716f2f04 Mon Sep 17 00:00:00 2001
From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com>
Date: Sat, 31 Aug 2024 17:23:38 +0100
Subject: [PATCH] Add logfile filter (#1413)
* Add logfile filter
* [pre-commit.ci lite] apply automatic fixes
* Add files via upload
* [pre-commit.ci lite] apply automatic fixes
---------
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
---
apps/predbat/web.py | 51 ++++++++++++++++++++++++++++++++++++++-------
1 file changed, 43 insertions(+), 8 deletions(-)
diff --git a/apps/predbat/web.py b/apps/predbat/web.py
index 8fee2776..bbdcd273 100644
--- a/apps/predbat/web.py
+++ b/apps/predbat/web.py
@@ -53,7 +53,7 @@ def get_attributes_html(self, entity):
continue
value = attributes[key]
if len(str(value)) > 30:
- value = str(value)[:30] + "..."
+ value = "(large data)"
text += "
{} | {} |
".format(key, value)
text += ""
return text
@@ -187,22 +187,57 @@ async def html_log(self, request):
if os.path.exists(logfile):
with open(logfile, "r") as f:
logdata = f.read()
+
+ # Decode method get arguments
+ args = request.query
+ errors = False
+ warnings = False
+ if "errors" in args:
+ errors = True
+ if "warnings" in args:
+ warnings = True
+
loglines = logdata.split("\n")
text = self.get_header("Predbat Log", refresh=10)
text += ""
+
+ if errors:
+ text += "Logfile (errors)
\n"
+ elif warnings:
+ text += "Logfile (Warnings)
\n"
+ else:
+ text += "Logfile (All)
\n"
+
+ text += '- All '
+ text += 'Warnings '
+ text += 'Errors
\n'
+
text += "\n"
total_lines = len(loglines)
count_lines = 0
lineno = total_lines - 1
- while count_lines < 1024:
+ while count_lines < 1024 and lineno >= 0:
line = loglines[lineno]
- if line:
- start_line = line[0:27]
- rest_line = line[27:]
- text += "{} | {} {} |
\n".format(lineno, start_line, rest_line)
+ line_lower = line.lower()
lineno -= 1
- count_lines += 1
+
+ start_line = line[0:27]
+ rest_line = line[27:]
+
+ if "error" in line_lower:
+ text += "{} | {} {} |
\n".format(lineno, start_line, rest_line)
+ count_lines += 1
+ continue
+ elif (not errors) and ("warn" in line_lower):
+ text += "{} | {} {} |
\n".format(lineno, start_line, rest_line)
+ count_lines += 1
+ continue
+
+ if line and (not errors) and (not warnings):
+ text += "{} | {} {} |
\n".format(lineno, start_line, rest_line)
+ count_lines += 1
+
text += "
"
text += "