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.
 
 
 

60 lines
2.3 KiB

  1. import fnmatch
  2. from nilmdb.utils.printf import printf
  3. import nilmdb.client
  4. def setup(self, sub):
  5. cmd = sub.add_parser("remove", help="Remove data",
  6. description="""
  7. Remove all data from a specified time range within a
  8. stream. If multiple streams or wildcards are
  9. provided, the same time range is removed from all
  10. streams.
  11. """)
  12. cmd.set_defaults(handler=cmd_remove)
  13. group = cmd.add_argument_group("Data selection")
  14. group.add_argument("path", nargs='+',
  15. help="Path of stream, e.g. /foo/bar/*",
  16. ).completer = self.complete.path
  17. group.add_argument("-s", "--start", required=True,
  18. metavar="TIME", type=self.arg_time,
  19. help="Starting timestamp (free-form, inclusive)",
  20. ).completer = self.complete.time
  21. group.add_argument("-e", "--end", required=True,
  22. metavar="TIME", type=self.arg_time,
  23. help="Ending timestamp (free-form, noninclusive)",
  24. ).completer = self.complete.time
  25. group = cmd.add_argument_group("Output format")
  26. group.add_argument("-q", "--quiet", action="store_true",
  27. help="Don't display names when removing "
  28. "from multiple paths")
  29. group.add_argument("-c", "--count", action="store_true",
  30. help="Output number of data points removed")
  31. return cmd
  32. def cmd_remove(self):
  33. streams = [s[0] for s in self.client.stream_list()]
  34. paths = []
  35. for path in self.args.path:
  36. new = fnmatch.filter(streams, path)
  37. if not new:
  38. self.die("error: no stream matched path: %s", path)
  39. paths.extend(new)
  40. try:
  41. for path in paths:
  42. if not self.args.quiet and len(paths) > 1:
  43. printf("Removing from %s\n", path)
  44. count = self.client.stream_remove(path,
  45. self.args.start, self.args.end)
  46. if self.args.count:
  47. printf("%d\n", count)
  48. except nilmdb.client.ClientError as e:
  49. self.die("error removing data: %s", str(e))
  50. return 0