From a16cb20d2ad762d4da17f701220a67d45b5ba407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Vachon?= Date: Wed, 25 Mar 2015 11:24:58 -0400 Subject: [PATCH] FIX: Stop ajax queries on view change --- .gitignore | 1 + .../table/cell_duration/cell_duration.js | 4 +- app/components/table/cell_host/cell_host.js | 6 +- .../cell_host_address/cell_host_address.js | 4 +- .../cell_host_status/cell_host_status.js | 4 +- .../table/cell_hosts_host/cell_hosts_host.js | 4 +- .../table/cell_last_check/cell_last_check.js | 4 +- .../cell_service_check/cell_service_check.js | 4 +- app/components/table/table.js | 137 ++++++++++-------- app/dashboard/dashboard.html | 2 +- app/dashboard/dashboard.js | 6 +- app/single_table/single_table.js | 6 +- 12 files changed, 101 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index 801e69c..54ae1d9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ tmp .DS_Store .idea *.swp +*.swo app/assets/css .sass-cache/ out/ diff --git a/app/components/table/cell_duration/cell_duration.js b/app/components/table/cell_duration/cell_duration.js index e9a9ed6..7e6ef86 100644 --- a/app/components/table/cell_duration/cell_duration.js +++ b/app/components/table/cell_duration/cell_duration.js @@ -6,6 +6,6 @@ angular.module('adagios.table.cell_duration', ['adagios.table']) angular.noop(); }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.duration = ['last_state_change']; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.duration = ['last_state_change']; }]); diff --git a/app/components/table/cell_host/cell_host.js b/app/components/table/cell_host/cell_host.js index 4e069bb..b960eb3 100644 --- a/app/components/table/cell_host/cell_host.js +++ b/app/components/table/cell_host/cell_host.js @@ -16,7 +16,7 @@ angular.module('adagios.table.cell_host', ['adagios.table']) } }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.host = ['host_state', 'host_name']; - tableConfig.cellWrappableField.host = 'host_name'; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.host = ['host_state', 'host_name']; + tableGlobalConfig.cellWrappableField.host = 'host_name'; }]); diff --git a/app/components/table/cell_host_address/cell_host_address.js b/app/components/table/cell_host_address/cell_host_address.js index af9c1ec..f2312d3 100644 --- a/app/components/table/cell_host_address/cell_host_address.js +++ b/app/components/table/cell_host_address/cell_host_address.js @@ -6,6 +6,6 @@ angular.module('adagios.table.cell_host_address', ['adagios.table']) angular.noop(); }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.host_address = ['host_address']; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.host_address = ['host_address']; }]); diff --git a/app/components/table/cell_host_status/cell_host_status.js b/app/components/table/cell_host_status/cell_host_status.js index 0560306..b041a27 100644 --- a/app/components/table/cell_host_status/cell_host_status.js +++ b/app/components/table/cell_host_status/cell_host_status.js @@ -23,6 +23,6 @@ angular.module('adagios.table.cell_host_status', ['adagios.table']) } }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.host_status = ['state', 'last_check', 'childs']; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.host_status = ['state', 'last_check', 'childs']; }]); diff --git a/app/components/table/cell_hosts_host/cell_hosts_host.js b/app/components/table/cell_hosts_host/cell_hosts_host.js index 1f82021..cbb1ef5 100644 --- a/app/components/table/cell_hosts_host/cell_hosts_host.js +++ b/app/components/table/cell_hosts_host/cell_hosts_host.js @@ -14,6 +14,6 @@ angular.module('adagios.table.cell_hosts_host', ['adagios.table']) } }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.hosts_host = ['name', 'state']; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.hosts_host = ['name', 'state']; }]); diff --git a/app/components/table/cell_last_check/cell_last_check.js b/app/components/table/cell_last_check/cell_last_check.js index df1268b..bb6e936 100644 --- a/app/components/table/cell_last_check/cell_last_check.js +++ b/app/components/table/cell_last_check/cell_last_check.js @@ -6,6 +6,6 @@ angular.module('adagios.table.cell_last_check', ['adagios.table']) angular.noop(); }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.last_check = ['last_check']; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.last_check = ['last_check']; }]); diff --git a/app/components/table/cell_service_check/cell_service_check.js b/app/components/table/cell_service_check/cell_service_check.js index 2e4585b..a34f6a5 100644 --- a/app/components/table/cell_service_check/cell_service_check.js +++ b/app/components/table/cell_service_check/cell_service_check.js @@ -12,6 +12,6 @@ angular.module('adagios.table.cell_service_check', ['adagios.table']) } }]) - .run(['tableConfig', function (tableConfig) { - tableConfig.cellToFieldsMap.service_check = ['state', 'description', 'plugin_output']; + .run(['tableGlobalConfig', function (tableGlobalConfig) { + tableGlobalConfig.cellToFieldsMap.service_check = ['state', 'description', 'plugin_output']; }]); diff --git a/app/components/table/table.js b/app/components/table/table.js index 335fb6d..89852ad 100644 --- a/app/components/table/table.js +++ b/app/components/table/table.js @@ -12,13 +12,16 @@ angular.module('adagios.table', ['adagios.live', 'adagios.table.cell_host_status' ]) - .value('tableConfig', {'cellToFieldsMap': {}, 'cellWrappableField': {}, 'index': 0}) + .value('tableGlobalConfig', {'cellToFieldsMap': {}, 'cellWrappableField': {}, 'nextTableIndex': 0}) - .controller('TableCtrl', ['$scope', '$interval', 'getServices', 'tableConfig', 'actionbarFilters', - function ($scope, $interval, getServices, tableConfig, actionbarFilters) { - console.log(tableConfig[tableConfig.index].additionnalQueryFields); + .value('tablesConfig', []) + + .value('ajaxQueries', []) + + .controller('TableCtrl', ['$scope', '$interval', 'getServices', 'tablesConfig', 'actionbarFilters', 'ajaxQueries', 'tableGlobalConfig', + function ($scope, $interval, getServices, tablesConfig, actionbarFilters, ajaxQueries, tableGlobalConfig) { var requestFields = [], - conf = tableConfig[tableConfig.index], + conf = tablesConfig[tableGlobalConfig.nextTableIndex], getData, i; @@ -31,13 +34,12 @@ angular.module('adagios.table', ['adagios.live', } angular.forEach($scope.cellsName, function (key) { - angular.forEach(tableConfig.cellToFieldsMap[key], function (_value) { + angular.forEach(tableGlobalConfig.cellToFieldsMap[key], function (_value) { requestFields.push(_value); }); }); getData = function (requestFields, filters, apiName, additionnalFields) { - console.log(additionnalFields); getServices(requestFields, filters, apiName, additionnalFields) .success(function (data) { $scope.entries = data; @@ -46,70 +48,73 @@ angular.module('adagios.table', ['adagios.live', getData(requestFields, conf.filters, conf.apiName, conf.additionnalQueryFields); - if (tableConfig.refreshInterval !== '0') { - $interval(function () { - getData(requestFields, conf.filters, conf.apiName, conf.additionnalQueryFields); - }, tableConfig.refreshInterval); + if (tableGlobalConfig.refreshInterval !== 0) { + ajaxQueries.push( + $interval(function () { + getData(requestFields, conf.filters, conf.apiName, conf.additionnalQueryFields); + }, tableGlobalConfig.refreshInterval) + ); } $scope.actionbarFilters = actionbarFilters; // Index needed to support multiple tables per view - $scope.tableIndex = tableConfig.index; - tableConfig.index += 1; + $scope.tableIndex = tableGlobalConfig.nextTableIndex; + tableGlobalConfig.nextTableIndex += 1; }]) - .directive('adgTable', ['$http', '$compile', 'tableConfig', function ($http, $compile, tableConfig) { - return { - restrict: 'E', - compile: function () { - return function (scope, element, attrs) { + .directive('adgTable', ['$http', '$compile', 'tablesConfig', 'tableGlobalConfig', + function ($http, $compile, tablesConfig, tableGlobalConfig) { + return { + restrict: 'E', + compile: function () { + return function (scope, element, attrs) { - var template = 'components/table/table.html', - conf; + var template = 'components/table/table.html', + conf; - if (!attrs.cellsText || !attrs.cellsName || !attrs.apiName || !attrs.isWrappable) { - throw new Error(' "cells-text", "cells-name", "api-name"' - + ' and "is-wrappable" attributes must be defined'); - } + if (!attrs.cellsText || !attrs.cellsName || !attrs.apiName || !attrs.isWrappable) { + throw new Error(' "cells-text", "cells-name", "api-name"' + + ' and "is-wrappable" attributes must be defined'); + } - tableConfig[attrs.tableId] = {}; - conf = tableConfig[attrs.tableId]; - conf.filters = {}; - conf.additionnalQueryFields = {}; + tablesConfig[attrs.tableId] = {}; + conf = tablesConfig[attrs.tableId]; + conf.filters = {}; + conf.additionnalQueryFields = {}; - conf.cells = { 'text': [], 'name': [] }; - conf.cells.text = attrs.cellsText.split(','); - conf.cells.name = attrs.cellsName.split(','); + conf.cells = { 'text': [], 'name': [] }; + conf.cells.text = attrs.cellsText.split(','); + conf.cells.name = attrs.cellsName.split(','); - conf.apiName = attrs.apiName; + conf.apiName = attrs.apiName; - conf.isWrappable = JSON.parse(attrs.isWrappable); - conf.noRepeatCell = attrs.noRepeatCell; - conf.tableId = attrs.tableId; + 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.filters) { + conf.filters = JSON.parse(attrs.filters); + } - if (!!attrs.additionnalQueryFields) { - conf.additionnalQueryFields = JSON.parse(attrs.additionnalQueryFields); - } + if (!!attrs.additionnalQueryFields) { + conf.additionnalQueryFields = JSON.parse(attrs.additionnalQueryFields); + } - if (!!attrs.refreshInterval) { - tableConfig.refreshInterval = attrs.refreshInterval; - } + if (!!attrs.refreshInterval) { + tableGlobalConfig.refreshInterval = parseInt(attrs.refreshInterval, 10); + } - $http.get(template, { cache: true }) - .success(function (data) { - var elem = $compile(data)(scope); - element.append(elem); - }); - }; - } - }; - }]) + $http.get(template, { cache: true }) + .success(function (data) { + var elem = $compile(data)(scope); + element.append(elem); + }); + }; + } + }; + }]) .directive('adgCell', ['$http', '$compile', function ($http, $compile) { @@ -134,6 +139,20 @@ angular.module('adagios.table', ['adagios.live', }; }]) + .service('resetTables', ['$interval', 'ajaxQueries', 'tablesConfig', 'tableGlobalConfig', + function ($interval, ajaxQueries, tablesConfig, tableGlobalConfig) { + return function () { + // Stop AJAX queries + angular.forEach(ajaxQueries, function (promise) { + $interval.cancel(promise); + }); + + // Delete tables config + tablesConfig.length = 0; + tableGlobalConfig.nextTableIndex = 0; + }; + }]) + .value('TableConfigObj', function (config) { this.title = config.title; this.CellsText = config.cells.text.join(); @@ -145,20 +164,20 @@ angular.module('adagios.table', ['adagios.live', this.additionnalQueryFields = config.additionnalQueryFields; }) - .filter('wrappableStyle', ['tableConfig', function (tableConfig) { + .filter('wrappableStyle', ['tablesConfig', 'tableGlobalConfig', function (tablesConfig, tableGlobalConfig) { return function (input, scope) { var last = '', entry = {}, parent_found = false, class_name = ['', ''], i, - fieldToWrap = tableConfig.cellWrappableField[tableConfig[scope.tableIndex].noRepeatCell]; + fieldToWrap = tableGlobalConfig.cellWrappableField[tablesConfig[scope.tableIndex].noRepeatCell]; if (fieldToWrap === undefined) { return input; } - if (tableConfig[scope.tableIndex].isWrappable) { + if (tablesConfig[scope.tableIndex].isWrappable) { class_name = ['state--hasChild', 'state--isChild']; } @@ -187,12 +206,12 @@ angular.module('adagios.table', ['adagios.live', }; }]) - .filter('noRepeat', ['tableConfig', function (tableConfig) { + .filter('noRepeat', ['tablesConfig', 'tableGlobalConfig', function (tablesConfig, tableGlobalConfig) { return function (items, scope) { var newItems = [], previous, - fieldToCompare = tableConfig.cellWrappableField[tableConfig[scope.tableIndex].noRepeatCell], - new_attr = tableConfig[scope.tableIndex].noRepeatCell + "_additionnalClass"; + fieldToCompare = tableGlobalConfig.cellWrappableField[tablesConfig[scope.tableIndex].noRepeatCell], + new_attr = tablesConfig[scope.tableIndex].noRepeatCell + "_additionnalClass"; angular.forEach(items, function (item) { diff --git a/app/dashboard/dashboard.html b/app/dashboard/dashboard.html index 4db0a83..6e81202 100644 --- a/app/dashboard/dashboard.html +++ b/app/dashboard/dashboard.html @@ -1,4 +1,4 @@ -
+

{{dashboardTactical[0].title}}

diff --git a/app/dashboard/dashboard.js b/app/dashboard/dashboard.js index 000712b..3cfc05a 100644 --- a/app/dashboard/dashboard.js +++ b/app/dashboard/dashboard.js @@ -15,10 +15,10 @@ angular.module('adagios.view.dashboard', ['ngRoute', }); }]) - .controller('DashboardCtrl', ['$scope', '$routeParams', 'dashboardConfig', 'getServices', 'tableConfig', + .controller('DashboardCtrl', ['$scope', '$routeParams', 'dashboardConfig', 'getServices', 'resetTables', 'TableConfigObj', 'TacticalConfigObj', 'getHostOpenProblems', 'getServiceOpenProblems', 'getHostProblems', 'getServiceProblems', - function ($scope, $routeParams, dashboardConfig, getServices, tableConfig, TableConfigObj, + function ($scope, $routeParams, dashboardConfig, getServices, resetTables, TableConfigObj, TacticalConfigObj, getHostOpenProblems, getServiceOpenProblems, getHostProblems, getServiceProblems) { var components = [], component, @@ -26,7 +26,7 @@ angular.module('adagios.view.dashboard', ['ngRoute', viewName, i = 0; - tableConfig.index = 0; + resetTables(); if (!!$routeParams.view) { viewName = $routeParams.view; diff --git a/app/single_table/single_table.js b/app/single_table/single_table.js index 2799303..cf6304d 100644 --- a/app/single_table/single_table.js +++ b/app/single_table/single_table.js @@ -16,11 +16,11 @@ angular.module('adagios.view.singleTable', ['ngRoute', }); }]) - .controller('SingleTableCtrl', ['$scope', '$routeParams', 'singleTableConfig', 'tableConfig', 'TableConfigObj', - function ($scope, $routeParams, singleTableConfig, tableConfig, TableConfigObj) { + .controller('SingleTableCtrl', ['$scope', '$routeParams', 'singleTableConfig', 'resetTables', 'TableConfigObj', + function ($scope, $routeParams, singleTableConfig, resetTables, TableConfigObj) { var viewName = ""; - tableConfig.index = 0; + resetTables(); if (!!$routeParams.view) { viewName = $routeParams.view;