From d795ad3c8e6c2862f02bef60b2aeb6d610861d3b Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 10 Jan 2020 12:31:40 +0100 Subject: [PATCH] BenchmarkCommand: add more flattening tests --- application/clicommands/BenchmarkCommand.php | 96 +++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/application/clicommands/BenchmarkCommand.php b/application/clicommands/BenchmarkCommand.php index 8c5cd0df..6ccd8c84 100644 --- a/application/clicommands/BenchmarkCommand.php +++ b/application/clicommands/BenchmarkCommand.php @@ -7,12 +7,104 @@ use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\FilterChain; use Icinga\Data\Filter\FilterExpression; use Icinga\Module\Director\Cli\Command; +use Icinga\Module\Director\CustomVariable\CustomVariable; +use Icinga\Module\Director\Data\Db\IcingaObjectFilterRenderer; +use Icinga\Module\Director\Data\Db\IcingaObjectQuery; use Icinga\Module\Director\Objects\HostGroupMembershipResolver; use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHostVar; +use Icinga\Module\Director\Objects\IcingaVar; class BenchmarkCommand extends Command { + public function testflatfilterAction() + { + $q = new IcingaObjectQuery('host', $this->db()); + $filter = Filter::fromQueryString( + // 'host.vars.snmp_community="*ub*"&(host.vars.location="London"|host.vars.location="Berlin")' + // 'host.vars.snmp_community="*ub*"&(host.vars.location="FRA DC"|host.vars.location="NBG DC")' + 'host.vars.priority="*igh"&(host.vars.location="FRA DC"|host.vars.location="NBG DC")' + ); + IcingaObjectFilterRenderer::apply($filter, $q); + echo $q->getSql() . "\n"; + + print_r($q->listNames()); + } + + public function rerendervarsAction() + { + $conn = $this->db(); + $db = $conn->getDbAdapter(); + $db->beginTransaction(); + $query = $db->select()->from( + array('v' => 'icinga_var'), + array( + 'v.varname', + 'v.varvalue', + 'v.checksum', + 'v.rendered_checksum', + 'v.rendered', + 'format' => "('json')", + ) + ); + Benchmark::measure('Ready to fetch all vars'); + $rows = $db->fetchAll($query); + Benchmark::measure('Got vars, storing flat'); + foreach ($rows as $row) { + $var = CustomVariable::fromDbRow($row); + $rendered = $var->render(); + $checksum = sha1($rendered, true); + if ($checksum === $row->rendered_checksum) { + continue; + } + + $where = $db->quoteInto('checksum = ?', $row->checksum); + $db->update( + 'icinga_var', + array( + 'rendered' => $rendered, + 'rendered_checksum' => $checksum + ), + $where + ); + } + + $db->commit(); + } + + public function flattenvarsAction() + { + $conn = $this->db(); + $db = $conn->getDbAdapter(); + $db->beginTransaction(); + $query = $db->select()->from(['v' => 'icinga_host_var'], [ + 'v.host_id', + 'v.varname', + 'v.varvalue', + 'v.format', + 'v.checksum' + ]); + Benchmark::measure('Ready to fetch all vars'); + $rows = $db->fetchAll($query); + Benchmark::measure('Got vars, storing flat'); + + foreach ($rows as $row) { + $var = CustomVariable::fromDbRow($row); + $checksum = $var->checksum(); + if (! IcingaVar::exists($checksum, $conn)) { + IcingaVar::generateForCustomVar($var, $conn); + } + + if ($row->checksum === null) { + $where = $db->quoteInto('host_id = ?', $row->host_id) + . $db->quoteInto(' AND varname = ?', $row->varname); + $db->update('icinga_host_var', ['checksum' => $checksum], $where); + } + } + + $db->commit(); + } + public function resolvehostgroupsAction() { $resolver = new HostGroupMembershipResolver($this->db()); @@ -21,7 +113,7 @@ class BenchmarkCommand extends Command public function filterAction() { - $flat = array(); + $flat = []; /** @var FilterChain|FilterExpression $filter */ $filter = Filter::fromQueryString( @@ -33,7 +125,7 @@ class BenchmarkCommand extends Command Benchmark::measure('db done'); foreach ($objs as $host) { - $flat[$host->get('id')] = (object) array(); + $flat[$host->get('id')] = (object) []; foreach ($host->getProperties() as $k => $v) { $flat[$host->get('id')]->$k = $v; }