public function getContent($params = array()) { $this->request->restrictAccess(Acl::RESOURCE_OWN_FARMS, Acl::PERM_FARMS_CREATE); $projects = []; if ($this->getContainer()->analytics->enabled && $this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID)) { $costCenter = $this->getContainer()->analytics->ccs->get($this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID)); $currentYear = (new \DateTime('now', new \DateTimeZone('UTC')))->format('Y'); $quarters = new Quarters(SettingEntity::getQuarters()); $currentQuarter = $quarters->getQuarterForDate(new \DateTime('now', new \DateTimeZone('UTC'))); if ($costCenter instanceof CostCentreEntity) { $projectsIterator = new SharedProjectsFilterIterator($costCenter->getProjects(), $costCenter->ccId, $this->user, $this->getEnvironment()); foreach ($projectsIterator as $item) { $quarterBudget = QuarterlyBudgetEntity::findOne([['year' => $currentYear], ['subjectType' => QuarterlyBudgetEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $item->projectId], ['quarter' => $currentQuarter]]); $projects[] = array('projectId' => $item->projectId, 'name' => $item->name, 'budgetRemain' => !is_null($quarterBudget) && $quarterBudget->budget > 0 ? max(0, round($quarterBudget->budget - $quarterBudget->cumulativespend)) : null); } //$costCentreName = $costCenter->name; $isLocked = $costCenter->getProperty(CostCentrePropertyEntity::NAME_LOCKED); $accountCcs = AccountCostCenterEntity::findOne([['accountId' => $this->environment->clientId], ['ccId' => $costCenter->ccId]]); if ($isLocked || !$accountCcs instanceof AccountCostCenterEntity) { $costCentreLocked = 1; } else { $costCentreLocked = 0; } } else { $costCentreName = ''; $costCentreLocked = 0; } } return ['costCenterLocked' => $costCentreLocked, 'projects' => $projects]; }
/** * Checks whether cost ceter can be removed * * If Cost centre can't be archived and removed it will throw an exception * * @return boolean Returns TRUE if cost center can be removed or false if it can be archived or * throws an exception otherwise * @throws AnalyticsException */ public function checkRemoval() { //Checks data integrity if ($this->ccId == Usage::DEFAULT_CC_ID) { throw new AnalyticsException(sprintf("'%s' is default automatically created Cost Center and it can not be archived.", $this->name)); } $accountCcs = \Scalr::getDb()->GetAll("\n SELECT ac.account_id, c.name FROM account_ccs ac\n JOIN clients c ON c.id = ac.account_id\n WHERE ac.cc_id = UNHEX(?)\n LIMIT 4\n ", [$this->type('ccId')->toDb($this->ccId)]); $someofthem = ''; foreach ($accountCcs as $ac) { $cnt = 0; if ($cnt++ > 3) { $someofthem .= ' ...'; break; } $someofthem .= ', "' . $ac['name'] . '"'; } if (count($accountCcs) > 0) { throw new AnalyticsException(sprintf("Cost center '%s' can not be archived because it is used by following account%s: %s. " . "Please contact your scalr admin to set free '%s' before you can archive it.", $this->name, count($accountCcs) > 1 ? 's' : '', substr($someofthem, 2), $this->name)); } $env = \Scalr::getDb()->GetRow("\n SELECT e.id, e.name FROM client_environments e\n JOIN client_environment_properties ep ON ep.env_id = e.id\n WHERE ep.name = ? AND ep.value = ?\n LIMIT 1\n ", [\Scalr_Environment::SETTING_CC_ID, strtolower($this->ccId)]); if ($env) { throw new AnalyticsException(sprintf("Cost center '%s' can not be archived because it is used by the environment '%s' (id:%d). " . "Please contact your scalr admin to reallocate %s to a new cost center before you can archive '%s'.", $this->name, $env['name'], $env['id'], $env['name'], $this->name)); } //Gets all projects related to cost centre $relatedProjects = $this->getProjects(); //Whether Cost Centre is assigned to active Projects if ($relatedProjects->filterByArchived(false)->count()) { $someofthem = ''; $cnt = 0; foreach ($relatedProjects as $p) { if ($cnt++ > 3) { $someofthem .= ' ...'; break; } $someofthem .= ', "' . $p->name . '"'; } throw new AnalyticsException(sprintf("Cost center '%s' can not be archived because it is used by %d project%s including: %s. " . "Please contact your scalr admin to reallocate %s to a new cost center before you can archive '%s'.", $this->name, $relatedProjects->count(), $relatedProjects->count() > 1 ? 's' : '', substr($someofthem, 2), $relatedProjects->count() > 1 ? 'them' : substr($someofthem, 2), $this->name)); } //It can be comletely removed if there isn't any project that has been assigned to cost centre. $bAllowedToRemove = $relatedProjects->count() == 0 && $this->ccId != Usage::DEFAULT_CC_ID; //Are there any record for this cost centre in the usage statistics? if ($bAllowedToRemove && \Scalr::getContainer()->analytics->enabled) { $budget = QuarterlyBudgetEntity::findOne([['subjectType' => QuarterlyBudgetEntity::SUBJECT_TYPE_CC], ['subjectId' => $this->ccId], ['cumulativespend' => ['$gt' => 0]]]); if ($budget) { //It can only be archived $bAllowedToRemove = false; } } return $bAllowedToRemove; }
/** * Checks whether project can be removed * * If Project can't be archived and removed it will throw an exception * * @return boolean Returns TRUE if Project can be removed or false if it can be archived or * throws an exception otherwise * @throws AnalyticsException */ public function checkRemoval() { //Checks data integrity if ($this->projectId == Usage::DEFAULT_PROJECT_ID) { throw new AnalyticsException(sprintf("'%s' is default automatically created Project and it can not be archived.", $this->name)); } $farm = \Scalr::getDb()->GetRow("\n SELECT f.id, f.name FROM farms f\n JOIN farm_settings fs ON fs.farmid = f.id\n WHERE fs.name = ? AND fs.value = ?\n LIMIT 1\n ", [Entity\FarmSetting::PROJECT_ID, strtolower($this->projectId)]); if ($farm) { throw new AnalyticsException(sprintf("Project '%s' can not be archived because it is used by the farm '%s' (id:%d). " . "Reallocate '%s' to another project first.", $this->name, $farm['name'], $farm['id'], $farm['name'], $this->name)); } $bAllowedToRemove = $this->projectId != Usage::DEFAULT_PROJECT_ID; //Are there any record for this project in the usage statistics? if ($bAllowedToRemove && \Scalr::getContainer()->analytics->enabled) { $budget = QuarterlyBudgetEntity::findOne([['subjectType' => QuarterlyBudgetEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $this->projectId], ['cumulativespend' => ['$gt' => 0]]]); if ($budget) { //It can only be archived $bAllowedToRemove = false; } } return $bAllowedToRemove; }
/** * Checks whether project can be removed * * If Project can't be archived and removed it will throw an exception * * @return boolean Returns TRUE if Project can be removed or false if it can be archived or * throws an exception otherwise * @throws AnalyticsException */ public function checkRemoval() { //Checks data integrity $farm = \Scalr::getDb()->GetRow("\n SELECT f.id, f.name FROM farms f\n JOIN farm_settings fs ON fs.farmid = f.id\n WHERE fs.name = ? AND fs.value = ?\n LIMIT 1\n ", [\DBFarm::SETTING_PROJECT_ID, strtolower($this->projectId)]); if ($farm) { throw new AnalyticsException(sprintf("Project %s can not be archived because it is used by the farm %s (id:%d). " . "Please contact your scalr admin to reallocate %s to a new project before you can archive '%s'.", $this->name, $farm['name'], $farm['id'], $farm['name'], $this->name)); } $bAllowedToRemove = $this->projectId != Usage::DEFAULT_PROJECT_ID; //Are there any record for this project in the usage statistics? if ($bAllowedToRemove && \Scalr::getContainer()->analytics->enabled) { $budget = QuarterlyBudgetEntity::findOne([['subjectType' => QuarterlyBudgetEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $this->projectId], ['cumulativespend' => ['$gt' => 0]]]); if ($budget) { //It can only be archived $bAllowedToRemove = false; } } return $bAllowedToRemove; }
private function getFarmCostData($farmId) { $result = []; $costCenter = $this->getContainer()->analytics->ccs->get($this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID)); $currentYear = (new \DateTime('now', new \DateTimeZone('UTC')))->format('Y'); $quarters = new Quarters(SettingEntity::getQuarters()); $currentQuarter = $quarters->getQuarterForDate(new \DateTime('now', new \DateTimeZone('UTC'))); $projects = []; if ($farmId) { $farm = DBFarm::LoadByID($farmId); $currentProjectId = $farm->GetSetting(Entity\FarmSetting::PROJECT_ID); $currentProject = ProjectEntity::findPk($currentProjectId); /* @var $currentProject ProjectEntity */ if (!empty($currentProject)) { $quarterBudget = QuarterlyBudgetEntity::findOne([['year' => $currentYear], ['subjectType' => QuarterlyBudgetEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $currentProject->projectId], ['quarter' => $currentQuarter]]); $projects[] = ['projectId' => $currentProject->projectId, 'name' => "{$currentProject->name} / {$currentProject->getCostCenter()->name}", 'budgetRemain' => !is_null($quarterBudget) && $quarterBudget->budget > 0 ? max(0, round($quarterBudget->budget - $quarterBudget->cumulativespend)) : null]; } $result['projectId'] = $farm->GetSetting(Entity\FarmSetting::PROJECT_ID); $result['farmCostMetering'] = $result['projectId'] ? $this->getContainer()->analytics->usage->getFarmCostMetering($this->user->getAccountId(), $farmId) : null; } if ($costCenter instanceof CostCentreEntity) { $projectsIterator = new SharedProjectsFilterIterator($costCenter->getProjects(), $costCenter->ccId, $this->user, $this->getEnvironment()); foreach ($projectsIterator as $item) { /* @var $item Scalr\Stats\CostAnalytics\Entity\ProjectEntity */ if (!empty($currentProjectId) && $item->projectId == $currentProjectId) { continue; } $quarterBudget = QuarterlyBudgetEntity::findOne([['year' => $currentYear], ['subjectType' => QuarterlyBudgetEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $item->projectId], ['quarter' => $currentQuarter]]); $projects[] = array('projectId' => $item->projectId, 'name' => "{$item->name} / {$costCenter->name}", 'budgetRemain' => !is_null($quarterBudget) && $quarterBudget->budget > 0 ? max(0, round($quarterBudget->budget - $quarterBudget->cumulativespend)) : null); } $costCentreName = $costCenter->name; $isLocked = $costCenter->getProperty(CostCentrePropertyEntity::NAME_LOCKED); $accountCcs = AccountCostCenterEntity::findOne([['accountId' => $this->environment->clientId], ['ccId' => $costCenter->ccId]]); if ($isLocked || !$accountCcs instanceof AccountCostCenterEntity) { $costCentreLocked = 1; } else { $costCentreLocked = 0; } } else { $costCentreName = ''; $costCentreLocked = 0; } $supportedClouds = $this->getContainer()->analytics->prices->getSupportedClouds(); $result['analytics'] = array('costCenterName' => $costCentreName, 'costCenterLocked' => $costCentreLocked, 'projects' => $projects, 'unsupportedClouds' => array_values(array_diff($this->environment->getEnabledPlatforms(), $supportedClouds))); return $result; }