Skip to content

Commit

Permalink
mail_tracking: add autovacuum to delete old tracking records
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybackman committed Aug 8, 2024
1 parent 8984d40 commit 36b36cf
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
24 changes: 24 additions & 0 deletions mail_tracking/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,27 @@ def event_process(self, request, post, metadata, event_type=None):
# - return 'NONE' if this request is not for you
# - return 'ERROR' if any error
return "NONE" # pragma: no cover


def _get_old_mail_tracking_email_domain(self, max_age_days):
target_write_date = fields.Datetime.subtract(

Check warning on line 479 in mail_tracking/models/mail_tracking_email.py

View check run for this annotation

Codecov / codecov/patch

mail_tracking/models/mail_tracking_email.py#L479

Added line #L479 was not covered by tests
fields.Datetime.now(), days=max_age_days
)
return [("write_date", "<", target_write_date)]

Check warning on line 482 in mail_tracking/models/mail_tracking_email.py

View check run for this annotation

Codecov / codecov/patch

mail_tracking/models/mail_tracking_email.py#L482

Added line #L482 was not covered by tests

@api.autovacuum
def _gc_mail_tracking_email(self, max_age_days=180, limit=5000):
enable_deletion = (

Check warning on line 486 in mail_tracking/models/mail_tracking_email.py

View check run for this annotation

Codecov / codecov/patch

mail_tracking/models/mail_tracking_email.py#L486

Added line #L486 was not covered by tests
self.env["ir.config_parameter"]
.sudo()
.get_param("mail_tracking.enable_old_mail_tracking_email_deletion")
)

if not enable_deletion:
_logger.info("Mail tracking email deletion is disabled.")
return False

Check warning on line 494 in mail_tracking/models/mail_tracking_email.py

View check run for this annotation

Codecov / codecov/patch

mail_tracking/models/mail_tracking_email.py#L493-L494

Added lines #L493 - L494 were not covered by tests

domain = self._get_old_mail_tracking_email_domain(max_age_days)
records_to_delete = self.search(domain, limit=limit)
_logger.debug("Deleting %s mail.tracking.email records", len(records_to_delete))
return records_to_delete.unlink()

Check warning on line 499 in mail_tracking/models/mail_tracking_email.py

View check run for this annotation

Codecov / codecov/patch

mail_tracking/models/mail_tracking_email.py#L496-L499

Added lines #L496 - L499 were not covered by tests
7 changes: 7 additions & 0 deletions mail_tracking/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ class ResConfigSettings(models.TransientModel):
related="company_id.mail_tracking_show_aliases",
readonly=False,
)
enable_old_mail_tracking_email_deletion = fields.Boolean(
"Enable deletion of old mail tracking records",
config_parameter="mail_tracking.enable_old_mail_tracking_email_deletion",
help="Enables the autovacuum to delete old mail tracking records to reduce "
"the database size. This sets an ir.config.parameter "
"mail_tracking.enable_old_mail_tracking_email_deletion",
)
12 changes: 7 additions & 5 deletions mail_tracking/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand All @@ -9,10 +8,11 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -490,7 +490,9 @@ <h3><a class="toc-backref" href="#toc-entry-9">Images</a></h3>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-10">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
80 changes: 80 additions & 0 deletions mail_tracking/tests/test_gc_mail_tracking_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import fields
from odoo.tests.common import SavepointCase


class TestMailTrackingEmailCleanUp(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.settings = cls.env["res.config.settings"].create(
{"enable_old_mail_tracking_email_deletion": True}
)
cls.settings.execute()
cls.partner = cls.env.ref("base.res_partner_address_28")
cls.message = cls.env["mail.message"].create(
{
"model": "res.partner",
"res_id": cls.partner.id,
"body": "TEST",
"message_type": "email",
"subtype_id": cls.env.ref("mail.mt_comment").id,
"author_id": cls.partner.id,
"date": "2024-03-26",
}
)
cls.recent_mail_tracking_email = cls.env["mail.tracking.email"].create(
{"mail_message_id": cls.message.id}
)
# Can't set the write_date directly as it gets overwritten by the ORM
cls.old_mail_tracking_email = cls.env["mail.tracking.email"].create(
{"mail_message_id": cls.message.id}
)
cls.total_count = 2
cls.recent_count = 1
cls.domain = [
("mail_message_id", "=", cls.message.id),
]

def _set_write_date(self):
# Set the write_date of the old record to be older than the max_age_days
# Update DB directly to avoid ORM overwriting the write_date
old_write_date = fields.Datetime.subtract(fields.Datetime.now(), days=200)
self.env.cr.execute(
"UPDATE mail_tracking_email SET write_date = %s WHERE id = %s",
(old_write_date, self.old_mail_tracking_email.id),
)

def test_deletion_of_mail_tracking_email(self):
self._set_write_date()
self.assertEqual(
len(self.env["mail.tracking.email"].search(self.domain)), self.total_count
)
self.env["mail.tracking.email"]._gc_mail_tracking_email()
self.assertEqual(
len(self.env["mail.tracking.email"].search(self.domain)), self.recent_count
)
self.assertTrue(self.recent_mail_tracking_email.exists())

def test_deletion_follows_configuration_variable(self):
self._set_write_date()
self.assertEqual(
len(self.env["mail.tracking.email"].search(self.domain)), self.total_count
)
# when disabled, no deletions should happen
self.settings.enable_old_mail_tracking_email_deletion = False
self.settings.execute()
self.env["mail.tracking.email"]._gc_mail_tracking_email()
self.assertEqual(
len(self.env["mail.tracking.email"].search(self.domain)), self.total_count
)
# when enabled, deletions should happen
self.settings.enable_old_mail_tracking_email_deletion = True
self.settings.execute()
self.env["mail.tracking.email"]._gc_mail_tracking_email()
self.assertEqual(
len(self.env["mail.tracking.email"].search(self.domain)), self.recent_count
)
22 changes: 22 additions & 0 deletions mail_tracking/views/res_config_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@
</div>
</div>
</div>
<div
class="col-12 col-lg-6 o_setting_box"
id="mail_tracking_deletion_job_settings"
>
<div class="o_setting_left_pane">
<field name="enable_old_mail_tracking_email_deletion" />
</div>
<div class="o_setting_right_pane">
<div class="content-group">
<label
for="enable_old_mail_tracking_email_deletion"
string="Enable deletion of old mail tracking records"
/>
<div
class="text-muted"
id="enable_old_mail_tracking_email_deletion"
>
Scheduled job to delete old mail tracking records to reduce the database size.
</div>
</div>
</div>
</div>
</div>
</field>
</record>
Expand Down

0 comments on commit 36b36cf

Please sign in to comment.