mediawiki-extensions-Growth.../maintenance/updateMetrics.php
Martin Urbanec 13b7dcabdd Calculate mentorship-related metrics
Since the values of mentorship-related metrics
can change without any event within GE (mentors
stops being active, or number of registrations
change significantly), I decided to make use
of a maintenance script that will run daily
via a systemd timer, and push data into Grafana
when it does.

The same system can be used by other metrics
when appropriate, by adding a metric under
\GrowthExperiments\PeriodicMetrics and to
updateMetrics.php maintenance script.

Bug: T318684
Change-Id: Ie6c43d1877c5e06750487e982d00f091f00f4592
2022-11-03 22:12:18 +01:00

76 行
1.8 KiB
PHP

<?php
namespace GrowthExperiments\Maintenance;
use GrowthExperiments\GrowthExperimentsServices;
use GrowthExperiments\PeriodicMetrics\MetricsFactory;
use GrowthExperiments\Util;
use InvalidArgumentException;
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
use Maintenance;
use MediaWiki\MediaWikiServices;
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class UpdateMetrics extends Maintenance {
/** @var StatsdDataFactoryInterface */
private $statsDataFactory;
/** @var MetricsFactory */
private $metricsFactory;
public function __construct() {
parent::__construct();
$this->requireExtension( 'GrowthExperiments' );
$this->addDescription( 'Push calculated metrics to StatsD' );
$this->addOption( 'verbose', 'Output values of metrics calculated' );
}
/**
* Init all services
*/
private function initServices(): void {
$services = MediaWikiServices::getInstance();
$this->statsDataFactory = $services->getPerDbNameStatsdDataFactory();
$this->metricsFactory = GrowthExperimentsServices::wrap( $services )
->getMetricsFactory();
}
/**
* @inheritDoc
*/
public function execute() {
$this->initServices();
foreach ( MetricsFactory::METRICS as $metricName ) {
try {
$metric = $this->metricsFactory->newMetric( $metricName );
} catch ( InvalidArgumentException $e ) {
$this->error( 'ERROR: Metric "' . $metricName . '" failed to be constructed' );
Util::logException( $e );
continue;
}
$metricValue = $metric->calculate();
$this->statsDataFactory->gauge(
$metric->getStatsdKey(),
$metricValue
);
if ( $this->hasOption( 'verbose' ) ) {
$this->output( $metricName . ' is ' . $metricValue . '.' . PHP_EOL );
}
}
}
}
$maintClass = UpdateMetrics::class;
require_once RUN_MAINTENANCE_IF_MAIN;