Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
MetricSpec | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
7.17 | |
0.00% |
0 / 1 |
__construct | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
7.17 |
1 | <?php |
2 | |
3 | namespace Wikimedia\WRStats; |
4 | |
5 | /** |
6 | * Class representation of normalized metric specifications |
7 | * |
8 | * @internal |
9 | */ |
10 | class MetricSpec { |
11 | /** The default (value axis) resolution */ |
12 | public const DEFAULT_RESOLUTION = 1; |
13 | |
14 | /** @var string */ |
15 | public $type; |
16 | /** @var float|int */ |
17 | public $resolution; |
18 | /** @var array<string,SequenceSpec> Sequences in ascending order of expiry */ |
19 | public $sequences; |
20 | |
21 | /** |
22 | * @param array $spec |
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 | } |