81 lines
2.0 KiB
Plaintext
Executable File
81 lines
2.0 KiB
Plaintext
Executable File
#!<%= @virtual_env %>/bin/python
|
|
#
|
|
from datetime import datetime
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
import argparse
|
|
import subprocess
|
|
|
|
VSQL = "/usr/sbin/vsql"
|
|
|
|
GET_KEYS_SQL = """
|
|
SELECT DISTINCT
|
|
partition_key
|
|
FROM
|
|
partitions
|
|
WHERE
|
|
partition_key <= %s AND table_schema = '%s';
|
|
"""
|
|
|
|
DROP_SQL = "SELECT DROP_PARTITION('%s', %s);"
|
|
|
|
SCHEMAS_DICT = {"MonMetrics": "MonMetrics.Measurements",
|
|
"MonAlarms": "MonAlarms.StateHistory"}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
|
|
help_text = """
|
|
Number of months of partitions to retain.
|
|
"""
|
|
parser.add_argument('-m', '--months', required=True,
|
|
help=help_text)
|
|
|
|
args = parser.parse_args()
|
|
drop_partitions(args)
|
|
|
|
|
|
def get_drop_key(months_ago):
|
|
the_past = datetime.utcnow() - relativedelta(months=+int(months_ago))
|
|
partition_key = str(the_past.year) + str(the_past.month).zfill(2)
|
|
return partition_key
|
|
|
|
|
|
def drop_partitions(args):
|
|
drop_key = get_drop_key(args.months)
|
|
print "Deleting partitions '%s' and older" % drop_key
|
|
for schema, table in SCHEMAS_DICT.iteritems():
|
|
keys = vsql(GET_KEYS_SQL % (drop_key, schema))
|
|
if len(keys) == 0:
|
|
msg = " No partitions older than or equal to '%s' found for '%s'"
|
|
print msg % (drop_key, schema)
|
|
continue
|
|
for key in keys:
|
|
print " Dropping '%s' partition for '%s'" % (key, table)
|
|
vsql(DROP_SQL % (table, key))
|
|
|
|
|
|
def vsql(sql):
|
|
results = []
|
|
p = subprocess.Popen([VSQL, "-c", sql],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE)
|
|
|
|
out, err = p.communicate()
|
|
if p.returncode != 0:
|
|
print("vsql failed: %s %s" % (out, err))
|
|
return ""
|
|
for l in out.splitlines():
|
|
if '--' in l or 'partition_key' in l:
|
|
# header row, skip
|
|
continue
|
|
if 'row' in l:
|
|
# we're done
|
|
return results
|
|
results.append(l.strip())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|