-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] auditlog - Auto-vacuum logs, HTTP requests and HTTP user sessions
- Loading branch information
Showing
8 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ | |
from . import http_session | ||
from . import http_request | ||
from . import log | ||
from . import autovacuum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |