- """ Compute statistics of the mass mailing campaign """
- results = dict.fromkeys(ids, False)
- for mass_mailing in self.browse(cr, uid, ids, context=context):
- results[mass_mailing.id] = {
- 'sent': len(mass_mailing.statistics_ids),
- 'delivered': len([stat for stat in mass_mailing.statistics_ids if not stat.bounced]), # mail.state == 'sent' and
- 'opened': len([stat for stat in mass_mailing.statistics_ids if stat.opened]),
- 'replied': len([stat for stat in mass_mailing.statistics_ids if stat.replied]),
- 'bounced': len([stat for stat in mass_mailing.statistics_ids if stat.bounced]),
+ """ Compute statistics of the mass mailing """
+ results = dict.fromkeys(ids, {
+ 'sent': 0,
+ 'delivered': 0,
+ 'opened': 0,
+ 'replied': 0,
+ 'bounced': 0,
+ })
+ cr.execute("""
+ SELECT
+ mass_mailing_id,
+ COUNT(id) AS sent,
+ COUNT(CASE WHEN bounced is null THEN 1 ELSE null END) AS delivered,
+ COUNT(CASE WHEN opened is not null THEN 1 ELSE null END) AS opened,
+ COUNT(CASE WHEN replied is not null THEN 1 ELSE null END) AS replied ,
+ COUNT(CASE WHEN bounced is not null THEN 1 ELSE null END) AS bounced
+ FROM
+ mail_mail_statistics
+ WHERE
+ mass_mailing_id IN %s
+ GROUP BY
+ mass_mailing_id
+ """, (tuple(ids), ))
+ for (campaign_id, sent, delivered, opened, replied, bounced) in cr.fetchall():
+ results[campaign_id] = {
+ 'sent': sent,
+ # delivered: shouldn't be: all mails - (failed + bounced) ?
+ 'delivered': delivered,
+ 'opened': opened,
+ 'replied': replied,
+ 'bounced': bounced,