| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| declare(strict_types=1); |
|
|
| namespace Piwik\Plugins\BotTracking\DataTable; |
|
|
| use InvalidArgumentException; |
| use Piwik\DataTable; |
| use Piwik\DataTable\DataTableInterface; |
| use Piwik\Plugins\BotTracking\Columns\Metrics\DiscrepancyScore; |
| use Piwik\Plugins\BotTracking\Metrics; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class FavouredPagesScorer |
| { |
| |
| private $variant; |
|
|
| public function __construct(string $variant) |
| { |
| if ( |
| $variant !== DiscrepancyScore::VARIANT_HUMAN_FAVOURED |
| && $variant !== DiscrepancyScore::VARIANT_AI_FAVOURED |
| ) { |
| throw new InvalidArgumentException('Unknown DiscrepancyScore variant: ' . $variant); |
| } |
| $this->variant = $variant; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function addScores(DataTableInterface $table): void |
| { |
| if ($table instanceof DataTable\Map) { |
| foreach ($table->getDataTables() as $childTable) { |
| $this->addScores($childTable); |
| } |
| return; |
| } |
|
|
| if (!$table instanceof DataTable) { |
| return; |
| } |
|
|
| $strongColumn = $this->variant === DiscrepancyScore::VARIANT_HUMAN_FAVOURED |
| ? Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS |
| : Metrics::COLUMN_AI_CHATBOT_REQUESTS; |
|
|
| |
| |
| $maxStrong = 0; |
| foreach ($table->getRows() as $row) { |
| if ($row->isSummaryRow()) { |
| continue; |
| } |
| $value = (int) $row->getColumn($strongColumn); |
| if ($value > $maxStrong) { |
| $maxStrong = $value; |
| } |
| } |
|
|
| foreach ($table->getRows() as $row) { |
| |
| if ($row->isSummaryRow()) { |
| continue; |
| } |
|
|
| $human = (int) $row->getColumn(Metrics::COLUMN_UNIQUE_HUMAN_PAGEVIEWS); |
| $ai = (int) $row->getColumn(Metrics::COLUMN_AI_CHATBOT_REQUESTS); |
|
|
| if ($this->variant === DiscrepancyScore::VARIANT_HUMAN_FAVOURED) { |
| $strong = $human; |
| $weak = $ai; |
| } else { |
| $strong = $ai; |
| $weak = $human; |
| } |
|
|
| $row->setColumn(Metrics::COLUMN_DISCREPANCY_SCORE, self::score($strong, $weak, $maxStrong)); |
| } |
|
|
| |
| |
| |
| $ops = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); |
| if (!is_array($ops)) { |
| $ops = []; |
| } |
| $ops[Metrics::COLUMN_DISCREPANCY_SCORE] = 'skip'; |
| $table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $ops); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function score(int $strong, int $weak, int $maxStrong): float |
| { |
| $total = $strong + $weak; |
| if ($total <= 0) { |
| return 0.0; |
| } |
|
|
| $lean = max(0, ($strong - $weak) / $total); |
|
|
| $anchor = log10($maxStrong + 1); |
| $volume = $anchor > 0 ? log10($strong + 1) / $anchor : 0.0; |
|
|
| return round(100 * $lean * $volume, 1); |
| } |
| } |
|
|