Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dns sorting 3507 #4994

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
97 changes: 96 additions & 1 deletion modules/monitoring/application/controllers/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,20 @@ public function servicegridAction()
'decorators' => ['ViewHelper', ['Label', ['placement' => 'APPEND']]]
]);


$sortByDns = (bool) $this->params->get('sortbydnshierarchy', false) ? true : null;
$this->view->sortByDnsToggle = $sortByDnsToggle = new Form(['method' => 'GET']);
$sortByDnsToggle->setUidDisabled();
$sortByDnsToggle->setTokenDisabled();
$sortByDnsToggle->setAttrib('class', 'filter-toggle inline icinga-controls');
$sortByDnsToggle->addElement('checkbox', 'sortbydnshierarchy', [
'disableHidden' => true,
'autosubmit' => true,
'value' => $sortByDns !== null,
'label' => $this->translate('Sort by DNS hierarchy'),
'decorators' => ['ViewHelper', ['Label', ['placement' => 'APPEND']]]
]);

if ($this->params->get('flipped', false)) {
$pivot = $query
->pivot(
Expand Down Expand Up @@ -712,13 +726,93 @@ public function servicegridAction()
$this->view->horizontalPaginator = $pivot->paginateXAxis();
$this->view->verticalPaginator = $pivot->paginateYAxis();
list($pivotData, $pivotHeader) = $pivot->toArray();

$sortByDns = (bool) $this->params->get('sortbydnshierarchy', false) ? !true : null;
if ($sortByDns !== null) {
$keySort = ($this->params->get('flipped', false) ? 'cols' : 'rows');

uksort($pivotData, [ $this, "compareByDnsHierarchy" ]);
uksort($pivotHeader[$keySort], [ $this, "compareByDnsHierarchy" ]);

$pivotHeader[$keySort] = $this->addGroupHeader($pivotHeader[$keySort]);

}

$this->view->pivotData = $pivotData;
$this->view->pivotHeader = $pivotHeader;
if ($this->params->get('flipped', false)) {
$this->render('servicegrid-flipped');
}
}

/**
* Add group headers to the header array
* The group name will be the FQDN without the the part before the first dot.
*/

private function addGroupHeader($header) {
$newHeader = [];
$previousGroup = null;
foreach ($header as $key => $val) {
$hostnameElements = explode('.', $key);
if (count($hostnameElements) > 2) {
if (filter_var($key, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
array_pop($hostnameElements);
}
else {
array_shift($hostnameElements);
}
$group = join('.', $hostnameElements);
if (is_null($previousGroup) || $previousGroup != $group) {
$newHeader['GROUP:' . $group] = $group;
$previousGroup = $group;
}
}
$newHeader[$key] = $val;
}
return $newHeader;
}

/**
* Split a FQDN by dots and compare each part beginning from top level
*/

private function compareByDnsHierarchy($a, $b) {
$keysA = explode('.', $a);
if (!filter_var($a, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$keysA = array_reverse($keysA);
}

$keysB = explode('.', $b);
if (!filter_var($b, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$keysB = array_reverse($keysB);
}

$cnt = min([$cntA = count($keysA), $cntB = count($keysB)]);

$firstDiff = -1;
$cmp = [ -1 => 0 ];

for ($i = 0; $i <= $cnt; $i++) {
$ka = isset($keysA[$i]) ? $keysA[$i] : "";
$kb = isset($keysB[$i]) ? $keysB[$i] : "";

if ($cmp[$i] = $ka <=> $kb) {
if ($firstDiff === -1) {
$firstDiff = $i;
}
}
}

if (count($keysA) != count($keysB)) {
return count($keysA) <=> count($keysB);
}
else {
return ($cmp[$firstDiff]);
}
}


/**
* Apply filters on a DataView
*
Expand All @@ -733,7 +827,8 @@ protected function filterQuery(DataView $dataView)
'stateType', // hostsAction() and servicesAction()
'addColumns', // addColumns()
'problems', // servicegridAction()
'flipped' // servicegridAction()
'flipped', // servicegridAction()
'sortbydnshierarchy' // servicegridAction()
));

if ($this->params->get('format') !== 'sql' || $this->hasPermission('config/authentication/roles/show')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (! $this->compact): ?>
<div class="controls">
<?= $this->tabs ?>
<?= $this->problemToggle ?>
<?= $this->sortByDnsToggle ?>
<div class="sort-controls-container">
<?= $this->sortBox ?>
</div>
Expand Down Expand Up @@ -36,6 +37,9 @@ foreach ($pivotData as $serviceDescription => $_) {
)
) ?></th>
<?php foreach ($pivotHeader['cols'] as $hostName => $hostAlias): ?>
<?php if (strpos($hostName, "GROUP:") === 0): ?>
<th class="groupheader rotate-45"><div><span><?= $hostAlias ?></span></div></th>
<?php continue; endif; ?>
<th class="rotate-45"><div><span><?= $this->qlink(
$this->ellipsis($hostAlias, 24),
Url::fromPath('monitoring/list/services')->addFilter(
Expand Down Expand Up @@ -71,6 +75,9 @@ foreach ($pivotData as $serviceDescription => $_) {
);
?></th>
<?php foreach (array_keys($pivotHeader['cols']) as $hostName): ?>
<?php if (strpos($hostName, "GROUP:") === 0): ?>
<td class="groupheader" style="border-left: 1px solid darkgray;"></td>
<?php continue; endif; ?>
<td><?php
$service = $pivotData[$serviceDescription][$hostName];
if ($service === null): ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (! $this->compact): ?>
<div class="controls">
<?= $this->tabs ?>
<?= $this->problemToggle ?>
<?= $this->sortByDnsToggle ?>
<div class="sort-controls-container">
<?= $this->sortBox ?>
</div>
Expand Down Expand Up @@ -55,6 +56,9 @@ foreach ($pivotData as $hostName => $_) {

<?php $i = 0 ?>
<?php foreach ($pivotHeader['rows'] as $hostName => $hostDisplayName): ?>
<?php if (strpos($hostName, "GROUP:") === 0): ?>
<tr class='groupheader'><th class='groupheader'><?= $hostDisplayName; ?></th><td colspan="<?= count($pivotHeader['cols']); ?>"></td></tr>
<?php continue; endif; ?>
<tr>
<th><?php
$serviceFilter = Filter::matchAny();
Expand Down
12 changes: 12 additions & 0 deletions modules/monitoring/public/css/service-grid.less
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
}
}

td.groupheader {
border-left: 1px solid darkgray;
}

tr.groupheader {
border-top: 1px solid darkgray;
}

.service-grid-table .groupheader {
color: lightgray;
}

.joystick-pagination {
margin: 0 auto;
font-size: 130%;
Expand Down