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
34
35 public const RE_VALID_NAME_AND_LABEL_NAME = "/^[a-zA-Z_][a-zA-Z0-9_]*$/";
36 public const DEFAULT_SAMPLE_RATE = 1.0;
37
44 public static function validateNewSampleRate( float $newSampleRate ): void {
45 if ( $newSampleRate < 0.0 || $newSampleRate > 1.0 ) {
46 throw new InvalidArgumentException( "Sample rate can only be between 0.0 and 1.0. Got: " . $newSampleRate );
47 }
48 }
49
57 public static function getFilteredSamples( float $sampleRate, array $samples ): array {
58 if ( $sampleRate === 1.0 ) {
59 return $samples;
60 }
61 $output = [];
62 $randMax = mt_getrandmax();
63 foreach ( $samples as $sample ) {
64 if ( mt_rand() / $randMax < $sampleRate ) {
65 $output[] = $sample;
66 }
67 }
68 return $output;
69 }
70
79 public static function validateMetricName( string $name ) {
80 if ( $name === "" ) {
81 throw new InvalidArgumentException( "Stats: Metric name cannot be empty." );
82 }
83 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $name ) ) {
84 throw new InvalidConfigurationException( "Invalid metric name: '" . $name . "'" );
85 }
86 }
87
96 public static function validateLabelKey( string $key ) {
97 if ( $key === "" ) {
98 throw new InvalidArgumentException( "Stats: Label key cannot be empty." );
99 }
100 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $key ) ) {
101 throw new InvalidConfigurationException( "Invalid label key: '" . $key . "'" );
102 }
103 }
104
105 public static function validateLabelValue( string $value ) {
106 if ( $value === "" ) {
107 throw new InvalidArgumentException( "Stats: Label value cannot be empty." );
108 }
109 }
110
117 public static function normalizeArray( array $entities ): array {
118 $normalizedEntities = [];
119 foreach ( $entities as $entity ) {
120 $normalizedEntities[] = self::normalizeString( $entity );
121 }
122 return $normalizedEntities;
123 }
124
134 public static function normalizeString( string $entity ): string {
135 $entity = preg_replace( '/[^a-z\d]+/i', '_', $entity );
136 return trim( $entity, "_" );
137 }
138}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Functionality common to all metric types.
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.