1 | <?php |
||
2 | |||
3 | namespace Insense\LaravelTelescopePruning\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Support\Facades\DB; |
||
7 | |||
8 | class EntryModel extends Model |
||
9 | { |
||
10 | /** |
||
11 | * The table associated with the model. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $table = 'telescope_entries'; |
||
16 | |||
17 | /** |
||
18 | * The name of the "updated at" column. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | const UPDATED_AT = null; |
||
23 | |||
24 | /** |
||
25 | * The attributes that should be cast to native types. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $casts = [ |
||
30 | 'content' => 'json', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * The primary key for the model. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $primaryKey = 'uuid'; |
||
39 | |||
40 | /** |
||
41 | * The "type" of the auto-incrementing ID. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $keyType = 'string'; |
||
46 | |||
47 | /** |
||
48 | * Prevent Eloquent from overriding uuid with `lastInsertId`. |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | public $incrementing = false; |
||
53 | |||
54 | /** |
||
55 | * Get the current connection name for the model. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getConnectionName() |
||
60 | { |
||
61 | return config('telescope.storage.database.connection'); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Scope the query to exclude entries with monitored tags. |
||
66 | * |
||
67 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
68 | * |
||
69 | * @return \Illuminate\Database\Eloquent\Builder |
||
70 | */ |
||
71 | public function scopeNotMonitored($query) |
||
72 | { |
||
73 | return $query->whereDoesntHave('tags', function ($query) { |
||
74 | $query->whereIn('tag', function ($query) { |
||
75 | $query->select('tag')->from('telescope_monitoring'); |
||
76 | }); |
||
77 | }); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Scope the query to include entries with monitored tags. |
||
82 | * |
||
83 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
84 | * |
||
85 | * @return \Illuminate\Database\Eloquent\Builder |
||
86 | */ |
||
87 | public function scopeMonitored($query) |
||
88 | { |
||
89 | return $query->whereHas('tags', function ($query) { |
||
90 | $query->whereIn('tag', function ($query) { |
||
91 | $query->select('tag')->from('telescope_monitoring'); |
||
92 | }); |
||
93 | }); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Scope the query to exclude entries with specific whitelisted tags. |
||
98 | * |
||
99 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
100 | * |
||
101 | * @return \Illuminate\Database\Eloquent\Builder |
||
102 | */ |
||
103 | public function scopeExcludeSpecificWhitelisted($query) |
||
104 | { |
||
105 | return $query->whereDoesntHave('tags', function ($query) { |
||
106 | $query->whereIn('tag', config('telescope-pruning.specific_tags', [])); |
||
107 | }); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Scope the query to include entries with specific whitelisted tags. |
||
112 | * |
||
113 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
114 | * |
||
115 | * @return \Illuminate\Database\Eloquent\Builder |
||
116 | */ |
||
117 | public function scopeIncludeSpecificWhitelisted($query) |
||
118 | { |
||
119 | return $query->whereHas('tags', function ($query) { |
||
120 | $query->whereIn('tag', config('telescope-pruning.specific_tags', [])); |
||
121 | }); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Scope the query to exclude entries with whitelisted tags. |
||
126 | * |
||
127 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
128 | * |
||
129 | * @return \Illuminate\Database\Eloquent\Builder |
||
130 | */ |
||
131 | public function scopeNotWhitelisted($query) |
||
132 | { |
||
133 | return $query->when( |
||
134 | config('telescope-pruning.skip_monitored_tags', true), |
||
135 | function ($query) { |
||
136 | $query->notMonitored(); |
||
137 | } |
||
138 | )->excludeSpecificWhitelisted(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Scope the query to include entries with whitelisted tags. |
||
143 | * |
||
144 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
145 | * |
||
146 | * @return \Illuminate\Database\Eloquent\Builder |
||
147 | */ |
||
148 | public function scopeWhitelisted($query) |
||
149 | { |
||
150 | return $query->where(function ($query) { |
||
151 | $query->when(!empty(config('telescope-pruning.specific_tags')), function ($query) { |
||
152 | $query->includeSpecificWhitelisted(); |
||
153 | })->orWhere(function ($query) { |
||
154 | $query->monitored(); |
||
155 | }); |
||
156 | }); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Scope the query to include prunable non-whitelisted entries. |
||
161 | * |
||
162 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
163 | * |
||
164 | * @return \Illuminate\Database\Eloquent\Builder |
||
165 | */ |
||
166 | public function scopePrunableNonWhitelisted($query) |
||
167 | { |
||
168 | return $query->whereNotIn( |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
169 | 'batch_id', |
||
170 | static::whitelisted()->select('batch_id')->groupBy('batch_id') |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Scope the query to include prunable non-whitelisted entries. |
||
176 | * |
||
177 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
178 | * |
||
179 | * @return \Illuminate\Database\Eloquent\Builder |
||
180 | */ |
||
181 | public function scopePrunableWhitelisted($query) |
||
182 | { |
||
183 | return $query->whereIn( |
||
0 ignored issues
–
show
|
|||
184 | 'batch_id', |
||
185 | static::whitelisted()->select('batch_id')->groupBy('batch_id') |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Prune the entries based on the limit and whitelisted tags. |
||
191 | * |
||
192 | * @param int $limit |
||
193 | * |
||
194 | * @return int |
||
195 | */ |
||
196 | public static function prune($limit = 0, $whitelistedLimit = null) |
||
197 | { |
||
198 | return static::pruneNonWhitelisted($limit) + static::pruneWhitelisted($whitelistedLimit); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Prune the non-whitelisted entries based on the limit. |
||
203 | * |
||
204 | * @param int $limit |
||
205 | * |
||
206 | * @return int |
||
207 | */ |
||
208 | public static function pruneNonWhitelisted($limit = 0) |
||
209 | { |
||
210 | if (is_null($limit)) { |
||
0 ignored issues
–
show
|
|||
211 | // Pruning is disabled |
||
212 | return 0; |
||
213 | } |
||
214 | |||
215 | $subQuery = static::prunableNonWhitelisted()->select('batch_id') |
||
216 | ->groupBy('batch_id'); |
||
217 | $prunableBatchCount = DB::connection(config('telescope.storage.database.connection')) |
||
218 | ->table(DB::raw("({$subQuery->toSql()}) as sub"))->mergeBindings($subQuery->getQuery())->count(); |
||
219 | |||
220 | if ($prunableBatchCount <= $limit) { |
||
221 | // Prunable batch count is within the limit, so don't prune any entries. |
||
222 | return 0; |
||
223 | } |
||
224 | |||
225 | $numPrunes = $prunableBatchCount - $limit; |
||
226 | |||
227 | return static::whereIn( |
||
228 | 'batch_id', |
||
229 | function ($query) use ($numPrunes) { |
||
230 | $query->select('batch_id')->fromSub( |
||
231 | static::prunableNonWhitelisted() |
||
232 | ->select('batch_id')->selectRaw('max(sequence) as seq') |
||
233 | ->groupBy('batch_id')->orderBy('seq') |
||
234 | ->limit($numPrunes)->toBase(), |
||
235 | 'prunable_nonwhitelisted' |
||
236 | ); |
||
237 | } |
||
238 | )->delete(); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Prune the whitelisted entries based on the limit. |
||
243 | * |
||
244 | * @param int $limit |
||
245 | * |
||
246 | * @return int |
||
247 | */ |
||
248 | public static function pruneWhitelisted($limit = null) |
||
249 | { |
||
250 | if (is_null($limit)) { |
||
251 | // Pruning is disabled |
||
252 | return 0; |
||
253 | } |
||
254 | |||
255 | $subQuery = static::whitelisted()->select('batch_id') |
||
256 | ->groupBy('batch_id'); |
||
257 | $prunableBatchCount = DB::connection(config('telescope.storage.database.connection')) |
||
258 | ->table(DB::raw("({$subQuery->toSql()}) as sub"))->mergeBindings($subQuery->getQuery())->count(); |
||
259 | |||
260 | if ($prunableBatchCount <= $limit) { |
||
261 | // Prunable batch count is within the limit, so don't prune any entries. |
||
262 | return 0; |
||
263 | } |
||
264 | |||
265 | $numPrunes = $prunableBatchCount - $limit; |
||
266 | |||
267 | return static::whereIn( |
||
268 | 'batch_id', |
||
269 | function ($query) use ($numPrunes) { |
||
270 | $query->select('batch_id')->fromSub( |
||
271 | static::prunableWhitelisted() |
||
272 | ->select('batch_id')->selectRaw('max(sequence) as seq') |
||
273 | ->groupBy('batch_id')->orderBy('seq') |
||
274 | ->limit($numPrunes)->toBase(), |
||
275 | 'prunable_whitelisted' |
||
276 | ); |
||
277 | } |
||
278 | )->delete(); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * The tags related to the Telescope entries. |
||
283 | * |
||
284 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
285 | */ |
||
286 | public function tags() |
||
287 | { |
||
288 | return $this->hasMany(EntryTagModel::class, 'entry_uuid'); |
||
289 | } |
||
290 | } |
||
291 |