2012-03-27 05:54:26 +02:00
|
|
|
<?php
|
|
|
|
|
2013-12-27 22:15:30 +01:00
|
|
|
final class DrydockLogQuery extends DrydockQuery {
|
2012-11-02 00:53:17 +01:00
|
|
|
|
2012-03-27 05:54:26 +02:00
|
|
|
private $resourceIDs;
|
|
|
|
private $leaseIDs;
|
|
|
|
|
|
|
|
public function withResourceIDs(array $ids) {
|
|
|
|
$this->resourceIDs = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withLeaseIDs(array $ids) {
|
|
|
|
$this->leaseIDs = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 21:30:17 +01:00
|
|
|
public function loadPage() {
|
2012-03-27 05:54:26 +02:00
|
|
|
$table = new DrydockLog();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT log.* FROM %T log %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
2012-11-02 00:53:17 +01:00
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
2012-03-27 05:54:26 +02:00
|
|
|
|
|
|
|
return $table->loadAllFromArray($data);
|
|
|
|
}
|
|
|
|
|
2013-12-26 21:30:17 +01:00
|
|
|
public function willFilterPage(array $logs) {
|
2013-12-27 22:15:19 +01:00
|
|
|
$resource_ids = array_filter(mpull($logs, 'getResourceID'));
|
|
|
|
if ($resource_ids) {
|
|
|
|
$resources = id(new DrydockResourceQuery())
|
|
|
|
->setParentQuery($this)
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withIDs($resource_ids)
|
|
|
|
->execute();
|
|
|
|
} else {
|
|
|
|
$resources = array();
|
|
|
|
}
|
2013-12-26 21:30:17 +01:00
|
|
|
|
|
|
|
foreach ($logs as $key => $log) {
|
2013-12-27 22:15:19 +01:00
|
|
|
$resource = null;
|
|
|
|
if ($log->getResourceID()) {
|
|
|
|
$resource = idx($resources, $log->getResourceID());
|
|
|
|
if (!$resource) {
|
|
|
|
unset($logs[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-12-26 21:30:17 +01:00
|
|
|
$log->attachResource($resource);
|
|
|
|
}
|
|
|
|
|
2013-12-27 22:15:19 +01:00
|
|
|
$lease_ids = array_filter(mpull($logs, 'getLeaseID'));
|
|
|
|
if ($lease_ids) {
|
|
|
|
$leases = id(new DrydockLeaseQuery())
|
|
|
|
->setParentQuery($this)
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withIDs($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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 21:30:17 +01:00
|
|
|
return $logs;
|
|
|
|
}
|
|
|
|
|
2012-11-02 00:53:17 +01:00
|
|
|
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
|
2012-03-27 05:54:26 +02:00
|
|
|
$where = array();
|
|
|
|
|
|
|
|
if ($this->resourceIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'resourceID IN (%Ld)',
|
|
|
|
$this->resourceIDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->leaseIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'leaseID IN (%Ld)',
|
|
|
|
$this->leaseIDs);
|
|
|
|
}
|
|
|
|
|
2013-12-26 21:30:17 +01:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
2012-11-02 00:53:17 +01:00
|
|
|
|
2012-03-27 05:54:26 +02:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|