Created
April 19, 2012 18:34
-
-
Save mleinart/2422885 to your computer and use it in GitHub Desktop.
Whisper-resize with a filter option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, os, time, traceback | |
import whisper | |
from optparse import OptionParser | |
now = int(time.time()) | |
option_parser = OptionParser( | |
usage='''%prog path timePerPoint:timeToStore [timePerPoint:timeToStore]* | |
timePerPoint and timeToStore specify lengths of time, for example: | |
60:1440 60 seconds per datapoint, 1440 datapoints = 1 day of retention | |
15m:8 15 minutes per datapoint, 8 datapoints = 2 hours of retention | |
1h:7d 1 hour per datapoint, 7 days of retention | |
12h:2y 12 hours per datapoint, 2 years of retention | |
''') | |
option_parser.add_option( | |
'--xFilesFactor', default=None, | |
type='float', help="Change the xFilesFactor") | |
option_parser.add_option( | |
'--aggregationMethod', default=None, | |
type='string', help="Change the aggregation function (%s)" % | |
', '.join(whisper.aggregationMethods)) | |
option_parser.add_option( | |
'--force', default=False, action='store_true', | |
help="Perform a destructive change") | |
option_parser.add_option( | |
'--newfile', default=None, action='store', | |
help="Create a new database file without removing the existing one") | |
option_parser.add_option( | |
'--nobackup', action='store_true', | |
help='Delete the .bak file after successful execution') | |
option_parser.add_option( | |
'--filterabove', default=None, action='store', | |
help='Filter out values above the specified') | |
option_parser.add_option( | |
'--filterbelow', default=None, action='store', | |
help='Filter out values below the specified') | |
(options, args) = option_parser.parse_args() | |
if len(args) < 2: | |
option_parser.print_usage() | |
sys.exit(1) | |
path = args[0] | |
new_archives = [whisper.parseRetentionDef(retentionDef) | |
for retentionDef in args[1:]] | |
info = whisper.info(path) | |
old_archives = info['archives'] | |
# sort by precision, lowest to highest | |
old_archives.sort(key=lambda a: a['secondsPerPoint'], reverse=True) | |
if options.xFilesFactor is None: | |
xff = info['xFilesFactor'] | |
else: | |
xff = options.xFilesFactor | |
if options.aggregationMethod is None: | |
aggregationMethod = info['aggregationMethod'] | |
else: | |
aggregationMethod = options.aggregationMethod | |
print 'Retrieving all data from the archives' | |
for archive in old_archives: | |
fromTime = now - archive['retention'] + archive['secondsPerPoint'] | |
untilTime = now | |
timeinfo,values = whisper.fetch(path, fromTime, untilTime) | |
archive['data'] = (timeinfo,values) | |
if options.newfile is None: | |
tmpfile = path + '.tmp' | |
if os.path.exists(tmpfile): | |
print 'Removing previous temporary database file: %s' % tmpfile | |
os.unlink(tmpfile) | |
newfile = tmpfile | |
else: | |
newfile = options.newfile | |
print 'Creating new whisper database: %s' % newfile | |
whisper.create(newfile, new_archives, xFilesFactor=xff, aggregationMethod=aggregationMethod) | |
size = os.stat(newfile).st_size | |
print 'Created: %s (%d bytes)' % (newfile,size) | |
print 'Migrating data...' | |
for archive in old_archives: | |
timeinfo, values = archive['data'] | |
if options.filterabove: | |
values = [ v for v in values if v < options.filterabove ] | |
if options.filterbelow: | |
values = [ v for v in values if v > options.filterbelow ] | |
datapoints = zip( range(*timeinfo), values ) | |
datapoints = filter(lambda p: p[1] is not None, datapoints) | |
whisper.update_many(newfile, datapoints) | |
if options.newfile is not None: | |
sys.exit(0) | |
backup = path + '.bak' | |
print 'Renaming old database to: %s' % backup | |
os.rename(path, backup) | |
try: | |
print 'Renaming new database to: %s' % path | |
os.rename(tmpfile, path) | |
except: | |
traceback.print_exc() | |
print '\nOperation failed, restoring backup' | |
os.rename(backup, path) | |
sys.exit(1) | |
if options.nobackup: | |
print "Unlinking backup: %s" % backup | |
os.unlink(backup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment