Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AwsLoadMetric | |
0.00% |
0 / 28 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| getPeriod | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDimensions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| collect | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getMetric | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| getAwsClient | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2018 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoDelivery\model\Metrics; |
| 22 | |
| 23 | use DateInterval; |
| 24 | use DateTime; |
| 25 | use oat\awsTools\AwsClient; |
| 26 | use oat\tao\model\metrics\implementations\abstractMetrics; |
| 27 | |
| 28 | class AwsLoadMetric extends abstractMetrics implements InfrastructureLoadMetricInterface |
| 29 | { |
| 30 | private $awsClient; |
| 31 | |
| 32 | public const OPTION_PERIOD = 'period'; |
| 33 | public const OPTION_NAMESPACE = 'namespace'; |
| 34 | public const OPTION_DIMENSIONS = 'dimensions'; |
| 35 | |
| 36 | /** |
| 37 | * in seconds |
| 38 | * @return int |
| 39 | */ |
| 40 | public function getPeriod() |
| 41 | { |
| 42 | return $this->getOption(self::OPTION_PERIOD); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return string |
| 47 | */ |
| 48 | public function getNamespace() |
| 49 | { |
| 50 | return $this->getOption(self::OPTION_NAMESPACE); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return array |
| 55 | */ |
| 56 | public function getDimensions() |
| 57 | { |
| 58 | return $this->getOption(self::OPTION_DIMENSIONS); |
| 59 | } |
| 60 | |
| 61 | public function collect($force = false) |
| 62 | { |
| 63 | $active = $this->getPersistence()->get(self::class); |
| 64 | if (!$active || $force) { |
| 65 | $active = $this->getMetric(); |
| 66 | $this->getPersistence()->set(self::class, $active, $this->getOption(self::OPTION_TTL)); |
| 67 | } |
| 68 | return $active; |
| 69 | } |
| 70 | |
| 71 | private function getMetric() |
| 72 | { |
| 73 | $cloudWatchClient = $this->getAwsClient()->getCloudWatchClient(); |
| 74 | |
| 75 | $period = $this->getPeriod(); |
| 76 | $interval = new DateInterval('PT' . $period . 'S'); |
| 77 | $since = (new DateTime())->sub($interval); |
| 78 | |
| 79 | $result = $cloudWatchClient->getMetricStatistics([ |
| 80 | 'Namespace' => $this->getNamespace(), |
| 81 | 'MetricName' => 'CPUUtilization', |
| 82 | 'StartTime' => $since, |
| 83 | 'EndTime' => new DateTime(), |
| 84 | 'Period' => $period, |
| 85 | 'Statistics' => ['Average', 'Maximum'], |
| 86 | 'Dimensions' => $this->getDimensions(), |
| 87 | ]); |
| 88 | $this->logInfo('RDS_METRICS:' . json_encode($result->toArray())); |
| 89 | $data = $result->get('Datapoints'); |
| 90 | $v = array_shift($data); |
| 91 | return $v['Average']; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Requires lib-generis-aws at least 0.9.3 |
| 96 | * @return AwsClient |
| 97 | */ |
| 98 | public function getAwsClient() |
| 99 | { |
| 100 | if (!$this->awsClient) { |
| 101 | $this->awsClient = $this->getServiceLocator()->get('generis/awsClient'); |
| 102 | } |
| 103 | return $this->awsClient; |
| 104 | } |
| 105 | } |