| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\WRStats; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class MetricSpec { |
| 11 |
|
| 12 |
public const DEFAULT_RESOLUTION = 1; |
| 13 |
|
| 14 |
|
| 15 |
public $type; |
| 16 |
|
| 17 |
public $resolution; |
| 18 |
|
| 19 |
public $sequences; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function __construct( array $spec ) { |
| 25 |
$this->type = $spec['type'] ?? 'counter'; |
| 26 |
$this->resolution = $spec['resolution'] ?? self::DEFAULT_RESOLUTION; |
| 27 |
foreach ( [ 'timeStep', 'expiry' ] as $var ) { |
| 28 |
if ( isset( $spec[$var] ) ) { |
| 29 |
throw new WRStatsError( "$var must be in the sequences array" ); |
| 30 |
} |
| 31 |
} |
| 32 |
$seqArrays = $spec['sequences'] ?? []; |
| 33 |
if ( !$seqArrays ) { |
| 34 |
$seqArrays = [ [] ]; |
| 35 |
} |
| 36 |
$sequences = []; |
| 37 |
foreach ( $seqArrays as $i => $seqArray ) { |
| 38 |
if ( !is_array( $seqArray ) ) { |
| 39 |
throw new WRStatsError( 'sequences must be an array of arrays' ); |
| 40 |
} |
| 41 |
$seqSpec = new SequenceSpec( $seqArray ); |
| 42 |
while ( isset( $sequences[$seqSpec->name] ) ) { |
| 43 |
$seqSpec->name .= "s$i"; |
| 44 |
} |
| 45 |
$sequences[$seqSpec->name] = $seqSpec; |
| 46 |
} |
| 47 |
uasort( $sequences, static function ( SequenceSpec $a, SequenceSpec $b ) { |
| 48 |
return $a->hardExpiry <=> $b->hardExpiry; |
| 49 |
} ); |
| 50 |
$this->sequences = $sequences; |
| 51 |
} |
| 52 |
} |