Debian Codegraph @

includes/libs/Stats/StatsUtils.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 * @file
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 * StatsUtils Implementation
30 *
31 * Functionality common to all metric types.
32 *
33 * @author Cole White
34 * @since 1.38
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 * Validates the new sample rate. Throws InvalidArgumentException if provided an invalid rate.
43 *
44 * @param float $newSampleRate
45 * @throws InvalidArgumentException
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 * Returns a subset of samples based on configured sample rate.
55 *
56 * @param float $sampleRate
57 * @param array $samples
58 * @return array
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 * Determines if provided string is a valid name.
76 *
77 * @param string $name
78 * @return void
79 * @throws InvalidArgumentException
80 * @throws InvalidConfigurationException
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 * Determines if provided string is a valid label key.
93 *
94 * @param string $key
95 * @return void
96 * @throws InvalidArgumentException
97 * @throws InvalidConfigurationException
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 * Merges two associative arrays of labels. Prioritizes leftmost labels.
116 *
117 * @param array $leftLabels
118 * @param array $rightLabels
119 * @return array
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 * Normalize an array of strings.
137 *
138 * @param string[] $entities
139 * @return string[]
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 * Normalize strings to a metrics-compatible format.
151 *
152 * Replace any other non-alphanumeric characters with underscores.
153 * Eliminate repeated underscores.
154 * Trim leading or trailing underscores.
155 *
156 * @param string $entity
157 * @return string
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 }