Fetch homes and panes already ordered from the database

Doing so in userspace is not required and too inefficient.
This commit is contained in:
Johannes Meyer 2022-04-29 15:35:08 +02:00 committed by Yonas Habteab
parent 9a1f8816ae
commit 40716e7dbd
3 changed files with 6 additions and 21 deletions

View File

@ -49,7 +49,7 @@ class Home extends Model
public function getDefaultSort() public function getDefaultSort()
{ {
return 'name'; return 'priority';
} }
public function createRelations(Relations $relations) public function createRelations(Relations $relations)

View File

@ -48,7 +48,7 @@ class Pane extends Model
public function getDefaultSort() public function getDefaultSort()
{ {
return 'icingaweb_dashboard.name'; return 'priority';
} }
public function createRelations(Relations $relations) public function createRelations(Relations $relations)

View File

@ -18,13 +18,6 @@ trait DashboardEntries
*/ */
private $dashboards = []; private $dashboards = [];
/**
* Whether to sort the entries when retrieving using getEntries()
*
* @var bool
*/
private $sortEntries = false;
public function hasEntries() public function hasEntries()
{ {
return ! empty($this->dashboards); return ! empty($this->dashboards);
@ -46,23 +39,12 @@ trait DashboardEntries
public function getEntries() public function getEntries()
{ {
if ($this->sortEntries) {
$this->sortEntries = false;
// An entry can be added individually afterwards, it might be the case that the priority
// order gets mixed up, so we have to sort things here before being able to render them
uasort($this->dashboards, function (BaseDashboard $x, BaseDashboard $y) {
return $x->getPriority() <=> $y->getPriority();
});
}
return $this->dashboards; return $this->dashboards;
} }
public function setEntries(array $entries) public function setEntries(array $entries)
{ {
$this->dashboards = $entries; $this->dashboards = $entries;
$this->sortEntries = true;
return $this; return $this;
} }
@ -73,7 +55,6 @@ trait DashboardEntries
$this->getEntry($dashboard->getName())->fromArray($dashboard->toArray(false)); $this->getEntry($dashboard->getName())->fromArray($dashboard->toArray(false));
} else { } else {
$this->dashboards[$dashboard->getName()] = $dashboard; $this->dashboards[$dashboard->getName()] = $dashboard;
$this->sortEntries = true;
} }
return $this; return $this;
@ -134,14 +115,18 @@ trait DashboardEntries
array_splice($data, $position, 0, [$dashboard]); array_splice($data, $position, 0, [$dashboard]);
} }
$entries = [];
foreach ($data as $index => $item) { foreach ($data as $index => $item) {
if (count($data) !== 1) { if (count($data) !== 1) {
$item->setPriority($index); $item->setPriority($index);
} }
$entries[$item->getName()] = $item;
$this->manageEntry($item, $dashboard->getName() === $item->getName() ? $origin : null); $this->manageEntry($item, $dashboard->getName() === $item->getName() ? $origin : null);
} }
$this->setEntries($entries);
return $this; return $this;
} }
} }