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.
 
 
 

77 lines
3.1 KiB

  1. from __future__ import print_function
  2. from nilmdb.utils.printf import *
  3. import nilmdb.client
  4. def setup(self, sub):
  5. cmd = sub.add_parser("extract", help="Extract data",
  6. description="""
  7. Extract data from a stream.
  8. """)
  9. cmd.set_defaults(verify = cmd_extract_verify,
  10. handler = cmd_extract)
  11. group = cmd.add_argument_group("Data selection")
  12. group.add_argument("path",
  13. help="Path of stream, e.g. /foo/bar",
  14. ).completer = self.complete.path
  15. group.add_argument("-s", "--start", required=True,
  16. metavar="TIME", type=self.arg_time,
  17. help="Starting timestamp (free-form, inclusive)",
  18. ).completer = self.complete.time
  19. group.add_argument("-e", "--end", required=True,
  20. metavar="TIME", type=self.arg_time,
  21. help="Ending timestamp (free-form, noninclusive)",
  22. ).completer = self.complete.time
  23. group = cmd.add_argument_group("Output format")
  24. group.add_argument("-b", "--bare", action="store_true",
  25. help="Exclude timestamps from output lines")
  26. group.add_argument("-a", "--annotate", action="store_true",
  27. help="Include comments with some information "
  28. "about the stream")
  29. group.add_argument("-T", "--timestamp-raw", action="store_true",
  30. help="Show raw timestamps in annotated information")
  31. group.add_argument("-c", "--count", action="store_true",
  32. help="Just output a count of matched data points")
  33. return cmd
  34. def cmd_extract_verify(self):
  35. if self.args.start is not None and self.args.end is not None:
  36. if self.args.start > self.args.end:
  37. self.parser.error("start is after end")
  38. def cmd_extract(self):
  39. streams = self.client.stream_list(self.args.path)
  40. if len(streams) != 1:
  41. self.die("error getting stream info for path %s", self.args.path)
  42. layout = streams[0][1]
  43. if self.args.timestamp_raw:
  44. time_string = nilmdb.utils.time.float_time_to_string
  45. else:
  46. time_string = nilmdb.utils.time.format_time
  47. if self.args.annotate:
  48. printf("# path: %s\n", self.args.path)
  49. printf("# layout: %s\n", layout)
  50. printf("# start: %s\n", time_string(self.args.start))
  51. printf("# end: %s\n", time_string(self.args.end))
  52. printed = False
  53. for dataline in self.client.stream_extract(self.args.path,
  54. self.args.start,
  55. self.args.end,
  56. self.args.count):
  57. if self.args.bare and not self.args.count:
  58. # Strip timestamp (first element). Doesn't make sense
  59. # if we are only returning a count.
  60. dataline = ' '.join(dataline.split(' ')[1:])
  61. print(dataline)
  62. printed = True
  63. if not printed:
  64. if self.args.annotate:
  65. printf("# no data\n")
  66. return 2
  67. return 0