/** * Save metric. * * @param string $name * @param string $retrieveMethod * @param string $calcFunction * @param int $metricId optional * @param string $filePath optional * @param bool $isInvert optional * @throws Exception * @throws Scalr_Exception_Core * @throws Scalr_Exception_InsufficientPermissions * @throws \Scalr\Exception\ModelException */ public function xSaveAction($name, $retrieveMethod, $calcFunction = null, $metricId = null, $filePath = null, $isInvert = false) { $this->request->restrictAccess(Acl::RESOURCE_GENERAL_CUSTOM_SCALING_METRICS, Acl::PERM_GENERAL_CUSTOM_SCALING_METRICS_MANAGE); $validator = new Validator(); if ($metricId) { /* @var $metric Entity\ScalingMetric */ $metric = Entity\ScalingMetric::findPk($metricId); if (!$metric) { throw new Scalr_UI_Exception_NotFound(); } $this->user->getPermissions()->validate($metric); } else { $metric = new Entity\ScalingMetric(); $metric->accountId = $this->user->getAccountId(); $metric->envId = $this->getEnvironmentId(); $metric->alias = 'custom'; $metric->algorithm = Entity\ScalingMetric::ALGORITHM_SENSOR; } if (!preg_match('/^' . Entity\ScalingMetric::NAME_REGEXP . '$/', $name)) { $validator->addError('name', 'Metric name should be both alphanumeric and greater than 5 chars'); } if ($retrieveMethod == Entity\ScalingMetric::RETRIEVE_METHOD_URL_REQUEST) { $validator->addErrorIf($validator->validateUrl($filePath) !== true, 'filePath', 'Invalid URL'); } else { $validator->addErrorIf($validator->validateNotEmpty($calcFunction) !== true, 'calcFunction', 'Calculation function is required'); } $criteria = []; $criteria[] = ['name' => $name]; if ($metricId) { $criteria[] = ['id' => ['$ne' => $metricId]]; } if (Entity\ScalingMetric::findOne($criteria)) { $validator->addError('name', 'Metric with the same name already exists'); } if ($validator->isValid($this->response)) { $metric->name = $name; $metric->filePath = $filePath; $metric->retrieveMethod = $retrieveMethod; $metric->calcFunction = $calcFunction; $metric->isInvert = $isInvert; $metric->save(); $this->response->success('Scaling metric has been successfully saved.'); $this->response->data(['metric' => get_object_vars($metric)]); } }
/** * Save metric. * * @param string $name * @param string $retrieveMethod * @param string $calcFunction * @param int $metricId optional * @param string $filePath optional * @throws Exception * @throws Scalr_Exception_Core * @throws Scalr_Exception_InsufficientPermissions * @throws \Scalr\Exception\ModelException */ public function xSaveAction($name, $retrieveMethod, $calcFunction, $metricId = null, $filePath = null) { $this->request->restrictAccess(Acl::RESOURCE_GENERAL_CUSTOM_SCALING_METRICS); $validator = new Validator(); if ($metricId) { /* @var \Scalr\Model\Entity\ScalingMetric $metric */ $metric = Entity\ScalingMetric::findPk($metricId); if (!$metric) { throw new Scalr_UI_Exception_NotFound(); } $this->user->getPermissions()->validate($metric); } else { $metric = new Entity\ScalingMetric(); $metric->accountId = $this->user->getAccountId(); $metric->envId = $this->getEnvironmentId(); $metric->alias = 'custom'; $metric->algorithm = Entity\ScalingMetric::ALGORITHM_SENSOR; } if (!preg_match('/^[A-Za-z0-9]{5,}$/', $name)) { $validator->addError('name', 'Metric name should be alphanumeric and greater than 5 chars'); } $criteria = []; $criteria[] = ['name' => $name]; if ($metricId) { $criteria[] = ['id' => ['$ne' => $metricId]]; } if (Entity\ScalingMetric::findOne($criteria)) { $validator->addError('name', 'Metric with the same name already exist'); } if ($validator->isValid($this->response)) { $metric->name = $name; $metric->filePath = $filePath; $metric->retrieveMethod = $retrieveMethod; $metric->calcFunction = $calcFunction; $metric->save(); $this->response->success('Scaling metric successfully saved'); $this->response->data(['metric' => get_object_vars($metric)]); } }
/** * * @return Entity\ScalingMetric */ function getMetric() { if (!$this->metric) { $this->metric = Entity\ScalingMetric::findPk($this->metricId); } return $this->metric; }