-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.py
46 lines (36 loc) · 1.42 KB
/
cleanup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import json
import logging
from crate import client
from sys import stdout
# Set up logging
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(stdout)])
# Load the configuration
try:
with open("./config.json") as json_file:
config = json.load(json_file)
except Exception as e:
logging.error(f"Error loading configuration: {e}")
raise
def delete_old_records(table, time_index, duration):
try:
connection = client.connect(
os.getenv("CRATE_HOST"),
username=os.getenv("CRATE_USER"),
password=os.getenv("CRATE_PASSWORD"),
)
cursor = connection.cursor()
# Extract number and unit from the duration
num, unit = int(duration[:-1]), duration[-1]
# Convert the unit to a format acceptable by CrateDB's interval function
units_map = {"s": "second", "m": "minute", "h": "hour", "d": "day"}
unit = units_map.get(unit)
# Form the SQL query
query = f"DELETE FROM {table} WHERE {time_index} < CURRENT_TIMESTAMP - INTERVAL '{num} {unit}'"
cursor.execute(query)
connection.commit()
logging.info(f"Successfully deleted records older than {duration} from {table}")
except Exception as e:
logging.error(f"Error deleting records from {table}: {e}")
for table, attrs in config.items():
delete_old_records(table, attrs["time-index"], attrs["retention"])