diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:43:12 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:43:12 +0000 |
commit | cd989f9c3aff968e19a3aeabc4eb9085787a6673 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Web/Table/BasketSnapshotTable.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.tar.xz icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.zip |
Adding upstream version 1.10.2.upstream/1.10.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Web/Table/BasketSnapshotTable.php')
-rw-r--r-- | library/Director/Web/Table/BasketSnapshotTable.php | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/library/Director/Web/Table/BasketSnapshotTable.php b/library/Director/Web/Table/BasketSnapshotTable.php new file mode 100644 index 0000000..08f808a --- /dev/null +++ b/library/Director/Web/Table/BasketSnapshotTable.php @@ -0,0 +1,125 @@ +<?php + +namespace Icinga\Module\Director\Web\Table; + +use ipl\Html\Html; +use gipfl\IcingaWeb2\Link; +use gipfl\IcingaWeb2\Table\ZfQueryBasedTable; +use Icinga\Date\DateFormatter; +use Icinga\Module\Director\Core\Json; +use Icinga\Module\Director\DirectorObject\Automation\Basket; +use RuntimeException; + +class BasketSnapshotTable extends ZfQueryBasedTable +{ + use DbHelper; + + protected $searchColumns = [ + 'basket_name', + 'summary' + ]; + + /** @var Basket */ + protected $basket; + + public function setBasket(Basket $basket) + { + $this->basket = $basket; + $this->searchColumns = []; + + return $this; + } + + public function renderRow($row) + { + $this->splitByDay($row->ts_create_seconds); + $link = $this->linkToSnapshot($this->renderSummary($row->summary), $row); + + if ($this->basket === null) { + $columns = [ + [ + new Link( + Html::tag('strong', $row->basket_name), + 'director/basket', + ['name' => $row->basket_name] + ), + Html::tag('br'), + $link, + ], + DateFormatter::formatTime($row->ts_create / 1000), + ]; + } else { + $columns = [ + $link, + DateFormatter::formatTime($row->ts_create / 1000), + ]; + } + return $this::row($columns); + } + + protected function renderSummary($summary) + { + $summary = Json::decode($summary); + if ($summary === null) { + return '-'; + } + $result = []; + if (! is_object($summary) && ! is_array($summary)) { + throw new RuntimeException(sprintf( + 'Got invalid basket summary: %s ', + var_export($summary, 1) + )); + } + + foreach ($summary as $type => $count) { + $result[] = sprintf( + '%dx %s', + $count, + $type + ); + } + + if (empty($result)) { + return '-'; + } + + return implode(', ', $result); + } + + protected function linkToSnapshot($caption, $row) + { + return new Link($caption, 'director/basket/snapshot', [ + 'checksum' => bin2hex($this->wantBinaryValue($row->content_checksum)), + 'ts' => $row->ts_create, + 'name' => $row->basket_name, + ]); + } + + public function prepareQuery() + { + $query = $this->db()->select()->from([ + 'b' => 'director_basket' + ], [ + 'b.uuid', + 'b.basket_name', + 'bs.ts_create', + 'ts_create_seconds' => '(bs.ts_create / 1000)', + 'bs.content_checksum', + 'bc.summary', + ])->join( + ['bs' => 'director_basket_snapshot'], + 'bs.basket_uuid = b.uuid', + [] + )->join( + ['bc' => 'director_basket_content'], + 'bc.checksum = bs.content_checksum', + [] + )->order('bs.ts_create DESC'); + + if ($this->basket !== null) { + $query->where('b.uuid = ?', $this->quoteBinary($this->basket->get('uuid'))); + } + + return $query; + } +} |