<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


class UtmCampaign(models.Model):
    _inherit = 'utm.campaign'

    mailing_ids = fields.One2many(
        'mailing.mailing', 'campaign_id',
        string='Mass Mailings')
    mailing_clicks_ratio = fields.Integer(default=0, compute="_compute_mailing_clicks_ratio", string="Number of clicks")
    mailing_items = fields.Integer(compute="_compute_mailing_items", string='Mailings')
    mailing_clicked = fields.Integer(compute="_compute_mailing_items", string='Mailings Clicked')
    # stat fields
    total = fields.Integer(compute="_compute_statistics")
    scheduled = fields.Integer(compute="_compute_statistics")
    failed = fields.Integer(compute="_compute_statistics")
    ignored = fields.Integer(compute="_compute_statistics")
    sent = fields.Integer(compute="_compute_statistics", string="Sent Emails")
    delivered = fields.Integer(compute="_compute_statistics")
    opened = fields.Integer(compute="_compute_statistics")
    replied = fields.Integer(compute="_compute_statistics")
    bounced = fields.Integer(compute="_compute_statistics")
    received_ratio = fields.Integer(compute="_compute_statistics", string='Received Ratio')
    opened_ratio = fields.Integer(compute="_compute_statistics", string='Opened Ratio')
    replied_ratio = fields.Integer(compute="_compute_statistics", string='Replied Ratio')
    bounced_ratio = fields.Integer(compute="_compute_statistics", string='Bounced Ratio')

    @api.depends('mailing_items', 'mailing_clicked')
    def _compute_items_total(self):
        for campaign in self:
            campaign.items_total += campaign.mailing_items
            campaign.clicked_total += campaign.mailing_clicked

    def _compute_mailing_items(self):
        super(UtmCampaign, self)._compute_clicks_ratio()
        query = """SELECT trace.campaign_id AS campaign_id, COUNT(DISTINCT(trace.id)) AS items_total, COUNT(DISTINCT(click.mailing_trace_id)) AS clicked_total
                    FROM mailing_trace AS trace
                    LEFT OUTER JOIN link_tracker_click as click ON click.mailing_trace_id = trace.id
                    WHERE trace.campaign_id IN %s
                    GROUP BY trace.campaign_id """
        params = [tuple(self.ids)]
        self.env.cr.execute(query, params)
        clicked_data = self.env.cr.dictfetchall()
        mapped_data = {datum['campaign_id']: {'clicked_total': datum['clicked_total'], 'items_total': datum['items_total']} for datum in clicked_data}

        for campaign in self:
            campaign_items_values = mapped_data.get(campaign.id, {})
            campaign.mailing_items = campaign_items_values.get('items_total', 0)
            campaign.mailing_clicked = campaign_items_values.get('clicked_total', 0)

    @api.depends('mailing_items', 'mailing_clicked')
    def _compute_mailing_clicks_ratio(self):
        for campaign in self:
            campaign.mailing_clicks_ratio = campaign.mailing_clicked / campaign.mailing_items * 100 if campaign.mailing_items > 0 else 0

    def _compute_statistics(self):
        """ Compute statistics of the mass mailing campaign """
        self.env.cr.execute("""
            SELECT
                c.id as campaign_id,
                COUNT(s.id) AS total,<commit_msg>[FIX] mass_mailing: avoid computing statistics when creating campaigns<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


class UtmCampaign(models.Model):
    _inherit = 'utm.campaign'

    mailing_ids = fields.One2many(
        'mailing.mailing', 'campaign_id',
        string='Mass Mailings')
    mailing_clicks_ratio = fields.Integer(default=0, compute="_compute_mailing_clicks_ratio", string="Number of clicks")
    mailing_items = fields.Integer(compute="_compute_mailing_items", string='Mailings')
    mailing_clicked = fields.Integer(compute="_compute_mailing_items", string='Mailings Clicked')
    # stat fields
    total = fields.Integer(compute="_compute_statistics")
    scheduled = fields.Integer(compute="_compute_statistics")
    failed = fields.Integer(compute="_compute_statistics")
    ignored = fields.Integer(compute="_compute_statistics")
    sent = fields.Integer(compute="_compute_statistics", string="Sent Emails")
    delivered = fields.Integer(compute="_compute_statistics")
    opened = fields.Integer(compute="_compute_statistics")
    replied = fields.Integer(compute="_compute_statistics")
    bounced = fields.Integer(compute="_compute_statistics")
    received_ratio = fields.Integer(compute="_compute_statistics", string='Received Ratio')
    opened_ratio = fields.Integer(compute="_compute_statistics", string='Opened Ratio')
    replied_ratio = fields.Integer(compute="_compute_statistics", string='Replied Ratio')
    bounced_ratio = fields.Integer(compute="_compute_statistics", string='Bounced Ratio')

    @api.depends('mailing_items', 'mailing_clicked')
    def _compute_items_total(self):
        for campaign in self:
            campaign.items_total += campaign.mailing_items
            campaign.clicked_total += campaign.mailing_clicked

    def _compute_mailing_items(self):
        super(UtmCampaign, self)._compute_clicks_ratio()
        mapped_data = {}

        if self.ids:
            query = """SELECT trace.campaign_id AS campaign_id, COUNT(DISTINCT(trace.id)) AS items_total, COUNT(DISTINCT(click.mailing_trace_id)) AS clicked_total
                        FROM mailing_trace AS trace
                        LEFT OUTER JOIN link_tracker_click as click ON click.mailing_trace_id = trace.id
                        WHERE trace.campaign_id IN %s
                        GROUP BY trace.campaign_id """
            params = [tuple(self.ids)]
            self.env.cr.execute(query, params)
            clicked_data = self.env.cr.dictfetchall()
            mapped_data = {datum['campaign_id']: {'clicked_total': datum['clicked_total'], 'items_total': datum['items_total']} for datum in clicked_data}

        for campaign in self:
            campaign_items_values = mapped_data.get(campaign.id, {})
            campaign.mailing_items = campaign_items_values.get('items_total', 0)
            campaign.mailing_clicked = campaign_items_values.get('clicked_total', 0)

    @api.depends('mailing_items', 'mailing_clicked')
    def _compute_mailing_clicks_ratio(self):
        for campaign in self:
            campaign.mailing_clicks_ratio = campaign.mailing_clicked / campaign.mailing_items * 100 if campaign.mailing_items > 0 else 0

    def _compute_statistics(self):
        """ Compute statistics of the mass mailing campaign """
        self.env.cr.execute("""
            SELECT
                c.id as campaign_id,
                COUNT(s.id) AS total,
{'fraction_non_alphanumeric': 0.10389988358556461, 'fraction_numerical': 0.002910360884749709, 'mean_word_length': 3.6659877800407332, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5810346', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1933, 'n_words': 391}