1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vormkracht10\SlowQuery\Notifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Database\Events\QueryExecuted; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Facades\App; |
10
|
|
|
use Illuminate\Support\Facades\Config; |
11
|
|
|
use Illuminate\Support\Facades\Request; |
12
|
|
|
use NotificationChannels\Discord\DiscordChannel; |
13
|
|
|
use NotificationChannels\Discord\DiscordMessage; |
14
|
|
|
|
15
|
|
|
class SlowQueryDetected extends Notification |
16
|
|
|
{ |
17
|
|
|
use Queueable; |
18
|
|
|
|
19
|
|
|
public function __construct(QueryExecuted $query) |
20
|
|
|
{ |
21
|
|
|
$this->query = $query; |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function via() |
25
|
|
|
{ |
26
|
|
|
return [DiscordChannel::class]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function toDiscord() |
30
|
|
|
{ |
31
|
|
|
$message = Arr::random([ |
32
|
|
|
'☝🏻', '⚠️', '❗️', '🚨', '🤖', |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
$message .= ' Slow query has been detected on **' . Config::get('app.name') . '**'; |
36
|
|
|
$additional = []; |
37
|
|
|
|
38
|
|
|
if (!App::runningInConsole()) { |
39
|
|
|
$url = Request::fullUrl(); |
40
|
|
|
|
41
|
|
|
$additional = [ |
42
|
|
|
'url' => $url, |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return DiscordMessage::create($message, array_merge([ |
47
|
|
|
'title' => Config::get('app.name'), |
48
|
|
|
'color' => '15105570', |
49
|
|
|
'fields' => [ |
50
|
|
|
[ |
51
|
|
|
'name' => 'SQL', |
52
|
|
|
'value' => $this->query->sql, |
53
|
|
|
'inline' => false, |
54
|
|
|
], [ |
55
|
|
|
'name' => 'Time run', |
56
|
|
|
'value' => ($this->query->time / 1000) . ' seconds', |
57
|
|
|
'inline' => false, |
58
|
|
|
], [ |
59
|
|
|
'name' => 'URL', |
60
|
|
|
'value' => $url ?? 'Console', |
61
|
|
|
'inline' => false, |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
], $additional)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: