add python script for min, avg, and max

This commit is contained in:
Youngjoon Lee 2024-12-10 14:54:13 +09:00
parent 137bd72435
commit 0c03dcde86
No known key found for this signature in database
GPG Key ID: 303963A54A81DD4D

View File

@ -0,0 +1,48 @@
import argparse
import json
from collections.abc import Iterable
from typing import Any
import pandas as pd
import mixlog
def analyze_monitors(input_stream: Iterable[str]) -> None:
df = pd.DataFrame(monitor_records(input_stream))
result = {
"min": df["min"].min(),
"avg": (df["num_conns"] * df["avg"]).sum() / df["num_conns"].sum(),
"max": df["max"].max(),
}
print(result)
def monitor_records(input_stream: Iterable[str]) -> list[Any]:
records = []
for line in input_stream:
try:
record = json.loads(line)
except json.JSONDecodeError:
continue
if "message_type" in record and "num_conns" in record:
records.append(record)
return records
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Mix connection monitor analysis")
parser.add_argument(
"--log-path",
nargs="?",
type=str,
help="An input log file path. If not provided, input will be read from stdin.",
)
args = parser.parse_args()
input = mixlog.get_input_stream(args.log_path)
analyze_monitors(input)