Added ajax for the dashboard and timeline on main page

This commit is contained in:
Dmitry Khomutov 2017-01-15 01:11:32 +07:00
commit 40b5de70e5
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
15 changed files with 415 additions and 212 deletions

View file

@ -124,43 +124,6 @@ class BuildController extends Controller
return $rtn;
}
/**
* AJAX call to get build data:
*/
public function data($buildId)
{
$response = new JsonResponse();
$build = BuildFactory::getBuildById($buildId);
if (!$build) {
$response->setResponseCode(404);
$response->setContent([]);
return $response;
}
$response->setContent($this->getBuildData($build));
return $response;
}
/**
* AJAX call to get build meta:
*/
public function meta($buildId)
{
$build = BuildFactory::getBuildById($buildId);
$key = $this->getParam('key', null);
$numBuilds = $this->getParam('num_builds', 1);
$data = null;
if ($key && $build) {
$data = $this->buildStore->getMeta($key, $build->getProjectId(), $buildId, $build->getBranch(), $numBuilds);
}
$response = new JsonResponse();
$response->setContent($data);
return $response;
}
/**
* Get build data from database and json encode it:
*/
@ -208,6 +171,7 @@ class BuildController extends Controller
$response = new b8\Http\Response\RedirectResponse();
$response->setHeader('Location', APP_URL.'build/view/' . $build->getId());
return $response;
}
@ -228,6 +192,7 @@ class BuildController extends Controller
$response = new b8\Http\Response\RedirectResponse();
$response->setHeader('Location', APP_URL.'project/view/' . $build->getProjectId());
return $response;
}
@ -239,21 +204,6 @@ class BuildController extends Controller
return AnsiConverter::convert($log);
}
/**
* Allows the UI to poll for the latest running and pending builds.
*/
public function latest()
{
$rtn = [
'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_NEW)),
'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)),
];
$response = new JsonResponse();
$response->setContent($rtn);
return $response;
}
/**
* Formats a list of builds into rows suitable for the dropdowns in the PHPCI header bar.
* @param $builds
@ -278,4 +228,64 @@ class BuildController extends Controller
ksort($rtn['items']);
return $rtn;
}
public function ajaxData($buildId)
{
$response = new JsonResponse();
$build = BuildFactory::getBuildById($buildId);
if (!$build) {
$response->setResponseCode(404);
$response->setContent([]);
return $response;
}
$response->setContent($this->getBuildData($build));
return $response;
}
public function ajaxMeta($buildId)
{
$build = BuildFactory::getBuildById($buildId);
$key = $this->getParam('key', null);
$numBuilds = $this->getParam('num_builds', 1);
$data = null;
if ($key && $build) {
$data = $this->buildStore->getMeta($key, $build->getProjectId(), $buildId, $build->getBranch(), $numBuilds);
}
$response = new JsonResponse();
$response->setContent($data);
return $response;
}
public function ajaxQueue()
{
$rtn = [
'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_NEW)),
'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)),
];
$response = new JsonResponse();
$response->setContent($rtn);
return $response;
}
public function ajaxTimeline()
{
$builds = $this->buildStore->getLatestBuilds(null, 10);
foreach ($builds as &$build) {
$build = BuildFactory::getBuild($build);
}
$view = new b8\View('Home/ajax-timeline');
$view->builds = $builds;
$this->response->disableLayout();
$this->response->setContent($view->render());
return $this->response;
}
}

View file

@ -44,8 +44,8 @@ class HomeController extends Controller
*/
public function init()
{
$this->buildStore = b8\Store\Factory::getStore('Build');
$this->projectStore = b8\Store\Factory::getStore('Project');
$this->buildStore = b8\Store\Factory::getStore('Build');
$this->projectStore = b8\Store\Factory::getStore('Project');
$this->groupStore = b8\Store\Factory::getStore('ProjectGroup');
}
@ -61,33 +61,12 @@ class HomeController extends Controller
$build = BuildFactory::getBuild($build);
}
$this->view->builds = $builds;
$this->view->builds = $builds;
$this->view->groups = $this->getGroupInfo();
return $this->view->render();
}
/**
* AJAX get latest builds table (HTML)
*/
public function latest()
{
$this->response->disableLayout();
$this->response->setContent($this->getLatestBuildsHtml());
return $this->response;
}
/**
* Ajax request for the project overview section of the dashboard.
*/
public function summary()
{
$this->response->disableLayout();
$projects = $this->projectStore->getWhere([], 50, 0, [], ['title' => 'ASC']);
$this->response->setContent($this->getSummaryHtml($projects));
return $this->response;
}
/**
* Generate the HTML for the project overview section of the dashboard.
* @param $projects
@ -119,29 +98,12 @@ class HomeController extends Controller
$failures[$project->getId()] = $failure;
}
$summaryView = new b8\View('SummaryTable');
$summaryView->projects = $projects;
$summaryView->builds = $summaryBuilds;
$summaryView->successful = $successes;
$summaryView->failed = $failures;
$summaryView->counts = $counts;
return $summaryView->render();
}
/**
* Get latest builds and render as a table.
*/
protected function getLatestBuildsHtml()
{
$builds = $this->buildStore->getWhere([], 5, 0, [], ['id' => 'DESC']);
$view = new b8\View('BuildsTable');
foreach ($builds['items'] as &$build) {
$build = BuildFactory::getBuild($build);
}
$view->builds = $builds['items'];
$view = new b8\View('Home/dashboard-projects');
$view->projects = $projects;
$view->builds = $summaryBuilds;
$view->successful = $successes;
$view->failed = $failures;
$view->counts = $counts;
return $view->render();
}
@ -152,15 +114,15 @@ class HomeController extends Controller
*/
protected function getGroupInfo()
{
$rtn = [];
$rtn = [];
$groups = $this->groupStore->getWhere([], 100, 0, [], ['title' => 'ASC']);
foreach ($groups['items'] as $group) {
$thisGroup = ['title' => $group->getTitle()];
$projects = $this->projectStore->getByGroupId($group->getId());
$thisGroup = ['title' => $group->getTitle()];
$projects = $this->projectStore->getByGroupId($group->getId());
$thisGroup['projects'] = $projects['items'];
$thisGroup['summary'] = $this->getSummaryHtml($thisGroup['projects']);
$rtn[] = $thisGroup;
$thisGroup['summary'] = $this->getSummaryHtml($thisGroup['projects']);
$rtn[] = $thisGroup;
}
return $rtn;

View file

@ -142,24 +142,6 @@ class ProjectController extends PHPCensor\Controller
return $response;
}
/**
* AJAX get latest builds.
*
* @param int $projectId
*
* @return b8\Http\Response
*/
public function builds($projectId)
{
$branch = $this->getParam('branch', '');
$perPage = (integer)$this->getParam('per_page', 10);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), 0, $perPage);
$this->response->disableLayout();
$this->response->setContent($builds[0]);
return $this->response;
}
/**
* Render latest builds for project as HTML table.
*
@ -179,7 +161,7 @@ class ProjectController extends PHPCensor\Controller
$order = ['id' => 'DESC'];
$builds = $this->buildStore->getWhere($criteria, $perPage, $start, [], $order);
$view = new b8\View('BuildsTable');
$view = new b8\View('Project/ajax-builds');
foreach ($builds['items'] as &$build) {
$build = BuildFactory::getBuild($build);
@ -190,6 +172,35 @@ class ProjectController extends PHPCensor\Controller
return [$view->render(), $builds['count']];
}
/**
* Render latest builds for project as HTML table.
*
* @param int $projectId
*
* @return array
*/
protected function getDashboardProjectHtml($projectId)
{
$count = $this->buildStore->getWhere(
['project_id' => $projectId],
1,
0,
[],
['id' => 'DESC']
);
$counts = $count['count'];
$view = new b8\View('Home/ajax-dashboard-project');
$view->project = $this->projectStore->getById($projectId);
$view->builds = $this->buildStore->getLatestBuilds($projectId);
$view->successful = $this->buildStore->getLastBuildByStatus($projectId, PHPCensor\Model\Build::STATUS_SUCCESS);
$view->failed = $this->buildStore->getLastBuildByStatus($projectId, PHPCensor\Model\Build::STATUS_FAILED);
$view->counts = $counts;
return $view->render();
}
/**
* Add a new project. Handles both the form, and processing.
*/
@ -215,7 +226,7 @@ class ProjectController extends PHPCensor\Controller
$form = $this->projectForm($values);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('ProjectForm');
$view = new b8\View('Project/edit');
$view->type = 'add';
$view->project = null;
$view->form = $form;
@ -278,7 +289,7 @@ class ProjectController extends PHPCensor\Controller
$form = $this->projectForm($values, 'edit/' . $projectId);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('ProjectForm');
$view = new b8\View('Project/edit');
$view->type = 'edit';
$view->project = $project;
$view->form = $form;
@ -403,18 +414,6 @@ class ProjectController extends PHPCensor\Controller
return $form;
}
/**
* Get an array of repositories from Github's API.
*/
protected function githubRepositories()
{
$github = new Github();
$response = new b8\Http\Response\JsonResponse();
$response->setContent($github->getRepositories());
return $response;
}
/**
* Get the validator to use to check project references.
* @param $values
@ -457,4 +456,48 @@ class ProjectController extends PHPCensor\Controller
return true;
};
}
/**
* @param int $projectId
*
* @return b8\Http\Response
*/
public function ajaxBuilds($projectId)
{
$branch = $this->getParam('branch', '');
$perPage = (integer)$this->getParam('per_page', 10);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), 0, $perPage);
$this->response->disableLayout();
$this->response->setContent($builds[0]);
return $this->response;
}
/**
* @param int $projectId
*
* @return b8\Http\Response
*/
public function ajaxDashboardProject($projectId)
{
$builds = $this->getDashboardProjectHtml($projectId);
$this->response->disableLayout();
$this->response->setContent($builds);
return $this->response;
}
/**
* Get an array of repositories from Github's API.
*/
public function ajaxGithubRepositories()
{
$github = new Github();
$response = new b8\Http\Response\JsonResponse();
$response->setContent($github->getRepositories());
return $response;
}
}

View file

@ -166,7 +166,7 @@ class UserController extends Controller
$form = $this->userForm($values);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('UserForm');
$view = new b8\View('User/edit');
$view->type = 'add';
$view->user = null;
$view->form = $form;
@ -208,7 +208,7 @@ class UserController extends Controller
$form = $this->userForm($values, 'edit/' . $userId);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('UserForm');
$view = new b8\View('User/edit');
$view->type = 'edit';
$view->user = $user;
$view->form = $form;