Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit a032578

Browse files
committed
ENH: refs #917. Schedule temp/unofficial scalars for deletion
1 parent 0b6f18a commit a032578

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

modules/tracker/Notification.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Tracker_Notification extends ApiEnabled_Notification
1616
{
1717
public $moduleName = 'tracker';
1818
public $_models = array('User');
19-
public $_moduleModels = array('Trend');
19+
public $_moduleModels = array('Scalar', 'Trend');
2020
public $_moduleComponents = array('Api');
2121

2222
/** init notification process*/
@@ -33,6 +33,7 @@ public function init()
3333
$this->addCallBack('CALLBACK_CORE_USER_DELETED', 'userDeleted');
3434

3535
$this->addTask('TASK_TRACKER_SEND_THRESHOLD_NOTIFICATION', 'sendEmail', 'Send threshold violation email');
36+
$this->addTask('TASK_TRACKER_DELETE_TEMP_SCALAR', 'deleteTempScalar', 'Delete an unofficial/temporary scalar value');
3637
$this->enableWebAPI($this->moduleName);
3738
}//end init
3839

@@ -72,6 +73,17 @@ public function userDeleted($args)
7273
$user = $args['userDao'];
7374
}
7475

76+
/**
77+
* Delete temporary (unofficial) scalars after n hours, where n is specified as
78+
* a module configuration option
79+
*/
80+
public function deleteTempScalar($params)
81+
{
82+
$scalarId = $params['scalarId'];
83+
$scalar = $this->Tracker_Scalar->load($scalarId);
84+
$this->Tracker_Scalar->delete($scalar);
85+
}
86+
7587
/**
7688
* Send an email to the user that a threshold was crossed
7789
*/

modules/tracker/controllers/components/ApiComponent.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ public function scalarAdd($args)
204204
$notifyComponent = MidasLoader::loadComponent('ThresholdNotification', 'tracker');
205205
$notifyComponent->scheduleNotifications($scalar, $notifications);
206206
}
207+
if(!$official)
208+
{
209+
$jobModel = MidasLoader::loadModel('Job', 'scheduler');
210+
$settingModel = MidasLoader::loadModel('Setting');
211+
$nHours = $settingModel->getValueByName('tempScalarTtl', 'tracker');
212+
if(!$nHours)
213+
{
214+
$nHours = 24; //default to 24 hours
215+
}
216+
foreach($notifications as $notification)
217+
{
218+
$job = MidasLoader::newDao('JobDao', 'scheduler');
219+
$job->setTask('TASK_TRACKER_DELETE_TEMP_SCALAR');
220+
$job->setPriority(1);
221+
$job->setRunOnlyOnce(1);
222+
$job->setFireTime(date('Y-m-j G:i:s', strtotime('+'.$nHours.' hours')));
223+
$job->setTimeInterval(0);
224+
$job->setStatus(SCHEDULER_JOB_STATUS_TORUN);
225+
$job->setCreatorId($user->getKey());
226+
$job->setParams(JsonComponent::encode(array('scalarId' => $scalar->getKey())));
227+
$jobModel->save($job);
228+
}
229+
}
207230
return $scalar;
208231
}
209232

@@ -229,6 +252,16 @@ public function resultsUploadJson($args)
229252
$user = $this->_getUser($args);
230253

231254
$official = !array_key_exists('unofficial', $args);
255+
if(!$official)
256+
{
257+
$jobModel = MidasLoader::loadModel('Job', 'scheduler');
258+
$settingModel = MidasLoader::loadModel('Setting');
259+
$nHours = $settingModel->getValueByName('tempScalarTtl', 'tracker');
260+
if(!$nHours)
261+
{
262+
$nHours = 24; //default to 24 hours
263+
}
264+
}
232265

233266
// Unofficial submissions only require read access to the community
234267
$community = $communityModel->load($args['communityId']);
@@ -362,6 +395,22 @@ public function resultsUploadJson($args)
362395
$notifyComponent = MidasLoader::loadComponent('ThresholdNotification', 'tracker');
363396
$notifyComponent->scheduleNotifications($scalar, $notifications);
364397
}
398+
if(!$official)
399+
{
400+
foreach($notifications as $notification)
401+
{
402+
$job = MidasLoader::newDao('JobDao', 'scheduler');
403+
$job->setTask('TASK_TRACKER_DELETE_TEMP_SCALAR');
404+
$job->setPriority(1);
405+
$job->setRunOnlyOnce(1);
406+
$job->setFireTime(date('Y-m-j G:i:s', strtotime('+'.$nHours.' hours')));
407+
$job->setTimeInterval(0);
408+
$job->setStatus(SCHEDULER_JOB_STATUS_TORUN);
409+
$job->setCreatorId($user->getKey());
410+
$job->setParams(JsonComponent::encode(array('scalarId' => $scalar->getKey())));
411+
$jobModel->save($job);
412+
}
413+
}
365414
}
366415
}
367416
}

0 commit comments

Comments
 (0)