Skip to content

Commit 9e38d79

Browse files
Changes to enable send alerts via Slack, #PG-4530
1 parent 6672d3b commit 9e38d79

File tree

16 files changed

+97
-69
lines changed

16 files changed

+97
-69
lines changed

API.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ public function getAlerts($idSites, $ifSuperUserReturnAllAlerts = false)
129129
* @param bool|string $reportCondition
130130
* @param bool|string $reportValue
131131
* @param array $reportMediums
132+
* @param string $slackChannelID
132133
* @return int ID of new Alert
133134
*/
134-
public function addAlert($name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false, array $reportMediums = [])
135+
public function addAlert($name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false, array $reportMediums = [], string $slackChannelID = '')
135136
{
136137
$idSites = Site::getIdSitesFromIdSitesString($idSites);
137138

138-
$this->checkAlert($idSites, $name, $period, $emailMe, $additionalEmails, $phoneNumbers, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums);
139+
$this->checkAlert($idSites, $name, $period, $emailMe, $additionalEmails, $phoneNumbers, $slackChannelID, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums);
139140

140141
$name = Common::unsanitizeInputValue($name);
141142
$login = Piwik::getCurrentUserLogin();
@@ -147,7 +148,7 @@ public function addAlert($name, $idSites, $period, $emailMe, $additionalEmails,
147148

148149
$metricValue = Common::forceDotAsSeparatorForDecimalPoint((float)$metricValue);
149150

150-
return $this->getModel()->createAlert($name, $idSites, $login, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums);
151+
return $this->getModel()->createAlert($name, $idSites, $login, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums, $slackChannelID);
151152
}
152153

153154
private function filterAdditionalEmails($additionalEmails)
@@ -181,12 +182,13 @@ private function filterPhoneNumbers($phoneNumbers)
181182
return array_values($phoneNumbers);
182183
}
183184

184-
private function checkAlert($idSites, $name, $period, &$emailMe, &$additionalEmails, &$phoneNumbers, $metricCondition, $metricValue, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums)
185+
private function checkAlert($idSites, $name, $period, &$emailMe, &$additionalEmails, &$phoneNumbers, &$slackChannelID, $metricCondition, $metricValue, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums)
185186
{
186187
Piwik::checkUserHasViewAccess($idSites);
187188
$additionalEmails = in_array('email', $reportMediums) ? $this->filterAdditionalEmails($additionalEmails) : [];
188189
$phoneNumbers = in_array('mobile', $reportMediums) ? $this->filterPhoneNumbers($phoneNumbers) : [];
189190
$emailMe = in_array('email', $reportMediums) && $emailMe;
191+
$slackChannelID = in_array('slack', $reportMediums) ? $slackChannelID : '';
190192

191193
$this->validator->checkName($name);
192194
$this->validator->checkPeriod($period);
@@ -224,17 +226,18 @@ private function checkAlert($idSites, $name, $period, &$emailMe, &$additionalEma
224226
* @param bool|string $reportCondition
225227
* @param bool|string $reportValue
226228
* @param array $reportMediums
229+
* @param string $slackChannelID
227230
*
228231
* @return boolean
229232
*/
230-
public function editAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false, array $reportMediums = [])
233+
public function editAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false, array $reportMediums = [], string $slackChannelID = '')
231234
{
232235
// make sure alert exists and user has permission to read
233236
$this->getAlert($idAlert);
234237

235238
$idSites = Site::getIdSitesFromIdSitesString($idSites);
236239

237-
$this->checkAlert($idSites, $name, $period, $emailMe, $additionalEmails, $phoneNumbers, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums);
240+
$this->checkAlert($idSites, $name, $period, $emailMe, $additionalEmails, $phoneNumbers, $slackChannelID, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId, $reportMediums);
238241

239242
$name = Common::unsanitizeInputValue($name);
240243

@@ -245,7 +248,7 @@ public function editAlert($idAlert, $name, $idSites, $period, $emailMe, $additio
245248

246249
$metricValue = Common::forceDotAsSeparatorForDecimalPoint((float)$metricValue);
247250

248-
return $this->getModel()->updateAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums);
251+
return $this->getModel()->updateAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums, $slackChannelID);
249252
}
250253

251254
/**

Controller.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Piwik\Plugins\CustomAlerts;
1111

1212
use Piwik\Common;
13+
use Piwik\Container\StaticContainer;
1314
use Piwik\Date;
1415
use Piwik\Period;
1516
use Piwik\Piwik;
@@ -277,6 +278,13 @@ private function addBasicCreateAndEditVariables($view, $alert)
277278
$view->comparablesDates = $comparablesDates;
278279
$view->reportMetadata = $this->findReportMetadata($alert);
279280
$view->supportsSMS = $this->supportsPlugin('MobileMessaging');
281+
$supportsSlack = $this->supportsPlugin('Slack');
282+
$isSlackOAuthTokenAdded = false;
283+
if ($supportsSlack) {
284+
$slackSettings = StaticContainer::get(\Piwik\Plugins\Slack\SystemSettings::class);
285+
$isSlackOAuthTokenAdded = !empty($slackSettings->slackOauthToken->getValue());
286+
}
287+
$view->isSlackOAuthTokenAdded = $isSlackOAuthTokenAdded;
280288
$view->periodOptions = array(
281289
array('key' => 'day', 'value' => Piwik::translate('Intl_PeriodDay')),
282290
array('key' => 'week', 'value' => Piwik::translate('Intl_PeriodWeek')),

CustomAlerts.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public function removePhoneNumberFromAllAlerts($phoneNumber)
178178
$alert['report'],
179179
$alert['report_condition'],
180180
$alert['report_matched'],
181-
$alert['report_mediums']
181+
$alert['report_mediums'],
182+
$alert['slack_channel_id']
182183
);
183184
}
184185
}

Model.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public static function install()
3636
`compared_to` SMALLINT (4) UNSIGNED NOT NULL DEFAULT 1 ,
3737
`email_me` BOOLEAN NOT NULL DEFAULT '0',
3838
`additional_emails` TEXT ,
39-
`phone_numbers` TEXT ";
39+
`phone_numbers` TEXT ,
40+
`slack_channel_id` TEXT ";
4041

4142
DbHelper::createTable('alert', $tableAlert);
4243

@@ -67,6 +68,7 @@ public static function install()
6768
`email_me` BOOLEAN NOT NULL DEFAULT '0',
6869
`additional_emails` TEXT ,
6970
`phone_numbers` TEXT ,
71+
`slack_channel_id` TEXT ,
7072
PRIMARY KEY (idtriggered)";
7173

7274
DbHelper::createTable('alert_triggered', $tableAlertLog);
@@ -251,11 +253,12 @@ public function getAllAlertsForPeriod($period)
251253
* @param string $reportCondition
252254
* @param string $reportValue
253255
* @param array $reportMediums
256+
* @param string $slackChannelID
254257
*
255258
* @return int ID of new Alert
256259
* @throws \Exception
257260
*/
258-
public function createAlert($name, $idSites, $login, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums)
261+
public function createAlert($name, $idSites, $login, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums, $slackChannelID)
259262
{
260263
$idAlert = $this->getNextAlertId();
261264

@@ -274,7 +277,8 @@ public function createAlert($name, $idSites, $login, $period, $emailMe, $additio
274277
'compared_to' => $comparedTo,
275278
'report_condition' => $reportCondition,
276279
'report_matched' => $reportValue,
277-
'report_mediums' => json_encode($reportMediums)
280+
'report_mediums' => json_encode($reportMediums),
281+
'slack_channel_id' => $slackChannelID
278282
);
279283

280284
$db = $this->getDb();
@@ -332,11 +336,12 @@ private function removeAllSites($idAlert)
332336
* @param string $reportCondition
333337
* @param string $reportValue
334338
* @param array $reportMediums
339+
* @param string $slackChannelID
335340
*
336341
* @return int
337342
* @throws \Exception
338343
*/
339-
public function updateAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums)
344+
public function updateAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue, $reportMediums, $slackChannelID)
340345
{
341346
$alert = array(
342347
'name' => $name,
@@ -352,6 +357,7 @@ public function updateAlert($idAlert, $name, $idSites, $period, $emailMe, $addit
352357
'report_condition' => $reportCondition,
353358
'report_matched' => $reportValue,
354359
'report_mediums' => json_encode($reportMediums),
360+
'slack_channel_id' => $slackChannelID
355361
);
356362

357363
$db = $this->getDb();
@@ -381,7 +387,7 @@ public function triggerAlert($idAlert, $idSite, $valueNew, $valueOld, $datetime)
381387
{
382388
$alert = $this->getAlert($idAlert);
383389

384-
$keysToKeep = array('idalert', 'name', 'login', 'period', 'metric', 'metric_condition', 'metric_matched', 'report', 'report_condition', 'report_matched', 'report_mediums', 'compared_to', 'email_me', 'additional_emails', 'phone_numbers');
390+
$keysToKeep = array('idalert', 'name', 'login', 'period', 'metric', 'metric_condition', 'metric_matched', 'report', 'report_condition', 'report_matched', 'report_mediums', 'compared_to', 'email_me', 'additional_emails', 'phone_numbers', 'slack_channel_id');
385391

386392
$triggeredAlert = array();
387393
foreach ($keysToKeep as $key) {
@@ -396,7 +402,7 @@ public function triggerAlert($idAlert, $idSite, $valueNew, $valueOld, $datetime)
396402
$triggeredAlert['idsite'] = $idSite;
397403
$triggeredAlert['additional_emails'] = json_encode($triggeredAlert['additional_emails']);
398404
$triggeredAlert['phone_numbers'] = json_encode($triggeredAlert['phone_numbers']);
399-
$triggeredAlert['report_mediums'] = json_encode($triggeredAlert['report_mediums']);
405+
$triggeredAlert['report_mediums'] = json_encode($triggeredAlert['report_mediums']);
400406

401407
$db = $this->getDb();
402408
$db->insert(

Notifier.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function sendNewAlerts($period, $idSite)
4545
$this->sendAlertsPerSmsToRecipient($alerts, new \Piwik\Plugins\MobileMessaging\Model(), $phoneNumber);
4646
}
4747

48+
Piwik::postEvent('CustomAlerts.sendNewAlerts', [$triggeredAlerts]);
49+
4850
foreach ($triggeredAlerts as $triggeredAlert) {
4951
$this->markAlertAsSent($triggeredAlert);
5052
}

Updates/5.1.0.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function getMigrations(Updater $updater)
4545
return array(
4646
$this->migration->db->addColumn('alert', 'report_mediums', 'TEXT NOT NULL', 'report_matched'),
4747
$this->migration->db->addColumn('alert_triggered', 'report_mediums', 'TEXT NOT NULL', 'report_matched'),
48-
$this->migration->db->addColumn('alert', 'slack_channel_id', 'VARCHAR(1000) NULL', 'phone_numbers'),
49-
$this->migration->db->addColumn('alert_triggered', 'slack_channel_id', 'VARCHAR(1000) NULL', 'phone_numbers'),
48+
$this->migration->db->addColumn('alert', 'slack_channel_id', 'TEXT NULL', 'phone_numbers'),
49+
$this->migration->db->addColumn('alert_triggered', 'slack_channel_id', 'TEXT NULL', 'phone_numbers'),
5050
$this->migration->db->sql("UPDATE `$alertTableName` set report_mediums=CASE WHEN (email_me=1 OR additional_emails!='[]') AND phone_numbers!='[]' THEN '$emailPhoneJson' WHEN (email_me=1 OR additional_emails!='[]') AND phone_numbers='[]' THEN '$emailJson' WHEN (email_me!=1 AND additional_emails='[]') AND phone_numbers!='[]' THEN '$phoneJson' ELSE '$emptyJson' END"),
5151
$this->migration->db->sql("UPDATE `$alertTriggeredTableName` set report_mediums=CASE WHEN (email_me=1 OR additional_emails!='[]') AND phone_numbers!='[]' THEN '$emailPhoneJson' WHEN (email_me=1 OR additional_emails!='[]') AND phone_numbers='[]' THEN '$emailJson' WHEN (email_me!=1 AND additional_emails='[]') AND phone_numbers!='[]' THEN '$phoneJson' ELSE '$emptyJson' END"),
5252
);

lang/en.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@
7878
"MediumMobile": "Mobile",
7979
"MediumSlack": "Slack",
8080
"MediumTitle": "Send alerts via",
81-
"MediumDescription": "Select the medium to send alerts.",
82-
"EmptyReportMediums": "Report mediums cannot be empty.",
81+
"MediumDescription": "Choose how you want to receive alerts when this custom alert is triggered.",
82+
"EmptyReportMediums": "At least one delivery method must be selected.",
8383
"InvalidReportMediums": "Invalid report mediums value. Allowed values are %1$s.",
8484
"InvalidEmailReportParameter": "Please select \"Send to me\" or specify valid email addresses to email the report.",
85-
"InvalidPhoneNumberReportParameter": "Phone numbers cannot be empty."
85+
"InvalidPhoneNumberReportParameter": "Phone numbers cannot be empty. Please activate at least one phone number by accessing the Mobile Messaging settings page."
8686
}
8787
}

templates/form.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
supports-s-m-s="{{ supportsSMS|default(null)|json_encode }}"
1010
mobile-macro="{{ mobileMacro|default(null)|json_encode }}"
1111
phone-numbers="{{ phoneNumbers|default([])|json_encode }}"
12+
is-slack-oauth-token-added="{{ isSlackOAuthTokenAdded|default(null)|json_encode }}"
1213
report-metadata="{{ reportMetadata|default(null)|json_encode }}"
1314
alert-group-conditions="{{ alertGroupConditions|default(null)|json_encode }}"
1415
metric-condition-options="{{ metricConditionOptions|default(null)|json_encode }}"

tests/Fixtures/CustomAlerts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function createAlert($name, $period, $idSites, $metric, $report, $login
7676
}
7777

7878
$model = new Model();
79-
$model->createAlert($name, $idSites, $login, $period, 0, $emails, $phoneNumbers, $metric, 'less_than', 5, $comparedTo = 1, $report, 'matches_exactly', $reportMatched, ['email', 'mobile']);
79+
$model->createAlert($name, $idSites, $login, $period, 0, $emails, $phoneNumbers, $metric, 'less_than', 5, $comparedTo = 1, $report, 'matches_exactly', $reportMatched, ['email', 'mobile'], '');
8080
}
8181

8282
private function triggerAlert($idAlert, $valueNew, $valueOld, $datetime)

tests/Integration/ApiTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ protected function createAlert(
6161
$report,
6262
$reportCondition,
6363
'Piwik',
64-
['email', 'mobile']
64+
['email', 'mobile'],
65+
''
6566
);
6667
return $id;
6768
}
@@ -225,6 +226,7 @@ protected function assertIsAlert(
225226
'email_me' => 0,
226227
'additional_emails' => array('[email protected]', '[email protected]'),
227228
'phone_numbers' => array(),
229+
'slack_channel_id' => '',
228230
'compared_to' => 1,
229231
'id_sites' => $idSites,
230232
'report_mediums' => ['email', 'mobile']
@@ -580,6 +582,7 @@ public function test_triggerAlert_getTriggeredAlertsForPeriod_ShouldMarkAlertAsT
580582
'value_old' => 48,
581583
'additional_emails' => array('[email protected]', '[email protected]'),
582584
'phone_numbers' => array(),
585+
'slack_channel_id' => '',
583586
'email_me' => 0,
584587
'compared_to' => 1,
585588
'id_sites' => array(1, 2),

0 commit comments

Comments
 (0)