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