BranchActivityTable: new columns, better query

This commit is contained in:
Thomas Gelf 2021-10-05 22:32:01 +02:00
parent 98a8050718
commit 70c0b8f247
1 changed files with 36 additions and 58 deletions

View File

@ -2,12 +2,11 @@
namespace Icinga\Module\Director\Web\Table;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db\Branch\ObjectModification;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Branch\BranchActivity;
use Icinga\Module\Director\Util;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
class BranchActivityTable extends ZfQueryBasedTable
@ -17,9 +16,13 @@ class BranchActivityTable extends ZfQueryBasedTable
/** @var UuidInterface */
protected $branchUuid;
public function __construct(UuidInterface $branchUuid, $db)
/** @var ?UuidInterface */
protected $objectUuid;
public function __construct(UuidInterface $branchUuid, $db, UuidInterface $objectUuid = null)
{
$this->branchUuid = $branchUuid;
$this->objectUuid = $objectUuid;
parent::__construct($db);
}
@ -30,93 +33,68 @@ class BranchActivityTable extends ZfQueryBasedTable
public function renderRow($row)
{
return $this->renderBranchRow($row);
}
public function renderBranchRow($row)
{
$ts = $row->change_time / 1000;
$ts = (int) floor($row->timestamp_ns / 1000000);
$this->splitByDay($ts);
$changes = ObjectModification::fromSerialization(json_decode($row->change_set));
$action = 'action-' . $changes->getAction(). ' branched'; // not gray
$activity = BranchActivity::fromDbRow($row);
return $this::tr([
$this::td($this->makeBranchLink(
$changes,
Uuid::fromBytes($row->uuid),
Uuid::fromBytes($row->branch_uuid)
))->setSeparator(' '),
$this::td($this->makeBranchLink($activity))->setSeparator(' '),
$this::td(strftime('%H:%M:%S', $ts))
])->addAttributes(['class' => $action]);
])->addAttributes(['class' => ['action-' . $activity->getAction(), 'branched']]);
}
protected function linkObject($type, $name)
protected function linkObject(BranchActivity $activity)
{
// $type, UuidInterface $uuid
// Later on replacing, service_set -> serviceset
// multi column key :(
if ($type === 'service') {
return "\"$name\"";
}
$type = preg_replace('/^icinga_/', '', $activity->getObjectTable());
return Link::create(
"\"$name\"",
$activity->getObjectName(),
'director/' . str_replace('_', '', $type),
['name' => $name],
['uuid' => $activity->getObjectUuid()->toString()],
['title' => $this->translate('Jump to this object')]
);
}
protected function makeBranchLink(ObjectModification $modification, UuidInterface $uuid, UuidInterface $branch)
protected function makeBranchLink(BranchActivity $activity)
{
/** @var string|DbObject $class */
$class = $modification->getClassName();
$type = $class::create([])->getShortTableName();
// TODO: short type in table, not class name
$keyParams = $modification->getKeyParams();
if (is_object($keyParams)) {
$keyParams = (array)$keyParams;
}
if (is_array($keyParams)) {
if (array_keys($keyParams) === ['object_name']) {
$name = $keyParams['object_name'];
} else {
$name = json_encode($keyParams);
}
} else {
$name = $keyParams;
}
$author = 'branch owner';
$type = preg_replace('/^icinga_/', '', $activity->getObjectTable());
if (Util::hasPermission('director/showconfig')) {
// Later on replacing, service_set -> serviceset
$id = 0; // $row->id
return [
'[' . $author . ']',
'[' . $activity->getAuthor() . ']',
Link::create(
$modification->getAction(),
$activity->getAction(),
'director/branch/activity',
array_merge(['uuid' => $uuid->toString()], $this->extraParams),
array_merge(['ts' => $activity->getTimestampNs()], $this->extraParams),
['title' => $this->translate('Show details related to this change')]
),
str_replace('_', ' ', $type),
$this->linkObject($type, $name)
$this->linkObject($activity)
];
} else {
return sprintf(
'[%s] %s %s "%s"',
$author,
$modification->getAction(),
$activity->getAuthor(),
$activity->getAction(),
$type,
$name
'object name'
);
}
}
public function prepareQuery()
{
return $this->db()->select()->from('director_branch_activity')
->where('branch_uuid = ?', $this->branchUuid->getBytes())
->order('change_time DESC');
/** @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;
}
}