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