[icinga-checkins] icinga.org: icingaweb2/feature/livestatus-playground-4787: Livestatus\Query: resultrow method - base for more

git at icinga.org git at icinga.org
Sun Nov 16 17:17:20 CET 2014


Module: icingaweb2
Branch: feature/livestatus-playground-4787
Commit: 6bf0ca216ad73edd823c5b9cc2471d496920d9fc
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=6bf0ca216ad73edd823c5b9cc2471d496920d9fc

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sun Nov 16 16:35:02 2014 +0100

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 b3c3e89..113377a 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
      *



More information about the icinga-checkins mailing list