From 0c36fad72019d7e7c5b1dc08a79463254fc24890 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Tue, 24 Nov 2015 13:03:53 -0800 Subject: [PATCH] add "wormhole server usage" to dump usage DB --- src/wormhole/scripts/runner.py | 8 ++++- src/wormhole/servers/cmd_usage.py | 52 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/wormhole/servers/cmd_usage.py diff --git a/src/wormhole/scripts/runner.py b/src/wormhole/scripts/runner.py index 773f6ae..2be4bd1 100644 --- a/src/wormhole/scripts/runner.py +++ b/src/wormhole/scripts/runner.py @@ -4,7 +4,7 @@ from textwrap import dedent from .. import public_relay from .. import __version__ from . import cmd_send, cmd_receive -from ..servers import cmd_server +from ..servers import cmd_server, cmd_usage parser = argparse.ArgumentParser( usage="wormhole SUBCOMMAND (subcommand-options)", @@ -64,6 +64,12 @@ sp_restart.add_argument("--advertise-version", metavar="VERSION", sp_restart.add_argument("-n", "--no-daemon", action="store_true") sp_restart.set_defaults(func=cmd_server.restart_server) +sp_usage = sp.add_parser("show-usage", description="Display usage data", + usage="wormhole server usage") +sp_usage.add_argument("-n", default=100, type=int, help="show last N entries") +sp_usage.add_argument("-f", "--follow", action="store_true", help="wait for more usage") +sp_usage.set_defaults(func=cmd_usage.show_usage) + # CLI: send p = subparsers.add_parser("send", description="Send text message or file", diff --git a/src/wormhole/servers/cmd_usage.py b/src/wormhole/servers/cmd_usage.py new file mode 100644 index 0000000..a4590b2 --- /dev/null +++ b/src/wormhole/servers/cmd_usage.py @@ -0,0 +1,52 @@ +from __future__ import print_function +import os, time +from collections import defaultdict +from ..database import get_db +from ..errors import UsageError + +def abbrev(t): + if t is None: + return "-" + if t > 1.0: + return "%.3fs" % t + if t > 1e-3: + return "%.1fms" % (t*1e3) + return "%.1fus" % (t*1e6) + +def show_usage(args): + if not os.path.exists("relay.sqlite"): + raise UsageError("cannot find relay.sqlite, please run from the server directory") + if args.follow: + raise UsageError("--follow not yet implemented") + oldest_event = None + newest_event = None + counters = defaultdict(int) + db = get_db("relay.sqlite") + c = db.execute("SELECT * FROM `usage` ORDER BY `started` ASC LIMIT ?", (args.n,)) + for row in c.fetchall(): + counters["total"] += 1 + counters[row["result"]] += 1 + if oldest_event is None or row["started"] < oldest_event: + oldest_event = row["started"] + if newest_event is None or row["started"] > newest_event: + newest_event = row["started"] + followthrough = None + if row["waiting_time"] and row["total_time"]: + followthrough = row["total_time"] - row["waiting_time"] + #print(dir(row)) + print("%s: %-6s total=%7s wait=%7s ft=%7s" % + (time.ctime(row["started"]), row["result"], + abbrev(row["waiting_time"]), + abbrev(row["total_time"]), + abbrev(followthrough), + )) + total = counters["total"] + if total: + print("(most recent started %s ago)" % abbrev(time.time() - newest_event)) + elapsed = time.time() - oldest_event + print("%d events in %s (%.2f per hour)" % (total, abbrev(elapsed), + (3600 * total / elapsed))) + print(", ".join(["%s=%d (%s%%)" % (k, counters[k], (100.0 * counters[k] / total)) + for k in sorted(counters) + if k != "total"])) + return 0