Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Page: Avoid 1000+ queries for projects dropdown menu
Browse files Browse the repository at this point in the history
Before, we re-used ProjectsAction, which in turn re-uses JobAction, which
in turn queries several hundred results (for the last job of each
project) to termine the status color of the "Last job" column
on the "/projects" page. However, none of that is used by the dropdown
menu.

Replace it with a single DB query. Still re-used, to avoid code
duplication, but no longer re-using the rest of that action class.
  • Loading branch information
Krinkle committed Mar 3, 2023
1 parent 1293801 commit 1892f91
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
5 changes: 2 additions & 3 deletions inc/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ public function output() {
if ( $this->exceptionObj === null ) {
try {
$projectsAction = ProjectsAction::newFromContext( $context );
$projectsAction->doAction();
$projects = $projectsAction->getData();
$projects = $projectsAction->getProjectRows();

} catch ( Exception $e ) {
$pageObj = Error500Page::newFromContext( $context );
Expand Down Expand Up @@ -297,7 +296,7 @@ public function output() {
<li class="nav-header">Projects</li>
<?php
foreach ( $projects as $project ) {
echo $this->getPageLink( "project/{$project['id']}", $project['displayTitle'] );
echo $this->getPageLink( "project/{$project->id}", $project->display_title );
}
?>
</ul>
Expand Down
70 changes: 38 additions & 32 deletions inc/actions/ProjectsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,8 @@ public function doAction() {
return;
}

$sortDirQuery = '';
switch ( $sortDir ) {
case 'asc':
$sortDirQuery = 'ASC';
break;
case 'desc':
$sortDirQuery = 'DESC';
break;
}

$sortFieldQuery = '';
switch ( $sortField ) {
case 'title':
$sortFieldQuery = "ORDER BY display_title $sortDirQuery";
break;
case 'id':
$sortFieldQuery = "ORDER BY id $sortDirQuery";
break;
case 'creation':
$sortFieldQuery = "ORDER BY created $sortDirQuery";
break;
}

$projects = array();
$projectRows = $db->getRows(
"SELECT
id,
display_title,
created
FROM projects
$sortFieldQuery;"
);

$projectRows = $this->getProjectRows( $sortField, $sortDir );
if ( $projectRows ) {
foreach ( $projectRows as $projectRow ) {
// Get information about the latest job (if any)
Expand Down Expand Up @@ -95,4 +64,41 @@ public function doAction() {

$this->setData( $projects );
}

/**
* @param string|null $sortField
* @param string|null $sortDir
* @return stdClass[]
*/
public function getProjectRows( $sortField = null, $sortDir = null ) {
$db = $this->getContext()->getDB();

switch ( $sortDir ) {
case 'desc':
$sortDirQuery = 'DESC';
break;
default:
$sortDirQuery = 'ASC';
}

switch ( $sortField ) {
case 'id':
$sortFieldQuery = "ORDER BY id $sortDirQuery";
break;
case 'creation':
$sortFieldQuery = "ORDER BY created $sortDirQuery";
break;
default:
$sortFieldQuery = "ORDER BY display_title $sortDirQuery";
}

return $db->getRows(
"SELECT
id,
display_title,
created
FROM projects
$sortFieldQuery;"
);
}
}

0 comments on commit 1892f91

Please sign in to comment.