Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.31% covered (warning)
79.31%
23 / 29
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateMetrics
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 initServices
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace MediaWiki\Extension\MediaModeration\Maintenance;
4
5use InvalidArgumentException;
6use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
7use MediaWiki\Extension\MediaModeration\PeriodicMetrics\MediaModerationMetricsFactory;
8use MediaWiki\Logger\LoggerFactory;
9use MediaWiki\Maintenance\Maintenance;
10use Psr\Log\LoggerInterface;
11
12$IP = getenv( 'MW_INSTALL_PATH' );
13if ( $IP === false ) {
14    $IP = __DIR__ . '/../../..';
15}
16require_once "$IP/maintenance/Maintenance.php";
17
18/**
19 * Pushes information about the status of scanning to statsd.
20 * Copied, with modification, from GrowthExperiments maintenance/updateMetrics.php
21 */
22class UpdateMetrics extends Maintenance {
23
24    private StatsdDataFactoryInterface $statsDataFactory;
25    private MediaModerationMetricsFactory $mediaModerationMetricsFactory;
26    private LoggerInterface $logger;
27
28    public function __construct() {
29        parent::__construct();
30        $this->requireExtension( 'MediaModeration' );
31
32        $this->addDescription( 'Push calculated metrics to StatsD' );
33        $this->addOption( 'verbose', 'Output values of metrics calculated. Default is to not output.' );
34    }
35
36    /**
37     * Initialise dependencies (services and logger).
38     * This method is protected to allow mocking.
39     */
40    protected function initServices(): void {
41        $this->statsDataFactory = $this->getServiceContainer()->getPerDbNameStatsdDataFactory();
42        $this->mediaModerationMetricsFactory = $this->getServiceContainer()->get( 'MediaModerationMetricsFactory' );
43        $this->logger = LoggerFactory::getInstance( 'mediamoderation' );
44    }
45
46    /** @inheritDoc */
47    public function execute() {
48        $this->initServices();
49
50        foreach ( MediaModerationMetricsFactory::METRICS as $metricName ) {
51            try {
52                $metric = $this->mediaModerationMetricsFactory->newMetric( $metricName );
53            } catch ( InvalidArgumentException $_ ) {
54                $this->error( 'ERROR: Metric "' . $metricName . '" failed to be constructed' );
55                $this->logger->error(
56                    'Metric {metric_name} failed to be constructed.', [ 'metric_name' => $metricName ]
57                );
58                continue;
59            }
60
61            $metricValue = $metric->calculate();
62            $this->statsDataFactory->gauge(
63                $metric->getStatsdKey(),
64                $metricValue
65            );
66
67            if ( $this->hasOption( 'verbose' ) ) {
68                $this->output( $metric->getStatsdKey() . ' is ' . $metricValue . '.' . PHP_EOL );
69            }
70        }
71    }
72}
73
74$maintClass = UpdateMetrics::class;
75require_once RUN_MAINTENANCE_IF_MAIN;