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

Commit

Permalink
Adding some utility functions to Tracker submission model.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpatrick committed Jan 29, 2016
1 parent ba3d47b commit 2d811d9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions modules/tracker/models/base/SubmissionModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,26 @@ abstract public function getSubmissionsByProducer($producerDao);
* @return array submission DAOs
*/
abstract public function getScalars($submissionDao, $key = false);

/**
* Get submissions associated with a given producer.
*
* @param Tracker_ProducerDao $producerDao producer DAO
* @param false | string $date the date in which to check
* @param string $branch the branch of the submission for which to search
* @param bool $onlyOneDay whether to only get the last day.
* @return Tracker_SubmissionDao submission
*/
abstract public function getLatestSubmissionByProducerDateAndBranch($producerDao,
$date = false,
$branch = 'master',
$onlyOneDay = true);

/**
* Get trends associated with a submission.
* @param Tracker_SubmissionDao $submissionDao submission DAO
* @param bool $key whether to only retrieve key trends
* @return array trend DAOs
*/
abstract public function getTrends($submissionDao, $key = true);
}
71 changes: 71 additions & 0 deletions modules/tracker/models/pdo/SubmissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
class Tracker_SubmissionModel extends Tracker_SubmissionModelBase
{
const SEC_IN_DAY = 86400;

/**
* Create a submission.
*
Expand Down Expand Up @@ -150,4 +152,73 @@ public function getOrCreateSubmission($producerDao, $uuid)

return $submissionDao;
}

/**
* Get submissions associated with a given producer.
*
* @param Tracker_ProducerDao $producerDao producer DAO
* @param false | string $date the date in which to check
* @param string $branch the branch of the submission for which to search
* @param bool $onlyOneDay whether to only get the last day.
* @return Tracker_SubmissionDao submission | false
*/
public function getLatestSubmissionByProducerDateAndBranch($producerDao, $date = false, $branch = 'master',
$onlyOneDay = true) {
if ($date) {
$queryTime = date('Y-m-d H:i:s', strtotime($date));
} else {
$queryTime = date('Y-m-d H:i:s', time());
}
$dayBeforeQueryTime = date('Y-m-d H:i:s', strtotime($queryTime) - self::SEC_IN_DAY);
$sql = $this->database->select()->setIntegrityCheck(false)
->from('tracker_submission')
->join(
'tracker_scalar',
'tracker_submission.submission_id = tracker_scalar.submission_id',
array())
->where('tracker_submission.submit_time < ?', $queryTime);
if ($onlyOneDay) {
$sql = $sql->where('tracker_submission.submit_time > ?', $dayBeforeQueryTime);
}
$sql = $sql->where('tracker_submission.producer_id = ?', $producerDao->getKey())
->where('branch = ?', $branch)
->order('tracker_submission.submit_time','DSC')
->limit(1);
$res = $this->database->fetchAll($sql);
if (count($res) === 1) {
$submissionDao = $this->initDao('Submission', $res[0], $this->moduleName);
} else {
$submissionDao = false;
}
return $submissionDao;
}

/**
* Get trends associated with a submission.
* @param Tracker_SubmissionDao $submissionDao submission DAO
* @param bool $key whether to only retrieve key trends
* @return array trend DAOs
*/
public function getTrends($submissionDao, $key = true) {
if ($key) {
$sql = $this->database->select()->setIntegrityCheck(false)->from('tracker_trend')->join(
'tracker_scalar',
'tracker_scalar.trend_id = tracker_trend.trend_id',
array()
)->where('submission_id = ?', $submissionDao->getKey()
)->where('key_metric = ?', 1);
} else {
$sql = $this->database->select()->setIntegrityCheck(false)->from('tracker_scalar')
->where('submission_id = ?', $submissionDao->getKey());
}
$trendDaos = array();
$rows = $this->database->fetchAll($sql);

/** @var Zend_Db_Table_Row_Abstract $row */
foreach ($rows as $row) {
$trendDaos[] = $this->initDao('Trend', $row, $this->moduleName);
}

return $trendDaos;
}
}

0 comments on commit 2d811d9

Please sign in to comment.