Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
->setRules([
'@PER-CS' => true,
'@PHP84Migration' => true,
'no_unused_imports' => true,
])
->setFinder($finder)
;
455 changes: 455 additions & 0 deletions src/Command/Metrics/AbstractMetricsCommandBase.php

Large diffs are not rendered by default.

201 changes: 141 additions & 60 deletions src/Command/Metrics/AllMetricsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

namespace Platformsh\Cli\Command\Metrics;

use Platformsh\Cli\Model\Metrics\Aggregation;
use Platformsh\Cli\Model\Metrics\Field;
use Platformsh\Cli\Model\Metrics\Format;
use Platformsh\Cli\Model\Metrics\MetricKind;
use Platformsh\Cli\Model\Metrics\SourceField;
use Platformsh\Cli\Model\Metrics\SourceFieldPercentage;
use Platformsh\Cli\Selector\SelectorConfig;
use Platformsh\Cli\Selector\Selector;
use Khill\Duration\Duration;
use Platformsh\Cli\Model\Metrics\Field;
use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Service\Table;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -16,10 +21,10 @@
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'metrics:all', description: 'Show CPU, disk and memory metrics for an environment', aliases: ['metrics', 'met'])]
class AllMetricsCommand extends MetricsCommandBase
class AllMetricsCommand extends AbstractMetricsCommandBase
{
/** @var array<string, string> */
private array $tableHeader = [
private const TABLE_HEADER = [
'timestamp' => 'Timestamp',
'service' => 'Service',
'type' => 'Type',
Expand Down Expand Up @@ -50,91 +55,167 @@ class AllMetricsCommand extends MetricsCommandBase
];

/** @var string[] */
private array $defaultColumns = ['timestamp', 'service', 'cpu_percent', 'mem_percent', 'disk_percent', 'tmp_disk_percent'];
public function __construct(private readonly PropertyFormatter $propertyFormatter, private readonly Selector $selector, private readonly Table $table)
{
parent::__construct();
private array $defaultColumns = [
'timestamp',
'service',

'cpu_percent',
'mem_percent',
'disk_percent',
'inodes_percent',

'tmp_disk_percent',
'tmp_inodes_percent',
];

public function __construct(
private readonly PropertyFormatter $propertyFormatter,
Selector $selector,
Table $table
) {
parent::__construct($selector, $table);
}

protected function configure(): void
{
$this->addOption('bytes', 'B', InputOption::VALUE_NONE, 'Show sizes in bytes');
$this->addExample('Show metrics for the last ' . (new Duration())->humanize(self::DEFAULT_RANGE));
$this->addExample('Show metrics in five-minute intervals over the last hour', '-i 5m -r 1h');
$this->addExample('Show metrics over the last hour', ' -r 1h');
$this->addExample('Show metrics for all SQL services', '--type mariadb,%sql');
$this->addMetricsOptions();
$this->selector->addProjectOption($this->getDefinition());
$this->selector->addEnvironmentOption($this->getDefinition());
$this->addCompleter($this->selector);
Table::configureInput($this->getDefinition(), $this->tableHeader, $this->defaultColumns);
Table::configureInput($this->getDefinition(), self::TABLE_HEADER, $this->defaultColumns);
PropertyFormatter::configureInput($this->getDefinition());
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
protected function getChooseEnvFilter(): ?callable
{
$timeSpec = $this->validateTimeInput($input);
if ($timeSpec === false) {
return 1;
}

$selection = $this->selector->getSelection($input, new SelectorConfig(selectDefaultEnv: true, chooseEnvFilter: SelectorConfig::filterEnvsMaybeActive()));

if (!$this->table->formatIsMachineReadable()) {
$this->selector->ensurePrintedSelection($selection);
}
return SelectorConfig::filterEnvsMaybeActive();
}

// Only request the metrics fields that will be displayed.
//
// The fields are the selected column names (according to the $table
// service), filtered to only those that contain an underscore.
$fieldNames = array_filter($this->table->columnsToDisplay($this->tableHeader, $this->defaultColumns), fn($c): bool => str_contains((string) $c, '_'));
$values = $this->fetchMetrics($input, $timeSpec, $selection->getEnvironment(), $fieldNames);
if ($values === false) {
return 1;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
[$values, $environment] = $this->processQuery($input, [
MetricKind::API_TYPE_CPU,
MetricKind::API_TYPE_DISK,
MetricKind::API_TYPE_MEMORY,
MetricKind::API_TYPE_INODES,
], [MetricKind::API_AGG_AVG]);

$bytes = $input->getOption('bytes');

$rows = $this->buildRows($values, [
'cpu_used' => new Field('cpu_used', Field::FORMAT_ROUNDED_2DP),
'cpu_limit' => new Field('cpu_limit', Field::FORMAT_ROUNDED_2DP),
'cpu_percent' => new Field('cpu_percent', Field::FORMAT_PERCENT),

'mem_used' => new Field('mem_used', $bytes ? Field::FORMAT_ROUNDED : Field::FORMAT_MEMORY),
'mem_limit' => new Field('mem_limit', $bytes ? Field::FORMAT_ROUNDED : Field::FORMAT_MEMORY),
'mem_percent' => new Field('mem_percent', Field::FORMAT_PERCENT),

'disk_used' => new Field('disk_used', $bytes ? Field::FORMAT_ROUNDED : Field::FORMAT_DISK),
'disk_limit' => new Field('disk_limit', $bytes ? Field::FORMAT_ROUNDED : Field::FORMAT_DISK),
'disk_percent' => new Field('disk_percent', Field::FORMAT_PERCENT),

'tmp_disk_used' => new Field('tmp_disk_used', $bytes ? Field::FORMAT_ROUNDED : Field::FORMAT_DISK),
'tmp_disk_limit' => new Field('tmp_disk_limit', $bytes ? Field::FORMAT_ROUNDED : Field::FORMAT_DISK),
'tmp_disk_percent' => new Field('tmp_disk_percent', Field::FORMAT_PERCENT),

'inodes_used' => new Field('inodes_used', Field::FORMAT_ROUNDED),
'inodes_limit' => new Field('inodes_used', Field::FORMAT_ROUNDED),
'inodes_percent' => new Field('inodes_percent', Field::FORMAT_PERCENT),

'tmp_inodes_used' => new Field('tmp_inodes_used', Field::FORMAT_ROUNDED),
'tmp_inodes_limit' => new Field('tmp_inodes_used', Field::FORMAT_ROUNDED),
'tmp_inodes_percent' => new Field('tmp_inodes_percent', Field::FORMAT_PERCENT),
], $selection->getEnvironment());
'cpu_used' => new Field(
Format::Rounded2p,
new SourceField(MetricKind::CpuUsed, Aggregation::Avg),
),
'cpu_limit' => new Field(
Format::Rounded2p,
new SourceField(MetricKind::CpuLimit, Aggregation::Max),
),
'cpu_percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::CpuUsed, Aggregation::Avg),
new SourceField(MetricKind::CpuLimit, Aggregation::Max)
),
),

'mem_used' => new Field(
$bytes ? Format::Rounded : Format::Memory,
new SourceField(MetricKind::MemoryUsed, Aggregation::Avg),
),
'mem_limit' => new Field(
$bytes ? Format::Rounded : Format::Memory,
new SourceField(MetricKind::MemoryLimit, Aggregation::Max),
),
'mem_percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::MemoryUsed, Aggregation::Avg),
new SourceField(MetricKind::MemoryLimit, Aggregation::Max)
),
false,
),

'disk_used' => new Field(
$bytes ? Format::Rounded : Format::Disk,
new SourceField(MetricKind::DiskUsed, Aggregation::Avg, '/mnt'),
),
'disk_limit' => new Field(
$bytes ? Format::Rounded : Format::Disk,
new SourceField(MetricKind::DiskLimit, Aggregation::Max, '/mnt'),
),
'disk_percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::DiskUsed, Aggregation::Avg, '/mnt'),
new SourceField(MetricKind::DiskLimit, Aggregation::Max, '/mnt')
),
),

'tmp_disk_used' => new Field(
$bytes ? Format::Rounded : Format::Disk,
new SourceField(MetricKind::DiskUsed, Aggregation::Avg, '/tmp'),
),
'tmp_disk_limit' => new Field(
$bytes ? Format::Rounded : Format::Disk,
new SourceField(MetricKind::DiskLimit, Aggregation::Max, '/tmp'),
),
'tmp_disk_percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::DiskUsed, Aggregation::Avg, '/tmp'),
new SourceField(MetricKind::DiskLimit, Aggregation::Max, '/tmp')
),
),

'inodes_used' => new Field(
Format::Rounded,
new SourceField(MetricKind::InodesUsed, Aggregation::Avg, '/mnt'),
),
'inodes_limit' => new Field(
Format::Rounded,
new SourceField(MetricKind::InodesLimit, Aggregation::Max, '/mnt'),
),
'inodes_percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::InodesUsed, Aggregation::Avg, '/mnt'),
new SourceField(MetricKind::InodesLimit, Aggregation::Max, '/mnt')
),
),

'tmp_inodes_used' => new Field(
Format::Rounded,
new SourceField(MetricKind::InodesUsed, Aggregation::Avg, '/tmp'),
),
'tmp_inodes_limit' => new Field(
Format::Rounded,
new SourceField(MetricKind::InodesLimit, Aggregation::Max, '/tmp'),
),
'tmp_inodes_percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::InodesUsed, Aggregation::Avg, '/tmp'),
new SourceField(MetricKind::InodesLimit, Aggregation::Max, '/tmp')
),
),
], $environment);

if (!$this->table->formatIsMachineReadable()) {
$formatter = $this->propertyFormatter;
$this->stdErr->writeln(\sprintf(
'Metrics at <info>%s</info> intervals from <info>%s</info> to <info>%s</info>:',
(new Duration())->humanize($timeSpec->getInterval()),
$formatter->formatDate($timeSpec->getStartTime()),
$formatter->formatDate($timeSpec->getEndTime()),
(new Duration())->humanize($values['_grain']),
$formatter->formatDate($values['_from']),
$formatter->formatDate($values['_to']),
));
}

$this->table->render($rows, $this->tableHeader, $this->defaultColumns);
$this->table->render($rows, self::TABLE_HEADER, $this->defaultColumns);

if (!$this->table->formatIsMachineReadable()) {
$this->explainHighMemoryServices();
Expand Down
71 changes: 37 additions & 34 deletions src/Command/Metrics/CpuCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@

namespace Platformsh\Cli\Command\Metrics;

use Platformsh\Cli\Selector\SelectorConfig;
use Platformsh\Cli\Model\Metrics\Aggregation;
use Platformsh\Cli\Model\Metrics\Field;
use Platformsh\Cli\Model\Metrics\Format;
use Platformsh\Cli\Model\Metrics\MetricKind;
use Platformsh\Cli\Model\Metrics\SourceField;
use Platformsh\Cli\Model\Metrics\SourceFieldPercentage;
use Platformsh\Cli\Selector\Selector;
use Khill\Duration\Duration;
use Platformsh\Cli\Model\Metrics\Field;
use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Service\Table;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'metrics:cpu', description: 'Show CPU usage of an environment', aliases: ['cpu'])]
class CpuCommand extends MetricsCommandBase
class CpuCommand extends AbstractMetricsCommandBase
{
/** @var array<string, string> */
private array $tableHeader = [
private const TABLE_HEADER = [
'timestamp' => 'Timestamp',
'service' => 'Service',
'type' => 'Type',
Expand All @@ -29,9 +33,13 @@ class CpuCommand extends MetricsCommandBase

/** @var string[] */
private array $defaultColumns = ['timestamp', 'service', 'used', 'limit', 'percent'];
public function __construct(private readonly PropertyFormatter $propertyFormatter, private readonly Selector $selector, private readonly Table $table)
{
parent::__construct();

public function __construct(
private readonly PropertyFormatter $propertyFormatter,
Selector $selector,
Table $table
) {
parent::__construct($selector, $table);
}

protected function configure(): void
Expand All @@ -40,48 +48,43 @@ protected function configure(): void
$this->selector->addProjectOption($this->getDefinition());
$this->selector->addEnvironmentOption($this->getDefinition());
$this->addCompleter($this->selector);
Table::configureInput($this->getDefinition(), $this->tableHeader, $this->defaultColumns);
Table::configureInput($this->getDefinition(), self::TABLE_HEADER, $this->defaultColumns);
PropertyFormatter::configureInput($this->getDefinition());
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$timeSpec = $this->validateTimeInput($input);
if ($timeSpec === false) {
return 1;
}

$selection = $this->selector->getSelection($input, new SelectorConfig(selectDefaultEnv: true));

if (!$this->table->formatIsMachineReadable()) {
$this->selector->ensurePrintedSelection($selection);
}

$values = $this->fetchMetrics($input, $timeSpec, $selection->getEnvironment(), ['cpu_used', 'cpu_percent', 'cpu_limit']);
if ($values === false) {
return 1;
}
[$values, $environment] = $this->processQuery($input, [MetricKind::API_TYPE_CPU], [MetricKind::API_AGG_AVG]);

$rows = $this->buildRows($values, [
'used' => new Field('cpu_used', Field::FORMAT_ROUNDED_2DP),
'limit' => new Field('cpu_limit', Field::FORMAT_ROUNDED_2DP),
'percent' => new Field('cpu_percent', Field::FORMAT_PERCENT),
], $selection->getEnvironment());
'used' => new Field(
Format::Rounded2p,
new SourceField(MetricKind::CpuUsed, Aggregation::Avg),
),
'limit' => new Field(
Format::Rounded2p,
new SourceField(MetricKind::CpuLimit, Aggregation::Max),
),
'percent' => new Field(
Format::Percent,
new SourceFieldPercentage(
new SourceField(MetricKind::CpuUsed, Aggregation::Avg),
new SourceField(MetricKind::CpuLimit, Aggregation::Max)
),
),
], $environment);

if (!$this->table->formatIsMachineReadable()) {
$formatter = $this->propertyFormatter;
$this->stdErr->writeln(\sprintf(
'Average CPU usage at <info>%s</info> intervals from <info>%s</info> to <info>%s</info>:',
(new Duration())->humanize($timeSpec->getInterval()),
$formatter->formatDate($timeSpec->getStartTime()),
$formatter->formatDate($timeSpec->getEndTime()),
(new Duration())->humanize($values['_grain']),
$formatter->formatDate($values['_from']),
$formatter->formatDate($values['_to']),
));
}

$this->table->render($rows, $this->tableHeader, $this->defaultColumns);
$this->table->render($rows, self::TABLE_HEADER, $this->defaultColumns);

return 0;
}
Expand Down
Loading