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.
 
 
 

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