|
11 | 11 | CLEAN_DB_KEEP_ACTIVITIES_DAYS, CLEAN_DB_KEEP_OPERATION_LOG_DAYS, CLEAN_DB_KEEP_DELETE_OPERATION_LOG_DAYS, \ |
12 | 12 | CLEAN_DB_KEEP_DTABLE_DB_OP_LOG_DAYS, CLEAN_DB_KEEP_NOTIFICATIONS_USERNOTIFICATION_DAYS, \ |
13 | 13 | CLEAN_DB_KEEP_DTABLE_NOTIFICATIONS_DAYS, CLEAN_DB_KEEP_SESSION_LOG_DAYS, CLEAN_DB_KEEP_AUTO_RULES_TASK_LOG_DAYS, \ |
14 | | - CLEAN_DB_KEEP_USER_ACTIVITY_STATISTICS_DAYS, CLEAN_DB_KEEP_DTABLE_APP_PAGES_OPERATION_LOG_DAYS |
| 14 | + CLEAN_DB_KEEP_USER_ACTIVITY_STATISTICS_DAYS, CLEAN_DB_KEEP_DTABLE_APP_PAGES_OPERATION_LOG_DAYS, \ |
| 15 | + CLEAN_DB_KEEP_EMAIL_SENDING_LOG_DAYS, CLEAN_DB_KEEP_SYSADMIN_EXTRA_USERLOGINLOG_DAYS |
15 | 16 | from dtable_events.db import init_db_session_class |
16 | 17 |
|
17 | 18 | __all__ = [ |
@@ -42,6 +43,8 @@ def _parse_config(self): |
42 | 43 | auto_rules_task_log=CLEAN_DB_KEEP_AUTO_RULES_TASK_LOG_DAYS, |
43 | 44 | user_activity_statistics=CLEAN_DB_KEEP_USER_ACTIVITY_STATISTICS_DAYS, |
44 | 45 | dtable_app_pages_operation_log=CLEAN_DB_KEEP_DTABLE_APP_PAGES_OPERATION_LOG_DAYS, |
| 46 | + email_sending_log=CLEAN_DB_KEEP_EMAIL_SENDING_LOG_DAYS, |
| 47 | + sysadmin_extra_userloginlog=CLEAN_DB_KEEP_SYSADMIN_EXTRA_USERLOGINLOG_DAYS |
45 | 48 | ) |
46 | 49 |
|
47 | 50 | def start(self): |
@@ -70,9 +73,11 @@ class RetentionConfig: |
70 | 73 | dtable_notifications: int = 30 |
71 | 74 | session_log: int = 30 |
72 | 75 | auto_rules_task_log: int = 30 |
| 76 | + dtable_app_pages_operation_log: int = 14 |
| 77 | + email_sending_log: int = 60 |
| 78 | + sysadmin_extra_userloginlog: int = 60 |
73 | 79 | # Disabled by default |
74 | 80 | user_activity_statistics: int = 0 |
75 | | - dtable_app_pages_operation_log: int = 14 |
76 | 81 |
|
77 | 82 |
|
78 | 83 | class CleanDBRecordsTask(Thread): |
@@ -104,6 +109,9 @@ def timed_job(): |
104 | 109 | clean_auto_rules_task_log(session, self.retention_config.auto_rules_task_log) |
105 | 110 | clean_user_activity_statistics(session, self.retention_config.user_activity_statistics) |
106 | 111 | clean_dtable_app_pages_operation_log(session, self.retention_config.dtable_app_pages_operation_log) |
| 112 | + clean_email_sending_log(session, self.retention_config.email_sending_log) |
| 113 | + clean_operation_checkpoint(session) |
| 114 | + clean_sysadmin_extra_userloginlog(session, self.retention_config.sysadmin_extra_userloginlog) |
107 | 115 | except: |
108 | 116 | logging.exception('Could not clean database') |
109 | 117 | finally: |
@@ -270,3 +278,41 @@ def clean_dtable_app_pages_operation_log(session, keep_days: int): |
270 | 278 | session.commit() |
271 | 279 |
|
272 | 280 | logging.info('Removed %d entries from "dtable_app_pages_operation_log"', result.rowcount) |
| 281 | + |
| 282 | + |
| 283 | +def clean_email_sending_log(session, keep_days: int): |
| 284 | + if keep_days <= 0: |
| 285 | + logging.info('Skipping "email_sending_log" since retention time is set to %d', keep_days) |
| 286 | + return |
| 287 | + |
| 288 | + logging.info('Cleaning "email_sending_log" table (older than %d days)', keep_days) |
| 289 | + |
| 290 | + sql = 'DELETE FROM `email_sending_log` WHERE `timestamp` < DATE_SUB(NOW(), INTERVAL :days DAY)' |
| 291 | + result = session.execute(text(sql), {'days': keep_days}) |
| 292 | + session.commit() |
| 293 | + |
| 294 | + logging.info('Removed %d entries from "email_sending_log"', result.rowcount) |
| 295 | + |
| 296 | + |
| 297 | +def clean_operation_checkpoint(session): |
| 298 | + logging.info('Cleaning "operation_checkpoint" whose dtables not existing') |
| 299 | + |
| 300 | + sql = 'DELETE oc FROM `operation_checkpoint` oc LEFT JOIN `dtables` d ON oc.dtable_uuid=d.uuid WHERE d.uuid IS NULL' |
| 301 | + result = session.execute(text(sql)) |
| 302 | + session.commit() |
| 303 | + |
| 304 | + logging.info('Removed %d entries from "operation_checkpoint"', result.rowcount) |
| 305 | + |
| 306 | + |
| 307 | +def clean_sysadmin_extra_userloginlog(session, keep_days: int): |
| 308 | + if keep_days <= 0: |
| 309 | + logging.info('Skipping "sysadmin_extra_userloginlog" since retention time is set to %d', keep_days) |
| 310 | + return |
| 311 | + |
| 312 | + logging.info('Cleaning "sysadmin_extra_userloginlog" table (older than %d days)', keep_days) |
| 313 | + |
| 314 | + sql = 'DELETE FROM `sysadmin_extra_userloginlog` WHERE `login_date` < DATE_SUB(NOW(), INTERVAL :days DAY)' |
| 315 | + result = session.execute(text(sql), {'days': keep_days}) |
| 316 | + session.commit() |
| 317 | + |
| 318 | + logging.info('Removed %d entries from "sysadmin_extra_userloginlog"', result.rowcount) |
0 commit comments