public function getUsed($accountId = null, $envId = null)
{
$status = [];
$sql = "\n SELECT (\n SELECT COUNT(*)\n FROM servers r\n WHERE r.image_id = ? AND r.platform = ? {CLOUD_LOCATION} {SERVERS}\n ) AS serversEnvironment, (\n SELECT r.name\n FROM role_images ri\n JOIN roles r ON r.id = ri.role_id\n WHERE ri.image_id = ? AND ri.platform = ? AND ri.cloud_location = ? {ROLES}\n LIMIT 1\n ) AS roleName,\n SUM(IF(r.client_id IS NULL, 1, 0)) AS rolesScalr,\n SUM(IF(r.client_id IS NOT NULL AND r.env_id IS NULL, 1, 0)) AS rolesAccount,\n SUM(IF(r.client_id IS NOT NULL AND r.env_id IS NOT NULL, 1, 0)) AS rolesEnvironment\n FROM role_images ri\n JOIN roles r ON r.id = ri.role_id\n WHERE ri.image_id = ? AND ri.platform = ? AND ri.cloud_location = ? {IMAGES}\n ";
$args = [$this->id, $this->platform, $this->id, $this->platform, $this->cloudLocation, $this->id, $this->platform, $this->cloudLocation];
$sql = str_replace("{CLOUD_LOCATION}", $this->platform != \SERVER_PLATFORMS::GCE ? " AND r.cloud_location = " . $this->db()->qstr($this->cloudLocation) : "", $sql);
if ($this->accountId && $this->envId || $this->accountId && !$this->envId && $envId || !$this->accountId && $accountId && $envId) {
if ($this->accountId && $this->envId) {
// environment image
$id = $this->envId;
} else {
// account image in this environment
// scalr image in this environment
$id = $envId;
}
$status = $this->db()->GetRow(str_replace(["{SERVERS}", "{ROLES}", "{IMAGES}"], [" AND r.env_id = " . $this->db()->qstr($id), " AND r.env_id = " . $this->db()->qstr($id), " AND (r.env_id = " . $this->db()->qstr($id) . " OR r.client_id = " . $this->db()->qstr($this->accountId) . " AND r.env_id IS NULL)"], $sql), $args);
} else {
if ($this->accountId && !$this->envId && !$envId || !$this->accountId && $accountId && !$envId) {
if ($this->accountId && !$this->envId && !$envId) {
// account image
$id = $this->accountId;
} else {
// scalr image on account scope
$id = $accountId;
}
$status = $this->db()->GetRow(str_replace(["{SERVERS}", "{ROLES}", "{IMAGES}"], [" AND r.client_id = " . $this->db()->qstr($id), " AND r.env_id IS NULL AND r.client_id = " . $this->db()->qstr($id), " AND r.client_id = " . $this->db()->qstr($id)], $sql), $args);
} else {
if (!$this->accountId && !$accountId) {
// scalr image
$status = $this->db()->GetRow(str_replace(["{SERVERS}", "{ROLES}", "{IMAGES}"], ["", " AND r.client_id IS NULL", ""], $sql), $args);
}
}
}
$emptyValue = true;
foreach ($status as $name => &$cnt) {
if (is_numeric($cnt) || is_null($cnt)) {
$cnt = (int) $cnt;
}
if (is_int($cnt) && $cnt > 0) {
$emptyValue = false;
}
}
return $emptyValue ? false : $status;
}