1
|
|
|
<?php namespace Arcanedev\LaravelMetrics\Metrics; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException; |
4
|
|
|
use Arcanedev\LaravelMetrics\Helpers\TrendDatePeriod; |
5
|
|
|
use Arcanedev\LaravelMetrics\Results\TrendResult; |
6
|
|
|
use Cake\Chronos\Chronos; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Trend |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\LaravelMetrics\Metrics |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class Trend extends Metric |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Constants |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Trend metric unit constants. |
24
|
|
|
*/ |
25
|
|
|
const BY_MONTHS = 'month'; |
26
|
|
|
const BY_WEEKS = 'week'; |
27
|
|
|
const BY_DAYS = 'day'; |
28
|
|
|
const BY_HOURS = 'hour'; |
29
|
|
|
const BY_MINUTES = 'minute'; |
30
|
|
|
|
31
|
|
|
/* ----------------------------------------------------------------- |
32
|
|
|
| Traits |
33
|
|
|
| ----------------------------------------------------------------- |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
use Concerns\AggregatesTrends, |
37
|
|
|
Concerns\HasExpressions, |
38
|
|
|
Concerns\HasRanges, |
39
|
|
|
Concerns\FormatsTrends; |
40
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
42
|
|
|
| Getters |
43
|
|
|
| ----------------------------------------------------------------- |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the metric type. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
20 |
|
public function type(): string |
52
|
|
|
{ |
53
|
20 |
|
return 'trend'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
57
|
|
|
| Main Methods |
58
|
|
|
| ----------------------------------------------------------------- |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Calculate the `count` of the metric. |
63
|
|
|
* |
64
|
|
|
* @param string $unit |
65
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
66
|
|
|
* @param string|null $dateColumn |
67
|
|
|
* @param string|null $column |
68
|
|
|
* |
69
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
70
|
|
|
*/ |
71
|
20 |
|
public function count($unit, $model, $dateColumn = null, $column = null) |
72
|
|
|
{ |
73
|
20 |
|
return $this->aggregate('count', $unit, $model, $column, $dateColumn); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return a value result showing a average aggregate over time. |
78
|
|
|
* |
79
|
|
|
* @param string $unit |
80
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
81
|
|
|
* @param string $column |
82
|
|
|
* @param string|null $dateColumn |
83
|
|
|
* |
84
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
85
|
|
|
*/ |
86
|
|
|
public function average(string $unit, $model, $column, $dateColumn = null) |
87
|
|
|
{ |
88
|
|
|
return $this->aggregate('avg', $unit, $model, $column, $dateColumn); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return a value result showing a sum aggregate over time. |
93
|
|
|
* |
94
|
|
|
* @param string $unit |
95
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
96
|
|
|
* @param string $column |
97
|
|
|
* @param string|null $dateColumn |
98
|
|
|
* |
99
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
100
|
|
|
*/ |
101
|
|
|
public function sum(string $unit, $model, $column, $dateColumn = null) |
102
|
|
|
{ |
103
|
|
|
return $this->aggregate('sum', $unit, $model, $column, $dateColumn); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return a value result showing a max aggregate over time. |
108
|
|
|
* |
109
|
|
|
* @param string $unit |
110
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
111
|
|
|
* @param string $column |
112
|
|
|
* @param string|null $dateColumn |
113
|
|
|
* |
114
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
115
|
|
|
*/ |
116
|
|
|
public function max(string $unit, $model, $column, $dateColumn = null) |
117
|
|
|
{ |
118
|
|
|
return $this->aggregate('max', $unit, $model, $column, $dateColumn); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return a value result showing a min aggregate over time. |
123
|
|
|
* |
124
|
|
|
* @param string $unit |
125
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
126
|
|
|
* @param string $column |
127
|
|
|
* @param string|null $dateColumn |
128
|
|
|
* |
129
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
130
|
|
|
*/ |
131
|
|
|
public function min(string $unit, $model, $column, $dateColumn = null) |
132
|
|
|
{ |
133
|
|
|
return $this->aggregate('min', $unit, $model, $column, $dateColumn); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Make a new result instance. |
138
|
|
|
* |
139
|
|
|
* @param mixed|null $value |
140
|
|
|
* |
141
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
142
|
|
|
*/ |
143
|
20 |
|
protected function result($value = null) |
144
|
|
|
{ |
145
|
20 |
|
return new TrendResult($value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* ----------------------------------------------------------------- |
149
|
|
|
| Other Methods |
150
|
|
|
| ----------------------------------------------------------------- |
151
|
|
|
*/ |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Handle the aggregate calculation of the metric. |
155
|
|
|
* |
156
|
|
|
* @param string $method |
157
|
|
|
* @param string $unit |
158
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
159
|
|
|
* @param string|null $column |
160
|
|
|
* @param string|null $dateColumn |
161
|
|
|
* |
162
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\TrendResult|mixed |
163
|
|
|
*/ |
164
|
20 |
|
protected function aggregate(string $method, string $unit, $model, ?string $column = null, ?string $dateColumn = null) |
165
|
|
|
{ |
166
|
20 |
|
$range = $this->request->input('range'); |
167
|
20 |
|
$timezone = $this->request->input('timezone'); |
168
|
20 |
|
$twelveHourTime = $this->request->input('twelveHourTime') === 'true'; |
169
|
|
|
|
170
|
20 |
|
$query = static::getQuery($model); |
171
|
20 |
|
$column = $column ?? $query->getModel()->getCreatedAtColumn(); |
|
|
|
|
172
|
20 |
|
$dateColumn = $dateColumn ?? $query->getModel()->getCreatedAtColumn(); |
173
|
20 |
|
$expression = $this->getExpression($query, 'trend_date_format', $dateColumn, [$unit, $query, $timezone]); |
174
|
|
|
|
175
|
20 |
|
$dates = TrendDatePeriod::make( |
176
|
20 |
|
$startingDate = TrendDatePeriod::getStartingDate($unit, $range), |
177
|
20 |
|
$endingDate = Chronos::now(), |
178
|
5 |
|
$unit, |
179
|
5 |
|
$timezone |
180
|
|
|
)->mapWithKeys(function (Chronos $date) use ($twelveHourTime, $unit) { |
181
|
|
|
return [ |
182
|
20 |
|
static::formatAggregateKey($date, $unit) => [ |
183
|
20 |
|
'label' => static::formatLabelBy($date, $unit, $twelveHourTime), |
184
|
20 |
|
'value' => 0, |
185
|
|
|
] |
186
|
|
|
]; |
187
|
20 |
|
}); |
188
|
|
|
|
189
|
20 |
|
$wrappedColumn = $query->getQuery()->getGrammar()->wrap($column); |
190
|
|
|
|
191
|
20 |
|
$results = $query->select([ |
|
|
|
|
192
|
20 |
|
DB::raw("{$expression} as date_result"), |
193
|
20 |
|
DB::raw("{$method}({$wrappedColumn}) as aggregate") |
194
|
|
|
]) |
195
|
20 |
|
->whereBetween($dateColumn, [$startingDate, $endingDate]) |
196
|
20 |
|
->groupBy('date_result') |
197
|
20 |
|
->orderBy('date_result') |
198
|
20 |
|
->get() |
199
|
|
|
->mapWithKeys(function ($result) use ($method, $unit, $twelveHourTime) { |
200
|
20 |
|
$date = static::parseDateResult($result->getAttribute('date_result'), $unit); |
201
|
20 |
|
$value = $result->getAttribute('aggregate'); |
202
|
|
|
|
203
|
|
|
return [ |
204
|
20 |
|
static::formatAggregateKey($date, $unit) => [ |
205
|
20 |
|
'label' => static::formatLabelBy($date, $unit, $twelveHourTime), |
206
|
20 |
|
'value' => $method === 'count' ? intval($value) : round($value, 0), |
207
|
|
|
], |
208
|
|
|
]; |
209
|
20 |
|
}); |
210
|
|
|
|
211
|
20 |
|
$results = $dates->merge($results)->sortKeys(); |
212
|
|
|
|
213
|
20 |
|
if ($results->count() > $range) |
214
|
20 |
|
$results->shift(); |
215
|
|
|
|
216
|
20 |
|
return $this->result()->trend($results->all()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Parse the date result. |
221
|
|
|
* |
222
|
|
|
* @param string $date |
223
|
|
|
* @param string $unit |
224
|
|
|
* |
225
|
|
|
* @return \Cake\Chronos\Chronos |
226
|
|
|
*/ |
227
|
20 |
|
protected function parseDateResult(string $date, string $unit): Chronos |
228
|
|
|
{ |
229
|
|
|
switch ($unit) { |
230
|
20 |
|
case self::BY_MONTHS: |
231
|
4 |
|
return Chronos::createFromFormat('Y-m', $date); |
232
|
|
|
|
233
|
16 |
|
case self::BY_WEEKS: |
234
|
4 |
|
[$year, $week] = explode('-', $date); |
|
|
|
|
235
|
4 |
|
return (new Chronos)->setISODate($year, $week)->setTime(0, 0); |
236
|
|
|
|
237
|
12 |
|
case self::BY_DAYS: |
238
|
4 |
|
return Chronos::createFromFormat('Y-m-d', $date); |
239
|
|
|
|
240
|
8 |
|
case self::BY_HOURS: |
241
|
4 |
|
return Chronos::createFromFormat('Y-m-d H:00', $date); |
242
|
|
|
|
243
|
4 |
|
case self::BY_MINUTES: |
244
|
4 |
|
return Chronos::createFromFormat('Y-m-d H:i:00', $date); |
245
|
|
|
|
246
|
|
|
default: |
247
|
|
|
throw InvalidTrendUnitException::make($unit); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Prepare the metric for JSON serialization. |
253
|
|
|
* |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
|
|
public function toArray(): array |
257
|
|
|
{ |
258
|
|
|
return array_merge( |
259
|
|
|
parent::toArray(), |
260
|
|
|
['ranges' => $this->rangesToArray()] |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
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: