Web notifications. Issue #156.

This commit is contained in:
Mark Amoin 2018-04-03 15:43:11 +08:00 committed by Dmitry Khomutov
commit 3f56e4cc54
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
10 changed files with 512 additions and 135 deletions

View file

@ -365,9 +365,17 @@ class BuildController extends WebController
public function ajaxQueue()
{
$sPending = 'pending';
$sRunning = 'running';
$pending = $this->buildStore->getByStatus(Build::STATUS_PENDING);
$running = $this->buildStore->getByStatus(Build::STATUS_RUNNING);
$rtn = [
'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_PENDING)),
'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)),
$sPending => $this->formatBuilds($pending),
$sRunning => $this->formatBuilds($running),
];
$response = new JsonResponse();

View file

@ -0,0 +1,114 @@
<?php
namespace PHPCensor\Controller;
use PHPCensor\Model\Build;
use PHPCensor\WebController;
use PHPCensor\Store\Factory;
//use PHPCensor\View;
//use PHPCensor\Model\Project;
//use PHPCensor\Http\Response;
//use PHPCensor\Store\BuildStore;
//use PHPCensor\Store\ProjectStore;
//use PHPCensor\Store\ProjectGroupStore;
//use PHPCensor\Service\BuildService;
//use b8\Http\Response\JsonResponse;
use PHPCensor\Http\Response\JsonResponse;
use PHPCensor\Service\WebNotificationService;
/**
* Web Notifications Controller
*/
class WebNotificationsController extends WebController
{
/**
* @var BuildStore
*/
protected $buildStore;
/**
* @var ProjectStore
*/
protected $projectStore;
/**
* @var ProjectGroupStore
*/
protected $groupStore;
/**
* Initialise the controller, set up stores and services.
*/
public function init()
{
parent::init();
$this->buildStore = Factory::getStore('Build');
$this->projectStore = Factory::getStore('Project');
$this->groupStore = Factory::getStore('ProjectGroup');
}
/**
* Provides JSON format for web notification UI of all last
* builds that have success and failed status.
* This is similar to WidgetAllProjectsController::update()
* but instead, this only returns JSON.
* @param int $projectId
* @return \PHPCensor\Http\Response\JsonResponse
* @see \PHPCensor\Controller\WidgetAllProjectsController
*/
public function widgetsAllProjectsUpdate($projectId)
{
$success = $this->buildStore->getLastBuildByStatus($projectId, Build::STATUS_SUCCESS);
$failed = $this->buildStore->getLastBuildByStatus($projectId, Build::STATUS_FAILED);
$oSuccess = WebNotificationService::formatBuild($success);
$oFailed = WebNotificationService::formatBuild($failed);
//@keys count and items Follow the for-loop structure
//found in
//\PHPCensor\Service\WebNotificationService::formatBuilds()
$aSuccess = [
'count' => count($oSuccess),
'items' => [$projectId => ['build' => $oSuccess]]
];
$aFailed = [
'count' => count($oFailed),
'items' => [$projectId => ['build' => $oFailed]]
];
$builds = [
'success' => $aSuccess,
'failed' => $aFailed
];
$response = new JsonResponse();
$response->setContent($builds);
return $response;
}
/**
* Provides JSON format for web notification UI of all last
* builds that have pending and running status.
* This is similar to WidgetAllProjectsController::update()
* but instead, this only returns JSON.
* @return \PHPCensor\Http\Response\JsonResponse
*/
public function buildsUpdated()
{
$pending = $this->buildStore->getByStatus(Build::STATUS_PENDING);
$running = $this->buildStore->getByStatus(Build::STATUS_RUNNING);
$rtn = [
'pending' => WebNotificationService::formatBuilds($pending),
'running' => WebNotificationService::formatBuilds($running)
];
$response = new JsonResponse();
$response->setContent($rtn);
return $response;
}
}

View file

@ -11,6 +11,8 @@ use PHPCensor\Http\Response;
use PHPCensor\Store\BuildStore;
use PHPCensor\Store\ProjectStore;
use PHPCensor\Store\ProjectGroupStore;
use PHPCensor\Service\BuildService;
use b8\Http\Response\JsonResponse;
/**
* Widget All Projects Controller

View file

@ -0,0 +1,82 @@
<?php
namespace PHPCensor\Service;
use PHPCensor\Config;
use Pheanstalk\Pheanstalk;
use Pheanstalk\PheanstalkInterface;
use PHPCensor\BuildFactory;
use PHPCensor\Model\Build;
use PHPCensor\Model\Project;
use PHPCensor\Store\BuildStore;
/**
* A service that listens for creation, duplication and deletion of builds for web notification UI.
*/
class WebNotificationService
{
/**
* Similar to BuildController::formatBuilds() but uses
* pure object to be used for rendering web notifications.
* @param array $builds
* @return array Formatted builds
* @see \PHPCensor\Controller\WidgetLastBuildsController::webNotificationUpdate().
*/
public static function formatBuilds($builds)
{
$rtn = ['count' => $builds['count'], 'items' => []];
foreach ($builds['items'] as $buildItem) {
$build = self::formatBuild($buildItem);
$rtn['items'][$buildItem->getId()]['build'] = $build;
}
ksort($rtn['items']);
return $rtn;
}
/**
* Provides structured keys for web notification.
* @param Build $build
* @return array
*/
public static function formatBuild($build)
{
if (empty($build) || is_null($build)) {
return [];
}
$status = $build->getStatus();
$datePerformed = '';
$dateFinished = '';
/*
BUG: Lang::out() automatically renders the values for
either 'created_x' or 'started_x' instead of just
returning them.
*/
if ($status === Build::STATUS_PENDING) {
$datePerformed = 'Created: ' . $build->getCreateDate()->format('H:i');
} elseif ($status === Build::STATUS_RUNNING) {
$datePerformed = 'Started: ' . $build->getStartDate()->format('H:i');
}
if (!is_null($build->getFinishDate())) {
$dateFinished = 'Finished: ' . $build->getFinishDate()->format('H:i');
}
return [
'branch' => $build->getBranch(),
'url' => APP_URL .
'build/view/' .
$build->getId(),
'committer_email' => $build->getCommitterEmail(),
'img_src' => 'https://www.gravatar.com/avatar/' .
md5($build->getCommitterEmail()) .
'?d=mm&s=40',
'project_title' => $build->getProject()->getTitle(),
'status' => $status,
'date_performed' => $datePerformed,
'date_finished' => $dateFinished
];
}
}

View file

@ -32,6 +32,7 @@ $user = $this->getUser();
<script src="<?= APP_URL; ?>assets/vendor/sprintf-js/dist/sprintf.min.js"></script>
<script src="<?= APP_URL; ?>assets/js/app.js?v2" type="text/javascript"></script>
<script src="<?= APP_URL; ?>assets/vendor/notifyjs/dist/notify.js" type="text/javascript"></script>
<script>
var APP_URL = '<?= APP_URL; ?>';