Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InactiveMentorsMetric
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 calculate
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 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 GrowthExperiments\Mentorship\Provider\MentorProvider;
6use MediaWiki\User\UserEditTracker;
7use MediaWiki\User\UserIdentityLookup;
8use MediaWiki\Utils\MWTimestamp;
9use Wikimedia\LightweightObjectStore\ExpirationAwareness;
10
11class InactiveMentorsMetric implements IMetric {
12
13    /** @var int Number of seconds a mentor has to be without any edit to be inactive */
14    private const INACTIVITY_THRESHOLD = 30 * ExpirationAwareness::TTL_DAY;
15
16    /** @var UserEditTracker */
17    private $userEditTracker;
18
19    /** @var UserIdentityLookup */
20    private $userIdentityLookup;
21
22    /** @var MentorProvider */
23    private $mentorProvider;
24
25    /**
26     * @param UserEditTracker $userEditTracker
27     * @param UserIdentityLookup $userIdentityLookup
28     * @param MentorProvider $mentorProvider
29     */
30    public function __construct(
31        UserEditTracker $userEditTracker,
32        UserIdentityLookup $userIdentityLookup,
33        MentorProvider $mentorProvider
34    ) {
35        $this->userEditTracker = $userEditTracker;
36        $this->userIdentityLookup = $userIdentityLookup;
37        $this->mentorProvider = $mentorProvider;
38    }
39
40    /**
41     * @inheritDoc
42     */
43    public function calculate(): int {
44        $inactiveMentors = 0;
45        foreach ( $this->mentorProvider->getAutoAssignedMentors() as $mentorName ) {
46            $mentor = $this->userIdentityLookup->getUserIdentityByName( $mentorName );
47            if ( $mentor === null ) {
48                continue;
49            }
50
51            $lastEditTimestamp = $this->userEditTracker->getLatestEditTimestamp( $mentor );
52            $secondsSinceLastEdit = (int)MWTimestamp::now( TS_UNIX ) -
53                (int)MWTimestamp::getInstance( $lastEditTimestamp )->getTimestamp( TS_UNIX );
54
55            if ( $secondsSinceLastEdit > self::INACTIVITY_THRESHOLD ) {
56                $inactiveMentors++;
57            }
58        }
59        return $inactiveMentors;
60    }
61
62    /**
63     * @inheritDoc
64     */
65    public function getStatsdKey(): string {
66        return 'GrowthExperiments.Mentorship.InactiveMentors';
67    }
68}