MediaWiki master
StatsUtils.php
Go to the documentation of this file.
1<?php
20declare( strict_types=1 );
21
22namespace Wikimedia\Stats;
23
24use InvalidArgumentException;
26
37
39 public const RE_VALID_NAME_AND_LABEL_NAME = "/^[a-zA-Z_][a-zA-Z0-9_]*$/";
40
42 public const DEFAULT_SAMPLE_RATE = 1.0;
43
50 public static function validateNewSampleRate( float $newSampleRate ): void {
51 if ( $newSampleRate < 0.0 || $newSampleRate > 1.0 ) {
52 throw new InvalidArgumentException( "Sample rate can only be between 0.0 and 1.0. Got: " . $newSampleRate );
53 }
54 }
55
63 public static function getFilteredSamples( float $sampleRate, array $samples ): array {
64 if ( $sampleRate === 1.0 ) {
65 return $samples;
66 }
67 $output = [];
68 $randMax = mt_getrandmax();
69 foreach ( $samples as $sample ) {
70 if ( mt_rand() / $randMax < $sampleRate ) {
71 $output[] = $sample;
72 }
73 }
74 return $output;
75 }
76
85 public static function validateMetricName( string $name ) {
86 if ( $name === "" ) {
87 throw new InvalidArgumentException( "Stats: Metric name cannot be empty." );
88 }
89 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $name ) ) {
90 throw new InvalidConfigurationException( "Invalid metric name: '" . $name . "'" );
91 }
92 }
93
102 public static function validateLabelKey( string $key ) {
103 if ( $key === "" ) {
104 throw new InvalidArgumentException( "Stats: Label key cannot be empty." );
105 }
106 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $key ) ) {
107 throw new InvalidConfigurationException( "Invalid label key: '" . $key . "'" );
108 }
109 }
110
111 public static function validateLabelValue( string $value ) {
112 if ( $value === "" ) {
113 throw new InvalidArgumentException( "Stats: Label value cannot be empty." );
114 }
115 }
116
124 public static function mergeLabels( array $leftLabels, array $rightLabels ): array {
125 $output = [];
126 foreach ( $leftLabels as $key => $value ) {
127 $output[$key] = $value;
128 }
129 foreach ( $rightLabels as $key => $value ) {
130 if ( array_key_exists( $key, $output ) ) {
131 continue;
132 }
133 $output[$key] = $value;
134 }
135 return $output;
136 }
137
144 public static function normalizeArray( array $entities ): array {
145 $normalizedEntities = [];
146 foreach ( $entities as $entity ) {
147 $normalizedEntities[] = self::normalizeString( $entity );
148 }
149 return $normalizedEntities;
150 }
151
162 public static function normalizeString( string $entity ): string {
163 $entity = preg_replace( "/[^a-z0-9]/i", "_", $entity );
164 $entity = preg_replace( "/_+/", "_", $entity );
165 return trim( $entity, "_" );
166 }
167}
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.