'Compute statistics of the mass mailing campaign'

def _compute_statistics(self):

    self.env.cr.execute('\n                                                SELECT\n                                                                c.id    as    campaign_id,\n                                                                COUNT(s.id)    AS    total,\n                                                                COUNT(CASE    WHEN    s.sent    is    not    null    THEN    1    ELSE    null    END)    AS    sent,\n                                                                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,\n                                                                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,\n                                                                COUNT(CASE    WHEN    s.id    is    not    null    AND    s.bounced    is    null    THEN    1    ELSE    null    END)    AS    delivered,\n                                                                COUNT(CASE    WHEN    s.opened    is    not    null    THEN    1    ELSE    null    END)    AS    opened,\n                                                                COUNT(CASE    WHEN    s.replied    is    not    null    THEN    1    ELSE    null    END)    AS    replied    ,\n                                                                COUNT(CASE    WHEN    s.bounced    is    not    null    THEN    1    ELSE    null    END)    AS    bounced\n                                                FROM\n                                                                mail_mail_statistics    s\n                                                RIGHT    JOIN\n                                                                mail_mass_mailing_campaign    c\n                                                                ON    (c.id    =    s.mass_mailing_campaign_id)\n                                                WHERE\n                                                                c.id    IN    %s\n                                                GROUP    BY\n                                                                c.id\n                                ', (tuple(self.ids),))
    for row in self.env.cr.dictfetchall():
        total = (row['total'] or 1)
        row['delivered'] = (row['sent'] - row['bounced'])
        row['received_ratio'] = ((100.0 * row['delivered']) / total)
        row['opened_ratio'] = ((100.0 * row['opened']) / total)
        row['replied_ratio'] = ((100.0 * row['replied']) / total)
        row['bounced_ratio'] = ((100.0 * row['bounced']) / total)
        self.browse(row.pop('campaign_id')).update(row)
