1#!/usr/bin/env python
2
3import sys
4import time
5import signal
6import optparse
7
8try:
9  import whisper
10except ImportError:
11  raise SystemExit('[ERROR] Please make sure whisper is installed properly')
12
13# update this callback to do the logic you want.
14# a future version could use a config while in which this fn is defined.
15
16
17def update_value(timestamp, value):
18  if value is None:
19    return value
20  return value * 1024 * 1024 * 1024
21
22
23# Ignore SIGPIPE
24signal.signal(signal.SIGPIPE, signal.SIG_DFL)
25
26now = int(time.time())
27yesterday = now - (60 * 60 * 24)
28
29option_parser = optparse.OptionParser(usage='''%prog [options] path''')
30option_parser.add_option(
31  '--from', default=yesterday, type='int', dest='_from',
32  help=("Unix epoch time of the beginning of "
33        "your requested interval (default: 24 hours ago)"))
34option_parser.add_option(
35  '--until', default=now, type='int',
36  help="Unix epoch time of the end of your requested interval (default: now)")
37option_parser.add_option(
38  '--pretty', default=False, action='store_true',
39  help="Show human-readable timestamps instead of unix times")
40
41(options, args) = option_parser.parse_args()
42
43if len(args) < 1:
44  option_parser.print_usage()
45  sys.exit(1)
46
47path = args[0]
48from_time = int(options._from)
49until_time = int(options.until)
50
51try:
52  data = whisper.fetch(path, from_time, until_time)
53  if not data:
54    raise SystemExit('No data in selected timerange')
55  (timeInfo, values_old) = data
56except whisper.WhisperException as exc:
57  raise SystemExit('[ERROR] %s' % str(exc))
58
59(start, end, step) = timeInfo
60t = start
61for value_old in values_old:
62  value_str_old = str(value_old)
63  value_new = update_value(t, value_old)
64  value_str_new = str(value_new)
65  if options.pretty:
66    timestr = time.ctime(t)
67  else:
68    timestr = str(t)
69
70  print("%s\t%s -> %s" % (timestr, value_str_old, value_str_new))
71  try:
72    if value_new is not None:
73      whisper.update(path, value_new, t)
74    t += step
75  except whisper.WhisperException as exc:
76    raise SystemExit('[ERROR] %s' % str(exc))
77