From a501ec6ea26ff0ab3320acddb336cdbba70b6d60 Mon Sep 17 00:00:00 2001 From: Aleksander Kiryk Date: Tue, 20 Sep 2022 12:15:20 +0200 Subject: [PATCH] Unify CLI for ASCII and old plots --- .ci.yml | 2 +- common.py | 7 +++++++ graph.py | 28 +++++++++++++++++++--------- sargraph.py | 2 -- watch.py | 4 ---- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/.ci.yml b/.ci.yml index 1a5bc0d..4965451 100644 --- a/.ci.yml +++ b/.ci.yml @@ -29,7 +29,7 @@ simple_test: - sargraph chart stop - test -f plot.svg - - test -f plog.png + - test -f plot.png - test -f plot.ascii - cat chart.log diff --git a/common.py b/common.py index 24fefe3..998c570 100644 --- a/common.py +++ b/common.py @@ -46,6 +46,13 @@ def stof(s): return float(s.replace(',', '.')) +# Return a string without given suffix or unchange if it doesn't have it +def cut_suffix(s, sfx): + if s.endswith(sfx): + s = s[:-len(sfx)] + return s + + # Scale a value until it has a convenient size and unit, round the value # and return a string representation with the new value and its unit def unit_str(value, units, step=1024): diff --git a/graph.py b/graph.py index ef83b63..bd68129 100644 --- a/graph.py +++ b/graph.py @@ -198,21 +198,31 @@ def graph(session, fname='plot.png'): OUTPUT_TYPE = "pngcairo" OUTPUT_EXT = "png" if "SARGRAPH_OUTPUT_TYPE" in os.environ: - if os.environ["SARGRAPH_OUTPUT_TYPE"] == "svg": - OUTPUT_TYPE = "svg" - OUTPUT_EXT = "svg" + otype = os.environ["SARGRAPH_OUTPUT_TYPE"].lower() + + # png is the default, so don't change anything + if otype != "png": + OUTPUT_TYPE = otype + OUTPUT_EXT = otype + elif fname.lower().endswith('.png'): + # png is the default, so don't change anything + pass elif fname.lower().endswith('.svg'): OUTPUT_TYPE = "svg" OUTPUT_EXT = "svg" - elif fname.lower().endswith('.png'): - # Otherwise leave the default png - pass + elif fname.lower().endswith('.ascii'): + OUTPUT_TYPE = "ascii" + OUTPUT_EXT = "ascii" else: pass # fail("unknown graph extension") # Leave just the base name - fname = fname[:-4] + fname = cut_suffix(fname, f".{OUTPUT_EXT}") + + # ASCII plots have their own routine + if OUTPUT_TYPE == "ascii": + return ascii_graph(session, fname) read_comments(session) @@ -430,7 +440,7 @@ def render_ascii_plot( print('\n\n') -def ascii_graph(session, fname='plot.png'): +def ascii_graph(session, fname='plot.ascii'): data = read_data(session) titles = [f"""cpu load (average = {AVERAGE_LOAD} %)""", f"""ram usage (max = {MAX_USED_RAM})""", @@ -450,7 +460,7 @@ def ascii_graph(session, fname='plot.png'): summary += f"Duration: {START_DATE} .. {END_DATE} ({DURATION})" render_ascii_plot( - f'{Path(fname).with_suffix(".plot.txt")}', + Path(fname).with_suffix(".ascii"), summary, titles, ["time", "time", "time"], diff --git a/sargraph.py b/sargraph.py index 1cd5050..755e5b1 100755 --- a/sargraph.py +++ b/sargraph.py @@ -106,9 +106,7 @@ elif cmd[0] == 'save': elif cmd[0] == 'plot': if len(cmd) < 2: graph.graph(sid) - graph.ascii_graph(sid) else: graph.graph(sid, cmd[1]) - graph.ascii_graph(sid, cmd[1]) else: fail(f"unknown command '{cmd[0]}'") diff --git a/watch.py b/watch.py index 5ccc0eb..2d702ab 100644 --- a/watch.py +++ b/watch.py @@ -174,10 +174,8 @@ def watch(session, fsdev): pass elif label_line: graph.graph(session, label_line) - graph.ascii_graph(session, label_line) elif not dont_plot: graph.graph(session) - graph.ascii_graph(session) dont_plot = True die = 1 break @@ -190,10 +188,8 @@ def watch(session, fsdev): summarize(session) if not label_line: graph.graph(session) - graph.ascii_graph(session) else: graph.graph(session, label_line) - graph.ascii_graph(session, label_line) elif label_line.startswith('label:'): label_line = label_line[len('label:'):] with open(f"{session}.txt", "a") as f: