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.
 
 
 

132 lines
5.3 KiB

  1. from nilmdb.utils.printf import *
  2. import nilmdb.client
  3. import nilmdb.utils.timestamper as timestamper
  4. import nilmdb.utils.time
  5. import sys
  6. def setup(self, sub):
  7. cmd = sub.add_parser("insert", help="Insert data",
  8. description="""
  9. Insert data into a stream.
  10. """)
  11. cmd.set_defaults(verify = cmd_insert_verify,
  12. handler = cmd_insert)
  13. cmd.add_argument("-q", "--quiet", action='store_true',
  14. help='suppress unnecessary messages')
  15. group = cmd.add_argument_group("Timestamping",
  16. description="""
  17. To add timestamps, specify the
  18. arguments --timestamp and --rate,
  19. and provide a starting time.
  20. """)
  21. group.add_argument("-t", "--timestamp", action="store_true",
  22. help="Add timestamps to each line")
  23. group.add_argument("-r", "--rate", type=float,
  24. help="Data rate, in Hz",
  25. ).completer = self.complete.rate
  26. group = cmd.add_argument_group("Start time",
  27. description="""
  28. Start time may be manually
  29. specified with --start, or guessed
  30. from the filenames using
  31. --filename. Set the TZ environment
  32. variable to change the default
  33. timezone.""")
  34. exc = group.add_mutually_exclusive_group()
  35. exc.add_argument("-s", "--start",
  36. metavar="TIME", type=self.arg_time,
  37. help="Starting timestamp (free-form)",
  38. ).completer = self.complete.time
  39. exc.add_argument("-f", "--filename", action="store_true",
  40. help="Use filename to determine start time")
  41. group = cmd.add_argument_group("End time",
  42. description="""
  43. End time for the overall stream.
  44. (required when not using --timestamp).
  45. Set the TZ environment
  46. variable to change the default
  47. timezone.""")
  48. group.add_argument("-e", "--end",
  49. metavar="TIME", type=self.arg_time,
  50. help="Ending timestamp (free-form)",
  51. ).completer = self.complete.time
  52. group = cmd.add_argument_group("Required parameters")
  53. group.add_argument("path",
  54. help="Path of stream, e.g. /foo/bar",
  55. ).completer = self.complete.path
  56. group.add_argument("file", nargs = '?', default='-',
  57. help="File to insert (default: - (stdin))")
  58. return cmd
  59. def cmd_insert_verify(self):
  60. if self.args.timestamp:
  61. if not self.args.rate:
  62. self.die("error: --rate is needed, but was not specified")
  63. if not self.args.filename and self.args.start is None:
  64. self.die("error: need --start or --filename when adding timestamps")
  65. else:
  66. if self.args.start is None or self.args.end is None:
  67. self.die("error: when not adding timestamps, --start and "
  68. "--end are required")
  69. def cmd_insert(self):
  70. # Find requested stream
  71. streams = self.client.stream_list(self.args.path)
  72. if len(streams) != 1:
  73. self.die("error getting stream info for path %s", self.args.path)
  74. arg = self.args
  75. try:
  76. filename = arg.file
  77. if filename == '-':
  78. infile = sys.stdin
  79. else:
  80. try:
  81. infile = open(filename, "rb")
  82. except IOError:
  83. self.die("error opening input file %s", filename)
  84. if arg.start is None:
  85. try:
  86. arg.start = nilmdb.utils.time.parse_time(filename).totimestamp()
  87. except ValueError:
  88. self.die("error extracting start time from filename '%s'",
  89. filename)
  90. if arg.timestamp:
  91. data = timestamper.TimestamperRate(infile, arg.start, arg.rate)
  92. else:
  93. data = iter(lambda: infile.read(1048576), '')
  94. # Print info
  95. if not arg.quiet:
  96. printf(" Input file: %s\n", filename)
  97. printf(" Start time: %s\n",
  98. nilmdb.utils.time.format_time(arg.start))
  99. if arg.end:
  100. printf(" End time: %s\n",
  101. nilmdb.utils.time.format_time(arg.end))
  102. if arg.timestamp:
  103. printf("Timestamper: %s\n", str(data))
  104. # Insert the data
  105. self.client.stream_insert(arg.path, data, arg.start, arg.end)
  106. except nilmdb.client.Error as e:
  107. # TODO: It would be nice to be able to offer better errors
  108. # here, particularly in the case of overlap, which just shows
  109. # ugly bracketed ranges of 16-digit numbers and a mangled URL.
  110. # Need to consider adding something like e.prettyprint()
  111. # that is smarter about the contents of the error.
  112. self.die("error inserting data: %s", str(e))
  113. return