Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewcomersByMentorMetric
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 calculate
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
6
 getStatsdKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\PeriodicMetrics;
4
5use Wikimedia\LightweightObjectStore\ExpirationAwareness;
6use Wikimedia\Rdbms\IReadableDatabase;
7
8class NewcomersByMentorMetric implements IMetric {
9
10    /** @var MetricsFactory */
11    private $metricsFactory;
12
13    /** @var IReadableDatabase */
14    private $dbr;
15
16    /**
17     * @param MetricsFactory $metricsFactory
18     * @param IReadableDatabase $dbr
19     */
20    public function __construct(
21        MetricsFactory $metricsFactory,
22        IReadableDatabase $dbr
23    ) {
24        $this->metricsFactory = $metricsFactory;
25        $this->dbr = $dbr;
26    }
27
28    /**
29     * @inheritDoc
30     */
31    public function calculate(): int {
32        $autoMentors = $this->metricsFactory
33            ->newMetric( AutoAssignedMentorsMetric::class )
34            ->calculate();
35
36        if ( $autoMentors === 0 ) {
37            // prevent a "Divison by zero" PHP Warning coming from $recentAccounts / $autoMentors
38            // below
39            return 0;
40        }
41
42        $recentAccounts = $this->dbr->newSelectQueryBuilder()
43            ->select( 'COUNT(*)' )
44            ->from( 'logging' )
45            ->where( [
46                'log_type' => 'newusers',
47                'log_action' => 'create',
48                $this->dbr->expr( 'log_timestamp', '>', $this->dbr->timestamp(
49                    (int)wfTimestamp() - ExpirationAwareness::TTL_MONTH
50                ) )
51            ] )
52            ->caller( __METHOD__ )
53            ->fetchField();
54
55        return (int)round(
56            $recentAccounts / $autoMentors
57        );
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function getStatsdKey(): string {
64        return 'GrowthExperiments.Mentorship.NewcomerByMentors';
65    }
66}