| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import sys |
| 4 |
import time |
| 5 |
import signal |
| 6 |
import optparse |
| 7 |
|
| 8 |
try: |
| 9 |
import whisper |
| 10 |
except 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 |
|
| 17 |
def update_value(timestamp, value): |
| 18 |
if value is None: |
| 19 |
return value |
| 20 |
return value * 1024 * 1024 * 1024 |
| 21 |
|
| 22 |
|
| 23 |
# Ignore SIGPIPE |
| 24 |
signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 25 |
|
| 26 |
now = int(time.time()) |
| 27 |
yesterday = now - (60 * 60 * 24) |
| 28 |
|
| 29 |
option_parser = optparse.OptionParser(usage='''%prog [options] path''') |
| 30 |
option_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)")) |
| 34 |
option_parser.add_option( |
| 35 |
'--until', default=now, type='int', |
| 36 |
help="Unix epoch time of the end of your requested interval (default: now)") |
| 37 |
option_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 |
|
| 43 |
if len(args) < 1: |
| 44 |
option_parser.print_usage() |
| 45 |
sys.exit(1) |
| 46 |
|
| 47 |
path = args[0] |
| 48 |
from_time = int(options._from) |
| 49 |
until_time = int(options.until) |
| 50 |
|
| 51 |
try: |
| 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 |
| 56 |
except whisper.WhisperException as exc: |
| 57 |
raise SystemExit('[ERROR] %s' % str(exc)) |
| 58 |
|
| 59 |
(start, end, step) = timeInfo |
| 60 |
t = start |
| 61 |
for 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)) |