Skip to content

Commit

Permalink
[IMP] mass_mailing; speed up kanban view
Browse files Browse the repository at this point in the history
The kanban view displays mailing statistics for all mailings that are
sending or done. To compute these statistics, it uses the `total` as the
expected number of emails to send. This `total` used to be computed as
the number of records matching the mailing filter.
This is both slow (filters are seldom written in an optimized way), and
incorrect because the recipients were frozen when the email was sent.
The number of matching records may have changed since then.

To fix both problems, introduce a new `expected` computed field that
simply returns the number of emails that were put in queue at the time
of sending, and thus represent the real number of expected emails.
This field is computed along with the rest of the statistics.
  • Loading branch information
odony committed Jul 4, 2018
1 parent c1de5b8 commit a5edf6e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions addons/mass_mailing/models/mass_mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ def default_get(self, fields):
statistics_ids = fields.One2many('mail.mail.statistics', 'mass_mailing_id', string='Emails Statistics')
total = fields.Integer(compute="_compute_total")
scheduled = fields.Integer(compute="_compute_statistics")
expected = fields.Integer(compute="_compute_statistics")
sent = fields.Integer(compute="_compute_statistics")
delivered = fields.Integer(compute="_compute_statistics")
opened = fields.Integer(compute="_compute_statistics")
Expand Down Expand Up @@ -433,7 +434,7 @@ def _compute_statistics(self):
self.env.cr.execute("""
SELECT
m.id as mailing_id,
COUNT(s.id) AS total,
COUNT(s.id) AS expected,
COUNT(CASE WHEN s.sent is not null THEN 1 ELSE null END) AS sent,
COUNT(CASE WHEN s.scheduled is not null AND s.sent is null AND s.exception is null THEN 1 ELSE null END) AS scheduled,
COUNT(CASE WHEN s.scheduled is not null AND s.sent is null AND s.exception is not null THEN 1 ELSE null END) AS failed,
Expand All @@ -453,7 +454,7 @@ def _compute_statistics(self):
m.id
""", (tuple(self.ids), ))
for row in self.env.cr.dictfetchall():
total = row.pop('total') or 1
total = row['expected'] or 1
row['received_ratio'] = 100.0 * row['delivered'] / total
row['opened_ratio'] = 100.0 * row['opened'] / total
row['replied_ratio'] = 100.0 * row['replied'] / total
Expand Down
4 changes: 2 additions & 2 deletions addons/mass_mailing/views/mass_mailing_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@
<field name="arch" type="xml">
<kanban>
<field name='color'/>
<field name='total'/>
<field name='expected'/>
<field name='failed'/>
<field name='sent_date'/>
<templates>
Expand Down Expand Up @@ -616,7 +616,7 @@
</div>
</div>
<div class='o_kanban_primary_bottom'>
<field name="delivered" widget="progressbar" title="Delivered" options="{'current_value': 'delivered', 'max_value': 'total', 'editable': false}"/>
<field name="delivered" widget="progressbar" title="Delivered" options="{'current_value': 'delivered', 'max_value': 'expected', 'editable': false}"/>
</div>
</t>
</div>
Expand Down

0 comments on commit a5edf6e

Please sign in to comment.