File size: 6,687 Bytes
36a5576
4088a7c
 
 
 
 
 
 
 
 
 
 
 
 
36a5576
 
 
 
 
 
 
 
 
 
 
 
 
 
4088a7c
36a5576
 
 
 
 
 
4088a7c
 
 
 
 
 
 
36a5576
 
4088a7c
 
 
 
 
 
 
 
 
 
 
 
36a5576
 
 
 
4088a7c
 
36a5576
d933a8b
36a5576
 
 
4088a7c
36a5576
 
 
 
 
d933a8b
36a5576
 
 
 
4088a7c
36a5576
4088a7c
36a5576
d933a8b
36a5576
 
 
 
 
d933a8b
36a5576
d933a8b
36a5576
 
4088a7c
 
 
 
 
 
 
 
36a5576
 
 
4088a7c
 
 
d933a8b
4088a7c
 
 
36a5576
d933a8b
36a5576
d933a8b
36a5576
 
4088a7c
36a5576
 
d933a8b
36a5576
 
 
 
 
 
 
 
 
 
 
 
 
 
4088a7c
 
 
 
36a5576
 
 
 
4088a7c
 
 
d933a8b
4088a7c
 
 
 
36a5576
d933a8b
4088a7c
36a5576
 
 
 
 
 
 
 
 
 
 
 
4088a7c
 
 
36a5576
4088a7c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// ===== Screen 8: Volume & Revenue Drivers =====

function fetchPageData() {
    const filters = getFilterState();

    fetch('/diagnostics/api/diagnostics/data', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(filters)
    })
    .then(r => r.json())
    .then(data => {
        renderKPICards('diagKpiGrid', data.kpis);

        // Section 0: Marketing ROI Scorecard Table
        renderRoiScorecard(data.tables.marketing_roi_scorecard);

        // Section 1: Revenue Decomposition
        renderChart('chart_decomposition', data.charts.chart_decomposition);

        // Section 2: Volume Contribution by Period + Donut + Support Table
        renderChart('chart_vol_contribution', data.charts.chart_vol_contribution);
        renderChart('chart_base_incremental', data.charts.chart_base_incremental);
        renderSupportContribTable(data.tables.support_contribution);

        // Section 3: Investment Mix + Efficiency Trend
        renderChart('chart_inv_mix',          data.charts.chart_inv_mix);
        renderChart('chart_efficiency_trend', data.charts.chart_efficiency_trend);

        // Section 4: Contribution Waterfall + Period Change
        renderChart('chart_contribution', data.charts.chart_contribution);
        renderChart('chart_waterfall',    data.charts.chart_waterfall);

        // Section 5: Seasonality + Summary Table
        renderChart('chart_seasonality', data.charts.chart_seasonality);
        renderOverallSummary(data.tables.overall_summary);

        updateTimestamp();
    })
    .catch(err => console.error('Error loading diagnostics data:', err));
}


// ---- helpers ----
function changeClass(val) {
    if (val > 0) return 'positive';
    if (val < 0) return 'negative';
    return 'neutral';
}

function changeArrow(val) {
    if (val > 0) return '▲';
    if (val < 0) return '▼';
    return '—';
}


// ---- Section 0: Marketing ROI Scorecard Table ----
function renderRoiScorecard(rows) {
    const tbody = document.getElementById('roiScorecardBody');
    if (!tbody || !rows) return;

    if (rows.length === 0) {
        tbody.innerHTML = '<tr><td colspan="10" style="text-align:center;color:#737373;">No data available</td></tr>';
        return;
    }

    tbody.innerHTML = rows.map(r => {
        const invCls  = changeClass(r.inv_chg);
        const roiCls  = changeClass(r.roi_chg);
        const effCls  = changeClass(r.efficiency_cur - r.efficiency_prev);
        const isTotal = r.driver === 'Total Marketing';

        const rowStyle  = isTotal ? 'style="font-weight:700;border-top:2px solid rgba(255,255,255,0.12);background:rgba(255,255,255,0.03);"' : '';
        const nameStyle = `style="font-weight:${isTotal ? '700' : '600'};color:${r.color};"`;

        return `<tr ${rowStyle}>
            <td ${nameStyle}>
                <i data-feather="${r.icon}" style="width:14px;height:14px;display:inline;vertical-align:middle;margin-right:6px;"></i>
                ${r.driver}
            </td>
            <td class="num">${r.inv_cur_fmt}</td>
            <td class="num" style="color:#737373;">${r.inv_prev_fmt}</td>
            <td class="num ${invCls}">
                <span class="change-badge ${invCls}">${changeArrow(r.inv_chg)} ${Math.abs(r.inv_chg).toFixed(1)}%</span>
            </td>
            <td class="num">${r.vol_uplift_pct.toFixed(1)}%</td>
            <td class="num ${effCls}" style="font-weight:600;">${r.efficiency_cur_fmt}</td>
            <td class="num" style="color:#737373;">${r.efficiency_prev_fmt}</td>
            <td class="num ${roiCls}" style="font-weight:600;">${r.roi_cur_fmt}</td>
            <td class="num" style="color:#737373;">${r.roi_prev_fmt}</td>
            <td class="num ${roiCls}">
                <span class="change-badge ${roiCls}">${changeArrow(r.roi_chg)} ${Math.abs(r.roi_chg).toFixed(1)}%</span>
            </td>
        </tr>`;
    }).join('');

    if (typeof feather !== 'undefined') feather.replace();
}


// ---- Section 2: Support vs Contribution Table ----
function renderSupportContribTable(rows) {
    const tbody = document.getElementById('supportContribBody');
    if (!tbody || !rows) return;

    if (rows.length === 0) {
        tbody.innerHTML = '<tr><td colspan="7" style="text-align:center;color:#737373;">No data available</td></tr>';
        return;
    }

    const catColors = {
        'Media':  '#3987e5',
        'Promo':  '#22c55e',
        'Base':   '#737373',
    };

    tbody.innerHTML = rows.map(r => {
        const catColor = catColors[r.category] || '#9ca3af';
        const isBase   = r.category === 'Base';
        const rowStyle = isBase ? 'style="color:#737373;"' : '';

        return `<tr ${rowStyle}>
            <td style="font-weight:600;">${r.driver}</td>
            <td>
                <span style="padding:2px 8px;border-radius:4px;font-size:11px;font-weight:600;
                             background:${catColor}22;color:${catColor};">
                    ${r.category}
                </span>
            </td>
            <td class="num">${r.support}</td>
            <td class="num" style="font-weight:600;">${r.rev_contribution}</td>
            <td class="num">${r.vol_contribution}</td>
            <td class="num">${r.efficiency}</td>
            <td class="num">${r.pct_of_total}</td>
        </tr>`;
    }).join('');
}


// ---- Section 5: Overall KPI Summary Table ----
function renderOverallSummary(rows) {
    const tbody = document.getElementById('overallSummaryBody');
    if (!tbody || !rows) return;

    if (rows.length === 0) {
        tbody.innerHTML = '<tr><td colspan="5" style="text-align:center;color:#737373;">No data available</td></tr>';
        return;
    }

    tbody.innerHTML = rows.map(r => {
        const cls = changeClass(r.change_pct);
        const indent = r.indent ? 'style="padding-left: 28px; color: #737373;"' : '';
        return `<tr>
            <td ${indent}>
                <i data-feather="${r.icon}" style="width:14px;height:14px;display:inline;vertical-align:middle;margin-right:6px;"></i>
                ${r.metric}
            </td>
            <td class="num" style="font-weight:600;">${r.current_fmt}</td>
            <td class="num">${r.previous_fmt}</td>
            <td class="num ${cls}">${r.change_abs_fmt}</td>
            <td class="num ${cls}">
                <span class="change-badge ${cls}">
                    ${changeArrow(r.change_pct)} ${Math.abs(r.change_pct).toFixed(1)}%
                </span>
            </td>
        </tr>`;
    }).join('');

    if (typeof feather !== 'undefined') feather.replace();
}

document.addEventListener('DOMContentLoaded', fetchPageData);