Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
5 / 15 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WRStatsFactory | |
33.33% |
5 / 15 |
|
20.00% |
1 / 5 |
26.96 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createWriter | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| createReader | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| createRateLimiter | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| setCurrentTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikimedia\WRStats; |
| 4 | |
| 5 | /** |
| 6 | * The main entry point to WRStats, for creating readers and writers. |
| 7 | * |
| 8 | * Readers and writers should generally be used for a batch and then discarded. |
| 9 | * Factory objects can be retained indefinitely. |
| 10 | * |
| 11 | * @since 1.39 |
| 12 | */ |
| 13 | class WRStatsFactory { |
| 14 | /** @var StatsStore */ |
| 15 | private $store; |
| 16 | |
| 17 | /** @var float|int|null */ |
| 18 | private $now; |
| 19 | |
| 20 | public function __construct( StatsStore $store ) { |
| 21 | $this->store = $store; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Create a writer. Writers gather a batch of increment operations and then |
| 26 | * commit them when flush() is called, or when the writer is destroyed. |
| 27 | * |
| 28 | * @param array $specs An array of metric specification arrays, indexed by |
| 29 | * name, where each element is an associative array with the following |
| 30 | * keys (all optional): |
| 31 | * - type: (string) Always "counter" |
| 32 | * - resolution: (int|float) The resolution of the counter value. |
| 33 | * For example, if this is 0.01, counters will be rounded to two |
| 34 | * decimal places. Necessary because we support backends that only |
| 35 | * store integers, but we present an interface that allows float |
| 36 | * values. |
| 37 | * - sequences: (array) An array of sequence specification, each |
| 38 | * sequence spec being an associative array with the following keys: |
| 39 | * - expiry: (int|float) The expiry time of the counters, in seconds. |
| 40 | * - timeStep: (int|float) The time duration represented by a counter |
| 41 | * bucket. If this is too small, many buckets will be required, |
| 42 | * making fetches slower. If this is too large, there will be some |
| 43 | * jitter in the resulting rates as the current time moves from one |
| 44 | * bucket to the next. |
| 45 | * |
| 46 | * @param string|string[] $prefix A string or array of strings to prefix |
| 47 | * before storage keys. |
| 48 | * @return WRStatsWriter |
| 49 | */ |
| 50 | public function createWriter( $specs, $prefix = 'WRStats' ) { |
| 51 | $writer = new WRStatsWriter( $this->store, $specs, $prefix ); |
| 52 | if ( $this->now !== null ) { |
| 53 | $writer->setCurrentTime( $this->now ); |
| 54 | } |
| 55 | return $writer; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Create a reader. Readers gather a batch of read operations, returning |
| 60 | * promises. The batch is executed when the first promise is resolved. |
| 61 | * |
| 62 | * @see createWriter |
| 63 | * |
| 64 | * @param array $specs |
| 65 | * @param string|string[] $prefix |
| 66 | * @return WRStatsReader |
| 67 | */ |
| 68 | public function createReader( $specs, $prefix = 'WRStats' ) { |
| 69 | $reader = new WRStatsReader( $this->store, $specs, $prefix ); |
| 70 | if ( $this->now !== null ) { |
| 71 | $reader->setCurrentTime( $this->now ); |
| 72 | } |
| 73 | return $reader; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Create a rate limiter. |
| 78 | * |
| 79 | * @param array<string,LimitCondition> $conditions An array in which the key is the |
| 80 | * condition name, and the value is a LimitCondition describing the limit. |
| 81 | * @param string|string[] $prefix A string or array of strings to prefix |
| 82 | * before storage keys. |
| 83 | * @param array $options An associative array of options: |
| 84 | * - bucketCount: Each window is divided into this many time buckets. |
| 85 | * Fetching the current count will typically result in a request for |
| 86 | * this many keys. |
| 87 | * @return WRStatsRateLimiter |
| 88 | */ |
| 89 | public function createRateLimiter( |
| 90 | $conditions, $prefix = 'WRStats', $options = [] |
| 91 | ) { |
| 92 | $rateLimiter = new WRStatsRateLimiter( |
| 93 | $this->store, $conditions, $prefix, $options ); |
| 94 | if ( $this->now !== null ) { |
| 95 | $rateLimiter->setCurrentTime( $this->now ); |
| 96 | } |
| 97 | return $rateLimiter; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set a current timestamp to be injected into new instances on creation |
| 102 | * |
| 103 | * @param float|int $now Seconds since epoch |
| 104 | */ |
| 105 | public function setCurrentTime( $now ) { |
| 106 | $this->now = $now; |
| 107 | } |
| 108 | } |