Debian Codegraph @

includes/libs/WRStats/WRStatsFactory.php

raw symbols SCIP
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 /**
21 * @param StatsStore $store
22 */
23 public function __construct( StatsStore $store ) {
24 $this->store = $store;
25 }
26
27 /**
28 * Create a writer. Writers gather a batch of increment operations and then
29 * commit them when flush() is called, or when the writer is destroyed.
30 *
31 * @param array $specs An array of metric specification arrays, indexed by
32 * name, where each element is an associative array with the following
33 * keys (all optional):
34 * - type: (string) Always "counter"
35 * - resolution: (int|float) The resolution of the counter value.
36 * For example, if this is 0.01, counters will be rounded to two
37 * decimal places. Necessary because we support backends that only
38 * store integers, but we present an interface that allows float
39 * values.
40 * - sequences: (array) An array of sequence specification, each
41 * sequence spec being an associative array with the following keys:
42 * - expiry: (int|float) The expiry time of the counters, in seconds.
43 * - timeStep: (int|float) The time duration represented by a counter
44 * bucket. If this is too small, many buckets will be required,
45 * making fetches slower. If this is too large, there will be some
46 * jitter in the resulting rates as the current time moves from one
47 * bucket to the next.
48 *
49 * @param string|string[] $prefix A string or array of strings to prefix
50 * before storage keys.
51 * @return WRStatsWriter
52 */
53 public function createWriter( $specs, $prefix = 'WRStats' ) {
54 $writer = new WRStatsWriter( $this->store, $specs, $prefix );
55 if ( $this->now !== null ) {
56 $writer->setCurrentTime( $this->now );
57 }
58 return $writer;
59 }
60
61 /**
62 * Create a reader. Readers gather a batch of read operations, returning
63 * promises. The batch is executed when the first promise is resolved.
64 *
65 * @see createWriter
66 *
67 * @param array $specs
68 * @param string|string[] $prefix
69 * @return WRStatsReader
70 */
71 public function createReader( $specs, $prefix = 'WRStats' ) {
72 $reader = new WRStatsReader( $this->store, $specs, $prefix );
73 if ( $this->now !== null ) {
74 $reader->setCurrentTime( $this->now );
75 }
76 return $reader;
77 }
78
79 /**
80 * Create a rate limiter.
81 *
82 * @param LimitCondition[] $conditions An array in which the key is the
83 * condition name, and the value is a LimitCondition describing the limit.
84 * @param string|string[] $prefix A string or array of strings to prefix
85 * before storage keys.
86 * @param array $options An associative array of options:
87 * - bucketCount: Each window is divided into this many time buckets.
88 * Fetching the current count will typically result in a request for
89 * this many keys.
90 * @return WRStatsRateLimiter
91 */
92 public function createRateLimiter(
93 $conditions, $prefix = 'WRStats', $options = []
94 ) {
95 $rateLimiter = new WRStatsRateLimiter(
96 $this->store, $conditions, $prefix, $options );
97 if ( $this->now !== null ) {
98 $rateLimiter->setCurrentTime( $this->now );
99 }
100 return $rateLimiter;
101 }
102
103 /**
104 * Set a current timestamp to be injected into new instances on creation
105 *
106 * @param float|int $now Seconds since epoch
107 */
108 public function setCurrentTime( $now ) {
109 $this->now = $now;
110 }
111 }