Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[15.0][PERF] usability_webhooks: auto delete log with direct query #228

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions usability_webhooks/models/api_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,32 @@ def autovacuum(self, days, chunk_size=None):
"""
days = (days > 0) and int(days) or 0
deadline = datetime.now() - timedelta(days=days)
records = self.env["api.log"].search(
[("create_date", "<=", fields.Datetime.to_string(deadline))],
limit=chunk_size,
order="create_date asc",
)
nb_records = len(records)
with self.env.norecompute():
records.unlink()
domain = [("create_date", "<=", fields.Datetime.to_string(deadline))]

# Count the number of records to be deleted
nb_records = self.env["api.log"].search_count(domain)

if chunk_size:
# Use direct SQL query for deletion with limit
query = """
DELETE FROM api_log
WHERE id IN (
SELECT id FROM api_log
WHERE create_date <= %s
ORDER BY create_date ASC
LIMIT %s
)
"""
self.env.cr.execute(
query, (fields.Datetime.to_string(deadline), chunk_size)
)
else:
# Use direct SQL query for deletion
query = """
DELETE FROM api_log
WHERE create_date <= %s
"""
self.env.cr.execute(query, (fields.Datetime.to_string(deadline),))

_logger.info("AUTOVACUUM - %s 'api.log' records deleted", nb_records)
return True
Loading