icingaweb2-module-director/library/Director/Db/Branch/BranchModificationInspectio...

89 lines
3.0 KiB
PHP
Raw Normal View History

2021-08-16 11:43:09 +02:00
<?php
namespace Icinga\Module\Director\Db\Branch;
2021-12-01 23:50:04 +01:00
use gipfl\Translation\StaticTranslator;
2021-08-16 11:43:09 +02:00
use gipfl\Translation\TranslationHelper;
use Icinga\Module\Director\Db;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use Ramsey\Uuid\UuidInterface;
class BranchModificationInspection
{
use TranslationHelper;
protected $connection;
2021-08-16 11:43:09 +02:00
protected $db;
public function __construct(Db $connection)
{
$this->connection = $connection;
2021-08-16 11:43:09 +02:00
$this->db = $connection->getDbAdapter();
}
public function describe($table, UuidInterface $uuid)
{
return static::describeModificationStatistics($this->loadSingleTableStats($table, $uuid));
}
public function describeBranch(UuidInterface $uuid)
{
$tables = [
$this->translate('API Users') => 'branched_icinga_apiuser',
$this->translate('Endpoints') => 'branched_icinga_endpoint',
$this->translate('Zones') => 'branched_icinga_zone',
$this->translate('Commands') => 'branched_icinga_command',
$this->translate('Hosts') => 'branched_icinga_host',
$this->translate('Hostgroups') => 'branched_icinga_hostgroup',
$this->translate('Services') => 'branched_icinga_service',
$this->translate('Servicegroups') => 'branched_icinga_servicegroup',
$this->translate('Users') => 'branched_icinga_user',
$this->translate('Timeperiods') => 'branched_icinga_timeperiod',
];
$parts = new HtmlDocument();
$parts->setSeparator(Html::tag('br'));
foreach ($tables as $label => $table) {
$info = $this->describe($table, $uuid);
if (! empty($info) && $info !== '-') {
$parts->add("$label: $info");
}
}
return $parts;
}
public static function describeModificationStatistics($stats)
{
2021-12-01 23:50:04 +01:00
$t = StaticTranslator::get();
2021-08-16 11:43:09 +02:00
$relevantStats = [];
if ($stats->cnt_created > 0) {
$relevantStats[] = sprintf($t->translate('%d created'), $stats->cnt_created);
}
if ($stats->cnt_deleted > 0) {
$relevantStats[] = sprintf($t->translate('%d deleted'), $stats->cnt_deleted);
}
if ($stats->cnt_modified > 0) {
$relevantStats[] = sprintf($t->translate('%d modified'), $stats->cnt_modified);
}
if (empty($relevantStats)) {
return '-';
}
return implode(', ', $relevantStats);
}
public function loadSingleTableStats($table, UuidInterface $uuid)
{
$query = $this->db->select()->from($table, [
'cnt_created' => "SUM(CASE WHEN branch_created = 'y' THEN 1 ELSE 0 END)",
'cnt_deleted' => "SUM(CASE WHEN branch_deleted = 'y' THEN 1 ELSE 0 END)",
'cnt_modified' => "SUM(CASE WHEN branch_deleted = 'n' AND branch_created = 'n' THEN 1 ELSE 0 END)",
])->where('branch_uuid = ?', $this->connection->quoteBinary($uuid->getBytes()));
2021-08-16 11:43:09 +02:00
return $this->db->fetchRow($query);
}
}