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.
 
 
 

80 lines
3.3 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("-m", "--markup", action="store_true",
  30. help="Include comments with interval starts and ends")
  31. group.add_argument("-T", "--timestamp-raw", action="store_true",
  32. help="Show raw timestamps in annotated information")
  33. group.add_argument("-c", "--count", action="store_true",
  34. help="Just output a count of matched data points")
  35. return cmd
  36. def cmd_extract_verify(self):
  37. if self.args.start is not None and self.args.end is not None:
  38. if self.args.start > self.args.end:
  39. self.parser.error("start is after end")
  40. def cmd_extract(self):
  41. streams = self.client.stream_list(self.args.path)
  42. if len(streams) != 1:
  43. self.die("error getting stream info for path %s", self.args.path)
  44. layout = streams[0][1]
  45. if self.args.timestamp_raw:
  46. time_string = nilmdb.utils.time.timestamp_to_string
  47. else:
  48. time_string = nilmdb.utils.time.timestamp_to_human
  49. if self.args.annotate:
  50. printf("# path: %s\n", self.args.path)
  51. printf("# layout: %s\n", layout)
  52. printf("# start: %s\n", time_string(self.args.start))
  53. printf("# end: %s\n", time_string(self.args.end))
  54. printed = False
  55. for dataline in self.client.stream_extract(self.args.path,
  56. self.args.start,
  57. self.args.end,
  58. self.args.count,
  59. self.args.markup):
  60. if self.args.bare and not self.args.count:
  61. # Strip timestamp (first element). Doesn't make sense
  62. # if we are only returning a count.
  63. dataline = ' '.join(dataline.split(' ')[1:])
  64. print(dataline)
  65. printed = True
  66. if not printed:
  67. if self.args.annotate:
  68. printf("# no data\n")
  69. return 2
  70. return 0