1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Metrics; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Concerns\{HasRanges, HasRoundedValue}; |
8
|
|
|
use Arcanedev\LaravelMetrics\Results\RangedValueResult; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RangedValue |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class RangedValue extends Metric |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Traits |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use HasRanges, |
23
|
|
|
HasRoundedValue; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Getters & Setters |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the metric type. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
8 |
|
public function type(): string |
36
|
|
|
{ |
37
|
8 |
|
return 'ranged-value'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Main Methods |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Calculate the `count` of the metric. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Database\Eloquent\Model|string $model |
49
|
|
|
* @param string|null $column |
50
|
|
|
* @param string|null $dateColumn |
51
|
|
|
* |
52
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
53
|
|
|
*/ |
54
|
12 |
|
protected function count($model, $column = null, $dateColumn = null) |
55
|
|
|
{ |
56
|
12 |
|
return $this->aggregate('count', $model, $column, $dateColumn); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Calculate the `average` of the metric. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
63
|
|
|
* @param string $column |
64
|
|
|
* @param string|null $dateColumn |
65
|
|
|
* |
66
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
67
|
|
|
*/ |
68
|
4 |
|
public function average($model, $column, $dateColumn = null) |
69
|
|
|
{ |
70
|
4 |
|
return $this->aggregate('avg', $model, $column, $dateColumn); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Calculate the `sum` of the metric. |
75
|
|
|
* |
76
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
77
|
|
|
* @param string $column |
78
|
|
|
* @param string|null $dateColumn |
79
|
|
|
* |
80
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
81
|
|
|
*/ |
82
|
4 |
|
public function sum($model, $column, $dateColumn = null) |
83
|
|
|
{ |
84
|
4 |
|
return $this->aggregate('sum', $model, $column, $dateColumn); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Calculate the `max` of the metric. |
89
|
|
|
* |
90
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
91
|
|
|
* @param string $column |
92
|
|
|
* @param string|null $dateColumn |
93
|
|
|
* |
94
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
95
|
|
|
*/ |
96
|
4 |
|
public function max($model, $column, $dateColumn = null) |
97
|
|
|
{ |
98
|
4 |
|
return $this->aggregate('max', $model, $column, $dateColumn); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Calculate the `min` of the metric. |
103
|
|
|
* |
104
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
105
|
|
|
* @param string $column |
106
|
|
|
* @param string|null $dateColumn |
107
|
|
|
* |
108
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
109
|
|
|
*/ |
110
|
4 |
|
public function min($model, $column, $dateColumn = null) |
111
|
|
|
{ |
112
|
4 |
|
return $this->aggregate('min', $model, $column, $dateColumn); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* ----------------------------------------------------------------- |
116
|
|
|
| Other Methods |
117
|
|
|
| ----------------------------------------------------------------- |
118
|
|
|
*/ |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Handle the aggregate calculation of the metric. |
122
|
|
|
* |
123
|
|
|
* @param string $method |
124
|
|
|
* @param \Illuminate\Database\Eloquent\Model|string $model |
125
|
|
|
* @param string|null $column |
126
|
|
|
* @param string|null $dateColumn |
127
|
|
|
* |
128
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
129
|
|
|
*/ |
130
|
28 |
|
private function aggregate(string $method, $model, $column = null, $dateColumn = null) |
131
|
|
|
{ |
132
|
28 |
|
$query = static::getQuery($model); |
133
|
28 |
|
$column = $column ?? $query->getModel()->getQualifiedKeyName(); |
|
|
|
|
134
|
28 |
|
$dateColumn = $dateColumn ?? $query->getModel()->getCreatedAtColumn(); |
|
|
|
|
135
|
28 |
|
$range = (int) $this->request->input('range', 1); |
136
|
28 |
|
$timezone = $this->getCurrentTimezone($this->request); |
137
|
|
|
|
138
|
28 |
|
$current = with(clone $query)->whereBetween($dateColumn, $this->currentRange($range, $timezone))->{$method}($column); |
139
|
28 |
|
$previous = with(clone $query)->whereBetween($dateColumn, $this->previousRange($range, $timezone))->{$method}($column); |
140
|
|
|
|
141
|
28 |
|
return $this->result($method === 'count' ? $current : $this->roundValue($current)) |
142
|
28 |
|
->previous($method === 'count' ? $previous : $this->roundValue($previous)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Prepare the metric for JSON serialization. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
4 |
|
public function toArray(): array |
151
|
|
|
{ |
152
|
4 |
|
return array_merge( |
153
|
4 |
|
parent::toArray(), |
154
|
4 |
|
['ranges' => $this->rangesToArray()] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Make a new result instance. |
160
|
|
|
* |
161
|
|
|
* @param mixed|null $value |
162
|
|
|
* |
163
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\RangedValueResult|mixed |
164
|
|
|
*/ |
165
|
28 |
|
protected function result($value = null) |
166
|
|
|
{ |
167
|
28 |
|
return new RangedValueResult($value); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: