1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 08:42:41 +01:00

Implement a lint count query

Summary: Ref T2683. This query is currently unbatched and happens inside a view. Leave it inside the view for now, but separate it and make it batchable.

Test Plan: {F34848}

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley, vrana, aran

Maniphest Tasks: T2683

Differential Revision: https://secure.phabricator.com/D5252
This commit is contained in:
epriestley 2014-05-10 15:21:05 -07:00
parent e4ea092f60
commit ac020bc420
3 changed files with 132 additions and 14 deletions

View file

@ -501,6 +501,7 @@ phutil_register_library_map(array(
'DiffusionInlineCommentPreviewController' => 'applications/diffusion/controller/DiffusionInlineCommentPreviewController.php',
'DiffusionLastModifiedController' => 'applications/diffusion/controller/DiffusionLastModifiedController.php',
'DiffusionLintController' => 'applications/diffusion/controller/DiffusionLintController.php',
'DiffusionLintCountQuery' => 'applications/diffusion/query/DiffusionLintCountQuery.php',
'DiffusionLintDetailsController' => 'applications/diffusion/controller/DiffusionLintDetailsController.php',
'DiffusionLintSaveRunner' => 'applications/diffusion/DiffusionLintSaveRunner.php',
'DiffusionLowLevelCommitFieldsQuery' => 'applications/diffusion/query/lowlevel/DiffusionLowLevelCommitFieldsQuery.php',
@ -3141,6 +3142,7 @@ phutil_register_library_map(array(
'DiffusionInlineCommentPreviewController' => 'PhabricatorInlineCommentPreviewController',
'DiffusionLastModifiedController' => 'DiffusionController',
'DiffusionLintController' => 'DiffusionController',
'DiffusionLintCountQuery' => 'PhabricatorQuery',
'DiffusionLintDetailsController' => 'DiffusionController',
'DiffusionLowLevelCommitFieldsQuery' => 'DiffusionLowLevelQuery',
'DiffusionLowLevelCommitQuery' => 'DiffusionLowLevelQuery',

View file

@ -0,0 +1,120 @@
<?php
final class DiffusionLintCountQuery extends PhabricatorQuery {
private $branchIDs;
private $paths;
private $codes;
public function withBranchIDs(array $branch_ids) {
$this->branchIDs = $branch_ids;
return $this;
}
public function withPaths(array $paths) {
$this->paths = $paths;
return $this;
}
public function withCodes(array $codes) {
$this->codes = $codes;
return $this;
}
public function execute() {
if (!$this->paths) {
throw new Exception(pht("Call withPaths() before execute()!"));
}
if (!$this->branchIDs) {
throw new Exception(pht("Call withBranchIDs() before execute()!"));
}
$conn_r = id(new PhabricatorRepositoryCommit())->establishConnection('r');
$this->paths = array_unique($this->paths);
list($dirs, $paths) = $this->processPaths();
$parts = array();
foreach ($dirs as $dir) {
$parts[$dir] = qsprintf(
$conn_r,
'path LIKE %>',
$dir);
}
foreach ($paths as $path) {
$parts[$path] = qsprintf(
$conn_r,
'path = %s',
$path);
}
$queries = array();
foreach ($parts as $key => $part) {
$queries[] = qsprintf(
$conn_r,
'SELECT %s path_prefix, COUNT(*) N FROM %T %Q',
$key,
PhabricatorRepository::TABLE_LINTMESSAGE,
$this->buildWhereClause($conn_r, $part));
}
$huge_union_query = '('.implode(') UNION ALL (', $queries).')';
$data = queryfx_all(
$conn_r,
'%Q',
$huge_union_query);
return $this->processResults($data);
}
private function buildWhereClause(AphrontDatabaseConnection $conn_r, $part) {
$where = array();
$where[] = $part;
if ($this->codes !== null) {
$where[] = qsprintf(
$conn_r,
'code IN (%Ls)',
$this->codes);
}
if ($this->branchIDs !== null) {
$where[] = qsprintf(
$conn_r,
'branchID IN (%Ld)',
$this->branchIDs);
}
return $this->formatWhereClause($where);
}
private function processPaths() {
$dirs = array();
$paths = array();
foreach ($this->paths as $path) {
$path = '/'.$path;
if (substr($path, -1) == '/') {
$dirs[] = $path;
} else {
$paths[] = $path;
}
}
return array($dirs, $paths);
}
private function processResults(array $data) {
$data = ipull($data, 'N', 'path_prefix');
// Strip the leading "/" back off each path.
$output = array();
foreach ($data as $path => $count) {
$output[substr($path, 1)] = $count;
}
return $output;
}
}

View file

@ -87,28 +87,24 @@ final class DiffusionBrowseTableView extends DiffusionView {
}
private static function loadLintMessagesCount(DiffusionRequest $drequest) {
$path = $drequest->getPath();
$branch = $drequest->loadBranch();
if (!$branch) {
return null;
}
$conn = $drequest->getRepository()->establishConnection('r');
$query = id(new DiffusionLintCountQuery())
->withBranchIDs(array($branch->getID()))
->withPaths(array($path));
$path = '/'.$drequest->getPath();
$where = (substr($path, -1) == '/'
? qsprintf($conn, 'AND path LIKE %>', $path)
: qsprintf($conn, 'AND path = %s', $path));
if ($drequest->getLint()) {
$where .= qsprintf($conn, ' AND code = %s', $drequest->getLint());
$code = $drequest->getLint();
if ($code) {
$query->withCodes(array($code));
}
return head(queryfx_one(
$conn,
'SELECT COUNT(*) FROM %T WHERE branchID = %d %Q',
PhabricatorRepository::TABLE_LINTMESSAGE,
$branch->getID(),
$where));
$result = $query->execute();
return idx($result, $path);
}
public function render() {