diff --git a/src/wormhole/scripts/runner.py b/src/wormhole/scripts/runner.py index 2be4bd1..d78205a 100644 --- a/src/wormhole/scripts/runner.py +++ b/src/wormhole/scripts/runner.py @@ -64,11 +64,15 @@ 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) +sp_show_usage = sp.add_parser("show-usage", description="Display usage data", + usage="wormhole server show-usage") +sp_show_usage.add_argument("-n", default=100, type=int, + help="show last N entries") +sp_show_usage.set_defaults(func=cmd_usage.show_usage) + +sp_tail_usage = sp.add_parser("tail-usage", description="Follow latest usage", + usage="wormhole server tail-usage") +sp_tail_usage.set_defaults(func=cmd_usage.tail_usage) # CLI: send p = subparsers.add_parser("send", diff --git a/src/wormhole/servers/cmd_usage.py b/src/wormhole/servers/cmd_usage.py index a4590b2..78eec21 100644 --- a/src/wormhole/servers/cmd_usage.py +++ b/src/wormhole/servers/cmd_usage.py @@ -13,40 +13,65 @@ def abbrev(t): return "%.1fms" % (t*1e3) return "%.1fus" % (t*1e6) +def print_event(event): + started, result, waiting_time, total_time = event + followthrough = None + if waiting_time and total_time: + followthrough = total_time - waiting_time + print("%s: %-6s total=%7s wait=%7s ft=%7s" % + (time.ctime(started), result, + abbrev(total_time), + abbrev(waiting_time), + abbrev(followthrough), + )) + 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 + oldest = None + newest = None counters = defaultdict(int) db = get_db("relay.sqlite") - c = db.execute("SELECT * FROM `usage` ORDER BY `started` ASC LIMIT ?", (args.n,)) + 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), - )) + if oldest is None or row["started"] < oldest: + oldest = row["started"] + if newest is None or row["started"] > newest: + newest = row["started"] + event = (row["started"], row["result"], + row["waiting_time"], row["total_time"]) + print_event(event) total = counters["total"] if total: - print("(most recent started %s ago)" % abbrev(time.time() - newest_event)) - elapsed = time.time() - oldest_event + print("(most recent started %s ago)" % abbrev(time.time() - newest)) + elapsed = time.time() - oldest 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)) + print(", ".join(["%s=%d (%d%%)" % + (k, counters[k], (100.0 * counters[k] / total)) for k in sorted(counters) if k != "total"])) return 0 + +def tail_usage(args): + if not os.path.exists("relay.sqlite"): + raise UsageError("cannot find relay.sqlite, please run from the server directory") + db = get_db("relay.sqlite") + # we don't seem to have unique row IDs, so this is an inaccurate and + # inefficient hack + seen = set() + while True: + old = time.time() - 2*60*60 + c = db.execute("SELECT * FROM `usage` WHERE `started` > ?" + " ORDER BY `started` ASC", (old,)) + for row in c.fetchall(): + event = (row["started"], row["result"], + row["waiting_time"], row["total_time"]) + if event not in seen: + print_event(event) + seen.add(event) + time.sleep(2) + return 0