Skip to content
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

[IMP] mail_tracking: email score performance #299

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mail_tracking/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Email tracking",
"summary": "Email tracking system for all mails sent",
"version": "11.0.1.0.0",
"version": "11.0.1.1.0",
"category": "Social Network",
"website": "http://github.com/OCA/social",
"author": "Tecnativa, "
Expand Down
22 changes: 15 additions & 7 deletions mail_tracking/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ def email_is_bounced(self, email):

@api.model
def email_score_from_email(self, email):
if email:
return self.search([
('recipient_address', '=', email.lower())]).email_score()
return 0.
if not email:
return 0.
data = self.read_group([('recipient_address', '=', email.lower())],
['recipient_address', 'state'], ['state'])
mapped_data = dict([(state['state'], state['state_count'])
for state in data])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the surrounding [...] brackets.

With brackets:

  1. Produce a lazy generator.
  2. Exhaust the generator converting it into a list.
  3. Convert the list in a dict.

Without brackets:

  1. Produce a lazy generator.
  2. Exhaust the generator converting it into a dict.

You can save tons of loops! 😊


Note that another idiomatic way (equivalent to doing it without brackets) to do so is:

mapped_data = {state['state']: state['state_count'] for state in data}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted and fixed 😉

Full coverage as well.

return self.with_context(mt_states=mapped_data).email_score()

@api.model
def _email_score_weights(self):
Expand All @@ -124,16 +127,21 @@ def _email_score_weights(self):

def email_score(self):
"""Default email score algorimth. Ready to be inherited

It can receive a recordset or mapped states dictionary via context.
Must return a value beetwen 0.0 and 100.0
- Bad reputation: Value between 0 and 50.0
- Unknown reputation: Value 50.0
- Good reputation: Value between 50.0 and 100.0
"""
weights = self._email_score_weights()
score = 50.0
for tracking in self:
score += weights.get(tracking.state, 0.0)
states = self.env.context.get('mt_states', False)
if states:
for state in states.keys():
score += weights.get(state, 0.0) * states[state]
else:
for tracking in self:
score += weights.get(tracking.state, 0.0)
if score > 100.0:
score = 100.0
elif score < 0.0:
Expand Down