MediaWiki 1.43.1
StatsUtils.php
Go to the documentation of this file.
1<?php
20declare( strict_types=1 );
21
22namespace Wikimedia\Stats;
23
24use InvalidArgumentException;
26
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
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
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
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
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
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
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
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}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
StatsUtils Implementation.
static normalizeString(string $entity)
Normalize strings to a metrics-compatible format.
static normalizeArray(array $entities)
Normalize an array of strings.
static validateMetricName(string $name)
Determines if provided string is a valid name.
static getFilteredSamples(float $sampleRate, array $samples)
Returns a subset of samples based on configured sample rate.
static validateLabelKey(string $key)
Determines if provided string is a valid label key.
static validateLabelValue(string $value)
static validateNewSampleRate(float $newSampleRate)
Validates the new sample rate.
static mergeLabels(array $leftLabels, array $rightLabels)
Merges two associative arrays of labels.