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

Commit e7f16f0

Browse files
committed
ENH: refs #917. Add user_id and official fields to tracker_scalar
This will allow for user-specific experimental scalar submissions
1 parent 080e0fd commit e7f16f0

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

modules/tracker/controllers/components/ApiComponent.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function itemAssociate($args)
8989
* @param testDatasetId (Optional) If this value pertains to a specific test dataset, pass its id here
9090
* @param truthDatasetId (Optional) If this value pertains to a specific ground truth dataset, pass its id here
9191
* @param silent (Optional) If set, do not perform treshold-based email notifications for this scalar
92+
* @param unofficial (Optional) If passed, creates an unofficial scalar visible only to the user performing the submission
9293
* @return The scalar dao that was created
9394
*/
9495
public function scalarAdd($args)
@@ -98,8 +99,10 @@ public function scalarAdd($args)
9899
$this->_checkKeys(array('communityId', 'producerDisplayName', 'metricName', 'value', 'producerRevision', 'submitTime'), $args);
99100
$user = $this->_getUser($args);
100101

102+
$official = !array_key_exists('unofficial', $args);
103+
101104
$community = $communityModel->load($args['communityId']);
102-
if(!$community || !$communityModel->policyCheck($community, $user, MIDAS_POLICY_WRITE))
105+
if(!$community || !$communityModel->policyCheck($community, $user, $official ? MIDAS_POLICY_WRITE : MIDAS_POLICY_READ))
103106
{
104107
throw new Exception('Write permission required on community', 403);
105108
}
@@ -192,7 +195,7 @@ public function scalarAdd($args)
192195
$producerRevision = trim($args['producerRevision']);
193196

194197
$scalarModel = MidasLoader::loadModel('Scalar', 'tracker');
195-
$scalar = $scalarModel->addToTrend($trend, $submitTime, $producerRevision, $value, true);
198+
$scalar = $scalarModel->addToTrend($trend, $submitTime, $producerRevision, $value, $user, true, $official);
196199

197200
if(!isset($args['silent']))
198201
{
@@ -215,6 +218,7 @@ public function scalarAdd($args)
215218
* @param truthDatasetId (Optional) If this value pertains to a specific ground truth dataset, pass its id here
216219
* @param parentKeys (Optional) Semicolon-separated list of parent keys to look for numeric results under. Use '.' to denote nesting, like in normal javascript syntax.
217220
* @param silent (Optional) If set, do not perform treshold-based email notifications for this scalar
221+
* @param unofficial (Optional) If passed, creates an unofficial scalar visible only to the user performing the submission
218222
* @return The list of scalars that were created. Non-numeric values are ignored.
219223
*/
220224
public function resultsUploadJson($args)
@@ -224,8 +228,11 @@ public function resultsUploadJson($args)
224228
$this->_checkKeys(array('communityId', 'producerDisplayName', 'producerRevision'), $args);
225229
$user = $this->_getUser($args);
226230

231+
$official = !array_key_exists('unofficial', $args);
232+
233+
// Unofficial submissions only require read access to the community
227234
$community = $communityModel->load($args['communityId']);
228-
if(!$community || !$communityModel->policyCheck($community, $user, MIDAS_POLICY_WRITE))
235+
if(!$community || !$communityModel->policyCheck($community, $user, $official ? MIDAS_POLICY_WRITE : MIDAS_POLICY_READ))
229236
{
230237
throw new Exception('Write permission required on community', 403);
231238
}
@@ -345,7 +352,7 @@ public function resultsUploadJson($args)
345352
continue;
346353
}
347354
$trend = $trendModel->createIfNeeded($producer->getKey(), $metricName, $configItemId, $testDatasetId, $truthDatasetId);
348-
$scalar = $scalarModel->addToTrend($trend, $submitTime, $producerRevision, $value, true);
355+
$scalar = $scalarModel->addToTrend($trend, $submitTime, $producerRevision, $value, $user, true, $official);
349356
$scalars[] = $scalar;
350357

351358
if(!isset($args['silent']))
@@ -367,7 +374,7 @@ public function resultsUploadJson($args)
367374
continue;
368375
}
369376
$trend = $trendModel->createIfNeeded($producer->getKey(), $metricName, $configItemId, $testDatasetId, $truthDatasetId);
370-
$scalar = $scalarModel->addToTrend($trend, $submitTime, $producerRevision, $value, true);
377+
$scalar = $scalarModel->addToTrend($trend, $submitTime, $producerRevision, $value, $user, true, $official);
371378
$scalars[] = $scalar;
372379

373380
if(!isset($args['silent']))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* 1. Adds a user_id value to a scalar record indicating which user uploaded the scalar
5+
* 2. Adds a binary "official" flag to a scalar record indicating if it is an official or experimental submission
6+
* 3. Adds submit_time and user_id indices to the tracker_scalar table
7+
*/
8+
class Tracker_Upgrade_1_0_1 extends MIDASUpgrade
9+
{
10+
public function preUpgrade()
11+
{
12+
}
13+
14+
public function mysql()
15+
{
16+
$this->db->query("ALTER TABLE `tracker_scalar` ADD COLUMN `user_id` bigint(20) NOT NULL DEFAULT -1");
17+
$this->db->query("ALTER TABLE `tracker_scalar` ADD COLUMN `official` tinyint(4) NOT NULL DEFAULT 1");
18+
19+
$this->db->query("ALTER TABLE `tracker_scalar` ADD INDEX (`submit_time`)");
20+
$this->db->query("ALTER TABLE `tracker_scalar` ADD INDEX (`user_id`)");
21+
}
22+
23+
public function pgsql()
24+
{
25+
$this->db->query("ALTER TABLE tracker_scalar ADD COLUMN user_id bigint NOT NULL DEFAULT -1");
26+
$this->db->query("ALTER TABLE tracker_scalar ADD COLUMN official smallint NOT NULL DEFAULT 1");
27+
28+
$this->db->query("CREATE INDEX tracker_scalar_idx_submit_time ON tracker_scalar (submit_time)");
29+
$this->db->query("CREATE INDEX tracker_scalar_idx_user_id ON tracker_scalar (user_id)");
30+
}
31+
32+
public function postUpgrade()
33+
{
34+
}
35+
}
36+
?>

modules/tracker/models/base/ScalarModelBase.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,38 @@ public function __construct()
2424
$this->_mainData = array(
2525
'scalar_id' => array('type' => MIDAS_DATA),
2626
'trend_id' => array('type' => MIDAS_DATA),
27+
'user_id' => array('type' => MIDAS_DATA),
28+
'official' => array('type' => MIDAS_DATA),
2729
'submit_time' => array('type' => MIDAS_DATA),
2830
'value' => array('type' => MIDAS_DATA),
2931
'producer_revision' => array('type' => MIDAS_DATA),
3032
'trend' => array('type' => MIDAS_MANY_TO_ONE,
3133
'model' => 'Trend',
3234
'module' => $this->moduleName,
3335
'parent_column' => 'trend_id',
34-
'child_column' => 'trend_id')
36+
'child_column' => 'trend_id'),
37+
'user' => array('type' => MIDAS_MANY_TO_ONE,
38+
'model' => 'User',
39+
'parent_column' => 'user_id',
40+
'child_column' => 'user_id')
3541
);
3642
$this->initialize();
3743
}
3844

3945
public abstract function associateItem($scalar, $item, $label);
4046
public abstract function getAssociatedItems($scalar);
4147
public abstract function getOtherValuesFromSubmission($scalar);
42-
public abstract function getByTrendAndTimestamp($trendId, $timestamp);
48+
public abstract function getByTrendAndTimestamp($trendId, $timestamp, $user = null);
4349

4450
/**
4551
* Add a new scalar point to the trend. If overwrite is true, and a scalar
4652
* already exists on the trend with the same submit time, this will replace that scalar value.
4753
*/
48-
public function addToTrend($trend, $submitTime, $producerRevision, $value, $overwrite = true)
54+
public function addToTrend($trend, $submitTime, $producerRevision, $value, $user, $overwrite = true, $official = true)
4955
{
5056
if($overwrite)
5157
{
52-
$dao = $this->getByTrendAndTimestamp($trend->getKey(), $submitTime);
58+
$dao = $this->getByTrendAndTimestamp($trend->getKey(), $submitTime, $user->getKey());
5359
if($dao)
5460
{
5561
$this->delete($dao);
@@ -61,6 +67,9 @@ public function addToTrend($trend, $submitTime, $producerRevision, $value, $over
6167
$scalar->setSubmitTime($submitTime);
6268
$scalar->setProducerRevision($producerRevision);
6369
$scalar->setValue($value);
70+
$scalar->setUserId($user instanceof UserDao ? $user->getKey() : -1);
71+
$scalar->setOfficial($official ? 1 : 0);
72+
6473
$this->save($scalar);
6574
return $scalar;
6675
}

modules/tracker/models/pdo/ScalarModel.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getAssociatedItems($scalar)
5252
}
5353

5454
/**
55-
* Return other values that are from the same submission (same submit_time and same producer)
55+
* Return other values that are from the same submission (same submit_time, producer, and user id)
5656
*/
5757
public function getOtherValuesFromSubmission($scalar)
5858
{
@@ -61,6 +61,7 @@ public function getOtherValuesFromSubmission($scalar)
6161
->from(array('s' => 'tracker_scalar'))
6262
->join(array('t' => 'tracker_trend'), 's.trend_id = t.trend_id')
6363
->where('s.submit_time = ?', $scalar->getSubmitTime())
64+
->where('s.user_id = ?', $scalar->getUserId())
6465
->where('t.producer_id = ?', $scalar->getTrend()->getProducerId());
6566
$rows = $this->database->fetchAll($sql);
6667
$scalars = array();
@@ -83,12 +84,16 @@ public function delete($scalar)
8384
/**
8485
* Get a scalar dao based on trend and timestamp
8586
*/
86-
public function getByTrendAndTimestamp($trendId, $timestamp)
87+
public function getByTrendAndTimestamp($trendId, $timestamp, $userId = null)
8788
{
8889
$sql = $this->database->select()
8990
->setIntegrityCheck(false)
9091
->where('trend_id = ?', $trendId)
9192
->where('submit_time = ?', $timestamp);
93+
if($userId !== null)
94+
{
95+
$sql->where('user_id = ?', $userId);
96+
}
9297
return $this->initDao('Scalar', $this->database->fetchRow($sql), $this->moduleName);
9398
}
9499
}

0 commit comments

Comments
 (0)