aboutsummaryrefslogtreecommitdiffstats
path: root/includes/libs/WRStats/MetricSpec.php
blob: 7d67f71a1588264219768a8592814d5ce93529ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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;

	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;
	}
}