File: RatePromise.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (106 lines) | stat: -rw-r--r-- 2,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace Wikimedia\WRStats;

/**
 * A WRStats query result promise. It contains the input parameters to a query.
 * When an accessor method is called, it triggers batch query execution in the
 * parent WRStatsReader.
 *
 * @since 1.39
 */
class RatePromise {
	/** @var WRStatsReader */
	private $reader;
	/** @var string */
	private $name;
	/** @var EntityKey */
	private $entity;
	/** @var MetricSpec */
	private $metricSpec;
	/** @var SequenceSpec */
	private $seqSpec;
	/** @var TimeRange */
	private $range;
	/** @var int|float|null Lazy-initialised query result */
	private $total;

	/**
	 * @internal Use via WRStatsReader from WRStatsFactory::createReader instead
	 * @param WRStatsReader $reader
	 * @param string $name
	 * @param EntityKey $entity
	 * @param MetricSpec $metricSpec
	 * @param SequenceSpec $seqSpec
	 * @param TimeRange $range
	 */
	public function __construct(
		WRStatsReader $reader,
		string $name,
		EntityKey $entity,
		MetricSpec $metricSpec,
		SequenceSpec $seqSpec,
		TimeRange $range
	) {
		$this->reader = $reader;
		$this->name = $name;
		$this->entity = $entity;
		$this->metricSpec = $metricSpec;
		$this->seqSpec = $seqSpec;
		$this->range = $range;
	}

	/**
	 * Get the total counter value summed over the specified time range.
	 *
	 * @return float|int
	 */
	public function total() {
		if ( $this->total === null ) {
			$this->total = $this->reader->internalGetCount(
				$this->name,
				$this->entity,
				$this->metricSpec,
				$this->seqSpec,
				$this->range
			);
		}
		return $this->total;
	}

	/**
	 * Get the counter value as a rate per second.
	 *
	 * @return float
	 */
	public function perSecond() {
		return $this->total() / $this->range->getDuration();
	}

	/**
	 * Get the counter value as a rate per minute
	 *
	 * @return float
	 */
	public function perMinute() {
		return $this->perSecond() * 60;
	}

	/**
	 * Get the counter value as a rate per hour
	 *
	 * @return float
	 */
	public function perHour() {
		return $this->perSecond() * 3600;
	}

	/**
	 * Get the counter value as a rate per day
	 *
	 * @return float
	 */
	public function perDay() {
		return $this->perSecond() * 86400;
	}
}