Permalink
Browse files

Added total errors trend for builds.

  • Loading branch information...
corpsee committed Jan 4, 2019
1 parent f2a6479 commit 939e672d108088247191f8ccd7390ef2a40e2138
@@ -637,3 +637,11 @@ h6,
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}

.arrow_trend_positive {
color: #00a65a;
}

.arrow_trend_negative {
color: #dd4b39;
}
@@ -207,7 +207,7 @@ protected function getBuildData(Build $build, $plugin, $severity, $isNew, $start
$errorView->errors = $errors['items'];
$data['errors'] = $build->getTotalErrorsCount($plugin, $severity, $isNew);
$data['errors_total'] = $build->getTotalErrorsCount();
$data['errors_total'] = $build->getErrorsTotal();
$data['error_html'] = $errorView->render();
return $data;
@@ -0,0 +1,27 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddBuildErrorsTotals extends AbstractMigration
{
public function up()
{
$this
->table('build')
->addColumn('errors_total', 'integer', ['null' => true])
->addColumn('errors_total_previous', 'integer', ['null' => true])
->addColumn('errors_new', 'integer', ['null' => true])
->save();
}
public function down()
{
$this
->table('project')
->removeColumn('errors_total')
->removeColumn('errors_total_previous')
->removeColumn('errors_new')
->save();
}
}
@@ -4,6 +4,9 @@
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Model;
use PHPCensor\Store\BuildErrorStore;
use PHPCensor\Store\BuildStore;
use PHPCensor\Store\Factory;
class Build extends Model
{
@@ -26,22 +29,25 @@ class Build extends Model
* @var array
*/
protected $data = [
'id' => null,
'project_id' => null,
'commit_id' => null,
'status' => null,
'log' => null,
'branch' => 'master',
'tag' => null,
'create_date' => null,
'start_date' => null,
'finish_date' => null,
'committer_email' => null,
'commit_message' => null,
'extra' => null,
'environment' => null,
'source' => Build::SOURCE_UNKNOWN,
'user_id' => 0,
'id' => null,
'project_id' => null,
'commit_id' => null,
'status' => null,
'log' => null,
'branch' => 'master',
'tag' => null,
'create_date' => null,
'start_date' => null,
'finish_date' => null,
'committer_email' => null,
'commit_message' => null,
'extra' => null,
'environment' => null,
'source' => Build::SOURCE_UNKNOWN,
'user_id' => 0,
'errors_total' => null,
'errors_total_previous' => null,
'errors_new' => null,
];
/**
@@ -565,4 +571,146 @@ public function setUserId($value)
return $this->setModified('user_id');
}
/**
* @return int
*
* @throws InvalidArgumentException
*/
public function getErrorsTotal()
{
if (null === $this->data['errors_total']) {
/** @var BuildStore $errorStore */
$store = Factory::getStore('Build');
$trend = $store->getBuildErrorsTrend($this->getId(), $this->getProjectId(), $this->getBranch());
if (!isset($trend[1])) {
$trend[1] = [
'build_id' => 0,
'count' => (int)$trend[0]['count'],
];
}
$this->setErrorsTotal((int)$trend[0]['count']);
$this->setErrorsTotalPrevious((int)$trend[1]['count']);
$store->save($this);
}
return $this->data['errors_total'];
}
/**
* @param $value
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function setErrorsTotal($value)
{
$this->validateNotNull('errors_total', $value);
$this->validateInt('errors_total', $value);
if ($this->data['errors_total'] === $value) {
return false;
}
$this->data['errors_total'] = $value;
return $this->setModified('errors_total');
}
/**
* @return int
*
* @throws InvalidArgumentException
*/
public function getErrorsTotalPrevious()
{
if (null === $this->data['errors_total_previous']) {
/** @var BuildStore $errorStore */
$store = Factory::getStore('Build');
$trend = $store->getBuildErrorsTrend($this->getId(), $this->getProjectId(), $this->getBranch());
if (!isset($trend[1])) {
$trend[1] = [
'build_id' => 0,
'count' => (int)$trend[0]['count'],
];
}
$this->setErrorsTotal((int)$trend[0]['count']);
$this->setErrorsTotalPrevious((int)$trend[1]['count']);
$store->save($this);
}
return $this->data['errors_total_previous'];
}
/**
* @param $value
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function setErrorsTotalPrevious($value)
{
$this->validateNotNull('errors_total_previous', $value);
$this->validateInt('errors_total_previous', $value);
if ($this->data['errors_total_previous'] === $value) {
return false;
}
$this->data['errors_total_previous'] = $value;
return $this->setModified('errors_total_previous');
}
/**
* @return int
*
* @throws InvalidArgumentException
*/
public function getErrorsNew()
{
if (null === $this->data['errors_new']) {
/** @var BuildStore $errorStore */
$store = Factory::getStore('Build');
$this->setErrorsNew(
(int)$store->getNewErrorsCount($this->getId())
);
$store->save($this);
}
return $this->data['errors_new'];
}
/**
* @param $value
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function setErrorsNew($value)
{
$this->validateNotNull('errors_new', $value);
$this->validateInt('errors_new', $value);
if ($this->data['errors_new'] === $value) {
return false;
}
$this->data['errors_new'] = $value;
return $this->setModified('errors_new');
}
}
@@ -4,12 +4,14 @@
use PHPCensor\Builder;
use PHPCensor\Plugin\PhpParallelLint;
use PHPCensor\Store\BuildStore;
use PHPCensor\Store\Factory;
use PHPCensor\Store\ProjectStore;
use PHPCensor\Store\BuildErrorStore;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Parser as YamlParser;
use PHPCensor\Model\Base\Build as BaseBuild;
use PHPCensor\Exception\InvalidArgumentException;
/**
* @author Dan Cryer <dan@block8.co.uk>
@@ -46,11 +48,6 @@ class Build extends BaseBuild
self::SOURCE_WEBHOOK_PULL_REQUEST_MERGED,
];
/**
* @var int|null
*/
protected $newErrorsCount = null;
/**
* @var array
*/
@@ -615,23 +612,6 @@ public function getSourceHumanize()
}
}
/**
* @return int
*
* @throws \Exception
*/
public function getNewErrorsCount()
{
if (null === $this->newErrorsCount) {
/** @var BuildErrorStore $store */
$store = Factory::getStore('BuildError');
$this->newErrorsCount = (int)$store->getNewErrorsCount($this->getId());
}
return $this->newErrorsCount;
}
/**
* Gets the total number of errors for a given build.
*
@@ -645,6 +625,10 @@ public function getNewErrorsCount()
*/
public function getTotalErrorsCount($plugin = null, $severity = null, $isNew = null)
{
if (null === $plugin && null === $severity && null === $isNew) {
return $this->getErrorsTotal();
}
$key =
$plugin . ':' .
((null === $severity) ? 'null' : (int)$severity) . ':' .
@@ -664,4 +648,23 @@ public function getTotalErrorsCount($plugin = null, $severity = null, $isNew = n
return $this->totalErrorsCount[$key];
}
/**
* @return int
*
* @throws InvalidArgumentException
*/
public function getErrorsTrend()
{
$total = $this->getErrorsTotal();
$previous = $this->getErrorsTotalPrevious();
if ($previous > $total) {
return 1;
} elseif ($previous < $total) {
return -1;
}
return 0;
}
}
@@ -299,28 +299,4 @@ public function getIsNewError($projectId, $hash)
return true;
}
/**
* @param int $buildId
*
* @return int
*
* @throws \Exception
*/
public function getNewErrorsCount($buildId)
{
$query = 'SELECT COUNT(*) AS {{total}} FROM {{' . $this->tableName . '}} WHERE {{build_id}} = :build AND {{is_new}} = true';
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':build', $buildId);
if ($stmt->execute()) {
$res = $stmt->fetch(\PDO::FETCH_ASSOC);
return (integer)$res['total'];
}
return 0;
}
}
Oops, something went wrong.

0 comments on commit 939e672

Please sign in to comment.