Skip to content

Commit 3ccab87

Browse files
committed
Validate the metrics --range against the environment's max_range
1 parent a6aaa33 commit 3ccab87

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

src/Command/Metrics/AllMetricsCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ protected function configure()
6767
*/
6868
protected function execute(InputInterface $input, OutputInterface $output)
6969
{
70-
$timeSpec = $this->validateTimeInput($input);
70+
$this->chooseEnvFilter = $this->filterEnvsByState(['active']);
71+
$this->validateInput($input, false, true);
72+
73+
$timeSpec = $this->validateTimeInput($input, $this->getSelectedEnvironment());
7174
if ($timeSpec === false) {
7275
return 1;
7376
}
7477

75-
$this->chooseEnvFilter = $this->filterEnvsByState(['active']);
76-
$this->validateInput($input, false, true);
77-
7878
/** @var \Platformsh\Cli\Service\Table $table */
7979
$table = $this->getService('table');
8080

src/Command/Metrics/CpuCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ protected function configure()
4141
*/
4242
protected function execute(InputInterface $input, OutputInterface $output)
4343
{
44-
$timeSpec = $this->validateTimeInput($input);
44+
$this->chooseEnvFilter = $this->filterEnvsByState(['active']);
45+
$this->validateInput($input, false, true);
46+
47+
$timeSpec = $this->validateTimeInput($input, $this->getSelectedEnvironment());
4548
if ($timeSpec === false) {
4649
return 1;
4750
}
4851

49-
$this->validateInput($input, false, true);
50-
5152
/** @var \Platformsh\Cli\Service\Table $table */
5253
$table = $this->getService('table');
5354

src/Command/Metrics/CurlCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ protected function configure()
2222

2323
protected function execute(InputInterface $input, OutputInterface $output)
2424
{
25+
$this->chooseEnvFilter = $this->filterEnvsByState(['active']);
2526
$this->validateInput($input, false, true);
2627

2728
// Initialize the API service so that it gets CommandBase's event listeners

src/Command/Metrics/DiskUsageCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ protected function configure()
5353
*/
5454
protected function execute(InputInterface $input, OutputInterface $output)
5555
{
56-
$timeSpec = $this->validateTimeInput($input);
56+
$this->chooseEnvFilter = $this->filterEnvsByState(['active']);
57+
$this->validateInput($input, false, true);
58+
59+
$timeSpec = $this->validateTimeInput($input, $this->getSelectedEnvironment());
5760
if ($timeSpec === false) {
5861
return 1;
5962
}
@@ -66,8 +69,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6669
$table = $this->getService('table');
6770
$table->removeDeprecatedColumns(['interval'], '', $input, $output);
6871

69-
$this->validateInput($input, false, true);
70-
7172
if (!$table->formatIsMachineReadable()) {
7273
$this->displayEnvironmentHeader();
7374
}

src/Command/Metrics/MemCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ protected function configure()
4343
*/
4444
protected function execute(InputInterface $input, OutputInterface $output)
4545
{
46-
$timeSpec = $this->validateTimeInput($input);
46+
$this->chooseEnvFilter = $this->filterEnvsByState(['active']);
47+
$this->validateInput($input, false, true);
48+
49+
$timeSpec = $this->validateTimeInput($input, $this->getSelectedEnvironment());
4750
if ($timeSpec === false) {
4851
return 1;
4952
}
5053

51-
$this->validateInput($input, false, true);
52-
5354
/** @var \Platformsh\Cli\Service\Table $table */
5455
$table = $this->getService('table');
5556

src/Command/Metrics/MetricsCommandBase.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected function addMetricsOptions()
123123
/**
124124
* Returns the metrics URL and collection information for the selected environment.
125125
*
126-
* @return array{'href': string, 'collection': string}|false
126+
* @return array{href: string, collection: string, max_range: string}|false
127127
* The link data or false on failure.
128128
*/
129129
protected function getMetricsLink(Environment $environment)
@@ -332,10 +332,11 @@ protected function fetchMetrics(InputInterface $input, TimeSpec $timeSpec, Envir
332332
* @see self::startTime, self::$endTime, self::$interval
333333
*
334334
* @param InputInterface $input
335+
* @param Environment|null $environment
335336
*
336337
* @return TimeSpec|false
337338
*/
338-
protected function validateTimeInput(InputInterface $input)
339+
protected function validateTimeInput(InputInterface $input, Environment $environment = null)
339340
{
340341
$interval = null;
341342
if ($intervalStr = $input->getOption('interval')) {
@@ -361,13 +362,24 @@ protected function validateTimeInput(InputInterface $input)
361362
$endTime = time();
362363
}
363364
if ($rangeStr = $input->getOption('range')) {
364-
$rangeSeconds = (new Duration())->toSeconds($rangeStr);
365+
$rangeDuration = $this->parseDuration($rangeStr);
366+
if (!$rangeDuration) {
367+
$this->stdErr->writeln('Invalid --range: <error>' . $rangeStr . '</error>');
368+
return false;
369+
}
370+
$rangeSeconds = $rangeDuration->toSeconds();
365371
if (empty($rangeSeconds)) {
366372
$this->stdErr->writeln('Invalid --range: <error>' . $rangeStr . '</error>');
367373
return false;
368374
} elseif ($rangeSeconds < self::MIN_RANGE) {
369375
$this->stdErr->writeln(\sprintf('The --range <error>%s</error> is too short: it must be at least %d seconds (%s).', $rangeStr, self::MIN_RANGE, (new Duration())->humanize(self::MIN_RANGE)));
370376
return false;
377+
} elseif ($environment && ($link = $this->getMetricsLink($environment)) && isset($link['max_range'])) {
378+
$maxRange = $this->parseDuration($link['max_range']);
379+
if ($rangeSeconds > $maxRange->toSeconds()) {
380+
$this->stdErr->writeln(\sprintf('The --range <error>%s</error> is too long: the maximum is %s.', $rangeStr, $link['max_range']));
381+
return false;
382+
}
371383
}
372384
$rangeSeconds = \intval($rangeSeconds);
373385
} else {
@@ -421,6 +433,22 @@ private function defaultInterval($range)
421433
return (int) $interval;
422434
}
423435

436+
/**
437+
* @param string $duration
438+
* @return Duration|false
439+
*/
440+
private function parseDuration($duration)
441+
{
442+
if (substr($duration, -1) === 'y') {
443+
$num = rtrim($duration, 'y');
444+
if (!is_numeric($num)) {
445+
return false;
446+
}
447+
return new Duration(intval($num) * 365.25 * 86400);
448+
}
449+
return (new Duration())->parse($duration);
450+
}
451+
424452
/**
425453
* Returns the deployment type of an environment (needed for differing queries).
426454
*

0 commit comments

Comments
 (0)