Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions modules/tracker/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Tracker_Notification extends ApiEnabled_Notification
public $_models = array('User');

/** @var array */
public $_moduleModels = array('AggregateMetric', 'AggregateMetricSpec', 'Scalar', 'Submission', 'Trend');
public $_moduleModels = array('AggregateMetric', 'AggregateMetricNotification', 'AggregateMetricSpec', 'Scalar', 'Submission', 'Trend');

/** @var array */
public $_moduleComponents = array('Api');
Expand Down Expand Up @@ -224,27 +224,37 @@ public function sendAggregateEmail($params)
$userDao = $this->User->load($params['recipient_id']);
if ($userDao === false) {
$this->getLogger()->warn(
'Attempting to send aggregate metric threshold notification to user id '.$params['recipientId'].': No such user.'
'Attempting to send aggregate metric threshold notification to user id '.$params['recipient_id'].': No such user.'
);

return;
}

/** @var Tracker_AggregateMetricDao $aggregateMetricDao */
$aggregateMetricDao = $this->Tracker_AggregateMetric->load($params['aggregate_metric_id']);
if ($aggregateMetricDao === false) {
$this->getLogger()->warn(
'Attempting to send aggregate metric threshold notification with aggregate metric '.$params['aggregate_metric_id'].': No such metric.'
);

return;
}

/** @var Tracker_AggregateMetricSpecDao $aggregateMetricSpecDao */
$aggregateMetricSpecDao = $this->Tracker_AggregateMetricSpec->load($params['aggregate_metric_spec_id']);
$aggregateMetricSpecDao = $aggregateMetricDao->getAggregateMetricSpec();
if ($aggregateMetricSpecDao === false) {
$this->getLogger()->warn(
'Attempting to send aggregate metric threshold notification with aggregate metric spec '.$params['aggregateMetricSpecId'].': No such spec.'
'Attempting to send aggregate metric threshold notification with aggregate metric spec that does not exist.'
);

return;
}

/** @var Tracker_AggregateMetricDao $aggregateMetricDao */
$aggregateMetricDao = $this->Tracker_AggregateMetric->load($params['aggregate_metric_id']);
/** @var Tracker_AggregateMetricNotificationDao $aggregateMetricNotificationDao */
$aggregateMetricNotificationDao = $this->Tracker_AggregateMetricNotification->load($params['aggregate_metric_notification_id']);
if ($aggregateMetricDao === false) {
$this->getLogger()->warn(
'Attempting to send aggregate metric threshold notification with aggregate metric '.$params['aggregateMetricId'].': No such metric.'
'Attempting to send aggregate metric threshold notification with aggregate metric notification id '.$params['aggregate_metric_notification_id'].': No such notification.'
);

return;
Expand All @@ -257,9 +267,9 @@ public function sendAggregateEmail($params)

$producerName = $producerDao->getDisplayName();
$metricName = $aggregateMetricSpecDao->getName();
$branch = $aggregateMetricSpecDao->getBranch();
$thresholdValue = $aggregateMetricSpecDao->getValue();
$thresholdComparison = $aggregateMetricSpecDao->getComparison();
$thresholdValue = $aggregateMetricNotificationDao->getValue();
$thresholdComparison = $aggregateMetricNotificationDao->getComparison();
$branch = $aggregateMetricNotificationDao->getBranch();
$metricValue = $aggregateMetricDao->getValue();
$subject = 'Threshold Alert: '.$producerName.': '.$metricName;

Expand Down
2 changes: 1 addition & 1 deletion modules/tracker/configs/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ description = "Track scalar results over time"
category = "Visualization"
dependencies = api,scheduler
uuid = "3048a9fa-89ab-4e61-a55e-a49379fa6dc"
version = "2.0.0"
version = "2.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/*=========================================================================
Midas Server
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
All rights reserved.
For more information visit http://www.kitware.com/.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0.txt

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** API aggregateMetricNotification controller for the tracker module. */
class Apitracker_AggregatemetricnotificationController extends ApiController
{
/** @var string */
public $moduleName = 'tracker';

/** Handle HTTP DELETE requests. Requires an id parameter. */
public function deleteAction()
{
$this->_genericAction(
$this->_request->getParams(),
$this->_request->getControllerName(),
$this->_request->getActionName(),
array('default' => $this->_request->getActionName()),
$this->moduleName,
false
);
}

/** Handle HTTP GET requests. Requires an id parameter. */
public function getAction()
{
$this->_genericAction(
$this->_request->getParams(),
$this->_request->getControllerName(),
$this->_request->getActionName(),
array('default' => $this->_request->getActionName()),
$this->moduleName,
false
);
}

/** Handle HTTP HEAD requests. */
public function headAction()
{
$this->_response->setHttpResponseCode(200); // 200 OK
}

/** Handle HTTP GET index or list requests. */
public function indexAction()
{
$this->_genericAction(
$this->_request->getParams(),
$this->_request->getControllerName(),
$this->_request->getActionName(),
array('default' => $this->_request->getActionName()),
$this->moduleName,
false
);
}

/** Handle HTTP OPTIONS requests. */
public function optionsAction()
{
$this->_response->setHeader('Allow', 'DELETE, GET, HEAD, OPTIONS, POST, PUT');
}

/** Handle HTTP POST requests. */
public function postAction()
{
$this->_genericAction(
$this->_request->getParams(),
$this->_request->getControllerName(),
$this->_request->getActionName(),
array('default' => $this->_request->getActionName()),
$this->moduleName,
false
);
}

/** Handle HTTP PUT requests. Requires an id parameter. */
public function putAction()
{
$this->_genericAction(
$this->_request->getParams(),
$this->_request->getControllerName(),
$this->_request->getActionName(),
array('default' => $this->_request->getActionName()),
$this->moduleName,
false
);
}
}
89 changes: 52 additions & 37 deletions modules/tracker/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,12 @@ public function aggregatemetricsUpdate($args)
$aggregateMetrics = $aggregateMetricModel->updateAggregateMetricsForSubmission($submissionDao);

if (array_key_exists('notify', $args) && $args['notify']) {
/** @var Tracker_AggregateMetricSpecModel $aggregateMetricSpecModel */
$aggregateMetricSpecModel = MidasLoader::loadModel('AggregateMetricSpec', 'tracker');
/** @var Tracker_ThresholdNotificationComponent $notifyComponent */
$notifyComponent = MidasLoader::loadComponent('ThresholdNotification', 'tracker');
/** @var Tracker_AggregateMetricNotificationModel $aggregateMetricNotificationModel */
$aggregateMetricNotificationModel = MidasLoader::loadModel('AggregateMetricNotification', 'tracker');
/** @var Tracker_AggregateMetricDao $aggregateMetricDao */
foreach ($aggregateMetrics as $aggregateMetricDao) {
/* @var Tracker_AggregateMetricSpecDao $aggregateMetricSpecDao */
$aggregateMetricSpecDao = $aggregateMetricDao->getAggregateMetricSpec();
$notificationJobs = $aggregateMetricSpecModel->scheduleNotificationJobs($aggregateMetricSpecDao, $aggregateMetricDao);
/* @var array $notificationJobs */
$notificationJobs = $aggregateMetricNotificationModel->scheduleNotificationJobs($aggregateMetricDao);
}
}

Expand Down Expand Up @@ -409,85 +406,103 @@ private function _loadAggregateMetricSpec($userDao, $aggregateMetricSpecId, $pol
}

/**
* Create a notification for a user against an aggregate metric spec, so that
* whenever an aggregate metric created from that aggregate metric spec
* Create a notification for a user against an aggregate metric notification,
* so that whenever an aggregate metric created from that aggregate metric spec
* is beyond the notification threshold, the user will be notified by email.
*
* @param userId The id of the user to create a notification for
* @param aggregateMetricSpecId The id of the aggregate metric spec
* @param userId The id of the user to tie to the notification
* @param aggregateMetricNotificationId The id of the aggregate metric notification
* @return UserDao the user DAO of the user who will be alerted
* @throws Exception
*/
public function aggregatemetricspecnotifieduserCreate($args)
{
$this->_checkKeys(array('userId', 'aggregateMetricSpecId'), $args);
$this->_checkKeys(array('userId', 'aggregateMetricNotificationId'), $args);
$user = $this->_getUser($args);

$aggregateMetricSpecId = $args['aggregateMetricSpecId'];
$aggregateMetricSpecDao = $this->_loadAggregateMetricSpec($user, $aggregateMetricSpecId, MIDAS_POLICY_ADMIN);
$aggregateMetricNotificationId = $args['aggregateMetricNotificationId'];
/** @var Tracker_AggregateMetricNotificationModel $aggregateMetricNotificationModel */
$aggregateMetricNotificationModel = MidasLoader::loadModel('AggregateMetricNotification', 'tracker');
/** @var Tracker_AggregateMetricNotificationDao $aggregateMetricNotificationDao */
$aggregateMetricNotificationDao = $aggregateMetricNotificationModel->load($aggregateMetricNotificationId);

/** @var Tracker_AggregateMetricSpecDao $aggregateMetricSpecDao */
$aggregateMetricSpecDao = $this->_loadAggregateMetricSpec($user, $aggregateMetricNotificationDao->getAggregateMetricSpecId(), MIDAS_POLICY_ADMIN);

/** @var UserModel $userModel */
$userModel = MidasLoader::loadModel('User');
/** @var UserDao $notificationUserDao */
$notificationUserDao = $userModel->load($args['userId']);

/** @var Tracker_AggregateMetricSpecModel $aggregateMetricSpecModel */
$aggregateMetricSpecModel = MidasLoader::loadModel('AggregateMetricSpec', 'tracker');
$aggregateMetricSpecModel->createUserNotification($aggregateMetricSpecDao, $notificationUserDao);
$aggregateMetricNotificationModel->createUserNotification($aggregateMetricNotificationDao, $notificationUserDao);

return $notificationUserDao;
}

/**
* Delete a notification for a user against an aggregate metric spec, so that
* the user will no longer receive notifications from aggregate metrics created
* from that aggregate metric spec.
* Delete a user from an aggregate metric notification,
* the user will no longer receive notifications when aggregate metrics created
* from the associated aggregate metric spec are beyond the notification threshold of the
* notification.
*
* @param userId The id of the user to delete a notification for
* @param aggregateMetricSpecId The id of the aggregate metric spec
* @param userId The id of the user to delete from the notification
* @param aggregateMetricNotificationId The id of the aggregate metric notification
* @return UserDao the user DAO of the user who will no longer be alerted
* @throws Exception
*/
public function aggregatemetricspecnotifieduserDelete($args)
{
$this->_checkKeys(array('userId', 'aggregateMetricSpecId'), $args);
$this->_checkKeys(array('userId', 'aggregateMetricNotificationId'), $args);
$user = $this->_getUser($args);

$aggregateMetricSpecId = $args['aggregateMetricSpecId'];
$aggregateMetricSpecDao = $this->_loadAggregateMetricSpec($user, $aggregateMetricSpecId, MIDAS_POLICY_ADMIN);
$aggregateMetricNotificationId = $args['aggregateMetricNotificationId'];
/** @var Tracker_AggregateMetricNotificationModel $aggregateMetricNotificationModel */
$aggregateMetricNotificationModel = MidasLoader::loadModel('AggregateMetricNotification', 'tracker');
/** @var Tracker_AggregateMetricNotificationDao $aggregateMetricNotificationDao */
$aggregateMetricNotificationDao = $aggregateMetricNotificationModel->load($aggregateMetricNotificationId);

/** @var Tracker_AggregateMetricSpecDao $aggregateMetricSpecDao */
$aggregateMetricSpecDao = $this->_loadAggregateMetricSpec($user, $aggregateMetricNotificationDao->getAggregateMetricSpecId(), MIDAS_POLICY_ADMIN);

/** @var UserModel $userModel */
$userModel = MidasLoader::loadModel('User');
/** @var UserDao $notificationUserDao */
$notificationUserDao = $userModel->load($args['userId']);

/** @var Tracker_AggregateMetricSpecModel $aggregateMetricSpecModel */
$aggregateMetricSpecModel = MidasLoader::loadModel('AggregateMetricSpec', 'tracker');
$aggregateMetricSpecModel->deleteUserNotification($aggregateMetricSpecDao, $notificationUserDao);
$aggregateMetricNotificationModel->deleteUserNotification($aggregateMetricNotificationDao, $notificationUserDao);

return $notificationUserDao;
}

/**
* Return a list of User Daos for all users with notifications on this aggregate metric spec.
* Return an array of associative arrays, with keys 'notification' => an AggregateMetricNotificationDao
* and 'users' => an array of UserDaos tied to the AggregateMetricNotificationDao, for each
* AggregateMetricNotification tied to the passed in AggregateMetricSpecId.
*
* @param aggregateMetricSpecId the id of the aggregate metric spec
* @return array of UserDao for all users with notification on the passed in aggregateMetricSpecId
* @return array of associative arrays with keys 'notification' and 'users'
*/
public function aggregatemetricspecnotifiedusersList($args)
public function aggregatemetricspecnotificationsList($args)
{
$this->_checkKeys(array('aggregateMetricSpecId'), $args);
$user = $this->_getUser($args);

/** @var Tracker_AggregateMetricSpecDao $aggregateMetricSpecDao */
$aggregateMetricSpecDao = $this->_loadAggregateMetricSpec($user, $args['aggregateMetricSpecId']);

/** @var Tracker_AggregateMetricSpecModel $aggregateMetricSpecModel */
$aggregateMetricSpecModel = MidasLoader::loadModel('AggregateMetricSpec', 'tracker');
$notifiedUsers = $aggregateMetricSpecModel->getAllNotifiedUsers($aggregateMetricSpecDao);
if ($notifiedUsers === false) {
$notifiedUsers = array();
/** @var Tracker_AggregateMetricNotificationModel $aggregateMetricNotificationModel */
$aggregateMetricNotificationModel = MidasLoader::loadModel('AggregateMetricNotification', 'tracker');
/** @var array $notifications */
$notifications = $aggregateMetricNotificationModel->findBy('aggregate_metric_spec_id', $aggregateMetricSpecDao->getAggregateMetricSpecId());
$response = array();
/** @var Tracker_AggregateMetricNotificationDao $notification */
foreach ($notifications as $notification) {
$response[] = array(
'notification' => $notification,
'users' => $aggregateMetricNotificationModel->getAllNotifiedUsers($notification),
);
}

return $notifiedUsers;
return $response;
}
}
Loading