Skip to content

Commit

Permalink
[FIX] auditlog: Allow passing a chunk size for autovacuum
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn authored and StefanRijnhart committed Jan 16, 2023
1 parent 491b70d commit ff27aa4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions auditlog/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ To activate it and/or change the delay, go to the

.. image:: https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png

In case you're having trouble with the amount of records to delete per run,
you can pass the amount of records to delete for one model per run as the second
parameter, the default is to delete all records in one go.

There are two possible groups configured to which one may belong. The first
is the Auditlog User group. This group has read-only access to the auditlogs of
individual records through the `View Logs` action. The second group is the
Expand Down
9 changes: 6 additions & 3 deletions auditlog/models/autovacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AuditlogAutovacuum(models.TransientModel):
_description = "Auditlog - Delete old logs"

@api.model
def autovacuum(self, days):
def autovacuum(self, days, chunk_size=None):
"""Delete all logs older than ``days``. This includes:
- CRUD logs (create, read, write, unlink)
- HTTP requests
Expand All @@ -26,9 +26,12 @@ def autovacuum(self, days):
data_models = ("auditlog.log", "auditlog.http.request", "auditlog.http.session")
for data_model in data_models:
records = self.env[data_model].search(
[("create_date", "<=", fields.Datetime.to_string(deadline))]
[("create_date", "<=", fields.Datetime.to_string(deadline))],
limit=chunk_size,
order="create_date asc",
)
nb_records = len(records)
records.unlink()
with self.env.norecompute():
records.unlink()
_logger.info("AUTOVACUUM - %s '%s' records deleted", nb_records, data_model)
return True
4 changes: 4 additions & 0 deletions auditlog/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ To activate it and/or change the delay, go to the

.. image:: ../static/description/autovacuum.png

In case you're having trouble with the amount of records to delete per run,
you can pass the amount of records to delete for one model per run as the second
parameter, the default is to delete all records in one go.

There are two possible groups configured to which one may belong. The first
is the Auditlog User group. This group has read-only access to the auditlogs of
individual records through the `View Logs` action. The second group is the
Expand Down
5 changes: 4 additions & 1 deletion auditlog/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Audit Log</title>
<style type="text/css">

Expand Down Expand Up @@ -401,6 +401,9 @@ <h1><a class="toc-backref" href="#id1">Usage</a></h1>
<cite>Configuration / Technical / Automation / Scheduled Actions</cite> menu and edit the
<cite>Auto-vacuum audit logs</cite> entry:</p>
<img alt="https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png" src="https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png" />
<p>In case you’re having trouble with the amount of records to delete per run,
you can pass the amount of records to delete for one model per run as the second
parameter, the default is to delete all records in one go.</p>
<p>There are two possible groups configured to which one may belong. The first
is the Auditlog User group. This group has read-only access to the auditlogs of
individual records through the <cite>View Logs</cite> action. The second group is the
Expand Down

0 comments on commit ff27aa4

Please sign in to comment.