Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateMetrics
0.00% covered (danger)
0.00%
0 / 22
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 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 initServices
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace GrowthExperiments\Maintenance;
4
5use GrowthExperiments\GrowthExperimentsServices;
6use GrowthExperiments\PeriodicMetrics\MetricsFactory;
7use GrowthExperiments\Util;
8use InvalidArgumentException;
9use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
10use MediaWiki\Maintenance\Maintenance;
11
12$IP = getenv( 'MW_INSTALL_PATH' );
13if ( $IP === false ) {
14    $IP = __DIR__ . '/../../..';
15}
16require_once "$IP/maintenance/Maintenance.php";
17
18class UpdateMetrics extends Maintenance {
19
20    /** @var StatsdDataFactoryInterface */
21    private $statsDataFactory;
22
23    /** @var MetricsFactory */
24    private $metricsFactory;
25
26    public function __construct() {
27        parent::__construct();
28        $this->requireExtension( 'GrowthExperiments' );
29
30        $this->addDescription( 'Push calculated metrics to StatsD' );
31        $this->addOption( 'verbose', 'Output values of metrics calculated' );
32    }
33
34    /**
35     * Init all services
36     */
37    private function initServices(): void {
38        $services = $this->getServiceContainer();
39
40        $this->statsDataFactory = $services->getPerDbNameStatsdDataFactory();
41        $this->metricsFactory = GrowthExperimentsServices::wrap( $services )
42            ->getMetricsFactory();
43    }
44
45    /**
46     * @inheritDoc
47     */
48    public function execute() {
49        $this->initServices();
50
51        foreach ( MetricsFactory::METRICS as $metricName ) {
52            try {
53                $metric = $this->metricsFactory->newMetric( $metricName );
54            } catch ( InvalidArgumentException $e ) {
55                $this->error( 'ERROR: Metric "' . $metricName . '" failed to be constructed' );
56                Util::logException( $e );
57                continue;
58            }
59
60            $metricValue = $metric->calculate();
61            $this->statsDataFactory->gauge(
62                $metric->getStatsdKey(),
63                $metricValue
64            );
65
66            if ( $this->hasOption( 'verbose' ) ) {
67                $this->output( $metricName . ' is ' . $metricValue . '.' . PHP_EOL );
68            }
69        }
70    }
71}
72
73$maintClass = UpdateMetrics::class;
74require_once RUN_MAINTENANCE_IF_MAIN;