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.

insert.py 5.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import sys
  2. from nilmdb.utils.printf import printf
  3. import nilmdb.client
  4. import nilmdb.utils.timestamper as timestamper
  5. import nilmdb.utils.time
  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 "
  65. "when adding timestamps")
  66. else:
  67. if self.args.start is None or self.args.end is None:
  68. self.die("error: when not adding timestamps, --start and "
  69. "--end are required")
  70. def cmd_insert(self):
  71. # Find requested stream
  72. streams = self.client.stream_list(self.args.path)
  73. if len(streams) != 1:
  74. self.die("error getting stream info for path %s", self.args.path)
  75. arg = self.args
  76. try:
  77. filename = arg.file
  78. if filename == '-':
  79. infile = sys.stdin.buffer
  80. else:
  81. try:
  82. infile = open(filename, "rb")
  83. except IOError:
  84. self.die("error opening input file %s", filename)
  85. if arg.start is None:
  86. try:
  87. arg.start = nilmdb.utils.time.parse_time(filename)
  88. except ValueError:
  89. self.die("error extracting start time from filename '%s'",
  90. filename)
  91. if arg.timestamp:
  92. data = timestamper.TimestamperRate(infile, arg.start, arg.rate)
  93. else:
  94. data = iter(lambda: infile.read(1048576), b'')
  95. # Print info
  96. if not arg.quiet:
  97. printf(" Input file: %s\n", filename)
  98. printf(" Start time: %s\n",
  99. nilmdb.utils.time.timestamp_to_human(arg.start))
  100. if arg.end:
  101. printf(" End time: %s\n",
  102. nilmdb.utils.time.timestamp_to_human(arg.end))
  103. if arg.timestamp:
  104. printf("Timestamper: %s\n", str(data))
  105. # Insert the data
  106. self.client.stream_insert(arg.path, data, arg.start, arg.end)
  107. except nilmdb.client.Error as e:
  108. # TODO: It would be nice to be able to offer better errors
  109. # here, particularly in the case of overlap, which just shows
  110. # ugly bracketed ranges of 16-digit numbers and a mangled URL.
  111. # Need to consider adding something like e.prettyprint()
  112. # that is smarter about the contents of the error.
  113. self.die("error inserting data: %s", str(e))
  114. return