You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
4.3 KiB

  1. from nilmdb.utils.printf import *
  2. import nilmdb.utils.time
  3. import fnmatch
  4. import argparse
  5. from argparse import ArgumentDefaultsHelpFormatter as def_form
  6. def setup(self, sub):
  7. cmd = sub.add_parser("list", help="List streams",
  8. formatter_class = def_form,
  9. description="""
  10. List streams available in the database,
  11. optionally filtering by layout or path. Wildcards
  12. are accepted.
  13. """)
  14. cmd.set_defaults(verify = cmd_list_verify,
  15. handler = cmd_list)
  16. group = cmd.add_argument_group("Stream filtering")
  17. group.add_argument("-p", "--path", metavar="PATH", default="*",
  18. help="Match only this path (-p can be omitted)",
  19. ).completer = self.complete.path
  20. group.add_argument("path_positional", default="*",
  21. nargs="?", help=argparse.SUPPRESS,
  22. ).completer = self.complete.path
  23. group.add_argument("-l", "--layout", default="*",
  24. help="Match only this stream layout",
  25. ).completer = self.complete.layout
  26. group = cmd.add_argument_group("Interval info")
  27. group.add_argument("-E", "--ext", action="store_true",
  28. help="Show extended stream info, like interval "
  29. "extents and row count")
  30. group = cmd.add_argument_group("Interval details")
  31. group.add_argument("-d", "--detail", action="store_true",
  32. help="Show available data time intervals")
  33. group.add_argument("-s", "--start",
  34. metavar="TIME", type=self.arg_time,
  35. help="Starting timestamp for intervals "
  36. "(free-form, inclusive)",
  37. ).completer = self.complete.time
  38. group.add_argument("-e", "--end",
  39. metavar="TIME", type=self.arg_time,
  40. help="Ending timestamp for intervals "
  41. "(free-form, noninclusive)",
  42. ).completer = self.complete.time
  43. group = cmd.add_argument_group("Misc options")
  44. group.add_argument("-T", "--timestamp-raw", action="store_true",
  45. help="Show raw timestamps when printing times")
  46. return cmd
  47. def cmd_list_verify(self):
  48. # A hidden "path_positional" argument lets the user leave off the
  49. # "-p" when specifying the path. Handle it here.
  50. got_opt = self.args.path != "*"
  51. got_pos = self.args.path_positional != "*"
  52. if got_pos:
  53. if got_opt:
  54. self.parser.error("too many paths specified")
  55. else:
  56. self.args.path = self.args.path_positional
  57. if self.args.start is not None and self.args.end is not None:
  58. if self.args.start >= self.args.end:
  59. self.parser.error("start must precede end")
  60. if self.args.start is not None or self.args.end is not None:
  61. if not self.args.detail:
  62. self.parser.error("--start and --end only make sense with --detail")
  63. def cmd_list(self):
  64. """List available streams"""
  65. streams = self.client.stream_list(extended = True)
  66. if self.args.timestamp_raw:
  67. time_string = nilmdb.utils.time.float_time_to_string
  68. else:
  69. time_string = nilmdb.utils.time.format_time
  70. for stream in streams:
  71. (path, layout, int_min, int_max, rows, seconds) = stream[:6]
  72. if not (fnmatch.fnmatch(path, self.args.path) and
  73. fnmatch.fnmatch(layout, self.args.layout)):
  74. continue
  75. printf("%s %s\n", path, layout)
  76. if self.args.ext:
  77. if int_min is None or int_max is None:
  78. printf(" interval extents: (no data)\n")
  79. else:
  80. printf(" interval extents: %s -> %s\n",
  81. time_string(int_min), time_string(int_max))
  82. printf(" total data: %d rows, %.6f seconds\n",
  83. rows or 0, seconds or 0);
  84. if self.args.detail:
  85. printed = False
  86. for (start, end) in self.client.stream_intervals(
  87. path, self.args.start, self.args.end):
  88. printf(" [ %s -> %s ]\n", time_string(start), time_string(end))
  89. printed = True
  90. if not printed:
  91. printf(" (no intervals)\n")