1
|
|
|
<?php namespace Arcanedev\LaravelMetrics\Metrics; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelMetrics\Concerns\ConvertsToArray; |
4
|
|
|
use Closure; |
5
|
|
|
use DateInterval; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
8
|
|
|
use Illuminate\Contracts\Support\Responsable; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Support\Facades\Cache; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use JsonSerializable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Metric |
17
|
|
|
* |
18
|
|
|
* @package Arcanedev\LaravelMetrics\Metrics |
19
|
|
|
* @author ARCANEDEV <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class Metric implements Arrayable, Jsonable, JsonSerializable, Responsable |
22
|
|
|
{ |
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Traits |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
use ConvertsToArray; |
29
|
|
|
|
30
|
|
|
/* ----------------------------------------------------------------- |
31
|
|
|
| Properties |
32
|
|
|
| ----------------------------------------------------------------- |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The Http request instance. |
37
|
|
|
* |
38
|
|
|
* @var \Illuminate\Http\Request |
39
|
|
|
*/ |
40
|
|
|
protected $request; |
41
|
|
|
|
42
|
|
|
/* ----------------------------------------------------------------- |
43
|
|
|
| Getters & Setters |
44
|
|
|
| ----------------------------------------------------------------- |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the metric's type. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
abstract public function type(): string; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the metric's title. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
8 |
|
public function title(): string |
60
|
|
|
{ |
61
|
8 |
|
$class = class_basename(static::class); |
62
|
|
|
|
63
|
8 |
|
return Str::title(Str::snake($class, ' ')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the Http request. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Http\Request $request |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
92 |
|
public function setRequest(Request $request) |
|
|
|
|
74
|
|
|
{ |
75
|
92 |
|
$this->request = $request; |
76
|
|
|
|
77
|
92 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Determine for how many minutes the metric should be cached. |
82
|
|
|
* |
83
|
|
|
* @return \DateTimeInterface|\DateInterval|float|int|void |
84
|
|
|
*/ |
85
|
84 |
|
public function cacheFor() |
86
|
|
|
{ |
87
|
|
|
// |
88
|
84 |
|
} |
89
|
|
|
|
90
|
|
|
/* ----------------------------------------------------------------- |
91
|
|
|
| Main Methods |
92
|
|
|
| ----------------------------------------------------------------- |
93
|
|
|
*/ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Resolve & calculate the metric. |
97
|
|
|
* |
98
|
|
|
* @param \Illuminate\Http\Request $request |
99
|
|
|
* |
100
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\Result|mixed |
101
|
|
|
*/ |
102
|
92 |
|
public function resolve(Request $request) |
103
|
|
|
{ |
104
|
92 |
|
$this->setRequest($request); |
105
|
|
|
|
106
|
|
|
$resolver = function () use ($request) { |
107
|
84 |
|
return $this->calculate($request); |
108
|
92 |
|
}; |
109
|
|
|
|
110
|
92 |
|
return ($cacheFor = $this->cacheFor()) |
111
|
8 |
|
? $this->cacheResult($cacheFor, $resolver) |
112
|
92 |
|
: $resolver(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Calculate the metric. |
117
|
|
|
* |
118
|
|
|
* @param \Illuminate\Http\Request $request |
119
|
|
|
* |
120
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\Result|mixed |
121
|
|
|
*/ |
122
|
|
|
abstract public function calculate(Request $request); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Make a new result instance. |
126
|
|
|
* |
127
|
|
|
* @param mixed|null $value |
128
|
|
|
* |
129
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\Result |
130
|
|
|
*/ |
131
|
|
|
abstract protected function result($value = null); |
132
|
|
|
|
133
|
|
|
/* ----------------------------------------------------------------- |
134
|
|
|
| Other Methods |
135
|
|
|
| ----------------------------------------------------------------- |
136
|
|
|
*/ |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the instance as an array. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
8 |
|
public function toArray(): array |
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
8 |
|
'class' => static::class, |
147
|
8 |
|
'type' => $this->type(), |
148
|
8 |
|
'title' => $this->title(), |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create an HTTP response that represents the object. |
154
|
|
|
* |
155
|
|
|
* @param \Illuminate\Http\Request $request |
156
|
|
|
* |
157
|
|
|
* @return \Illuminate\Http\Response |
158
|
|
|
*/ |
159
|
|
|
public function toResponse($request) |
160
|
|
|
{ |
161
|
|
|
return response()->json( |
|
|
|
|
162
|
|
|
$this->resolve($request)->toArray() |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the query builder. |
168
|
|
|
* |
169
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
172
|
|
|
*/ |
173
|
84 |
|
protected static function getQuery($model): Builder |
174
|
|
|
{ |
175
|
84 |
|
return $model instanceof Builder ? $model : (new $model)->newQuery(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Cache the result. |
180
|
|
|
* |
181
|
|
|
* @param mixed $cacheFor |
182
|
|
|
* @param \Closure $callback |
183
|
|
|
* |
184
|
|
|
* @return mixed |
185
|
|
|
*/ |
186
|
8 |
|
protected function cacheResult($cacheFor, Closure $callback) |
187
|
|
|
{ |
188
|
8 |
|
if (is_numeric($cacheFor)) |
189
|
4 |
|
$cacheFor = new DateInterval(sprintf('PT%dS', $cacheFor * 60)); |
190
|
|
|
|
191
|
8 |
|
$key = sprintf( |
192
|
8 |
|
'%s.%s.%s.%s.%s', |
193
|
8 |
|
Str::slug($this->getCachePrefix(), '.'), |
194
|
8 |
|
Str::slug(str_replace('\\', '_', static::class)), |
195
|
8 |
|
$this->request->input('range', 'no-range'), |
196
|
8 |
|
$this->request->input('timezone', 'no-timezone'), |
197
|
8 |
|
$this->request->input('twelveHourTime', '24-hour-time') |
198
|
|
|
); |
199
|
|
|
|
200
|
8 |
|
return Cache::remember($key, $cacheFor, $callback); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the cache's prefix. |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
8 |
|
protected function getCachePrefix(): string |
209
|
|
|
{ |
210
|
8 |
|
return config('metrics.cache.prefix', 'arcanedev.metrics'); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|