109 lines
4.4 KiB
Python
109 lines
4.4 KiB
Python
from nilmdb.utils.printf import *
|
|
import nilmdb.utils.time
|
|
|
|
import fnmatch
|
|
import argparse
|
|
from argparse import ArgumentDefaultsHelpFormatter as def_form
|
|
|
|
def setup(self, sub):
|
|
cmd = sub.add_parser("list", help="List streams",
|
|
formatter_class = def_form,
|
|
description="""
|
|
List streams available in the database,
|
|
optionally filtering by layout or path. Wildcards
|
|
are accepted.
|
|
""")
|
|
cmd.set_defaults(verify = cmd_list_verify,
|
|
handler = cmd_list)
|
|
|
|
group = cmd.add_argument_group("Stream filtering")
|
|
group.add_argument("-p", "--path", metavar="PATH", default="*",
|
|
help="Match only this path (-p can be omitted)",
|
|
).completer = self.complete.path
|
|
group.add_argument("path_positional", default="*",
|
|
nargs="?", help=argparse.SUPPRESS,
|
|
).completer = self.complete.path
|
|
group.add_argument("-l", "--layout", default="*",
|
|
help="Match only this stream layout",
|
|
).completer = self.complete.layout
|
|
|
|
group = cmd.add_argument_group("Interval info")
|
|
group.add_argument("-E", "--ext", action="store_true",
|
|
help="Show extended stream info, like interval "
|
|
"extents and row count")
|
|
|
|
group = cmd.add_argument_group("Interval details")
|
|
group.add_argument("-d", "--detail", action="store_true",
|
|
help="Show available data time intervals")
|
|
group.add_argument("-s", "--start",
|
|
metavar="TIME", type=self.arg_time,
|
|
help="Starting timestamp for intervals "
|
|
"(free-form, inclusive)",
|
|
).completer = self.complete.time
|
|
group.add_argument("-e", "--end",
|
|
metavar="TIME", type=self.arg_time,
|
|
help="Ending timestamp for intervals "
|
|
"(free-form, noninclusive)",
|
|
).completer = self.complete.time
|
|
|
|
group = cmd.add_argument_group("Misc options")
|
|
group.add_argument("-T", "--timestamp-raw", action="store_true",
|
|
help="Show raw timestamps when printing times")
|
|
|
|
return cmd
|
|
|
|
def cmd_list_verify(self):
|
|
# A hidden "path_positional" argument lets the user leave off the
|
|
# "-p" when specifying the path. Handle it here.
|
|
got_opt = self.args.path != "*"
|
|
got_pos = self.args.path_positional != "*"
|
|
if got_pos:
|
|
if got_opt:
|
|
self.parser.error("too many paths specified")
|
|
else:
|
|
self.args.path = self.args.path_positional
|
|
|
|
if self.args.start is not None and self.args.end is not None:
|
|
if self.args.start >= self.args.end:
|
|
self.parser.error("start must precede end")
|
|
|
|
if self.args.start is not None or self.args.end is not None:
|
|
if not self.args.detail:
|
|
self.parser.error("--start and --end only make sense with --detail")
|
|
|
|
def cmd_list(self):
|
|
"""List available streams"""
|
|
streams = self.client.stream_list(extended = True)
|
|
|
|
if self.args.timestamp_raw:
|
|
time_string = nilmdb.utils.time.timestamp_to_string
|
|
else:
|
|
time_string = nilmdb.utils.time.timestamp_to_human
|
|
|
|
for stream in streams:
|
|
(path, layout, int_min, int_max, rows, time) = stream[:6]
|
|
if not (fnmatch.fnmatch(path, self.args.path) and
|
|
fnmatch.fnmatch(layout, self.args.layout)):
|
|
continue
|
|
|
|
printf("%s %s\n", path, layout)
|
|
|
|
if self.args.ext:
|
|
if int_min is None or int_max is None:
|
|
printf(" interval extents: (no data)\n")
|
|
else:
|
|
printf(" interval extents: %s -> %s\n",
|
|
time_string(int_min), time_string(int_max))
|
|
printf(" total data: %d rows, %.6f seconds\n",
|
|
rows or 0,
|
|
nilmdb.utils.time.timestamp_to_seconds(time or 0))
|
|
|
|
if self.args.detail:
|
|
printed = False
|
|
for (start, end) in self.client.stream_intervals(
|
|
path, self.args.start, self.args.end):
|
|
printf(" [ %s -> %s ]\n", time_string(start), time_string(end))
|
|
printed = True
|
|
if not printed:
|
|
printf(" (no intervals)\n")
|