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