2021-04-22 04:34:32 -04:00
|
|
|
<?php
|
|
|
|
|
|
2021-08-20 04:56:25 -04:00
|
|
|
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */
|
2021-04-22 04:34:32 -04:00
|
|
|
|
2021-08-20 11:27:37 -04:00
|
|
|
namespace Icinga\Module\Icingadb\Widget\Detail;
|
2021-04-22 04:34:32 -04:00
|
|
|
|
|
|
|
|
use Icinga\Module\Icingadb\Util\PerfData;
|
|
|
|
|
use Icinga\Module\Icingadb\Util\PerfDataSet;
|
2021-06-22 04:29:25 -04:00
|
|
|
use ipl\Html\Attributes;
|
2021-04-22 04:34:32 -04:00
|
|
|
use ipl\Html\HtmlElement;
|
|
|
|
|
use ipl\Html\HtmlString;
|
|
|
|
|
use ipl\Html\Table;
|
2021-06-22 04:29:25 -04:00
|
|
|
use ipl\Html\Text;
|
2023-08-23 08:54:15 -04:00
|
|
|
use ipl\I18n\Translation;
|
2023-08-09 08:25:44 -04:00
|
|
|
use ipl\Web\Widget\EmptyState;
|
2023-08-23 08:54:15 -04:00
|
|
|
use ipl\Web\Widget\Icon;
|
2021-04-22 04:34:32 -04:00
|
|
|
|
|
|
|
|
class PerfDataTable extends Table
|
|
|
|
|
{
|
2023-08-23 08:54:15 -04:00
|
|
|
use Translation;
|
|
|
|
|
|
2021-04-22 04:34:32 -04:00
|
|
|
protected $defaultAttributes = [
|
|
|
|
|
'class' => 'performance-data-table collapsible',
|
|
|
|
|
'data-visible-rows' => 6
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/** @var string The perfdata string */
|
|
|
|
|
protected $perfdataStr;
|
|
|
|
|
|
|
|
|
|
/** @var int Max labels to show; 0 for no limit */
|
|
|
|
|
protected $limit;
|
|
|
|
|
|
|
|
|
|
/** @var string The color indicating the perfdata state */
|
|
|
|
|
protected $color;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the given perfdata string to the user
|
|
|
|
|
*
|
|
|
|
|
* @param string $perfdataStr The perfdata string
|
2023-08-18 06:22:24 -04:00
|
|
|
* @param int $limit Max labels to show; 0 for no limit
|
|
|
|
|
* @param string $color The color indicating the perfdata state
|
2021-04-22 04:34:32 -04:00
|
|
|
*/
|
2021-09-22 04:21:15 -04:00
|
|
|
public function __construct(string $perfdataStr, int $limit = 0, string $color = PerfData::PERFDATA_OK)
|
2021-04-22 04:34:32 -04:00
|
|
|
{
|
|
|
|
|
$this->perfdataStr = $perfdataStr;
|
|
|
|
|
$this->limit = $limit;
|
|
|
|
|
$this->color = $color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function assemble()
|
|
|
|
|
{
|
|
|
|
|
$pieChartData = PerfDataSet::fromString($this->perfdataStr)->asArray();
|
2023-08-18 06:22:24 -04:00
|
|
|
$keys = [
|
|
|
|
|
'' => '',
|
|
|
|
|
'label' => t('Label'),
|
|
|
|
|
'value' => t('Value'),
|
|
|
|
|
'min' => t('Min'),
|
|
|
|
|
'max' => t('Max'),
|
|
|
|
|
'warn' => t('Warning'),
|
|
|
|
|
'crit' => t('Critical')
|
|
|
|
|
];
|
2023-08-18 05:55:44 -04:00
|
|
|
|
2023-08-18 06:08:47 -04:00
|
|
|
$containsSparkline = false;
|
2021-04-22 04:34:32 -04:00
|
|
|
foreach ($pieChartData as $perfdata) {
|
2023-08-23 08:54:15 -04:00
|
|
|
if ($perfdata->isVisualizable() || ! $perfdata->isValid()) {
|
2023-08-18 06:08:47 -04:00
|
|
|
$containsSparkline = true;
|
2023-08-18 06:13:34 -04:00
|
|
|
break;
|
2021-04-22 04:34:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$headerRow = new HtmlElement('tr');
|
|
|
|
|
foreach ($keys as $key => $col) {
|
2023-08-18 06:22:24 -04:00
|
|
|
if (! $containsSparkline && $key === '') {
|
2021-04-22 04:34:32 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-08-16 10:39:41 -04:00
|
|
|
|
|
|
|
|
$headerRow->addHtml(new HtmlElement('th', Attributes::create([
|
2023-08-18 06:22:24 -04:00
|
|
|
'class' => $key === 'label' ? 'title' : null
|
|
|
|
|
]), Text::create($col)));
|
2021-04-22 04:34:32 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-22 04:29:25 -04:00
|
|
|
$this->getHeader()->addHtml($headerRow);
|
2021-04-22 04:34:32 -04:00
|
|
|
|
2023-08-18 06:33:41 -04:00
|
|
|
$count = 0;
|
|
|
|
|
foreach ($pieChartData as $perfdata) {
|
|
|
|
|
if ($this->limit > 0 && $count === $this->limit) {
|
2021-04-22 04:34:32 -04:00
|
|
|
break;
|
2023-08-18 06:08:47 -04:00
|
|
|
}
|
2021-04-22 04:34:32 -04:00
|
|
|
|
2023-08-18 06:33:41 -04:00
|
|
|
$count++;
|
2023-08-18 06:08:47 -04:00
|
|
|
$cols = [];
|
|
|
|
|
if ($containsSparkline) {
|
|
|
|
|
if ($perfdata->isVisualizable()) {
|
2021-04-22 04:34:32 -04:00
|
|
|
$cols[] = Table::td(
|
2023-08-18 06:08:47 -04:00
|
|
|
HtmlString::create($perfdata->asInlinePie($this->color)->render()),
|
|
|
|
|
['class' => 'sparkline-col']
|
2021-04-22 04:34:32 -04:00
|
|
|
);
|
2023-08-23 08:54:15 -04:00
|
|
|
} elseif (! $perfdata->isValid()) {
|
|
|
|
|
$cols[] = Table::td(
|
|
|
|
|
new Icon(
|
|
|
|
|
'triangle-exclamation',
|
|
|
|
|
[
|
|
|
|
|
'title' => $this->translate(
|
|
|
|
|
'Evaluation failed. Performance data is invalid.'
|
|
|
|
|
),
|
|
|
|
|
'class' => ['invalid-perfdata']
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
['class' => 'sparkline-col']
|
|
|
|
|
);
|
2023-08-18 06:08:47 -04:00
|
|
|
} else {
|
|
|
|
|
$cols[] = Table::td('');
|
2021-04-22 04:34:32 -04:00
|
|
|
}
|
2023-08-18 06:08:47 -04:00
|
|
|
}
|
2021-04-22 04:34:32 -04:00
|
|
|
|
2023-08-18 06:08:47 -04:00
|
|
|
foreach ($perfdata->toArray() as $column => $value) {
|
|
|
|
|
$cols[] = Table::td(
|
|
|
|
|
new HtmlElement(
|
|
|
|
|
'span',
|
2023-08-18 06:22:24 -04:00
|
|
|
Attributes::create(['class' => $value ? null : 'no-value']),
|
2023-08-18 06:08:47 -04:00
|
|
|
$value ? Text::create($value) : new EmptyState(t('None', 'value'))
|
|
|
|
|
),
|
2023-08-18 06:22:24 -04:00
|
|
|
['class' => $column === 'label' ? 'title' : null]
|
2023-08-18 06:08:47 -04:00
|
|
|
);
|
2021-04-22 04:34:32 -04:00
|
|
|
}
|
2023-08-18 06:08:47 -04:00
|
|
|
|
2023-08-18 06:22:24 -04:00
|
|
|
$this->addHtml(Table::tr($cols));
|
2021-04-22 04:34:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|