From 57f2ebd3d4b42ccdb48298441e3a8ee5143e395a Mon Sep 17 00:00:00 2001
From: aviau <alexandre.viau@savoirfairelinux.com>
Date: Thu, 21 May 2015 17:51:13 -0400
Subject: [PATCH] Allow for multiple table input sources

Change-Id: I7d5bbfd2991462eacc47d4f0a2ded46da2cb8f9d
---
 app/components/config/config.json            | 112 ++++++++++++-------
 app/components/surveil/status.js             |   8 +-
 app/components/table/table.js                |  29 +++--
 app/templates/dashboard/dashboard.html       |   8 +-
 app/templates/single_table/single_table.html |   2 +-
 5 files changed, 94 insertions(+), 65 deletions(-)

diff --git a/app/components/config/config.json b/app/components/config/config.json
index 02b5b91..d9080d8 100644
--- a/app/components/config/config.json
+++ b/app/components/config/config.json
@@ -35,16 +35,21 @@
                             "status_host_status"
                         ]
                     },
-                    "apiName": "hosts",
-                    "filters": {
-                        "is": {
-                            "acknowledged": [
-                                false
-                            ],
-                            "state": [
-                                "DOWN",
-                                "UNREACHABLE"
-                            ]
+                    "inputSource": {
+                        "service": "surveilStatus",
+                        "config": {
+                            "apiName": "hosts",
+                            "filters": {
+                                "is": {
+                                    "acknowledged": [
+                                        false
+                                    ],
+                                    "state": [
+                                        "DOWN",
+                                        "UNREACHABLE"
+                                    ]
+                                }
+                            }
                         }
                     },
                     "isWrappable": false,
@@ -69,21 +74,26 @@
                             "status_last_check"
                         ]
                     },
-                    "apiName": "services",
-                    "filters": {
-                        "isnot": {
-                            "state": [
-                                "OK"
-                            ],
-                            "host_state": [
-                                "DOWN",
-                                "UNREACHABLE"
-                            ]
-                        },
-                        "is": {
-                            "acknowledged": [
-                                false
-                            ]
+                    "inputSource": {
+                        "service": "surveilStatus",
+                        "config": {
+                            "apiName": "services",
+                            "filters": {
+                                "isnot": {
+                                    "state": [
+                                        "OK"
+                                    ],
+                                    "host_state": [
+                                        "DOWN",
+                                        "UNREACHABLE"
+                                    ]
+                                },
+                                "is": {
+                                    "acknowledged": [
+                                        false
+                                    ]
+                                }
+                            }
                         }
                     },
                     "isWrappable": true,
@@ -110,12 +120,17 @@
                             "status_host_status"
                         ]
                     },
-                    "apiName": "hosts",
-                    "filters": {
-                        "isnot": {
-                            "state": [
-                                "UP"
-                            ]
+                    "inputSource": {
+                        "service": "surveilStatus",
+                        "config": {
+                            "apiName": "hosts",
+                            "filters": {
+                                "isnot": {
+                                    "state": [
+                                        "UP"
+                                    ]
+                                }
+                            }
                         }
                     },
                     "isWrappable": false,
@@ -140,12 +155,17 @@
                             "status_last_check"
                         ]
                     },
-                    "apiName": "services",
-                    "filters": {
-                        "isnot": {
-                            "state": [
-                                "OK"
-                            ]
+                    "inputSource": {
+                        "service": "surveilStatus",
+                        "config": {
+                            "apiName": "services",
+                            "filters": {
+                                "isnot": {
+                                    "state": [
+                                        "OK"
+                                    ]
+                                }
+                            }
                         }
                     },
                     "isWrappable": true,
@@ -179,8 +199,13 @@
                             "status_host_status"
                         ]
                     },
-                    "apiName": "hosts",
-                    "filters": {},
+                    "inputSource": {
+                        "service": "surveilStatus",
+                        "config": {
+                            "apiName": "hosts",
+                            "filters": {}
+                        }
+                    },
                     "isWrappable": false,
                     "noRepeatCell": ""
                 }
@@ -210,8 +235,13 @@
                             "status_last_check"
                         ]
                     },
-                    "apiName": "services",
-                    "filters": {},
+                    "inputSource": {
+                        "service": "surveilStatus",
+                        "config": {
+                            "apiName": "services",
+                            "filters": {}
+                        }
+                    },
                     "isWrappable": false,
                     "noRepeatCell": "host"
                 }
diff --git a/app/components/surveil/status.js b/app/components/surveil/status.js
index 804316d..89e8569 100644
--- a/app/components/surveil/status.js
+++ b/app/components/surveil/status.js
@@ -250,7 +250,7 @@ angular.module('bansho.surveil')
                 return data;
             };
 
-            var getTableData = function (fields, filters, apiName) {
+            var getTableData = function (fields, inputSourceConfig) {
                 var hostFields = [],
                     serviceFields = [],
                     hostFilters = {},
@@ -264,8 +264,8 @@ angular.module('bansho.surveil')
                     i,
                     found = false;
 
-                if (apiName === 'hosts') {
-                    this.getObjects(fields, filters, 'hosts')
+                if (inputSourceConfig.apiName === 'hosts') {
+                    this.getObjects(fields, inputSourceConfig.filters, 'hosts')
                         .success(function (data) {
                             responsePromise.resolve(data);
                         });
@@ -289,7 +289,7 @@ angular.module('bansho.surveil')
                     serviceFields.push('host_name');
                 }
 
-                angular.forEach(filters, function (filterData, filterName) {
+                angular.forEach(inputSourceConfig.filters, function (filterData, filterName) {
                     angular.forEach(filterData, function (values, field) {
                         if (field in hostKeys) {
                             if (!(filterData in hostFilters)) {
diff --git a/app/components/table/table.js b/app/components/table/table.js
index edf5df5..5716064 100644
--- a/app/components/table/table.js
+++ b/app/components/table/table.js
@@ -22,7 +22,12 @@ angular.module('bansho.table', ['bansho.surveil',
             var requestFields = [],
                 conf = tablesConfig[tableGlobalConfig.nextTableIndex],
                 getData,
-                i;
+                i,
+                inputSourceServices;
+
+            inputSourceServices = {
+                surveilStatus: surveilStatus
+            };
 
             $scope.cellsName = conf.cells.name;
             $scope.cellsText = conf.cells.text;
@@ -52,8 +57,8 @@ angular.module('bansho.table', ['bansho.surveil',
                 });
             });
 
-            getData = function (requestFields, filters, apiName) {
-                var promise = surveilStatus.getTableData(requestFields, filters, apiName);
+            getData = function (requestFields, inputSource) {
+                var promise = inputSourceServices[inputSource.service].getTableData(requestFields, inputSource.config);
                 promise.then(function (data) {
                     $scope.entries = data;
                     conf.entries = data;
@@ -62,12 +67,12 @@ angular.module('bansho.table', ['bansho.surveil',
                 });
             };
 
-            getData(requestFields, conf.filters, conf.apiName);
+            getData(requestFields, conf.inputSource);
 
             if (tableGlobalConfig.refreshInterval !== 0) {
                 promisesManager.addAjaxPromise(
                     $interval(function () {
-                        getData(requestFields, conf.filters, conf.apiName);
+                        getData(requestFields, conf.inputSource);
                     }, tableGlobalConfig.refreshInterval)
                 );
             }
@@ -89,28 +94,23 @@ angular.module('bansho.table', ['bansho.surveil',
                         var template = 'components/table/table.html',
                             conf;
 
-                        if (!attrs.cellsText || !attrs.cellsName || !attrs.apiName || !attrs.isWrappable) {
-                            throw new Error('<bansho-table> "cells-text", "cells-name", "api-name" and "is-wrappable" attributes must be defined');
+                        if (!attrs.cellsText || !attrs.cellsName || !attrs.inputSource || !attrs.isWrappable) {
+                            throw new Error('<bansho-table> "cells-text", "cells-name", "inputSource" and "is-wrappable" attributes must be defined');
                         }
 
                         tablesConfig[attrs.tableId] = {};
                         conf = tablesConfig[attrs.tableId];
-                        conf.filters = {};
 
                         conf.cells = { 'text': [], 'name': [] };
                         conf.cells.text = attrs.cellsText.split(',');
                         conf.cells.name = attrs.cellsName.split(',');
 
-                        conf.apiName = attrs.apiName;
+                        conf.inputSource = JSON.parse(attrs.inputSource);
 
                         conf.isWrappable = JSON.parse(attrs.isWrappable);
                         conf.noRepeatCell = attrs.noRepeatCell;
                         tableGlobalConfig.tableId = attrs.tableId;
 
-                        if (!!attrs.filters) {
-                            conf.filters = JSON.parse(attrs.filters);
-                        }
-
                         if (!!attrs.refreshInterval) {
                             tableGlobalConfig.refreshInterval = parseInt(attrs.refreshInterval * 1000, 10);
                         }
@@ -160,8 +160,7 @@ angular.module('bansho.table', ['bansho.surveil',
         this.title = config.title;
         this.CellsText = config.cells.text.join();
         this.CellsName = config.cells.name.join();
-        this.ApiName = config.apiName;
-        this.Filters = config.filters;
+        this.InputSource = config.inputSource;
         this.IsWrappable = config.isWrappable;
         this.NoRepeatCell = config.noRepeatCell;
     })
diff --git a/app/templates/dashboard/dashboard.html b/app/templates/dashboard/dashboard.html
index bf63ed0..3564a3e 100644
--- a/app/templates/dashboard/dashboard.html
+++ b/app/templates/dashboard/dashboard.html
@@ -54,7 +54,7 @@
 
           <bansho-table cells-text="{{dashboardTables[0].CellsText}}"
                      cells-name="{{dashboardTables[0].CellsName}}"
-                     api-name="{{dashboardTables[0].ApiName}}"
+                     input-source="{{dashboardTables[0].InputSource}}"
                      filters="{{dashboardTables[0].Filters}}"
                      is-wrappable="{{dashboardTables[0].IsWrappable}}"
                      no-repeat-cell="{{dashboardTables[0].NoRepeatCell}}"
@@ -79,7 +79,7 @@
 
           <bansho-table cells-text="{{dashboardTables[1].CellsText}}"
                      cells-name="{{dashboardTables[1].CellsName}}"
-                     api-name="{{dashboardTables[1].ApiName}}"
+                     input-source="{{dashboardTables[1].InputSource}}"
                      filters="{{dashboardTables[1].Filters}}"
                      is-wrappable="{{dashboardTables[1].IsWrappable}}"
                      no-repeat-cell="{{dashboardTables[1].NoRepeatCell}}"
@@ -106,7 +106,7 @@
 
           <bansho-table cells-text="{{dashboardTables[2].CellsText}}"
                      cells-name="{{dashboardTables[2].CellsName}}"
-                     api-name="{{dashboardTables[2].ApiName}}"
+                     input-source="{{dashboardTables[2].InputSource}}"
                      filters="{{dashboardTables[2].Filters}}"
                      is-wrappable="{{dashboardTables[2].IsWrappable}}"
                      no-repeat-cell="{{dashboardTables[2].NoRepeatCell}}"
@@ -129,7 +129,7 @@
 
           <bansho-table cells-text="{{dashboardTables[3].CellsText}}"
                      cells-name="{{dashboardTables[3].CellsName}}"
-                     api-name="{{dashboardTables[3].ApiName}}"
+                     input-source="{{dashboardTables[3].InputSource}}"
                      filters="{{dashboardTables[3].Filters}}"
                      is-wrappable="{{dashboardTables[3].IsWrappable}}"
                      no-repeat-cell="{{dashboardTables[3].NoRepeatCell}}"
diff --git a/app/templates/single_table/single_table.html b/app/templates/single_table/single_table.html
index 958dd3d..83bc512 100644
--- a/app/templates/single_table/single_table.html
+++ b/app/templates/single_table/single_table.html
@@ -14,7 +14,7 @@
 
     <bansho-table cells-name="{{tableConfig.CellsName}}"
                cells-text="{{tableConfig.CellsText}}"
-               api-name="{{tableConfig.ApiName}}"
+               input-source="{{tableConfig.InputSource}}"
                filters="{{tableConfig.Filters}}"
                is-wrappable="{{tableConfig.IsWrappable}}"
                no-repeat-cell="{{tableConfig.NoRepeatCell}}"