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