wiki.techinc.nl/includes/libs/WRStats/MetricSpec.php
Timo Tijhof d1ff49d27d WRStats: Improve various docs
Change-Id: I3c3e9c155cedc347a600cad16301c484e50b6f90
2023-09-21 13:11:47 -07:00

52 lines
1.3 KiB
PHP

<?php
namespace Wikimedia\WRStats;
/**
* Class representation of normalized metric specifications
*
* @internal
*/
class MetricSpec {
/** The default (value axis) resolution */
public const DEFAULT_RESOLUTION = 1;
/** @var string */
public $type;
/** @var float|int */
public $resolution;
/** @var array<string,SequenceSpec> Sequences in ascending order of expiry */
public $sequences;
/**
* @param array $spec
*/
public function __construct( array $spec ) {
$this->type = $spec['type'] ?? 'counter';
$this->resolution = $spec['resolution'] ?? self::DEFAULT_RESOLUTION;
foreach ( [ 'timeStep', 'expiry' ] as $var ) {
if ( isset( $spec[$var] ) ) {
throw new WRStatsError( "$var must be in the sequences array" );
}
}
$seqArrays = $spec['sequences'] ?? [];
if ( !$seqArrays ) {
$seqArrays = [ [] ];
}
$sequences = [];
foreach ( $seqArrays as $i => $seqArray ) {
if ( !is_array( $seqArray ) ) {
throw new WRStatsError( 'sequences must be an array of arrays' );
}
$seqSpec = new SequenceSpec( $seqArray );
while ( isset( $sequences[$seqSpec->name] ) ) {
$seqSpec->name .= "s$i";
}
$sequences[$seqSpec->name] = $seqSpec;
}
uasort( $sequences, static function ( SequenceSpec $a, SequenceSpec $b ) {
return $a->hardExpiry <=> $b->hardExpiry;
} );
$this->sequences = $sequences;
}
}