Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
RatePromise
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 perSecond
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 perMinute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 perHour
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 perDay
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikimedia\WRStats;
4
5/**
6 * A WRStats query result promise. It contains the input parameters to a query.
7 * When an accessor method is called, it triggers batch query execution in the
8 * parent WRStatsReader.
9 *
10 * @since 1.39
11 */
12class RatePromise {
13    /** @var WRStatsReader */
14    private $reader;
15    /** @var string */
16    private $name;
17    /** @var EntityKey */
18    private $entity;
19    /** @var MetricSpec */
20    private $metricSpec;
21    /** @var SequenceSpec */
22    private $seqSpec;
23    /** @var TimeRange */
24    private $range;
25    /** @var int|float|null Lazy-initialised query result */
26    private $total;
27
28    /**
29     * @internal Use via WRStatsReader from WRStatsFactory::createReader instead
30     * @param WRStatsReader $reader
31     * @param string $name
32     * @param EntityKey $entity
33     * @param MetricSpec $metricSpec
34     * @param SequenceSpec $seqSpec
35     * @param TimeRange $range
36     */
37    public function __construct(
38        WRStatsReader $reader,
39        string $name,
40        EntityKey $entity,
41        MetricSpec $metricSpec,
42        SequenceSpec $seqSpec,
43        TimeRange $range
44    ) {
45        $this->reader = $reader;
46        $this->name = $name;
47        $this->entity = $entity;
48        $this->metricSpec = $metricSpec;
49        $this->seqSpec = $seqSpec;
50        $this->range = $range;
51    }
52
53    /**
54     * Get the total counter value summed over the specified time range.
55     *
56     * @return float|int
57     */
58    public function total() {
59        if ( $this->total === null ) {
60            $this->total = $this->reader->internalGetCount(
61                $this->name,
62                $this->entity,
63                $this->metricSpec,
64                $this->seqSpec,
65                $this->range
66            );
67        }
68        return $this->total;
69    }
70
71    /**
72     * Get the counter value as a rate per second.
73     *
74     * @return float
75     */
76    public function perSecond() {
77        return $this->total() / $this->range->getDuration();
78    }
79
80    /**
81     * Get the counter value as a rate per minute
82     *
83     * @return float
84     */
85    public function perMinute() {
86        return $this->perSecond() * 60;
87    }
88
89    /**
90     * Get the counter value as a rate per hour
91     *
92     * @return float
93     */
94    public function perHour() {
95        return $this->perSecond() * 3600;
96    }
97
98    /**
99     * Get the counter value as a rate per day
100     *
101     * @return float
102     */
103    public function perDay() {
104        return $this->perSecond() * 86400;
105    }
106}