From da0f8427204ad57aad08059906df0ea10a7ccf31 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:42:52 +0200 Subject: Adding upstream version 1.2.2. Signed-off-by: Daniel Baumann --- library/Cube/IcingaDb/CustomVariableDimension.php | 119 ++++++++++++++++++++++ 1 file changed, 119 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..2266395 --- /dev/null +++ b/library/Cube/IcingaDb/CustomVariableDimension.php @@ -0,0 +1,119 @@ +name = $name; + } + + public function getName() + { + return $this->name; + } + + public function getLabel() + { + return $this->label ?: $this->getName(); + } + + /** + * Set the label + * + * @param string $label + * @return $this + */ + public function setLabel($label) + { + $this->label = $label; + + return $this; + } + + /** + * Add a label + * + * @param string $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(['c_' . $this->getName(), 'flatvalue']); + + if ($this->wantNull) { + return new Expression("COALESCE($expression, '-')"); + } + + return $expression; + } + + public function addToCube(Cube $cube) + { + /** @var IcingaDbCube $cube */ + $name = $this->getName(); + $innerQuery = $cube->innerQuery(); + $sourceTable = $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', $name)); + + // Values might not be unique (wildcard dimensions) + $subQuery->getSelectBase()->groupBy([ + $subQuery->getResolver()->getAlias($subQuery->getModel()) . '.flatvalue', + 'object_id' + ]); + + $subQueryAlias = $cube->getDb()->quoteIdentifier(['c_' . $name]); + $innerQuery->getSelectBase()->groupBy($subQueryAlias . '.flatvalue'); + $innerQuery->getSelectBase()->join( + [$subQueryAlias => $subQuery->assembleSelect()], + [$subQueryAlias . '.object_id = ' . $innerQuery->getResolver()->getAlias($innerQuery->getModel()) . '.id'] + ); + } +} -- cgit v1.2.3