icingaweb2-module-director/library/Director/Web/Table/BranchActivityTable.php

117 lines
3.7 KiB
PHP
Raw Normal View History

2021-08-16 11:43:09 +02:00
<?php
namespace Icinga\Module\Director\Web\Table;
2021-12-06 23:03:06 +01:00
use gipfl\Format\LocalTimeFormat;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Branch\BranchActivity;
2021-08-16 11:43:09 +02:00
use Icinga\Module\Director\Util;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Ramsey\Uuid\UuidInterface;
class BranchActivityTable extends ZfQueryBasedTable
{
protected $extraParams = [];
/** @var UuidInterface */
protected $branchUuid;
/** @var ?UuidInterface */
protected $objectUuid;
2021-12-06 23:03:06 +01:00
/** @var LocalTimeFormat */
protected $timeFormat;
2022-07-01 08:36:01 +02:00
protected $linkToObject = true;
public function __construct(UuidInterface $branchUuid, $db, UuidInterface $objectUuid = null)
2021-08-16 11:43:09 +02:00
{
$this->branchUuid = $branchUuid;
$this->objectUuid = $objectUuid;
2021-12-06 23:03:06 +01:00
$this->timeFormat = new LocalTimeFormat();
2021-08-16 11:43:09 +02:00
parent::__construct($db);
}
public function assemble()
{
$this->getAttributes()->add('class', 'activity-log');
}
public function renderRow($row)
{
2022-07-01 08:36:01 +02:00
$ts = (int) floor(BranchActivity::fixFakeTimestamp($row->timestamp_ns) / 1000000);
2021-08-16 11:43:09 +02:00
$this->splitByDay($ts);
$activity = BranchActivity::fromDbRow($row);
2021-08-16 11:43:09 +02:00
return $this::tr([
$this::td($this->makeBranchLink($activity))->setSeparator(' '),
2021-12-06 23:03:06 +01:00
$this::td($this->timeFormat->getTime($ts))
])->addAttributes(['class' => ['action-' . $activity->getAction(), 'branched']]);
2021-08-16 11:43:09 +02:00
}
2022-07-01 08:36:01 +02:00
public function disableObjectLink()
{
$this->linkToObject = false;
return $this;
}
protected function linkObject(BranchActivity $activity)
2021-08-16 11:43:09 +02:00
{
2022-07-01 08:36:01 +02:00
if (! $this->linkToObject) {
return $activity->getObjectName();
}
// $type, UuidInterface $uuid
2021-08-16 11:43:09 +02:00
// Later on replacing, service_set -> serviceset
$type = preg_replace('/^icinga_/', '', $activity->getObjectTable());
2021-08-16 11:43:09 +02:00
return Link::create(
$activity->getObjectName(),
2021-08-16 11:43:09 +02:00
'director/' . str_replace('_', '', $type),
['uuid' => $activity->getObjectUuid()->toString()],
2021-08-16 11:43:09 +02:00
['title' => $this->translate('Jump to this object')]
);
}
protected function makeBranchLink(BranchActivity $activity)
2021-08-16 11:43:09 +02:00
{
$type = preg_replace('/^icinga_/', '', $activity->getObjectTable());
2021-08-16 11:43:09 +02:00
if (Util::hasPermission('director/showconfig')) {
// Later on replacing, service_set -> serviceset
return [
'[' . $activity->getAuthor() . ']',
2021-08-16 11:43:09 +02:00
Link::create(
$activity->getAction(),
2021-08-16 11:43:09 +02:00
'director/branch/activity',
array_merge(['ts' => $activity->getTimestampNs()], $this->extraParams),
2021-08-16 11:43:09 +02:00
['title' => $this->translate('Show details related to this change')]
),
str_replace('_', ' ', $type),
$this->linkObject($activity)
2021-08-16 11:43:09 +02:00
];
} else {
return sprintf(
'[%s] %s %s "%s"',
$activity->getAuthor(),
$activity->getAction(),
2021-08-16 11:43:09 +02:00
$type,
2022-07-22 14:32:39 +02:00
$activity->getObjectName()
2021-08-16 11:43:09 +02:00
);
}
}
public function prepareQuery()
{
/** @var Db $connection */
$connection = $this->connection();
$query = $this->db()->select()->from(['ba' => 'director_branch_activity'], 'ba.*')
->join(['b' => 'director_branch'], 'b.uuid = ba.branch_uuid', ['b.owner'])
->where('branch_uuid = ?', $connection->quoteBinary($this->branchUuid->getBytes()))
->order('timestamp_ns DESC');
if ($this->objectUuid) {
$query->where('ba.object_uuid = ?', $connection->quoteBinary($this->objectUuid->getBytes()));
}
return $query;
2021-08-16 11:43:09 +02:00
}
}