wormhole server show-usage / tail-usage
Use a separate "tail-usage" command instead of "show-usage -f". Make both work on py3 too.
This commit is contained in:
parent
0c36fad720
commit
a96f29d01f
|
@ -64,11 +64,15 @@ sp_restart.add_argument("--advertise-version", metavar="VERSION",
|
||||||
sp_restart.add_argument("-n", "--no-daemon", action="store_true")
|
sp_restart.add_argument("-n", "--no-daemon", action="store_true")
|
||||||
sp_restart.set_defaults(func=cmd_server.restart_server)
|
sp_restart.set_defaults(func=cmd_server.restart_server)
|
||||||
|
|
||||||
sp_usage = sp.add_parser("show-usage", description="Display usage data",
|
sp_show_usage = sp.add_parser("show-usage", description="Display usage data",
|
||||||
usage="wormhole server usage")
|
usage="wormhole server show-usage")
|
||||||
sp_usage.add_argument("-n", default=100, type=int, help="show last N entries")
|
sp_show_usage.add_argument("-n", default=100, type=int,
|
||||||
sp_usage.add_argument("-f", "--follow", action="store_true", help="wait for more usage")
|
help="show last N entries")
|
||||||
sp_usage.set_defaults(func=cmd_usage.show_usage)
|
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
|
# CLI: send
|
||||||
p = subparsers.add_parser("send",
|
p = subparsers.add_parser("send",
|
||||||
|
|
|
@ -13,40 +13,65 @@ def abbrev(t):
|
||||||
return "%.1fms" % (t*1e3)
|
return "%.1fms" % (t*1e3)
|
||||||
return "%.1fus" % (t*1e6)
|
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):
|
def show_usage(args):
|
||||||
if not os.path.exists("relay.sqlite"):
|
if not os.path.exists("relay.sqlite"):
|
||||||
raise UsageError("cannot find relay.sqlite, please run from the server directory")
|
raise UsageError("cannot find relay.sqlite, please run from the server directory")
|
||||||
if args.follow:
|
oldest = None
|
||||||
raise UsageError("--follow not yet implemented")
|
newest = None
|
||||||
oldest_event = None
|
|
||||||
newest_event = None
|
|
||||||
counters = defaultdict(int)
|
counters = defaultdict(int)
|
||||||
db = get_db("relay.sqlite")
|
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():
|
for row in c.fetchall():
|
||||||
counters["total"] += 1
|
counters["total"] += 1
|
||||||
counters[row["result"]] += 1
|
counters[row["result"]] += 1
|
||||||
if oldest_event is None or row["started"] < oldest_event:
|
if oldest is None or row["started"] < oldest:
|
||||||
oldest_event = row["started"]
|
oldest = row["started"]
|
||||||
if newest_event is None or row["started"] > newest_event:
|
if newest is None or row["started"] > newest:
|
||||||
newest_event = row["started"]
|
newest = row["started"]
|
||||||
followthrough = None
|
event = (row["started"], row["result"],
|
||||||
if row["waiting_time"] and row["total_time"]:
|
row["waiting_time"], row["total_time"])
|
||||||
followthrough = row["total_time"] - row["waiting_time"]
|
print_event(event)
|
||||||
#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"]
|
total = counters["total"]
|
||||||
if total:
|
if total:
|
||||||
print("(most recent started %s ago)" % abbrev(time.time() - newest_event))
|
print("(most recent started %s ago)" % abbrev(time.time() - newest))
|
||||||
elapsed = time.time() - oldest_event
|
elapsed = time.time() - oldest
|
||||||
print("%d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
|
print("%d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
|
||||||
(3600 * total / 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)
|
for k in sorted(counters)
|
||||||
if k != "total"]))
|
if k != "total"]))
|
||||||
return 0
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user