-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added slack notification channel. * Updated readme. *Added screenshots.
- Loading branch information
Showing
8 changed files
with
287 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
namespace YorCreative\QueryWatcher\Strategies\NotificationStrategy\Channels; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use YorCreative\QueryWatcher\Models\QueryModel; | ||
use YorCreative\QueryWatcher\Strategies\NotificationStrategy\NotificationChannelInterface; | ||
|
||
class Slack implements NotificationChannelInterface | ||
{ | ||
/** | ||
* @return bool | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return config('querywatcher.channels.slack.enabled'); | ||
} | ||
|
||
/** | ||
* @param QueryModel $model | ||
*/ | ||
public function notify(QueryModel $model): void | ||
{ | ||
// Core Base Information | ||
$payload = $this->buildCoreBaseEnrichment($model); | ||
|
||
// Contextual Information | ||
if ($model->trigger['action'] == 'Console') { | ||
$payload['blocks'] = $this->buildConsoleEnrichment($payload['blocks']); | ||
} else { | ||
$payload['blocks'] = $this->buildRequestEnrichment($payload['blocks'], $model); | ||
} | ||
|
||
// Fire off hook | ||
Http::retry(3)->contentType('application/json')->post(config('querywatcher.channels.slack.hook'), $payload); | ||
} | ||
|
||
/** | ||
* @note Slack Block Builder Reference | ||
* https://app.slack.com/block-kit-builder | ||
* | ||
* If you would like to improve your Slack message, see the block kit builder. | ||
*/ | ||
|
||
/** | ||
* @param QueryModel $model | ||
* @return \array[][] | ||
*/ | ||
protected function buildCoreBaseEnrichment(QueryModel $model): array | ||
{ | ||
return [ | ||
'blocks' => [ | ||
[ | ||
'type' => 'divider', | ||
], | ||
[ | ||
'type' => 'header', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => 'Captured Query', | ||
], | ||
], | ||
[ | ||
'type' => 'section', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => $model->sql, | ||
], | ||
], | ||
[ | ||
'type' => 'header', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => 'Query Bindings', | ||
], | ||
], | ||
[ | ||
'type' => 'section', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => json_encode($model->getBindings()), | ||
], | ||
], | ||
[ | ||
'type' => 'header', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => 'Execution Time', | ||
], | ||
], | ||
[ | ||
'type' => 'section', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => json_encode($model->time).' ms', | ||
], | ||
], | ||
[ | ||
'type' => 'header', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => 'Connection', | ||
], | ||
], | ||
[ | ||
'type' => 'section', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => $model->connection, | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param array $payload | ||
* @return array | ||
*/ | ||
protected function buildConsoleEnrichment(array $payload): array | ||
{ | ||
return array_merge($payload, [ | ||
[ | ||
'type' => 'header', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => 'Contextual Information', | ||
], | ||
], | ||
[ | ||
'type' => 'context', | ||
'elements' => [ | ||
[ | ||
'type' => 'mrkdwn', | ||
'text' => '*Trigger:* Console', | ||
], | ||
], | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @param array $payload | ||
* @param QueryModel $model | ||
* @return array | ||
*/ | ||
protected function buildRequestEnrichment(array $payload, QueryModel $model): array | ||
{ | ||
return array_merge($payload, [ | ||
[ | ||
'type' => 'header', | ||
'text' => [ | ||
'type' => 'plain_text', | ||
'text' => 'Contextual Information', | ||
], | ||
], | ||
[ | ||
'type' => 'context', | ||
'elements' => [ | ||
[ | ||
'type' => 'mrkdwn', | ||
'text' => '*Trigger:* '.$model->trigger['action'], | ||
], | ||
], | ||
], | ||
[ | ||
'type' => 'context', | ||
'elements' => [ | ||
[ | ||
'type' => 'mrkdwn', | ||
'text' => '*Method:* '.$model->trigger['context']['method'], | ||
], | ||
], | ||
], | ||
[ | ||
'type' => 'context', | ||
'elements' => [ | ||
[ | ||
'type' => 'mrkdwn', | ||
'text' => '*URL:* '.$model->trigger['context']['url'], | ||
], | ||
], | ||
], | ||
[ | ||
'type' => 'context', | ||
'elements' => [ | ||
0 => [ | ||
'type' => 'mrkdwn', | ||
'text' => '*Request Input:*', | ||
], | ||
], | ||
], | ||
[ | ||
'type' => 'section', | ||
'text' => [ | ||
'type' => 'mrkdwn', | ||
'text' => json_encode($model->trigger['context']['input']), | ||
], | ||
], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace YorCreative\QueryWatcher\Tests\Unit\Strategies\Channels; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use YorCreative\QueryWatcher\Strategies\NotificationStrategy\Channels\Slack; | ||
use YorCreative\QueryWatcher\Tests\TestCase; | ||
|
||
class SlackTest extends TestCase | ||
{ | ||
public Slack $slackChannel; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); // TODO: Change the autogenerated stub | ||
|
||
$this->slackChannel = new Slack(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group Unit | ||
* @group Channels | ||
* @group Strategies | ||
*/ | ||
public function it_can_send_http_request_to_slack() | ||
{ | ||
Http::fake(); | ||
|
||
$this->slackChannel->notify($this->queryModel); | ||
|
||
Http::assertSentCount(1); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group Unit | ||
* @group Channels | ||
* @group Strategies | ||
*/ | ||
public function it_can_determine_that_slack_channel_is_enabled() | ||
{ | ||
app()['config']->set('querywatcher.channels.slack.enabled', true); | ||
$this->assertTrue($this->slackChannel->isEnabled()); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group Unit | ||
* @group Channels | ||
* @group Strategies | ||
*/ | ||
public function it_can_determine_that_slack_channel_is_not_enabled() | ||
{ | ||
app()['config']->set('querywatcher.channels.slack.enabled', false); | ||
$this->assertFalse($this->slackChannel->isEnabled()); | ||
} | ||
} |