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