File size: 11,842 Bytes
78185c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

declare(strict_types=1);

namespace Piwik\Plugins\BotTracking\RecordBuilders;

use Piwik\ArchiveProcessor;
use Piwik\ArchiveProcessor\Record;
use Piwik\ArchiveProcessor\RecordBuilder;
use Piwik\Common;
use Piwik\Config\GeneralConfig;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Db;
use Piwik\Plugins\BotTracking\Archiver;
use Piwik\Plugins\BotTracking\Columns\Metrics\DiscrepancyScore;
use Piwik\Plugins\BotTracking\DataTable\FavouredPagesScorer;
use Piwik\Plugins\BotTracking\Metrics;
use Piwik\RankingQuery;
use Piwik\Tracker\Action;

/**
 * Builds the two flat blob records backing the Human-Favoured and AI-Favoured Pages reports, keyed by
 * page URL with `unique_human_pageviews`, `ai_chatbot_requests` and `discrepancy_score`.
 *
 * Both sides are aggregated from the log tables on the same `log_action.name`, so the union is an
 * exact label match. The 0–100 {@see DiscrepancyScore} is materialised per variant and used as the
 * truncation sort, so each record keeps the pages that rank highest for its own report.
 *
 * One record per variant (~2x storage) is needed because the score is variant-specific: a single
 * record truncated by one variant's score would drop the other variant's most-relevant rows.
 */
class AIChatbotFavouredPages extends RecordBuilder
{
    use AIChatbotPageMetricsTrait;

    /**
     * @var int
     */
    protected $maxRowsInTable;

    /**
     * @var int
     */
    private $rankingQueryLimit;

    public function __construct()
    {
        parent::__construct();

        $this->maxRowsInTable    = GeneralConfig::getIntegerConfigValue('datatable_archiving_maximum_rows_ai_chatbot_favoured_pages', 50000);
        $this->rankingQueryLimit = $this->getRankingQueryLimit($this->maxRowsInTable);
    }

    public function getRecordMetadata(ArchiveProcessor $archiveProcessor): array
    {
        $records = [];
        foreach ($this->variantByRecord() as $recordName => $variant) {
            // The discrepancy_score is table-relative (see FavouredPagesScorer), so it can't be summed
            // across child periods: sum the additive traffic, skip the score, then recompute it on the
            // aggregated union via the transform below. Core then truncates by the recomputed score.
            $records[] = Record::make(Record::TYPE_BLOB, $recordName)
                ->setColumnToSortByBeforeTruncation(Metrics::COLUMN_DISCREPANCY_SCORE)
                ->setBlobColumnAggregationOps([
                    Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS => 'sum',
                    Metrics::COLUMN_AI_CHATBOT_REQUESTS    => 'sum',
                    Metrics::COLUMN_DISCREPANCY_SCORE      => 'skip',
                ])
                ->setAggregatedRecordTransform(function (DataTable $table) use ($variant): void {
                    (new FavouredPagesScorer($variant))->addScores($table);
                });
        }

        return $records;
    }

    public function isEnabled(ArchiveProcessor $archiveProcessor): bool
    {
        // don't process reports for any segment (these reports declare no segment support)
        return $archiveProcessor->getParams()->getSegment()->isEmpty();
    }

    /**
     * Day archiving: build the human/AI union from the logs once, then materialise the per-variant
     * score on an independent copy for each record. Core truncates each by its score column.
     */
    protected function aggregate(ArchiveProcessor $archiveProcessor): array
    {
        // Only the request count is needed here; the shared query's other metrics are for the Content report.
        $aiTable = $this->queryPageOrDocumentUrls($archiveProcessor, Action::TYPE_PAGE_URL, $this->rankingQueryLimit, [Metrics::COLUMN_REQUESTS]);

        // With no AI chatbot page requests there is nothing to favour against, so skip the (expensive)
        // human-pageviews scan and store empty records.
        if ($aiTable->getRowsCount() === 0) {
            Common::destroy($aiTable);
            return $this->emptyRecords();
        }

        $humanTable = $this->queryHumanPageviews($archiveProcessor);

        $records = [];
        foreach ($this->variantByRecord() as $recordName => $variant) {
            // mergeHumanAndAiTables returns a fresh table each call, so scoring one variant cannot
            // affect the other.
            $table = self::mergeHumanAndAiTables($humanTable, $aiTable);
            (new FavouredPagesScorer($variant))->addScores($table);
            $records[$recordName] = $table;
        }

        Common::destroy($humanTable);
        Common::destroy($aiTable);

        return $records;
    }

    /**
     * @return array<string, DataTable> an empty record per variant
     */
    private function emptyRecords(): array
    {
        $records = [];
        foreach (array_keys($this->variantByRecord()) as $recordName) {
            $records[$recordName] = new DataTable();
        }

        return $records;
    }

    /**
     * Merges the human-pageviews and AI-requests tables (both keyed on `log_action.name`) into the
     * flat per-URL union: every URL on either side appears once, the missing side defaulting to 0.
     *
     * The two per-side truncation tails are combined into one "Others" summary row, left UNSCORED — a
     * discrepancy score over an aggregate of many URLs is meaningless — so the default exclude-low-
     * population filter hides it while it stays visible when that filter is off.
     *
     * Pure (DataTable in / out) so it is unit-testable without a database.
     */
    public static function mergeHumanAndAiTables(DataTable $humanTable, DataTable $aiTable): DataTable
    {
        $byLabel = [];

        // The AI table comes from the shared page-metrics query, which names the request count
        // COLUMN_REQUESTS (the Content report's column); read it here as this report's AI requests.
        foreach ($aiTable->getRows() as $row) {
            if ($row->isSummaryRow()) {
                continue;
            }
            $label = (string) $row->getColumn('label');
            if ($label === '') {
                continue;
            }
            $byLabel[$label] = [
                Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS => 0,
                Metrics::COLUMN_AI_CHATBOT_REQUESTS    => (int) $row->getColumn(Metrics::COLUMN_REQUESTS),
            ];
        }

        foreach ($humanTable->getRows() as $row) {
            if ($row->isSummaryRow()) {
                continue;
            }
            $label = (string) $row->getColumn('label');
            if ($label === '') {
                continue;
            }
            $human = (int) $row->getColumn(Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS);
            if (isset($byLabel[$label])) {
                $byLabel[$label][Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS] = $human;
            } else {
                $byLabel[$label] = [
                    Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS => $human,
                    Metrics::COLUMN_AI_CHATBOT_REQUESTS    => 0,
                ];
            }
        }

        $table = new DataTable();
        foreach ($byLabel as $label => $cols) {
            $table->addRow(new Row([
                Row::COLUMNS => [
                    'label'                                => (string) $label,
                    Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS => $cols[Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS],
                    Metrics::COLUMN_AI_CHATBOT_REQUESTS    => $cols[Metrics::COLUMN_AI_CHATBOT_REQUESTS],
                ],
            ]));
        }

        self::addCombinedOthersRow($table, $humanTable, $aiTable);

        return $table;
    }

    /**
     * Combines the human and AI tables' "Others" summary rows (the truncated tail of each side) into a
     * single summary row on $table, with no discrepancy_score (see mergeHumanAndAiTables). Does nothing
     * when neither side was truncated.
     */
    private static function addCombinedOthersRow(DataTable $table, DataTable $humanTable, DataTable $aiTable): void
    {
        $humanOthers = $humanTable->getSummaryRow();
        $aiOthers    = $aiTable->getSummaryRow();

        if (!$humanOthers instanceof Row && !$aiOthers instanceof Row) {
            return;
        }

        $label = ($humanOthers instanceof Row ? $humanOthers : $aiOthers)->getColumn('label') ?: DataTable::LABEL_SUMMARY_ROW;

        $summary = new Row([
            Row::COLUMNS => [
                'label'                                => $label,
                Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS => $humanOthers instanceof Row ? (int) $humanOthers->getColumn(Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS) : 0,
                Metrics::COLUMN_AI_CHATBOT_REQUESTS    => $aiOthers instanceof Row ? (int) $aiOthers->getColumn(Metrics::COLUMN_REQUESTS) : 0,
            ],
        ]);
        $summary->setIsSummaryRow();
        $table->addSummaryRow($summary);
    }

    /**
     * @return array<string, string> record name => DiscrepancyScore variant
     */
    private function variantByRecord(): array
    {
        return [
            Archiver::AI_CHATBOTS_HUMAN_FAVOURED_PAGES_RECORD => DiscrepancyScore::VARIANT_HUMAN_FAVOURED,
            Archiver::AI_CHATBOTS_AI_FAVOURED_PAGES_RECORD    => DiscrepancyScore::VARIANT_AI_FAVOURED,
        ];
    }

    /**
     * Human pageviews (distinct visits, matching Actions' page `nb_visits`) per page URL, keyed on
     * `log_action.name` to merge directly with the AI-side counts. The `idaction_event_category IS
     * NULL` clause mirrors the Actions Pages report: a URL seen only as an event context is not a
     * pageview, so it must not be counted here.
     */
    private function queryHumanPageviews(ArchiveProcessor $archiveProcessor): DataTable
    {
        $logAggregator = $archiveProcessor->getLogAggregator();
        $where         = $logAggregator->getWhereStatement('log_link_visit_action', 'server_time');
        $actionTable   = Common::prefixTable('log_action');
        $visitActions  = Common::prefixTable('log_link_visit_action');

        $innerSql = sprintf(
            "SELECT log_action.name AS url,
                    COUNT(DISTINCT log_link_visit_action.idvisit) AS %s
             FROM `%s` AS log_link_visit_action
             INNER JOIN `%s` AS log_action ON log_action.idaction = log_link_visit_action.idaction_url
             WHERE log_action.name IS NOT NULL
               AND log_action.name <> ''
               AND log_action.type = %d
               AND log_link_visit_action.idaction_event_category IS NULL
               AND %s
             GROUP BY log_action.name
             ORDER BY %s DESC",
            Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS,
            $visitActions,
            $actionTable,
            Action::TYPE_PAGE_URL,
            $where,
            Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS
        );

        $rankingQuery = new RankingQuery($this->rankingQueryLimit);
        $rankingQuery->addLabelColumn('url');
        $rankingQuery->addColumn(Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS, 'sum');

        $wrappedSql = $rankingQuery->generateRankingQuery($innerSql);

        $stmt = Db::query($wrappedSql, $logAggregator->getGeneralQueryBindParams());

        $table = new DataTable();
        while ($row = $stmt->fetch()) {
            /** @var array<string, int|string|null> $row */
            $label = (string) $row['url'];
            $raw   = $row[Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS] ?? 0;

            $table->sumRowWithLabel($label, [
                Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS => is_numeric($raw) ? (int) $raw : 0,
            ]);
        }

        return $table;
    }
}