mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-09-25 10:57:40 +02:00
Cleanup DashboardManager trait
This commit is contained in:
parent
3d5789d584
commit
d540ad4c0e
@ -4,30 +4,19 @@
|
||||
|
||||
namespace Icinga\Web\Dashboard\Common;
|
||||
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Authentication\Auth;
|
||||
use Icinga\Common\Database;
|
||||
use Icinga\Application\Modules;
|
||||
use Icinga\Exception\Http\HttpNotFoundException;
|
||||
use Icinga\Exception\ProgrammingError;
|
||||
use Icinga\Model;
|
||||
use Icinga\User;
|
||||
use Icinga\Web\Dashboard\Dashboard;
|
||||
use Icinga\Web\Dashboard\DashboardHome;
|
||||
use Icinga\Web\Dashboard\Dashlet;
|
||||
use Icinga\Web\Dashboard\Pane;
|
||||
use ipl\Sql\Connection;
|
||||
use ipl\Sql\Expression;
|
||||
use Icinga\Web\Dashboard\Util\DBUtils;
|
||||
use ipl\Stdlib\Filter;
|
||||
|
||||
trait DashboardManager
|
||||
{
|
||||
use Database;
|
||||
|
||||
/** @var User */
|
||||
private static $user;
|
||||
|
||||
/** @var Connection */
|
||||
private static $conn;
|
||||
use DashboardUserManager;
|
||||
|
||||
/**
|
||||
* A list of default panes loaded from monitoring|icingadb module
|
||||
@ -45,8 +34,8 @@ trait DashboardManager
|
||||
*/
|
||||
public function load(string $name = null)
|
||||
{
|
||||
$query = Model\Home::on(self::getConn());
|
||||
$query->filter(Filter::equal('username', $this::getUser()->getUsername()));
|
||||
$query = Model\Home::on(DBUtils::getConn());
|
||||
$query->filter(Filter::equal('icingaweb_dashboard_owner.id', $this::getUser()->getAdditional('id')));
|
||||
|
||||
if ($name !== null) {
|
||||
$query->filter(Filter::equal('name', $name));
|
||||
@ -76,25 +65,7 @@ trait DashboardManager
|
||||
}
|
||||
}
|
||||
|
||||
self::deployModuleDashlets();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Database connection
|
||||
*
|
||||
* This is needed because we don't want to always initiate a new DB connection when calling $this->getDb().
|
||||
* And as we are using PDO transactions to manage the dashboards, this wouldn't work if $this->getDb()
|
||||
* is called over again after a transaction has been initiated
|
||||
*
|
||||
* @return Connection
|
||||
*/
|
||||
public static function getConn(): Connection
|
||||
{
|
||||
if (self::$conn === null) {
|
||||
self::$conn = (new self())->getDb();
|
||||
}
|
||||
|
||||
return self::$conn;
|
||||
Modules\DashletManager::deployDashlets();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,10 +133,11 @@ trait DashboardManager
|
||||
|
||||
$home = $home instanceof DashboardHome ? $home : $this->getEntry($home);
|
||||
$home->removeEntries();
|
||||
|
||||
if ($home->getName() !== DashboardHome::DEFAULT_HOME) {
|
||||
self::getConn()->delete(DashboardHome::TABLE, ['id = ?' => $home->getUuid()]);
|
||||
DBUtils::getConn()->delete(DashboardHome::TABLE, ['id = ?' => $home->getUuid()]);
|
||||
} elseif (! $home->isDisabled()) {
|
||||
self::getConn()->update(DashboardHome::TABLE, ['disabled' => 1], [
|
||||
DBUtils::getConn()->update(DashboardHome::TABLE, ['disabled' => 1], [
|
||||
'id = ?' => $home->getUuid()
|
||||
]);
|
||||
}
|
||||
@ -175,7 +147,7 @@ trait DashboardManager
|
||||
|
||||
public function manageEntry($entry, BaseDashboard $origin = null, $manageRecursive = false)
|
||||
{
|
||||
$conn = self::getConn();
|
||||
$conn = DBUtils::getConn();
|
||||
$homes = is_array($entry) ? $entry : [$entry];
|
||||
|
||||
/** @var DashboardHome $home */
|
||||
@ -184,9 +156,9 @@ trait DashboardManager
|
||||
// Highest priority is 0, so count($entries) are always lowest prio + 1
|
||||
$priority = $home->getName() === DashboardHome::DEFAULT_HOME ? 0 : count($this->getEntries());
|
||||
$conn->insert(DashboardHome::TABLE, [
|
||||
'user_id' => self::getUser()->getAdditional('id'),
|
||||
'name' => $home->getName(),
|
||||
'label' => $home->getTitle(),
|
||||
'username' => self::getUser()->getUsername(),
|
||||
'priority' => $priority,
|
||||
'type' => $home->getType() !== Dashboard::SYSTEM ? $home->getType() : Dashboard::PRIVATE_DS
|
||||
]);
|
||||
@ -196,7 +168,7 @@ trait DashboardManager
|
||||
$conn->update(DashboardHome::TABLE, [
|
||||
'label' => $home->getTitle(),
|
||||
'priority' => $home->getPriority(),
|
||||
'disabled' => 0
|
||||
'disabled' => 'n'
|
||||
], ['id = ?' => $home->getUuid()]);
|
||||
}
|
||||
}
|
||||
@ -221,181 +193,4 @@ trait DashboardManager
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set this dashboard's user
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setUser(User $user): self
|
||||
{
|
||||
self::$user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this dashboard's user
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public static function getUser(): User
|
||||
{
|
||||
if (self::$user === null) {
|
||||
self::$user = Auth::getInstance()->getUser();
|
||||
}
|
||||
|
||||
return self::$user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get system defaults which are normally being
|
||||
* provided by icingadb or monitoring module
|
||||
*
|
||||
* @return Pane[]
|
||||
*/
|
||||
public static function getSystemDefaults(): array
|
||||
{
|
||||
return self::$defaultPanes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Browse all enabled modules configuration file and import all dashboards
|
||||
* provided by them into the DB table `icingaweb_module_dashlet`
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function deployModuleDashlets(): void
|
||||
{
|
||||
$mg = Icinga::app()->getModuleManager();
|
||||
foreach ($mg->getLoadedModules() as $module) {
|
||||
foreach ($module->getDashboard() as $dashboard) {
|
||||
$pane = new Pane($dashboard->getName());
|
||||
$pane->setProperties($dashboard->getProperties());
|
||||
|
||||
$priority = 0;
|
||||
foreach ($dashboard->getDashlets() as $name => $configPart) {
|
||||
$uuid = self::getSHA1($module->getName() . $pane->getName() . $name);
|
||||
$dashlet = new Dashlet($name, $configPart['url'], $pane);
|
||||
$dashlet->setProperties($configPart);
|
||||
$dashlet
|
||||
->setUuid($uuid)
|
||||
->setModuleDashlet(true)
|
||||
->setPriority($priority++)
|
||||
->setModule($module->getName());
|
||||
|
||||
// As we don't have a setter for labels, this might be ignored by the data extractor
|
||||
if (isset($configPart['label'])) {
|
||||
$dashlet->setTitle($configPart['label']);
|
||||
}
|
||||
|
||||
self::updateOrInsertModuleDashlet($dashlet);
|
||||
$pane->addEntry($dashlet);
|
||||
}
|
||||
|
||||
if (in_array($module->getName(), ['monitoring', 'icingadb'], true)) {
|
||||
self::$defaultPanes[$pane->getName()] = $pane;
|
||||
}
|
||||
}
|
||||
|
||||
$priority = 0;
|
||||
foreach ($module->getDashlets() as $dashlet) {
|
||||
$identifier = self::getSHA1($module->getName() . $dashlet->getName());
|
||||
$newDashlet = new Dashlet($dashlet->getName(), $dashlet->getUrl());
|
||||
$newDashlet->setProperties($dashlet->getProperties());
|
||||
$newDashlet
|
||||
->setUuid($identifier)
|
||||
->setModule($module->getName())
|
||||
->setPriority($priority++)
|
||||
->setModuleDashlet(true);
|
||||
|
||||
self::updateOrInsertModuleDashlet($newDashlet);
|
||||
$priority++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether the given module Dashlet already exists
|
||||
*
|
||||
* @param Dashlet $dashlet
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function moduleDashletExist(Dashlet $dashlet): bool
|
||||
{
|
||||
$query = Model\ModuleDashlet::on(self::getConn())->filter(Filter::equal('id', $dashlet->getUuid()));
|
||||
$query->getSelectBase()->columns(new Expression('1'));
|
||||
|
||||
return $query->execute()->hasResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update the given module dashlet
|
||||
*
|
||||
* @param Dashlet $dashlet
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function updateOrInsertModuleDashlet(Dashlet $dashlet): void
|
||||
{
|
||||
if (! $dashlet->isModuleDashlet()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! self::moduleDashletExist($dashlet)) {
|
||||
self::getConn()->insert('icingaweb_module_dashlet', [
|
||||
'id' => $dashlet->getUuid(),
|
||||
'name' => $dashlet->getName(),
|
||||
'label' => $dashlet->getTitle(),
|
||||
'pane' => $dashlet->getPane() ? $dashlet->getPane()->getName() : null,
|
||||
'module' => $dashlet->getModule(),
|
||||
'url' => $dashlet->getUrl()->getRelativeUrl(),
|
||||
'description' => $dashlet->getDescription(),
|
||||
'priority' => $dashlet->getPriority()
|
||||
]);
|
||||
} else {
|
||||
self::getConn()->update('icingaweb_module_dashlet', [
|
||||
'label' => $dashlet->getTitle(),
|
||||
'url' => $dashlet->getUrl()->getRelativeUrl(),
|
||||
'description' => $dashlet->getDescription(),
|
||||
'priority' => $dashlet->getPriority()
|
||||
], ['id = ?' => $dashlet->getUuid()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module dashlets from the database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getModuleDashlets(): array
|
||||
{
|
||||
$dashlets = [];
|
||||
$query = Model\ModuleDashlet::on(self::getConn());
|
||||
foreach ($query as $moduleDashlet) {
|
||||
$dashlet = new Dashlet($moduleDashlet->name, $moduleDashlet->url);
|
||||
if ($moduleDashlet->description) {
|
||||
$dashlet->setDescription(t($moduleDashlet->description));
|
||||
}
|
||||
|
||||
$dashlet->setProperties([
|
||||
'label' => t($moduleDashlet->label),
|
||||
'priority' => $moduleDashlet->priority,
|
||||
'uuid' => $moduleDashlet->id,
|
||||
'module' => $moduleDashlet->module
|
||||
]);
|
||||
|
||||
if (($pane = $moduleDashlet->pane)) {
|
||||
$dashlet->setPane(new Pane($pane));
|
||||
}
|
||||
|
||||
$dashlet->setModuleDashlet(true);
|
||||
$dashlets[$dashlet->getModule()][$dashlet->getName()] = $dashlet;
|
||||
}
|
||||
|
||||
return $dashlets;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user