Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageSplitterHooks
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getActiveInstrumentBucket
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 createInstrumentation
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getPageHash
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace WikimediaEvents\PageSplitter;
4
5use MediaWiki\Config\Config;
6
7/**
8 * Hooks used for PageSplitter-related logging
9 */
10class PageSplitterHooks {
11    private const IS_CONTROL = 1;
12    private const IS_TREATMENT = 2;
13    private const IS_UNSAMPLED = 3;
14
15    /** @var Config */
16    private $config;
17
18    /**
19     * @param Config $config
20     */
21    public function __construct( Config $config ) {
22        $this->config = $config;
23    }
24
25    /**
26     * Whether page is sampled, A/B test is active, and page is in the treatment bucket.
27     * @param int $pageId
28     * @return int One of IS_CONTROL, IS_TREATMENT or IS_UNSAMPLED
29     */
30    private function getActiveInstrumentBucket( int $pageId ): int {
31        $instrument = $this->createInstrumentation();
32        $pageHash = $this->getPageHash( $pageId );
33
34        if ( !$instrument->isSampled( $pageHash ) ) {
35            return self::IS_UNSAMPLED;
36        }
37        return (
38            $instrument->getBucket( $pageHash ) === $this->config->get( 'WMEPageSchemaSplitTestTreatment' )
39        ) ? self::IS_TREATMENT : self::IS_CONTROL;
40    }
41
42    /**
43     * @return PageSplitterInstrumentation
44     */
45    private function createInstrumentation(): PageSplitterInstrumentation {
46        $samplingRatio = $this->config->get( 'WMEPageSchemaSplitTestSamplingRatio' );
47        $buckets = $this->config->get( 'WMEPageSchemaSplitTestBuckets' );
48        return new PageSplitterInstrumentation( $samplingRatio, $buckets );
49    }
50
51    /**
52     * @param int $pageId
53     * @return float
54     */
55    private function getPageHash( int $pageId ): float {
56        $lookup = new PageHashGenerate();
57        return $lookup->getPageHash( $pageId );
58    }
59}