diff --git a/src/applications/drydock/query/DrydockBlueprintQuery.php b/src/applications/drydock/query/DrydockBlueprintQuery.php index 23081c8910..abad714d8f 100644 --- a/src/applications/drydock/query/DrydockBlueprintQuery.php +++ b/src/applications/drydock/query/DrydockBlueprintQuery.php @@ -1,78 +1,78 @@ ids = $ids; return $this; } public function withPHIDs(array $phids) { $this->phids = $phids; return $this; } public function withDatasourceQuery($query) { $this->datasourceQuery = $query; return $this; } protected function loadPage() { $table = new DrydockBlueprint(); $conn_r = $table->establishConnection('r'); $data = queryfx_all( $conn_r, 'SELECT blueprint.* FROM %T blueprint %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); $blueprints = $table->loadAllFromArray($data); $implementations = DrydockBlueprintImplementation::getAllBlueprintImplementations(); foreach ($blueprints as $blueprint) { if (array_key_exists($blueprint->getClassName(), $implementations)) { $blueprint->attachImplementation( $implementations[$blueprint->getClassName()]); } } return $blueprints; } protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { $where = array(); - if ($this->ids) { + if ($this->ids !== null) { $where[] = qsprintf( $conn_r, 'id IN (%Ld)', $this->ids); } - if ($this->phids) { + if ($this->phids !== null) { $where[] = qsprintf( $conn_r, 'phid IN (%Ls)', $this->phids); } - if ($this->datasourceQuery) { + if ($this->datasourceQuery !== null) { $where[] = qsprintf( $conn_r, 'blueprintName LIKE %>', $this->datasourceQuery); } return $this->formatWhereClause($where); } } diff --git a/src/applications/drydock/query/DrydockLeaseQuery.php b/src/applications/drydock/query/DrydockLeaseQuery.php index b2bbfe9d66..37f02bd748 100644 --- a/src/applications/drydock/query/DrydockLeaseQuery.php +++ b/src/applications/drydock/query/DrydockLeaseQuery.php @@ -1,112 +1,112 @@ ids = $ids; return $this; } public function withPHIDs(array $phids) { $this->phids = $phids; return $this; } public function withResourceIDs(array $ids) { $this->resourceIDs = $ids; return $this; } public function withStatuses(array $statuses) { $this->statuses = $statuses; return $this; } public function newResultObject() { return new DrydockLease(); } public function withDatasourceQuery($query) { $this->datasourceQuery = $query; return $this; } protected function loadPage() { return $this->loadStandardPage($this->newResultObject()); } protected function willFilterPage(array $leases) { $resource_ids = array_filter(mpull($leases, 'getResourceID')); if ($resource_ids) { $resources = id(new DrydockResourceQuery()) ->setParentQuery($this) ->setViewer($this->getViewer()) - ->withIDs($resource_ids) + ->withIDs(array_unique($resource_ids)) ->execute(); } else { $resources = array(); } foreach ($leases as $key => $lease) { $resource = null; if ($lease->getResourceID()) { $resource = idx($resources, $lease->getResourceID()); if (!$resource) { unset($leases[$key]); continue; } } $lease->attachResource($resource); } return $leases; } protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { $where = parent::buildWhereClauseParts($conn); - if ($this->resourceIDs) { + if ($this->resourceIDs !== null) { $where[] = qsprintf( $conn, 'resourceID IN (%Ld)', $this->resourceIDs); } - if ($this->ids) { + if ($this->ids !== null) { $where[] = qsprintf( $conn, 'id IN (%Ld)', $this->ids); } - if ($this->phids) { + if ($this->phids !== null) { $where[] = qsprintf( $conn, 'phid IN (%Ls)', $this->phids); } - if ($this->statuses) { + if ($this->statuses !== null) { $where[] = qsprintf( $conn, 'status IN (%Ld)', $this->statuses); } - if ($this->datasourceQuery) { + if ($this->datasourceQuery !== null) { $where[] = qsprintf( $conn, 'id = %d', (int)$this->datasourceQuery); } return $where; } } diff --git a/src/applications/drydock/query/DrydockLogQuery.php b/src/applications/drydock/query/DrydockLogQuery.php index 1e21864f10..47a6795463 100644 --- a/src/applications/drydock/query/DrydockLogQuery.php +++ b/src/applications/drydock/query/DrydockLogQuery.php @@ -1,113 +1,113 @@ resourceIDs = $ids; return $this; } public function withLeaseIDs(array $ids) { $this->leaseIDs = $ids; return $this; } protected function loadPage() { $table = new DrydockLog(); $conn_r = $table->establishConnection('r'); $data = queryfx_all( $conn_r, 'SELECT log.* FROM %T log %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); return $table->loadAllFromArray($data); } protected function willFilterPage(array $logs) { $resource_ids = array_filter(mpull($logs, 'getResourceID')); if ($resource_ids) { $resources = id(new DrydockResourceQuery()) ->setParentQuery($this) ->setViewer($this->getViewer()) - ->withIDs($resource_ids) + ->withIDs(array_unique($resource_ids)) ->execute(); } else { $resources = array(); } foreach ($logs as $key => $log) { $resource = null; if ($log->getResourceID()) { $resource = idx($resources, $log->getResourceID()); if (!$resource) { unset($logs[$key]); continue; } } $log->attachResource($resource); } $lease_ids = array_filter(mpull($logs, 'getLeaseID')); if ($lease_ids) { $leases = id(new DrydockLeaseQuery()) ->setParentQuery($this) ->setViewer($this->getViewer()) - ->withIDs($lease_ids) + ->withIDs(array_unique($lease_ids)) ->execute(); } else { $leases = array(); } foreach ($logs as $key => $log) { $lease = null; if ($log->getLeaseID()) { $lease = idx($leases, $log->getLeaseID()); if (!$lease) { unset($logs[$key]); continue; } } $log->attachLease($lease); } // These logs are meaningless and their policies aren't computable. They // shouldn't exist, but throw them away if they do. foreach ($logs as $key => $log) { if (!$log->getResource() && !$log->getLease()) { unset($logs[$key]); } } return $logs; } protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { $where = array(); - if ($this->resourceIDs) { + if ($this->resourceIDs !== null) { $where[] = qsprintf( $conn_r, 'resourceID IN (%Ld)', $this->resourceIDs); } - if ($this->leaseIDs) { + if ($this->leaseIDs !== null) { $where[] = qsprintf( $conn_r, 'leaseID IN (%Ld)', $this->leaseIDs); } $where[] = $this->buildPagingClause($conn_r); return $this->formatWhereClause($where); } } diff --git a/src/applications/drydock/query/DrydockLogSearchEngine.php b/src/applications/drydock/query/DrydockLogSearchEngine.php index 85114394e8..13777031d6 100644 --- a/src/applications/drydock/query/DrydockLogSearchEngine.php +++ b/src/applications/drydock/query/DrydockLogSearchEngine.php @@ -1,101 +1,117 @@ setParameter( 'resourcePHIDs', $this->readListFromRequest($request, 'resources')); $query->setParameter( 'leasePHIDs', $this->readListFromRequest($request, 'leases')); return $query; } public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { + $resource_phids = $saved->getParameter('resourcePHIDs', array()); + $lease_phids = $saved->getParameter('leasePHIDs', array()); + + // TODO: Change logs to use PHIDs instead of IDs. + $resource_ids = array(); + $lease_ids = array(); + + if ($resource_phids) { + $resource_ids = id(new DrydockResourceQuery()) + ->setViewer(PhabricatorUser::getOmnipotentUser()) + ->withPHIDs($resource_phids) + ->execute(); + $resource_ids = mpull($resource_ids, 'getID'); + } + + if ($lease_phids) { + $lease_ids = id(new DrydockLeaseQuery()) + ->setViewer(PhabricatorUser::getOmnipotentUser()) + ->withPHIDs($lease_phids) + ->execute(); + $lease_ids = mpull($lease_ids, 'getID'); + } - // TODO: Change logs to use PHIDs instead of IDs. - $resource_ids = id(new DrydockResourceQuery()) - ->setViewer(PhabricatorUser::getOmnipotentUser()) - ->withPHIDs($saved->getParameter('resourcePHIDs', array())) - ->execute(); - $resource_ids = mpull($resource_ids, 'getID'); - $lease_ids = id(new DrydockLeaseQuery()) - ->setViewer(PhabricatorUser::getOmnipotentUser()) - ->withPHIDs($saved->getParameter('leasePHIDs', array())) - ->execute(); - $lease_ids = mpull($lease_ids, 'getID'); - - return id(new DrydockLogQuery()) - ->withResourceIDs($resource_ids) - ->withLeaseIDs($lease_ids); + $query = new DrydockLogQuery(); + if ($resource_ids) { + $query->withResourceIDs($resource_ids); + } + if ($lease_ids) { + $query->withLeaseIDs($lease_ids); + } + + return $query; } public function buildSearchForm( AphrontFormView $form, PhabricatorSavedQuery $saved) { $form ->appendControl( id(new AphrontFormTokenizerControl()) ->setDatasource(new DrydockResourceDatasource()) ->setName('resources') ->setLabel(pht('Resources')) ->setValue($saved->getParameter('resourcePHIDs', array()))) ->appendControl( id(new AphrontFormTokenizerControl()) ->setDatasource(new DrydockLeaseDatasource()) ->setName('leases') ->setLabel(pht('Leases')) ->setValue($saved->getParameter('leasePHIDs', array()))); } protected function getURI($path) { return '/drydock/log/'.$path; } protected function getBuiltinQueryNames() { return array( 'all' => pht('All Logs'), ); } public function buildSavedQueryFromBuiltin($query_key) { $query = $this->newSavedQuery(); $query->setQueryKey($query_key); switch ($query_key) { case 'all': return $query; } return parent::buildSavedQueryFromBuiltin($query_key); } protected function renderResultList( array $logs, PhabricatorSavedQuery $query, array $handles) { $list = id(new DrydockLogListView()) ->setUser($this->requireViewer()) ->setLogs($logs); $result = new PhabricatorApplicationSearchResultView(); $result->setTable($list); return $result; } } diff --git a/src/applications/drydock/query/DrydockResourceQuery.php b/src/applications/drydock/query/DrydockResourceQuery.php index cdc465b93a..d07e729276 100644 --- a/src/applications/drydock/query/DrydockResourceQuery.php +++ b/src/applications/drydock/query/DrydockResourceQuery.php @@ -1,109 +1,109 @@ ids = $ids; return $this; } public function withPHIDs(array $phids) { $this->phids = $phids; return $this; } public function withTypes(array $types) { $this->types = $types; return $this; } public function withStatuses(array $statuses) { $this->statuses = $statuses; return $this; } public function withBlueprintPHIDs(array $blueprint_phids) { $this->blueprintPHIDs = $blueprint_phids; return $this; } public function withDatasourceQuery($query) { $this->datasourceQuery = $query; return $this; } protected function loadPage() { $table = new DrydockResource(); $conn_r = $table->establishConnection('r'); $data = queryfx_all( $conn_r, 'SELECT resource.* FROM %T resource %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); $resources = $table->loadAllFromArray($data); return $resources; } protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { $where = array(); - if ($this->ids) { + if ($this->ids !== null) { $where[] = qsprintf( $conn_r, 'id IN (%Ld)', $this->ids); } - if ($this->phids) { + if ($this->phids !== null) { $where[] = qsprintf( $conn_r, 'phid IN (%Ls)', $this->phids); } - if ($this->types) { + if ($this->types !== null) { $where[] = qsprintf( $conn_r, 'type IN (%Ls)', $this->types); } - if ($this->statuses) { + if ($this->statuses !== null) { $where[] = qsprintf( $conn_r, 'status IN (%Ls)', $this->statuses); } - if ($this->blueprintPHIDs) { + if ($this->blueprintPHIDs !== null) { $where[] = qsprintf( $conn_r, 'blueprintPHID IN (%Ls)', $this->blueprintPHIDs); } - if ($this->datasourceQuery) { + if ($this->datasourceQuery !== null) { $where[] = qsprintf( $conn_r, 'name LIKE %>', $this->datasourceQuery); } $where[] = $this->buildPagingClause($conn_r); return $this->formatWhereClause($where); } }