| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\WRStats; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class WRStatsReader { |
| 12 |
|
| 13 |
private $store; |
| 14 |
|
| 15 |
private $metricSpecs; |
| 16 |
|
| 17 |
private $prefixComponents; |
| 18 |
|
| 19 |
private $now; |
| 20 |
|
| 21 |
private $queuedKeys = []; |
| 22 |
|
| 23 |
private $cachedValues = []; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function __construct( StatsStore $store, $specs, $prefix ) { |
| 32 |
$this->store = $store; |
| 33 |
$this->metricSpecs = []; |
| 34 |
foreach ( $specs as $name => $spec ) { |
| 35 |
$this->metricSpecs[$name] = new MetricSpec( $spec ); |
| 36 |
} |
| 37 |
$this->prefixComponents = is_array( $prefix ) ? $prefix : [ $prefix ]; |
| 38 |
if ( !count( $this->prefixComponents ) ) { |
| 39 |
throw new WRStatsError( __METHOD__ . |
| 40 |
': there must be at least one prefix component' ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function latest( $numSeconds ) { |
| 53 |
$now = $this->now(); |
| 54 |
return new TimeRange( $now - $numSeconds, $now ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function timeRange( $start, $end ) { |
| 65 |
return new TimeRange( $start, $end ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function getRate( $metricName, ?EntityKey $entity, TimeRange $range ) { |
| 77 |
$metricSpec = $this->metricSpecs[$metricName] ?? null; |
| 78 |
if ( $metricSpec === null ) { |
| 79 |
throw new WRStatsError( "Unrecognised metric \"$metricName\"" ); |
| 80 |
} |
| 81 |
$entity ??= new LocalEntityKey; |
| 82 |
$now = $this->now(); |
| 83 |
$seqSpec = null; |
| 84 |
foreach ( $metricSpec->sequences as $seqSpec ) { |
| 85 |
$seqStart = $now - $seqSpec->softExpiry; |
| 86 |
if ( $seqStart <= $range->start ) { |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ( !$seqSpec ) { |
| 92 |
|
| 93 |
|
| 94 |
throw new WRStatsError( 'There should have been at least one sequence' ); |
| 95 |
} |
| 96 |
|
| 97 |
$timeStep = $seqSpec->timeStep; |
| 98 |
$firstBucket = (int)( $range->start / $timeStep ); |
| 99 |
$lastBucket = (int)ceil( $range->end / $timeStep ); |
| 100 |
for ( $bucket = $firstBucket; $bucket <= $lastBucket; $bucket++ ) { |
| 101 |
$key = $this->store->makeKey( |
| 102 |
$this->prefixComponents, |
| 103 |
[ $metricName, $seqSpec->name, $bucket ], |
| 104 |
$entity |
| 105 |
); |
| 106 |
if ( !isset( $this->cachedValues[$key] ) ) { |
| 107 |
$this->queuedKeys[$key] = true; |
| 108 |
} |
| 109 |
} |
| 110 |
return new RatePromise( $this, $metricName, $entity, $metricSpec, $seqSpec, $range ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function getRates( $metricNames, ?EntityKey $entity, TimeRange $range ) { |
| 123 |
$rates = []; |
| 124 |
foreach ( $metricNames as $name ) { |
| 125 |
$rates[$name] = $this->getRate( $name, $entity, $range ); |
| 126 |
} |
| 127 |
return $rates; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function fetch() { |
| 134 |
if ( !$this->queuedKeys ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
$this->cachedValues += $this->store->query( array_keys( $this->queuedKeys ) ); |
| 138 |
$this->queuedKeys = []; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function setCurrentTime( $now ) { |
| 147 |
$this->now = $now; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function resetCurrentTime() { |
| 155 |
$this->now = null; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private function now() { |
| 162 |
$this->now ??= microtime( true ); |
| 163 |
return $this->now; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function internalGetCount( |
| 176 |
$metricName, |
| 177 |
EntityKey $entity, |
| 178 |
MetricSpec $metricSpec, |
| 179 |
SequenceSpec $seqSpec, |
| 180 |
TimeRange $range |
| 181 |
) { |
| 182 |
$this->fetch(); |
| 183 |
$timeStep = $seqSpec->timeStep; |
| 184 |
$firstBucket = (int)( $range->start / $timeStep ); |
| 185 |
$lastBucket = (int)( $range->end / $timeStep ); |
| 186 |
$now = $this->now(); |
| 187 |
$total = 0; |
| 188 |
for ( $bucket = $firstBucket; $bucket <= $lastBucket; $bucket++ ) { |
| 189 |
$key = $this->store->makeKey( |
| 190 |
$this->prefixComponents, |
| 191 |
[ $metricName, $seqSpec->name, $bucket ], |
| 192 |
$entity |
| 193 |
); |
| 194 |
$value = $this->cachedValues[$key] ?? 0; |
| 195 |
if ( !$value ) { |
| 196 |
continue; |
| 197 |
} elseif ( $bucket === $firstBucket ) { |
| 198 |
if ( $bucket === $lastBucket ) { |
| 199 |
|
| 200 |
$bucketStartTime = $bucket * $timeStep; |
| 201 |
$rateInterpolationEndTime = min( $bucketStartTime + $timeStep, $now ); |
| 202 |
$interpolationDuration = $rateInterpolationEndTime - $bucketStartTime; |
| 203 |
if ( $interpolationDuration > 0 ) { |
| 204 |
$total += $value * $range->getDuration() / $interpolationDuration; |
| 205 |
} |
| 206 |
} else { |
| 207 |
$overlapDuration = max( ( $bucket + 1 ) * $timeStep - $range->start, 0 ); |
| 208 |
$total += $value * $overlapDuration / $timeStep; |
| 209 |
} |
| 210 |
} elseif ( $bucket === $lastBucket ) { |
| 211 |
|
| 212 |
$bucketStartTime = $bucket * $timeStep; |
| 213 |
$rateInterpolationEndTime = min( $bucketStartTime + $timeStep, $now ); |
| 214 |
$overlapDuration = max( $range->end - $bucketStartTime, 0 ); |
| 215 |
$interpolationDuration = $rateInterpolationEndTime - $bucketStartTime; |
| 216 |
if ( $overlapDuration === $interpolationDuration ) { |
| 217 |
|
| 218 |
$total += $value; |
| 219 |
} elseif ( $interpolationDuration > 0 ) { |
| 220 |
$total += $value * $overlapDuration / $interpolationDuration; |
| 221 |
} |
| 222 |
} else { |
| 223 |
$total += $value; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
$rounded = round( $total ) * $metricSpec->resolution; |
| 228 |
|
| 229 |
if ( is_int( $metricSpec->resolution ) ) { |
| 230 |
$rounded = (int)$rounded; |
| 231 |
} |
| 232 |
return $rounded; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function total( $rates ) { |
| 243 |
$result = []; |
| 244 |
foreach ( $rates as $key => $rate ) { |
| 245 |
$result[$key] = $rate->total(); |
| 246 |
} |
| 247 |
return $result; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function perSecond( $rates ) { |
| 257 |
$result = []; |
| 258 |
foreach ( $rates as $key => $rate ) { |
| 259 |
$result[$key] = $rate->perSecond(); |
| 260 |
} |
| 261 |
return $result; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function perMinute( $rates ) { |
| 271 |
$result = []; |
| 272 |
foreach ( $rates as $key => $rate ) { |
| 273 |
$result[$key] = $rate->perMinute(); |
| 274 |
} |
| 275 |
return $result; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function perHour( $rates ) { |
| 285 |
$result = []; |
| 286 |
foreach ( $rates as $key => $rate ) { |
| 287 |
$result[$key] = $rate->perHour(); |
| 288 |
} |
| 289 |
return $result; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public function perDay( $rates ) { |
| 299 |
$result = []; |
| 300 |
foreach ( $rates as $key => $rate ) { |
| 301 |
$result[$key] = $rate->perDay(); |
| 302 |
} |
| 303 |
return $result; |
| 304 |
} |
| 305 |
} |