summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/analyze-log.py53
-rw-r--r--lib/utils.py69
2 files changed, 73 insertions, 49 deletions
diff --git a/bin/analyze-log.py b/bin/analyze-log.py
index 55fc2db..80690fc 100755
--- a/bin/analyze-log.py
+++ b/bin/analyze-log.py
@@ -18,61 +18,16 @@ import sys
import re
-def kv_to_param(kv_str, cast):
- try:
- key, value = kv_str.split("=")
- value = cast(value)
- return key, value
- except ValueError:
- print(f"Invalid key-value pair: {kv_str}")
- raise
-
-
-def kv_to_param_f(kv_str):
- return kv_to_param(kv_str, dfatool.utils.soft_cast_float)
-
-
-def kv_to_param_i(kv_str):
- return kv_to_param(kv_str, dfatool.utils.soft_cast_int)
-
-
def parse_logfile(filename):
+ lf = dfatool.utils.Logfile()
+
if filename.endswith("xz"):
import lzma
with lzma.open(filename, "rt") as f:
- return parse_loghandle(f)
+ return lf.load(f)
with open(filename, "r") as f:
- return parse_loghandle(f)
-
-
-def parse_loghandle(handle):
- observations = list()
- for lineno, line in enumerate(handle):
- m = re.search(r"\[::\] *([^|]*?) *[|] *([^|]*?) *[|] *(.*)", line)
- if m:
- name_str = m.group(1)
- param_str = m.group(2)
- attr_str = m.group(3)
- try:
- param = dict(map(kv_to_param_i, param_str.split()))
- attr = dict(map(kv_to_param_f, attr_str.split()))
- observations.append(
- {
- "name": name_str,
- "param": param,
- "attribute": attr,
- }
- )
- except ValueError:
- print(
- f"Error parsing {filename}: invalid key-value pair in line {lineno+1}",
- file=sys.stderr,
- )
- print(f"Offending entry:\n{line}", file=sys.stderr)
- raise
-
- return observations
+ return lf.load(f)
def main():
diff --git a/lib/utils.py b/lib/utils.py
index 69e807c..390a198 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -33,6 +33,75 @@ class NpEncoder(json.JSONEncoder):
return super(NpEncoder, self).default(obj)
+class Logfile:
+ def __init__(self):
+ pass
+
+ def kv_to_param(self, kv_str, cast):
+ try:
+ key, value = kv_str.split("=")
+ value = cast(value)
+ return key, value
+ except ValueError:
+ logger.warning(f"Invalid key-value pair: {kv_str}")
+ raise
+
+ def kv_to_param_f(self, kv_str):
+ return self.kv_to_param(kv_str, soft_cast_float)
+
+ def kv_to_param_i(self, kv_str):
+ return self.kv_to_param(kv_str, soft_cast_int)
+
+ def load(self, f):
+ observations = list()
+ for lineno, line in enumerate(f):
+ m = re.search(r"\[::\] *([^|]*?) *[|] *([^|]*?) *[|] *(.*)", line)
+ if m:
+ name_str = m.group(1)
+ param_str = m.group(2)
+ attr_str = m.group(3)
+ try:
+ param = dict(map(self.kv_to_param_i, param_str.split()))
+ attr = dict(map(self.kv_to_param_f, attr_str.split()))
+ observations.append(
+ {
+ "name": name_str,
+ "param": param,
+ "attribute": attr,
+ }
+ )
+ except ValueError:
+ logger.warning(
+ f"Error parsing {filename}: invalid key-value pair in line {lineno+1}",
+ file=sys.stderr,
+ )
+ logger.warning(f"Offending entry:\n{line}", file=sys.stderr)
+ raise
+
+ return observations
+
+ def dump(self, observations, f):
+ for observation in observations:
+ name = observation["name"]
+ param = observation["param"]
+ attr = observation["attribute"]
+
+ param_str = " ".join(
+ map(
+ lambda kv: f"{kv[0]}={kv[1]}",
+ sorted(param.items(), key=lambda kv: kv[0]),
+ )
+ )
+ attr_str = " ".join(
+ map(
+ lambda kv: f"{kv[0]}={kv[1]}",
+ sorted(attr.items(), key=lambda kv: kv[0]),
+ )
+ )
+
+ print(f"[::] {name} | {param_str} | {attr_str}", file=f)
+
+
def running_mean(x: np.ndarray, N: int) -> np.ndarray:
"""
Compute `N` elements wide running average over `x`.