getAlgorithm() public static method

Get specified algorithm object
public static getAlgorithm ( string $algoName ) : Scalr_Scaling_Algorithms_DateTim\Scalr_Scaling_Algorithms_DateTime | Scalr_Scaling_Algorithms_Senso\Scalr_Scaling_Algorithms_Sensor
$algoName string Name of algorithm
return Scalr_Scaling_Algorithms_DateTim\Scalr_Scaling_Algorithms_DateTime | Scalr_Scaling_Algorithms_Senso\Scalr_Scaling_Algorithms_Sensor Algorithm object
Example #1
0
 function getScalingDecision()
 {
     $algo = Entity\ScalingMetric::getAlgorithm($this->getMetric()->algorithm);
     $sensor = Scalr_Scaling_Sensor::get($this->getMetric()->alias);
     $dbFarmRole = DBFarmRole::LoadByID($this->farmRoleId);
     if ($sensor) {
         try {
             $sensorValue = $sensor->getValue($dbFarmRole, $this);
             if ($sensorValue === false) {
                 return Scalr_Scaling_Decision::NOOP;
             }
         } catch (Exception $e) {
             $this->logger->warn(new FarmLogMessage($dbFarmRole->FarmID, sprintf("Unable to read Scaling Metric (%s) on farmrole %s value: %s", $this->getMetric()->alias, $dbFarmRole->ID, $e->getMessage())));
             return Scalr_Scaling_Decision::NOOP;
         }
         $this->logger->debug(sprintf(_("Raw sensor value (id: %s, metric_id: %s, metric name: %s): %s"), $this->id, $this->metricId, $this->getMetric()->name, json_encode($sensorValue)));
         switch ($this->getMetric()->calcFunction) {
             case Entity\ScalingMetric::CALC_FUNCTION_AVERAGE:
                 $value = is_array($sensorValue) && count($sensorValue) != 0 ? @array_sum($sensorValue) / count($sensorValue) : 0;
                 break;
             case Entity\ScalingMetric::CALC_FUNCTION_SUM:
                 $value = @array_sum($sensorValue);
                 break;
             case Entity\ScalingMetric::CALC_FUNCTION_MAXIMUM:
                 $value = @max($sensorValue);
                 break;
             case Entity\ScalingMetric::CALC_FUNCTION_MINIMUM:
                 $value = @min($sensorValue);
                 break;
             default:
                 $value = $sensorValue[0];
         }
         $this->lastValue = round($value, 5);
         // Sets the Server time to avoid differences between Database and Server time
         $this->dtLastPolled = time();
         $this->save(false, ['settings', 'metric_id']);
         $invert = $sensor instanceof Scalr_Scaling_Sensors_Custom ? $this->getMetric()->isInvert : $sensor->isInvert;
     } else {
         $invert = false;
     }
     if ($this->getMetric()->name == 'DateAndTime') {
         $decision = $algo->makeDecision($dbFarmRole, $this, $invert);
         $this->instancesNumber = $algo->instancesNumber;
         $this->lastValue = isset($algo->lastValue) ? $algo->lastValue : null;
         return $decision;
     } elseif ($this->getMetric()->name == 'FreeRam') {
         if ($this->lastValue == 0) {
             return Scalr_Scaling_Decision::NOOP;
         } else {
             return $algo->makeDecision($dbFarmRole, $this, $invert);
         }
     } else {
         return $algo->makeDecision($dbFarmRole, $this, $invert);
     }
 }
Example #2
0
 function getScalingDecision()
 {
     $algo = Entity\ScalingMetric::getAlgorithm($this->getMetric()->algorithm);
     $sensor = Scalr_Scaling_Sensor::get($this->getMetric()->alias);
     $dbFarmRole = DBFarmRole::LoadByID($this->farmRoleId);
     if ($sensor) {
         try {
             $sensorValue = $sensor->getValue($dbFarmRole, $this);
             if ($sensorValue === false) {
                 return Scalr_Scaling_Decision::NOOP;
             }
         } catch (Exception $e) {
             $this->logger->warn(new FarmLogMessage($dbFarmRole->FarmID, sprintf("Unable to read Scaling Metric (%s) on farmrole %s value: %s", $this->getMetric()->alias, $dbFarmRole->ID, $e->getMessage())));
             return Scalr_Scaling_Decision::NOOP;
         }
         $this->logger->info(sprintf(_("Raw sensor value (id: %s, metric_id: %s, metric name: %s): %s"), $this->id, $this->metricId, $this->getMetric()->name, serialize($sensorValue)));
         switch ($this->getMetric()->calcFunction) {
             default:
                 $value = $sensorValue[0];
                 break;
             case "avg":
                 $value = is_array($sensorValue) && count($sensorValue) != 0 ? @array_sum($sensorValue) / count($sensorValue) : 0;
                 break;
             case "sum":
                 $value = @array_sum($sensorValue);
                 break;
             case "max":
                 $value = @max($sensorValue);
                 break;
         }
         $this->lastValue = round($value, 5);
         $this->save();
         $invert = $sensor->isInvert;
     } else {
         $invert = false;
     }
     if ($this->getMetric()->name == 'DateAndTime') {
         $decision = $algo->makeDecision($dbFarmRole, $this, $invert);
         $this->instancesNumber = $algo->instancesNumber;
         $this->lastValue = $algo->lastValue;
         return $decision;
     } elseif ($this->getMetric()->name == 'FreeRam') {
         if ($this->lastValue == 0) {
             return Scalr_Scaling_Decision::NOOP;
         } else {
             return $algo->makeDecision($dbFarmRole, $this, $invert);
         }
     } else {
         return $algo->makeDecision($dbFarmRole, $this, $invert);
     }
 }