| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
declare( strict_types=1 ); |
| 21 |
|
| 22 |
namespace Wikimedia\Stats; |
| 23 |
|
| 24 |
use InvalidArgumentException; |
| 25 |
use Wikimedia\Stats\Exceptions\InvalidConfigurationException; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class StatsUtils { |
| 37 |
|
| 38 |
public const RE_VALID_NAME_AND_LABEL_NAME = "/^[a-zA-Z_][a-zA-Z0-9_]*$/"; |
| 39 |
public const DEFAULT_SAMPLE_RATE = 1.0; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static function validateNewSampleRate( float $newSampleRate ): void { |
| 48 |
if ( $newSampleRate < 0.0 || $newSampleRate > 1.0 ) { |
| 49 |
throw new InvalidArgumentException( "Sample rate can only be between 0.0 and 1.0. Got: " . $newSampleRate ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static function getFilteredSamples( float $sampleRate, array $samples ): array { |
| 61 |
if ( $sampleRate === 1.0 ) { |
| 62 |
return $samples; |
| 63 |
} |
| 64 |
$output = []; |
| 65 |
$randMax = mt_getrandmax(); |
| 66 |
foreach ( $samples as $sample ) { |
| 67 |
if ( mt_rand() / $randMax < $sampleRate ) { |
| 68 |
$output[] = $sample; |
| 69 |
} |
| 70 |
} |
| 71 |
return $output; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static function validateMetricName( string $name ) { |
| 83 |
if ( $name === "" ) { |
| 84 |
throw new InvalidArgumentException( "Stats: Metric name cannot be empty." ); |
| 85 |
} |
| 86 |
if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $name ) ) { |
| 87 |
throw new InvalidConfigurationException( "Invalid metric name: '" . $name . "'" ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static function validateLabelKey( string $key ) { |
| 100 |
if ( $key === "" ) { |
| 101 |
throw new InvalidArgumentException( "Stats: Label key cannot be empty." ); |
| 102 |
} |
| 103 |
if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $key ) ) { |
| 104 |
throw new InvalidConfigurationException( "Invalid label key: '" . $key . "'" ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public static function validateLabelValue( string $value ) { |
| 109 |
if ( $value === "" ) { |
| 110 |
throw new InvalidArgumentException( "Stats: Label value cannot be empty." ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static function mergeLabels( array $leftLabels, array $rightLabels ): array { |
| 122 |
$output = []; |
| 123 |
foreach ( $leftLabels as $key => $value ) { |
| 124 |
$output[$key] = $value; |
| 125 |
} |
| 126 |
foreach ( $rightLabels as $key => $value ) { |
| 127 |
if ( array_key_exists( $key, $output ) ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
$output[$key] = $value; |
| 131 |
} |
| 132 |
return $output; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static function normalizeArray( array $entities ): array { |
| 142 |
$normalizedEntities = []; |
| 143 |
foreach ( $entities as $entity ) { |
| 144 |
$normalizedEntities[] = self::normalizeString( $entity ); |
| 145 |
} |
| 146 |
return $normalizedEntities; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static function normalizeString( string $entity ): string { |
| 160 |
$entity = preg_replace( "/[^a-z0-9]/i", "_", $entity ); |
| 161 |
$entity = preg_replace( "/_+/", "_", $entity ); |
| 162 |
return trim( $entity, "_" ); |
| 163 |
} |
| 164 |
} |