From 6bf0ca216ad73edd823c5b9cc2471d496920d9fc Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 16 Nov 2014 16:35:02 +0100 Subject: [PATCH] Livestatus\Query: resultrow method - base for more This is where query-based column fixup voodoo starts --- library/Icinga/Protocol/Livestatus/Query.php | 80 ++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/library/Icinga/Protocol/Livestatus/Query.php b/library/Icinga/Protocol/Livestatus/Query.php index b3c3e89ad..113377abf 100644 --- a/library/Icinga/Protocol/Livestatus/Query.php +++ b/library/Icinga/Protocol/Livestatus/Query.php @@ -58,6 +58,11 @@ class Query extends SimpleQuery return $this->columns; } + public function withHeaders(& $row) + { + return array_combine($this->preparedHeaders, $row->toArray()); + } + /** * Whether the named columns value is generated by a filter expression */ @@ -66,6 +71,81 @@ class Query extends SimpleQuery return array_key_exists($column, $this->filter_flags); } + // completes a given row + public function resultRow(& $row) + { + // $row -> raw SplArray + // $res -> object + // $cv -> + // $result -> object to be returned + $result = (object) array(); + $res = $this->withHeaders($row); + $cv = array(); + if (array_key_exists('custom_variables', $res)) { + foreach ($this->parseArray($res['custom_variables']) as $cvp) { + $cv[$cvp[0]] = $cvp[1]; + } + } + + $combined = array(); + + foreach ($this->columns as $alias => $col) { + if (is_int($alias)) { + $alias = $col; + } + if ($col[0] === '_') { + $result->$alias = array_key_exists($alias, $cv) ? $cv[$alias] : null; + } else { + $func = 'mungeResult_' . $col; + if (method_exists($this, $func)) { + $this->$func($res[$this->available_columns[$col]], $result); + } elseif (is_array($this->available_columns[$col])) { + $combined[$alias] = $col; + $result->$alias = null; + } else { + if (strpos($this->available_columns[$col], ' ') === false) { + $result->$alias = $res[$this->available_columns[$col]]; + } else { + $result->$alias = $res[$alias]; + } + } + } + } + // TODO: Quite some redundancy here :( + if (! $this->filterIsSupported()) { + foreach ($this->filter->listFilteredColumns() as $col) { + if ($this->isFilterFlag($col)) { + $result->$col = (string) (int) $this->filterStringToFilter( + $this->filter_flags[$col] + )->matches((object) $res); + } else { + $func = 'combineResult_' . $col; + if (method_exists($this, $func)) { + $result->$col = $this->$func($result, $res); + } + } + } + } + + foreach ($combined as $alias => $col) { + if ($this->isFilterFlag($col)) { + $result->$alias = (string) (int) $this->filterStringToFilter( + $this->filter_flags[$col] + )->matches((object) $res); + continue; + } + $func = 'combineResult_' . $col; + if (method_exists($this, $func)) { + $result->$alias = $this->$func($result, $res); + } else { + $result->$alias = implode(' - ', $this->available_columns[$col]); + } + } + + + return $result; + } + /** * Parse the given encoded array *