-
-
Notifications
You must be signed in to change notification settings - Fork 622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.0][IMP][mail_tracking] Speed installation time and discard concurrent events #82
Changes from 1 commit
a7e9615
ab78e0c
2264297
36cab84
07a28ce
ef1d3af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
|
||
from . import models | ||
from . import controllers | ||
from .hooks import post_init_hook | ||
from .hooks import pre_init_hook |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
|
||
_logger = logging.getLogger(__name__) | ||
|
||
EVENT_OPEN_DELTA = 10 | ||
EVENT_CLICK_DELTA = 5 | ||
|
||
|
||
class MailTrackingEmail(models.Model): | ||
_name = "mail.tracking.email" | ||
|
@@ -104,7 +107,7 @@ def tracking_ids_recalculate(self, model, email_field, tracking_field, | |
obj.write({ | ||
tracking_field: [(5, False, False)] | ||
}) | ||
return True | ||
return objects | ||
|
||
@api.model | ||
def _tracking_ids_to_write(self, email): | ||
|
@@ -256,13 +259,41 @@ def _event_prepare(self, event_type, metadata): | |
_logger.info('Unknown event type: %s' % event_type) | ||
return False | ||
|
||
def _concurrent_events(self, event_type, metadata): | ||
m_event = self.env['mail.tracking.event'] | ||
self.ensure_one() | ||
concurrent_event_ids = False | ||
if event_type in {'open', 'click'}: | ||
ts = metadata.get('timestamp', time.time()) | ||
delta = EVENT_OPEN_DELTA if event_type == 'open' \ | ||
else EVENT_CLICK_DELTA | ||
domain = [ | ||
('timestamp', '>=', ts - delta), | ||
('timestamp', '<=', ts + delta), | ||
('tracking_email_id', '=', self.id), | ||
('event_type', '=', event_type), | ||
] | ||
if event_type == 'click': | ||
domain.append(('url', '=', metadata.get('url', False))) | ||
concurrent_event_ids = m_event.search(domain) | ||
return concurrent_event_ids | ||
|
||
@api.multi | ||
def event_create(self, event_type, metadata): | ||
event_ids = self.env['mail.tracking.event'] | ||
for tracking_email in self: | ||
vals = tracking_email._event_prepare(event_type, metadata) | ||
if vals: | ||
event_ids += event_ids.sudo().create(vals) | ||
other_ids = tracking_email._concurrent_events(event_type, metadata) | ||
if not other_ids: | ||
vals = tracking_email._event_prepare(event_type, metadata) | ||
if vals: | ||
event_ids += event_ids.sudo().create(vals) | ||
partners = self.tracking_ids_recalculate( | ||
'res.partner', 'email', 'tracking_email_ids', | ||
tracking_email.recipient_address) | ||
if partners: | ||
partners.email_score_calculate() | ||
else: | ||
_logger.info("Concurrent event '%s' discarded", event_type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, all of these messages as debug, not info |
||
return event_ids | ||
|
||
@api.model | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,12 @@ class ResPartner(models.Model): | |
string="Tracking emails count", store=True, readonly=True, | ||
compute="_compute_tracking_emails_count") | ||
email_score = fields.Float( | ||
string="Email score", | ||
compute="_compute_email_score", store=True, readonly=True) | ||
string="Email score", readonly=True, default=50.0) | ||
|
||
@api.one | ||
@api.depends('tracking_email_ids.state') | ||
def _compute_email_score(self): | ||
self.email_score = self.tracking_email_ids.email_score() | ||
@api.multi | ||
def email_score_calculate(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you remove the compute method? This way, you have the risk of not having updated the value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is causing a inter-block in PostgreSQL, because it's trying to read tracking email table inside the write of field state. Now email score calculation is made in event_create method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments added to remember why we changed compute method |
||
for partner in self: | ||
partner.email_score = partner.tracking_email_ids.email_score() | ||
|
||
@api.one | ||
@api.depends('tracking_email_ids') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,26 +3,14 @@ | |
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
import logging | ||
from openerp import api, SUPERUSER_ID | ||
from openerp.addons.mail_tracking.hooks import column_add_with_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protect the import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This addon depends on mail_tracking. I think this is not necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. This causes server doesn't start if you don't have both modules accessible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then should applies to every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's the question. Remember @yajo's PRs for protecting them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking about adding the protection directly in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you give me one sample as reference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to put:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def post_init_hook(cr, registry): | ||
with api.Environment.manage(): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
# Recalculate all mass_mailing contacts tracking_email_ids | ||
contacts = env['mail.mass_mailing.contact'].search([ | ||
('email', '!=', False), | ||
]) | ||
emails = contacts.mapped('email') | ||
_logger.info( | ||
"Recalculating 'tracking_email_ids' in " | ||
"'mail.mass_mailing.contact' model for %d email addresses", | ||
len(emails)) | ||
for n, email in enumerate(emails): | ||
env['mail.tracking.email'].tracking_ids_recalculate( | ||
'mail.mass_mailing.contact', 'email', 'tracking_email_ids', | ||
email) | ||
if n % 500 == 0: # pragma: no cover | ||
_logger.info(" Recalculated %d of %d", n, len(emails)) | ||
def pre_init_hook(cr): | ||
_logger.info("Creating mail.mass_mailing.contact.email_score column " | ||
"with value 50.0") | ||
column_add_with_value( | ||
cr, 'mail_mass_mailing_contact', 'email_score', 'double precision', | ||
50.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment that this is in seconds