Browse Source
Merge pull request #360 from osiell/8.0-auditlog-autovacuum
Merge pull request #360 from osiell/8.0-auditlog-autovacuum
[IMP] auditlog - Autovacuum logspull/394/head
Pedro M. Baeza
9 years ago
8 changed files with 126 additions and 10 deletions
-
21auditlog/README.rst
-
3auditlog/__openerp__.py
-
18auditlog/data/ir_cron.xml
-
1auditlog/models/__init__.py
-
41auditlog/models/autovacuum.py
-
3auditlog/models/log.py
-
1auditlog/tests/__init__.py
-
48auditlog/tests/test_autovacuum.py
@ -0,0 +1,18 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<openerp> |
|||
<data noupdate="1"> |
|||
|
|||
<record id="ir_cron_auditlog_autovacuum" model="ir.cron"> |
|||
<field name='name'>Auto-vacuum audit logs</field> |
|||
<field name='interval_number'>1</field> |
|||
<field name='interval_type'>days</field> |
|||
<field name="numbercall">-1</field> |
|||
<field name="active" eval="False"/> |
|||
<field name="doall" eval="False"/> |
|||
<field name="model">auditlog.autovacuum</field> |
|||
<field name="function">autovacuum</field> |
|||
<field name="args">(180,)</field> |
|||
</record> |
|||
|
|||
</data> |
|||
</openerp> |
@ -0,0 +1,41 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# © 2016 ABF OSIELL SARL, Sebastien Alix (http://osiell.com) |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
import logging |
|||
from datetime import datetime, timedelta |
|||
|
|||
from openerp import models, fields, api |
|||
|
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
class AuditlogAutovacuum(models.TransientModel): |
|||
_name = 'auditlog.autovacuum' |
|||
_description = "Auditlog - Delete old logs" |
|||
|
|||
@api.model |
|||
def autovacuum(self, days): |
|||
"""Delete all logs older than ``days``. This includes: |
|||
- CRUD logs (create, read, write, unlink) |
|||
- HTTP requests |
|||
- HTTP user sessions |
|||
|
|||
Called from a cron. |
|||
""" |
|||
days = (days > 0) and int(days) or 0 |
|||
deadline = datetime.now() - timedelta(days=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))]) |
|||
nb_records = len(records) |
|||
records.unlink() |
|||
_logger.info( |
|||
u"AUTOVACUUM - %s '%s' records deleted", |
|||
nb_records, data_model) |
|||
return True |
@ -0,0 +1,48 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# © 2016 ABF OSIELL SARL, Sebastien Alix (http://osiell.com) |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
import time |
|||
|
|||
from openerp.tests.common import TransactionCase |
|||
|
|||
|
|||
class TestAuditlogAutovacuum(TransactionCase): |
|||
|
|||
def setUp(self): |
|||
super(TestAuditlogAutovacuum, self).setUp() |
|||
self.groups_model_id = self.env.ref('base.model_res_groups').id |
|||
self.groups_rule = self.env['auditlog.rule'].create({ |
|||
'name': 'testrule for groups', |
|||
'model_id': self.groups_model_id, |
|||
'log_read': True, |
|||
'log_create': True, |
|||
'log_write': True, |
|||
'log_unlink': True, |
|||
'state': 'subscribed', |
|||
'log_type': 'full', |
|||
}) |
|||
|
|||
def tearDown(self): |
|||
self.groups_rule.unlink() |
|||
super(TestAuditlogAutovacuum, self).tearDown() |
|||
|
|||
def test_autovacuum(self): |
|||
log_model = self.env['auditlog.log'] |
|||
autovacuum_model = self.env['auditlog.autovacuum'] |
|||
group = self.env['res.groups'].create({ |
|||
'name': 'testgroup1', |
|||
}) |
|||
nb_logs = log_model.search_count([ |
|||
('model_id', '=', self.groups_model_id), |
|||
('res_id', '=', group.id), |
|||
]) |
|||
self.assertGreater(nb_logs, 0) |
|||
# Milliseconds are ignored by autovacuum, waiting 1s ensure that |
|||
# the logs generated will be processed by the vacuum |
|||
time.sleep(1) |
|||
autovacuum_model.autovacuum(days=0) |
|||
nb_logs = log_model.search_count([ |
|||
('model_id', '=', self.groups_model_id), |
|||
('res_id', '=', group.id), |
|||
]) |
|||
self.assertEqual(nb_logs, 0) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue