From d61b7618d9c04ff90fdf8d3b584ad5976faedad9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:16:36 +0200 Subject: Adding upstream version 1.3.2. Signed-off-by: Daniel Baumann --- library/Cube/IcingaDb/CustomVariableDimension.php | 166 ++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 library/Cube/IcingaDb/CustomVariableDimension.php (limited to 'library/Cube/IcingaDb/CustomVariableDimension.php') diff --git a/library/Cube/IcingaDb/CustomVariableDimension.php b/library/Cube/IcingaDb/CustomVariableDimension.php new file mode 100644 index 0000000..34a395c --- /dev/null +++ b/library/Cube/IcingaDb/CustomVariableDimension.php @@ -0,0 +1,166 @@ +sourceName = $matches[1]; + $this->varName = $matches[2]; + } + + $this->name = $name; + } + + /** + * Get the variable name without prefix + * + * @return string + */ + public function getVarName(): string + { + return $this->varName ?? $this->getName(); + } + + /** + * Get the variable source name + * + * @return ?string + */ + public function getSourceName(): ?string + { + return $this->sourceName; + } + + public function getName() + { + return $this->name; + } + + public function getLabel() + { + return $this->label ?: $this->getName(); + } + + public function setLabel($label) + { + $this->label = $label; + + return $this; + } + + public function addLabel($label) + { + if ($this->label === null) { + $this->setLabel($label); + } else { + $this->label .= ' & ' . $label; + } + + return $this; + } + + /** + * Define whether null values should be shown + * + * @param bool $wantNull + * @return $this + */ + public function wantNull($wantNull = true) + { + $this->wantNull = $wantNull; + + return $this; + } + + /** + * @param IcingaDbCube $cube + * @return Expression|string + */ + public function getColumnExpression(Cube $cube) + { + $expression = $cube->getDb()->quoteIdentifier([$this->createCustomVarAlias(), 'flatvalue']); + + if ($this->wantNull) { + return new Expression("COALESCE($expression, '-')"); + } + + return $expression; + } + + public function addToCube(Cube $cube) + { + /** @var IcingaDbCube $cube */ + $innerQuery = $cube->innerQuery(); + $sourceTable = $this->getSourceName() ?? $innerQuery->getModel()->getTableName(); + + $subQuery = $innerQuery->createSubQuery(new CustomvarFlat(), $sourceTable . '.vars'); + $subQuery->getSelectBase()->resetWhere(); // The link to the outer query is the ON condition + $subQuery->columns(['flatvalue', 'object_id' => $sourceTable . '.id']); + $subQuery->filter(Filter::like('flatname', $this->getVarName())); + + // Values might not be unique (wildcard dimensions) + $subQueryModelAlias = $subQuery->getResolver()->getAlias($subQuery->getModel()); + $subQuery->getSelectBase()->groupBy([ + $subQueryModelAlias . '.flatname', // Required by postgres, if there are any custom variable protections + $subQueryModelAlias . '.flatvalue', + 'object_id' + ]); + + $this->applyRestrictions($subQuery); + + $subQueryAlias = $cube->getDb()->quoteIdentifier([$this->createCustomVarAlias()]); + $innerQuery->getSelectBase()->groupBy($subQueryAlias . '.flatvalue'); + + $sourceIdPath = '.id'; + if ($innerQuery->getModel() instanceof Service && $sourceTable === 'host') { + $sourceIdPath = '.host_id'; + } + + $innerQuery->getSelectBase()->join( + [$subQueryAlias => $subQuery->assembleSelect()], + [ + $subQueryAlias . '.object_id = ' + . $innerQuery->getResolver()->getAlias($innerQuery->getModel()) . $sourceIdPath + ] + ); + } + + protected function createCustomVarAlias(): string + { + return implode('_', ['c', $this->getSourceName(), $this->getVarName()]); + } +} -- cgit v1.2.3