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.
 
 
 

56 lines
2.2 KiB

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