From f93e4861643edea76c8b0814495710762cce5fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Sun, 13 May 2018 12:34:28 +0200 Subject: [PATCH 01/25] Fix filters (in progress) --- .gitignore | 5 ++ public/controllers/agents.js | 51 ++++--------------- public/controllers/overview.js | 5 +- public/kibana-integrations/kibana-discover.js | 38 ++++++++++---- .../kibana-integrations/kibana-filter-bar.js | 17 +++++-- public/templates/agents/agents.head | 2 +- .../kibana-discover-template.html | 2 +- 7 files changed, 62 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index ddf8aadb6e..d602b4c03f 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,8 @@ typings/ # dotenv environment variables file .env + + +kibana/ + +*.zip \ No newline at end of file diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 8494ca905d..42b2bf23e4 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -15,9 +15,10 @@ import rison from 'rison' const app = modules.get('app/wazuh', []); -app.controller('agentsController', - function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler) { - +app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, $window) { + $window.sessionStorage.setItem('lasta',$location.search()._a) + $rootScope.completedAgent = false; + $rootScope.wazuhLoadFilters =false; $rootScope.page = 'agents'; $scope.extensions = appState.getExtensions().extensions; $scope.agentsAutoComplete = AgentsAutoComplete; @@ -198,18 +199,6 @@ app.controller('agentsController', }); $scope.$watch('tab', () => { - const str = $location.search()._a; - if(str){ - const decoded = rison.decode(str); - const tmp = decoded.filters.filter(item => !( - (item.query && item.query.match && item.query.match['rule.groups']) || - (item.exists && item.exists.field === 'rule.pci_dss')) - ); - - decoded.filters = tmp; - const encoded = rison.encode(decoded); - $location.search('_a', encoded) - } $location.search('tab', $scope.tab); $scope.tabView = 'panels'; @@ -226,7 +215,6 @@ app.controller('agentsController', // Update the implicit filter if (typeof tabFilters[$scope.tab] !== 'undefined' && tabFilters[$scope.tab].group === "") $rootScope.currentImplicitFilter = ""; else $rootScope.currentImplicitFilter = (typeof tabFilters[$scope.tab] !== 'undefined') ? tabFilters[$scope.tab].group : ''; - if($scope.tab === 'configuration'){ firstLoad(); } @@ -285,48 +273,26 @@ app.controller('agentsController', /** Prevents from double agent and come from autocomplete */ let lastAgent = null; - const checkDouble = id => { - if(lastAgent && lastAgent !== id){ - $rootScope.agentsAutoCompleteFired = true; - if(!$rootScope.$$phase) $rootScope.$digest(); - } - } + $scope.getAgent = async (newAgentId,fromAutocomplete) => { try { + $rootScope.completedAgent = false; if($scope.tab === 'configuration'){ return $scope.getAgentConfig(newAgentId); } - try { - // Try to parse the _a trace and detect if there is any agent.id filter - // in order to delete it from the _a trace - const str = $location.search()._a; - if(str){ - const decoded = rison.decode(str); - const tmp = decoded.filters.filter(item => !item.query.match['agent.id']); - decoded.filters = tmp; - const encoded = rison.encode(decoded); - $location.search('_a', encoded) - } - } catch (error) { - // If some rison.js related error is generated we simply clean the _a trace - console.log(error.message || error); // not blocking action - } let id = null; // They passed an id if (newAgentId) { id = newAgentId; - checkDouble(id); $location.search('agent', id); } else { if ($location.search().agent && !$rootScope.globalAgent) { // There's one in the url id = $location.search().agent; - checkDouble(id); } else { // We pick the one in the rootScope id = $rootScope.globalAgent; - checkDouble(id); $location.search('agent', id); delete $rootScope.globalAgent; } @@ -359,6 +325,7 @@ app.controller('agentsController', $scope.agent.rootcheck = data[2].data.data; validateRootCheck(); + $rootScope.completedAgent = true; if(!$scope.$$phase) $scope.$digest(); return; } catch (error) { @@ -377,7 +344,7 @@ app.controller('agentsController', } $rootScope.ownHandlers = []; $rootScope.comeFrom = 'agents'; - $location.search('_a',null); + //$location.search('_a',null); $location.search('tab', 'groups'); $location.path('/manager'); }; @@ -464,6 +431,7 @@ app.controller('agentsController', const firstLoad = async () => { try{ + $rootScope.completedAgent = false; $scope.configurationError = false; $scope.load = true; let id; @@ -509,6 +477,7 @@ app.controller('agentsController', $scope.isSynchronized = (($scope.agentMergedSum === $scope.groupMergedSum) && !([$scope.agentMergedSum,$scope.groupMergedSum].includes('Unknown')) ) ? true : false; $scope.load = false; + $rootScope.completedAgent = true; if(!$scope.$$phase) $scope.$digest(); return; } catch (error){ diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 3debba56af..ca1fd9bfd0 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -14,7 +14,8 @@ import * as modules from 'ui/modules' const app = modules.get('app/wazuh', []); -app.controller('overviewController', function ($scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq) { +app.controller('overviewController', function ($scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq,$window) { + $rootScope.rawVisualizations = null; $rootScope.page = 'overview'; @@ -210,7 +211,7 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap checkMetrics(tab, 'panels'); // Deleting app state traces in the url - $location.search('_a', null); + //$location.search('_a', null); }) .catch(error => errorHandler.handle(error, 'Overview')); diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index fbdf4be356..8a16217aa4 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -44,7 +44,7 @@ import 'ui/pager_control'; import 'ui/pager'; import { UtilsBrushEventProvider } from 'ui/utils/brush_event'; import { documentationLinks } from 'ui/documentation_links/documentation_links'; - +import chrome from 'ui/chrome' ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -758,8 +758,26 @@ function discoverController( /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function loadFilters() { - if ($scope.tab) { - let implicitFilter = []; + if($scope.tab !== 'general' && !$rootScope.currentImplicitFilter) { + return; + } + + + if ($scope.tab) { + const current = queryFilter.getFilters(); + const implicitFilter = []; + for(const item of current){ + if((item && item.query && item.query.match && item.query.match['rule.groups']) || + (item && item.query && item.query.match && item.query.match['agent.id']) || + (item && item.query && item.query.match && item.query.match['manager.name']) || + (item && item.exists && item.exists.field && item.exists.field === 'rule.pci_dss')) + continue; + else + implicitFilter.push(item) + } + + queryFilter.removeAll() + if (appState.getClusterInfo().status == 'enabled') { // The cluster filter @@ -906,10 +924,13 @@ function discoverController( } ); } - } - - queryFilter.addFilters(implicitFilter); - } + } + + queryFilter.addFilters(implicitFilter).then(data => { + if($rootScope.page === 'agents') $rootScope.wazuhLoadFilters = !$rootScope.wazuhLoadFilters + }); + + } } // Getting the location from the url @@ -918,11 +939,10 @@ function discoverController( if ($rootScope.page === 'agents') $scope.agentId = $location.search().agent; // Initial loading of filters - loadFilters(); + loadFilters(); // Watch for changes in the location $scope.$on('$routeUpdate', () => { - if ($location.search().tabView != $scope.tabView) { // No need to change the filters if ($scope.tabView !== "discover") { // Should do this the first time, to avoid the squeezing of the visualization $scope.updateQueryAndFetch($state.query); diff --git a/public/kibana-integrations/kibana-filter-bar.js b/public/kibana-integrations/kibana-filter-bar.js index 028a2f4939..9bebab50d2 100644 --- a/public/kibana-integrations/kibana-filter-bar.js +++ b/public/kibana-integrations/kibana-filter-bar.js @@ -13,10 +13,10 @@ import { FilterBarLibChangeTimeFilterProvider } from 'ui/filter_bar/lib/change_t import { FilterBarQueryFilterProvider } from 'ui/filter_bar/query_filter'; import { compareFilters } from 'ui/filter_bar/lib/compare_filters'; import { uiModules } from 'ui/modules'; - +import rison from 'rison' const module = uiModules.get('kibana'); -module.directive('filterBarW', function (Private, Promise, getAppState) { +module.directive('filterBarW', function (Private, Promise, getAppState,$location, $rootScope) { const mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); const mapFlattenAndWrapFilters = Private(FilterBarLibMapFlattenAndWrapFiltersProvider); const extractTimeFilter = Private(FilterBarLibExtractTimeFilterProvider); @@ -96,9 +96,17 @@ module.directive('filterBarW', function (Private, Promise, getAppState) { // update the scope filter list on filter changes $scope.$listen(queryFilter, 'update', function () { - updateFilters(); + if(queryFilter.getFilters().length) updateFilters(); }); + $scope.$watch('wazuhLoadFilters',() => { + if(queryFilter.getFilters().length) updateFilters(); + }) + + $rootScope.$watch('completedAgent',() => { + if(queryFilter.getFilters().length) updateFilters(); + }) + // when appState changes, update scope's state $scope.$watch(getAppState, function (appState) { $scope.state = appState; @@ -106,7 +114,7 @@ module.directive('filterBarW', function (Private, Promise, getAppState) { $scope.$watch('state.$newFilters', function (filters) { if (!filters) return; - + // If filters is not undefined and the length is greater than // one we need to set the newFilters attribute and allow the // users to decide what they want to apply. @@ -157,6 +165,7 @@ module.directive('filterBarW', function (Private, Promise, getAppState) { function updateFilters() { const filters = queryFilter.getFilters(); + if(!queryFilter.getFilters().length || ($rootScope.page === 'agents' && !$rootScope.completedAgent)) return; mapAndFlattenFilters(filters).then(function (results) { // used to display the current filters in the state $scope.filters = _.sortBy(results, function (filter) { diff --git a/public/templates/agents/agents.head b/public/templates/agents/agents.head index d573130d40..3658ae0728 100644 --- a/public/templates/agents/agents.head +++ b/public/templates/agents/agents.head @@ -74,7 +74,7 @@ - +
diff --git a/public/templates/kibana-template/kibana-discover-template.html b/public/templates/kibana-template/kibana-discover-template.html index 8faf68c58f..cb87e43a20 100644 --- a/public/templates/kibana-template/kibana-discover-template.html +++ b/public/templates/kibana-template/kibana-discover-template.html @@ -5,7 +5,7 @@
-

+

Date: Mon, 14 May 2018 09:45:43 +0200 Subject: [PATCH 02/25] Added new class to manage filters --- public/controllers/filter-handler.js | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 public/controllers/filter-handler.js diff --git a/public/controllers/filter-handler.js b/public/controllers/filter-handler.js new file mode 100644 index 0000000000..d239c17cad --- /dev/null +++ b/public/controllers/filter-handler.js @@ -0,0 +1,92 @@ +export default class FilterHandler { + constructor(pattern) { + this.pattern = pattern; + } + + base(){ + return { + meta: { + removable: false, + index: this.pattern, + negate: false, + disabled: false, + alias: null, + type: 'phrase', + key: null, + value: null, + params: { + query: null, + type: 'phrase' + } + }, + query: { + match: null + }, + $state: { + store: 'appState' + } + } + } + + agentQuery(agent) { + const result = this.base(); + result.meta.key = 'agent.id'; + result.meta.value = agent; + result.meta.params.query = agent; + result.query.match = { + 'agent.id': { + query: agent, + type: 'phrase' + } + }; + return result; + } + + ruleGroupQuery(group) { + const result = this.base(); + result.meta.key = 'rule.groups'; + result.meta.value = group; + result.meta.params.query = group; + result.query.match = { + 'rule.groups': { + query: group, + type: 'phrase' + } + }; + return result; + } + + managerQuery(manager,isCluster) { + const result = this.base(); + result.meta.key = isCluster ? 'cluster.name' : 'manager.name'; + result.meta.value = manager; + result.meta.params.query = manager; + result.query.match = + isCluster ? + { + 'cluster.name': { + query: manager, + type: 'phrase' + } + }: + { + 'manager.name': { + query: manager, + type: 'phrase' + } + }; + return result; + } + + pciQuery(){ + const result = this.base(); + result.meta.type = 'exists'; + result.meta.value = 'exists'; + result.meta.key = 'rule.pci_dss'; + result.exists = { + field: 'rule.pci_dss' + } + delete result.query; + return result; + } +} \ No newline at end of file From 28b9f89ca819019ecb9d07294346749141669e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 09:46:09 +0200 Subject: [PATCH 03/25] Assign filters on controller --- public/controllers/overview.js | 36 ++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/public/controllers/overview.js b/public/controllers/overview.js index ca1fd9bfd0..11a80595c9 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -9,12 +9,14 @@ * * Find more information about this on the LICENSE file. */ -import $ from 'jquery'; -import * as modules from 'ui/modules' +import $ from 'jquery'; +import * as modules from 'ui/modules' +import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); app.controller('overviewController', function ($scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq,$window) { + const filterHandler = new FilterHandler(appState.getCurrentPattern()); $rootScope.rawVisualizations = null; @@ -121,6 +123,31 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap virustotal: { group: 'virustotal' } }; + let filters = [] + + const assignFilters = tab => { + filters = []; + + filters.push(filterHandler.managerQuery( + appState.getClusterInfo().status == 'enabled' ? + appState.getClusterInfo().cluster : + appState.getClusterInfo().manager + )) + + if(tab !== 'general'){ + if(tab === 'pci') { + filters.push(filterHandler.pciQuery()) + } else { + filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group)); + } + } + + $rootScope.wzCurrentFilters = filters; + if(!$rootScope.$$phase) $rootScope.$digest(); + } + + assignFilters($scope.tab); + const generateMetric = id => { let html = $(id).html(); if (typeof html !== 'undefined' && html.includes(' { if ($scope.tab === tab) return; + assignFilters(tab); $rootScope.rawVisualizations = null; // Create current tab visualizations @@ -246,10 +274,6 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap $rootScope.ownHandlers = []; $rootScope.loadedVisualizations = []; - - // Update the implicit filter - if (tabFilters[$scope.tab].group === "") $rootScope.currentImplicitFilter = ""; - else $rootScope.currentImplicitFilter = tabFilters[$scope.tab].group; }); $scope.$on('$destroy', () => { From a532fa9f16420076f98e2923e0fcdfe11e37a28e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 09:46:29 +0200 Subject: [PATCH 04/25] Removed useless logic on Discover --- public/kibana-integrations/kibana-discover.js | 212 ++---------------- 1 file changed, 23 insertions(+), 189 deletions(-) diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index 8a16217aa4..ab720a8ac0 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -757,180 +757,24 @@ function discoverController( ////////////////////////////////////////////////////// WAZUH ////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - function loadFilters() { - if($scope.tab !== 'general' && !$rootScope.currentImplicitFilter) { - return; + const loadFilters = () => { + const filters = queryFilter.getFilters(); + const shouldPersist = []; + for(const item of filters){ + if((item && item.query && item.query.match && item.query.match['rule.groups']) || + (item && item.query && item.query.match && item.query.match['agent.id']) || + (item && item.query && item.query.match && item.query.match['manager.name']) || + (item && item.exists && item.exists.field && item.exists.field === 'rule.pci_dss')) + continue; + else + shouldPersist.push(item) } - - - if ($scope.tab) { - const current = queryFilter.getFilters(); - const implicitFilter = []; - for(const item of current){ - if((item && item.query && item.query.match && item.query.match['rule.groups']) || - (item && item.query && item.query.match && item.query.match['agent.id']) || - (item && item.query && item.query.match && item.query.match['manager.name']) || - (item && item.exists && item.exists.field && item.exists.field === 'rule.pci_dss')) - continue; - else - implicitFilter.push(item) - } - - queryFilter.removeAll() - - - if (appState.getClusterInfo().status == 'enabled') { - // The cluster filter - implicitFilter.push( - { - "meta":{ - "removable":false, - "index":$scope.indexPattern.id, - "negate":false, - "disabled":false, - "alias":null, - "type":"phrase", - "key":"cluster.name", - "value":appState.getClusterInfo().cluster, - "params":{ - "query":appState.getClusterInfo().cluster, - "type":"phrase"} - }, - "query":{ - "match":{ - "cluster.name":{ - "query":appState.getClusterInfo().cluster, - "type":"phrase"} - } - }, - "$state":{ - "store":"appState" - } - } - ); - } else { - // Manager name filter - implicitFilter.push( - { - "meta":{ - "removable":false, - "index":$scope.indexPattern.id, - "negate":false, - "disabled":false, - "alias":null, - "type":"phrase", - "key":"manager.name", - "value":appState.getClusterInfo().manager, - "params":{ - "query":appState.getClusterInfo().manager, - "type":"phrase"} - }, - "query":{ - "match":{ - "manager.name":{ - "query":appState.getClusterInfo().manager, - "type":"phrase"} - } - }, - "$state":{ - "store":"appState" - } - } - ); - } - - // Check if we are in the agents page and add the proper agent filter - if ($rootScope.page === 'agents' && $location.search().agent !== "" && $location.search().agent !== null && angular.isUndefined($location.search().agent) !== true) { - implicitFilter.push( - { - "meta":{ - "removable":false, - "index":$scope.indexPattern.id, - "negate":false, - "disabled":false, - "alias":null, - "type":"phrase", - "key":"agent.id", - "value":$location.search().agent, - "params":{ - "query":$location.search().agent, - "type":"phrase"} - }, - "query":{ - "match":{ - "agent.id":{ - "query":$location.search().agent, - "type":"phrase"} - } - }, - "$state":{ - "store":"appState" - } - } - ); - } - - // Build the full query using the implicit filter - if ($rootScope.currentImplicitFilter !== "" && $rootScope.currentImplicitFilter !== null && angular.isUndefined($rootScope.currentImplicitFilter) !== true) { - if ($rootScope.currentImplicitFilter === "pci_dss") { - implicitFilter.push( - { - "meta":{ - "removable":false, - "index":$scope.indexPattern.id, - "negate":false, - "disabled":false, - "alias":null, - "type":"exists", - "key":"rule.pci_dss", - "value":"exists" - }, - "exists":{ - "field":"rule.pci_dss" - }, - "$state":{ - "store":"appState" - } - } - ); - } else { - implicitFilter.push( - { - "meta":{ - "removable":false, - "index":$scope.indexPattern.id, - "negate":false, - "disabled":false, - "alias":null, - "type":"phrase", - "key":"rule.groups", - "value":$rootScope.currentImplicitFilter, - "params":{ - "query":$rootScope.currentImplicitFilter, - "type":"phrase" - } - }, - "query":{ - "match":{ - "rule.groups":{ - "query":$rootScope.currentImplicitFilter, - "type":"phrase" - } - } - }, - "$state":{ - "store":"appState" - } - } - ); - } - } - - queryFilter.addFilters(implicitFilter).then(data => { - if($rootScope.page === 'agents') $rootScope.wazuhLoadFilters = !$rootScope.wazuhLoadFilters - }); - - } + queryFilter.removeAll(); + const currentFilters = $rootScope.wzCurrentFilters; + const total = [...currentFilters,...shouldPersist] + queryFilter.addFilters(total) + .then(() => console.log('filters loaded')) + .catch(error => console.log(error.message || error)); } // Getting the location from the url @@ -939,24 +783,14 @@ function discoverController( if ($rootScope.page === 'agents') $scope.agentId = $location.search().agent; // Initial loading of filters - loadFilters(); + //loadFilters(); // Watch for changes in the location - $scope.$on('$routeUpdate', () => { - if ($location.search().tabView != $scope.tabView) { // No need to change the filters - if ($scope.tabView !== "discover") { // Should do this the first time, to avoid the squeezing of the visualization - $scope.updateQueryAndFetch($state.query); - } - $scope.tabView = $location.search().tabView; - } - if ($location.search().tab != $scope.tab) { // Changing filters - $scope.tab = $location.search().tab; - } - - if ($location.search().agent != $scope.agentId) { // Changing filters - $scope.agentId = $location.search().agent; - } - if ($location.search().tabView === $scope.tabView) loadFilters(); + $rootScope.$watch('wzCurrentFilters', () => { + console.log('---------------------------------------') + console.log($rootScope.wzCurrentFilters); + console.log('---------------------------------------') + loadFilters(); }); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// From 9e7b8bda4b9dcde4cb7d14c095848f239940054a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 10:05:25 +0200 Subject: [PATCH 05/25] Fixing filters (in progress) --- public/controllers/agents.js | 849 +++++++++--------- public/controllers/overview.js | 3 - .../kibana-integrations/kibana-filter-bar.js | 8 - public/templates/agents/agents.head | 2 +- 4 files changed, 437 insertions(+), 425 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 42b2bf23e4..982b1c7041 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -9,494 +9,517 @@ * * Find more information about this on the LICENSE file. */ -import beautifier from 'plugins/wazuh/utils/json-beautifier'; -import * as modules from 'ui/modules' -import rison from 'rison' +import beautifier from 'plugins/wazuh/utils/json-beautifier'; +import * as modules from 'ui/modules' +import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, $window) { - $window.sessionStorage.setItem('lasta',$location.search()._a) - $rootScope.completedAgent = false; - $rootScope.wazuhLoadFilters =false; - $rootScope.page = 'agents'; - $scope.extensions = appState.getExtensions().extensions; - $scope.agentsAutoComplete = AgentsAutoComplete; - - // Check the url hash and retrieve the tabView information - if ($location.search().tabView){ - $scope.tabView = $location.search().tabView; - } else { // If tabView doesn't exist, default it to 'panels' - $scope.tabView = "panels"; - $location.search("tabView", "panels"); - } - - // Check the url hash and retrivew the tab information - if ($location.search().tab){ - $scope.tab = $location.search().tab; - } else { // If tab doesn't exist, default it to 'general' - $scope.tab = "general"; - $location.search("tab", "general"); - - // Now we initialize the implicitFilter - $rootScope.currentImplicitFilter = ""; - } - - // Metrics Audit - const metricsAudit = { - auditNewFiles : '[vis-id="\'Wazuh-App-Agents-Audit-New-files-metric\'"]', - auditReadFiles : '[vis-id="\'Wazuh-App-Agents-Audit-Read-files-metric\'"]', - auditModifiedFiles: '[vis-id="\'Wazuh-App-Agents-Audit-Modified-files-metric\'"]', - auditRemovedFiles : '[vis-id="\'Wazuh-App-Agents-Audit-Removed-files-metric\'"]' - } - - // Metrics Vulnerability Detector - const metricsVulnerability = { - vulnCritical: '[vis-id="\'Wazuh-App-Agents-VULS-Metric-Critical-severity\'"]', - vulnHigh : '[vis-id="\'Wazuh-App-Agents-VULS-Metric-High-severity\'"]', - vulnMedium : '[vis-id="\'Wazuh-App-Agents-VULS-Metric-Medium-severity\'"]', - vulnLow : '[vis-id="\'Wazuh-App-Agents-VULS-Metric-Low-severity\'"]' - } - - // Metrics Scap - const metricsScap = { - scapLastScore : '[vis-id="\'Wazuh-App-Agents-OSCAP-Last-score\'"]', - scapHighestScore: '[vis-id="\'Wazuh-App-Agents-OSCAP-Higher-score-metric\'"]', - scapLowestScore : '[vis-id="\'Wazuh-App-Agents-OSCAP-Lower-score-metric\'"]' - } - - // Metrics Virustotal - const metricsVirustotal = { - virusMalicious: '[vis-id="\'Wazuh-App-Agents-Virustotal-Total-Malicious\'"]', - virusPositives: '[vis-id="\'Wazuh-App-Agents-Virustotal-Total-Positives\'"]', - virusTotal : '[vis-id="\'Wazuh-App-Agents-Virustotal-Total\'"]' + const filterHandler = new FilterHandler(appState.getCurrentPattern()); + + $rootScope.completedAgent = false; + $rootScope.page = 'agents'; + $scope.extensions = appState.getExtensions().extensions; + $scope.agentsAutoComplete = AgentsAutoComplete; + + // Check the url hash and retrieve the tabView information + if ($location.search().tabView){ + $scope.tabView = $location.search().tabView; + } else { // If tabView doesn't exist, default it to 'panels' + $scope.tabView = "panels"; + $location.search("tabView", "panels"); + } + + // Check the url hash and retrivew the tab information + if ($location.search().tab){ + $scope.tab = $location.search().tab; + } else { // If tab doesn't exist, default it to 'general' + $scope.tab = "general"; + $location.search("tab", "general"); + } + + // Metrics Audit + const metricsAudit = { + auditNewFiles : '[vis-id="\'Wazuh-App-Agents-Audit-New-files-metric\'"]', + auditReadFiles : '[vis-id="\'Wazuh-App-Agents-Audit-Read-files-metric\'"]', + auditModifiedFiles: '[vis-id="\'Wazuh-App-Agents-Audit-Modified-files-metric\'"]', + auditRemovedFiles : '[vis-id="\'Wazuh-App-Agents-Audit-Removed-files-metric\'"]' + } + + // Metrics Vulnerability Detector + const metricsVulnerability = { + vulnCritical: '[vis-id="\'Wazuh-App-Agents-VULS-Metric-Critical-severity\'"]', + vulnHigh : '[vis-id="\'Wazuh-App-Agents-VULS-Metric-High-severity\'"]', + vulnMedium : '[vis-id="\'Wazuh-App-Agents-VULS-Metric-Medium-severity\'"]', + vulnLow : '[vis-id="\'Wazuh-App-Agents-VULS-Metric-Low-severity\'"]' + } + + // Metrics Scap + const metricsScap = { + scapLastScore : '[vis-id="\'Wazuh-App-Agents-OSCAP-Last-score\'"]', + scapHighestScore: '[vis-id="\'Wazuh-App-Agents-OSCAP-Higher-score-metric\'"]', + scapLowestScore : '[vis-id="\'Wazuh-App-Agents-OSCAP-Lower-score-metric\'"]' + } + + // Metrics Virustotal + const metricsVirustotal = { + virusMalicious: '[vis-id="\'Wazuh-App-Agents-Virustotal-Total-Malicious\'"]', + virusPositives: '[vis-id="\'Wazuh-App-Agents-Virustotal-Total-Positives\'"]', + virusTotal : '[vis-id="\'Wazuh-App-Agents-Virustotal-Total\'"]' + } + + $rootScope.tabVisualizations = { + general : 7, + fim : 8, + pm : 4, + vuls : 7, + oscap : 13, + audit : 15, + pci : 3, + virustotal : 6, + configuration: 0 + }; + + // Object for matching nav items and Wazuh groups + const tabFilters = { + general : { group: '' }, + fim : { group: 'syscheck' }, + pm : { group: 'rootcheck' }, + vuls : { group: 'vulnerability-detector' }, + oscap : { group: 'oscap' }, + audit : { group: 'audit' }, + pci : { group: 'pci_dss' }, + virustotal: { group: 'virustotal' } + }; + + let filters = [] + const assignFilters = (tab,agent) => { + filters = []; + + filters.push(filterHandler.managerQuery( + appState.getClusterInfo().status == 'enabled' ? + appState.getClusterInfo().cluster : + appState.getClusterInfo().manager + )) + + if(tab !== 'general'){ + if(tab === 'pci') { + filters.push(filterHandler.pciQuery()) + } else { + filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group)); + } } - $rootScope.tabVisualizations = { - general : 7, - fim : 8, - pm : 4, - vuls : 7, - oscap : 13, - audit : 15, - pci : 3, - virustotal : 6, - configuration: 0 - }; - - // Object for matching nav items and Wazuh groups - const tabFilters = { - general : { group: '' }, - fim : { group: 'syscheck' }, - pm : { group: 'rootcheck' }, - vuls : { group: 'vulnerability-detector' }, - oscap : { group: 'oscap' }, - audit : { group: 'audit' }, - pci : { group: 'pci_dss' }, - virustotal: { group: 'virustotal' } - }; - - const generateMetric = id => { - let html = $(id).html(); - if (typeof html !== 'undefined' && html.includes('')[1] !== 'undefined'){ - return html.split('')[1].split('')[1] && - nonB.split('>')[1].split('')[1].split(' { + let html = $(id).html(); + if (typeof html !== 'undefined' && html.includes('')[1] !== 'undefined'){ + return html.split('')[1].split('')[1] && + nonB.split('>')[1].split('')[1].split(' { - for(let key in metricsObject) { - $scope[key] = () => generateMetric(metricsObject[key]); + const createMetrics = metricsObject => { + for(let key in metricsObject) { + $scope[key] = () => generateMetric(metricsObject[key]); + } + } + + const checkMetrics = (tab, subtab) => { + if(subtab === 'panels'){ + switch (tab) { + case 'audit': + createMetrics(metricsAudit); + break; + case 'vuls': + createMetrics(metricsVulnerability); + break; + case 'oscap': + createMetrics(metricsScap); + break; + case 'virustotal': + createMetrics(metricsVirustotal); + break; } } - const checkMetrics = (tab, subtab) => { - if(subtab === 'panels'){ - switch (tab) { - case 'audit': - createMetrics(metricsAudit); - break; - case 'vuls': - createMetrics(metricsVulnerability); - break; - case 'oscap': - createMetrics(metricsScap); - break; - case 'virustotal': - createMetrics(metricsVirustotal); - break; - } - } + if(!$rootScope.$$phase) $rootScope.$digest(); + } - if(!$rootScope.$$phase) $rootScope.$digest(); - } + // Switch subtab + $scope.switchSubtab = subtab => { + if($scope.tabView === subtab) return; + if(subtab === 'panels' && $scope.tab !== 'configuration'){ + $rootScope.rawVisualizations = null; - // Switch subtab - $scope.switchSubtab = subtab => { - if($scope.tabView === subtab) return; - if(subtab === 'panels' && $scope.tab !== 'configuration'){ - $rootScope.rawVisualizations = null; - - // Create current tab visualizations - genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - $rootScope.$broadcast('updateVis'); - - checkMetrics($scope.tab, 'panels'); - }) - .catch(error => errorHandler.handle(error, 'Agents')); - } else { - checkMetrics($scope.tab, subtab); - } + // Create current tab visualizations + genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) + .then(data => { + $rootScope.rawVisualizations = data.data.raw; + // Render visualizations + $rootScope.$broadcast('updateVis'); + + checkMetrics($scope.tab, 'panels'); + }) + .catch(error => errorHandler.handle(error, 'Agents')); + } else { + checkMetrics($scope.tab, subtab); } + } - // Switch tab - $scope.switchTab = tab => { - if ($scope.tab === tab) return; + // Switch tab + $scope.switchTab = tab => { + if ($scope.tab === tab) return; - if(tab !== 'configuration') { - $rootScope.rawVisualizations = null; - // Create current tab visualizations - genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - $rootScope.$broadcast('updateVis'); + assignFilters(tab); + if(tab !== 'configuration') { + $rootScope.rawVisualizations = null; + // Create current tab visualizations + genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${tab}/${appState.getCurrentPattern()}`) + .then(data => { + $rootScope.rawVisualizations = data.data.raw; + // Render visualizations + $rootScope.$broadcast('updateVis'); - checkMetrics(tab, 'panels'); + checkMetrics(tab, 'panels'); - }) - .catch(error => errorHandler.handle(error, 'Agents')); - } - }; + }) + .catch(error => errorHandler.handle(error, 'Agents')); + } + }; - // Watchers + // Watchers - // We watch the resultState provided by the discover - $scope.$watch('tabView', () => { - $location.search('tabView', $scope.tabView); + // We watch the resultState provided by the discover + $scope.$watch('tabView', () => { + $location.search('tabView', $scope.tabView); - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } + if ($rootScope.ownHandlers) { + for (let h of $rootScope.ownHandlers) { + h._scope.$destroy(); } - $rootScope.ownHandlers = []; + } + $rootScope.ownHandlers = []; - $rootScope.loadedVisualizations = []; - }); + $rootScope.loadedVisualizations = []; + }); - $scope.$watch('tab', () => { - $location.search('tab', $scope.tab); + $scope.$watch('tab', () => { + $location.search('tab', $scope.tab); - $scope.tabView = 'panels'; + $scope.tabView = 'panels'; - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } + if ($rootScope.ownHandlers) { + for (let h of $rootScope.ownHandlers) { + h._scope.$destroy(); } - $rootScope.ownHandlers = []; + } + $rootScope.ownHandlers = []; - $rootScope.loadedVisualizations = []; + $rootScope.loadedVisualizations = []; - // Update the implicit filter - if (typeof tabFilters[$scope.tab] !== 'undefined' && tabFilters[$scope.tab].group === "") $rootScope.currentImplicitFilter = ""; - else $rootScope.currentImplicitFilter = (typeof tabFilters[$scope.tab] !== 'undefined') ? tabFilters[$scope.tab].group : ''; - if($scope.tab === 'configuration'){ - firstLoad(); - } - }); + if($scope.tab === 'configuration'){ + firstLoad(); + } + }); - // Agent data - $scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "teal" : "red"; + // Agent data + $scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "teal" : "red"; - $scope.formatAgentStatus = (agentStatus) => { - return ['Active','Disconnected'].includes(agentStatus) ? agentStatus : 'Never connected'; - }; + $scope.formatAgentStatus = (agentStatus) => { + return ['Active','Disconnected'].includes(agentStatus) ? agentStatus : 'Never connected'; + }; - const calculateMinutes = (start,end) => { - let time = new Date(start); - let endTime = new Date(end); - let minutes = ((endTime - time) / 1000) / 60; - return minutes; - } + const calculateMinutes = (start,end) => { + let time = new Date(start); + let endTime = new Date(end); + let minutes = ((endTime - time) / 1000) / 60; + return minutes; + } - const validateRootCheck = () => { - $scope.agent.rootcheck.duration = 'Unknown'; - if ($scope.agent.rootcheck.end && $scope.agent.rootcheck.start) { - $scope.agent.rootcheck.duration = ((new Date($scope.agent.rootcheck.end) - new Date($scope.agent.rootcheck.start))/1000)/60; - $scope.agent.rootcheck.duration = Math.round($scope.agent.rootcheck.duration * 100) / 100; + const validateRootCheck = () => { + $scope.agent.rootcheck.duration = 'Unknown'; + if ($scope.agent.rootcheck.end && $scope.agent.rootcheck.start) { + $scope.agent.rootcheck.duration = ((new Date($scope.agent.rootcheck.end) - new Date($scope.agent.rootcheck.start))/1000)/60; + $scope.agent.rootcheck.duration = Math.round($scope.agent.rootcheck.duration * 100) / 100; - if($scope.agent.rootcheck.duration <= 0){ - $scope.agent.rootcheck.inProgress = true; - } - } else { - if (!$scope.agent.rootcheck.end) { - $scope.agent.rootcheck.end = 'Unknown'; - } - if (!$scope.agent.rootcheck.start){ - $scope.agent.rootcheck.start = 'Unknown'; - } + if($scope.agent.rootcheck.duration <= 0){ + $scope.agent.rootcheck.inProgress = true; + } + } else { + if (!$scope.agent.rootcheck.end) { + $scope.agent.rootcheck.end = 'Unknown'; + } + if (!$scope.agent.rootcheck.start){ + $scope.agent.rootcheck.start = 'Unknown'; } } - - const validateSysCheck = () => { - $scope.agent.syscheck.duration = 'Unknown'; - if ($scope.agent.syscheck.end && $scope.agent.syscheck.start) { - $scope.agent.syscheck.duration = ((new Date($scope.agent.syscheck.end) - new Date($scope.agent.syscheck.start))/1000)/60; - $scope.agent.syscheck.duration = Math.round($scope.agent.syscheck.duration * 100) / 100; - if($scope.agent.syscheck.duration <= 0){ - $scope.agent.syscheck.inProgress = true; - } - } else { - if (!$scope.agent.syscheck.end) { - $scope.agent.syscheck.end = 'Unknown'; - } - if (!$scope.agent.syscheck.start){ - $scope.agent.syscheck.start = 'Unknown'; - } + } + + const validateSysCheck = () => { + $scope.agent.syscheck.duration = 'Unknown'; + if ($scope.agent.syscheck.end && $scope.agent.syscheck.start) { + $scope.agent.syscheck.duration = ((new Date($scope.agent.syscheck.end) - new Date($scope.agent.syscheck.start))/1000)/60; + $scope.agent.syscheck.duration = Math.round($scope.agent.syscheck.duration * 100) / 100; + if($scope.agent.syscheck.duration <= 0){ + $scope.agent.syscheck.inProgress = true; + } + } else { + if (!$scope.agent.syscheck.end) { + $scope.agent.syscheck.end = 'Unknown'; + } + if (!$scope.agent.syscheck.start){ + $scope.agent.syscheck.start = 'Unknown'; } } + } - /** Prevents from double agent and come from autocomplete */ - let lastAgent = null; + /** Prevents from double agent and come from autocomplete */ + let lastAgent = null; - $scope.getAgent = async (newAgentId,fromAutocomplete) => { - try { - $rootScope.completedAgent = false; - if($scope.tab === 'configuration'){ - return $scope.getAgentConfig(newAgentId); - } + $scope.getAgent = async (newAgentId,fromAutocomplete) => { + try { + $rootScope.completedAgent = false; + if($scope.tab === 'configuration'){ + return $scope.getAgentConfig(newAgentId); + } - let id = null; + let id = null; - // They passed an id - if (newAgentId) { - id = newAgentId; + // They passed an id + if (newAgentId) { + id = newAgentId; + $location.search('agent', id); + } else { + if ($location.search().agent && !$rootScope.globalAgent) { // There's one in the url + id = $location.search().agent; + } else { // We pick the one in the rootScope + id = $rootScope.globalAgent; $location.search('agent', id); - } else { - if ($location.search().agent && !$rootScope.globalAgent) { // There's one in the url - id = $location.search().agent; - } else { // We pick the one in the rootScope - id = $rootScope.globalAgent; - $location.search('agent', id); - delete $rootScope.globalAgent; - } + delete $rootScope.globalAgent; } + } - if (id === '000' && $scope.tab === 'configuration') { - $scope.tab = 'general'; - $scope.switchTab('general'); - } + if (id === '000' && $scope.tab === 'configuration') { + $scope.tab = 'general'; + $scope.switchTab('general'); + } - const data = await Promise.all([ - apiReq.request('GET', `/agents/${id}`, {}), - apiReq.request('GET', `/syscheck/${id}/last_scan`, {}), - apiReq.request('GET', `/rootcheck/${id}/last_scan`, {}) - ]); - - // Agent - $scope.agent = data[0].data.data; - lastAgent = data[0].data.data.id; - if ($scope.agent.os) { - $scope.agentOS = $scope.agent.os.name + ' ' + $scope.agent.os.version; - } - else { $scope.agentOS = 'Unkwnown' }; + const data = await Promise.all([ + apiReq.request('GET', `/agents/${id}`, {}), + apiReq.request('GET', `/syscheck/${id}/last_scan`, {}), + apiReq.request('GET', `/rootcheck/${id}/last_scan`, {}) + ]); + + // Agent + $scope.agent = data[0].data.data; + lastAgent = data[0].data.data.id; + if ($scope.agent.os) { + $scope.agentOS = $scope.agent.os.name + ' ' + $scope.agent.os.version; + } + else { $scope.agentOS = 'Unkwnown' }; - // Syscheck - $scope.agent.syscheck = data[1].data.data; - validateSysCheck(); + // Syscheck + $scope.agent.syscheck = data[1].data.data; + validateSysCheck(); - // Rootcheck - $scope.agent.rootcheck = data[2].data.data; - validateRootCheck(); + // Rootcheck + $scope.agent.rootcheck = data[2].data.data; + validateRootCheck(); - $rootScope.completedAgent = true; - if(!$scope.$$phase) $scope.$digest(); - return; - } catch (error) { - errorHandler.handle(error,'Agents'); - if(!$rootScope.$$phase) $rootScope.$digest(); - } - }; - - $scope.goGroups = agent => { - $rootScope.globalAgent = agent; - $scope.agentsAutoComplete.reset(); - if($rootScope.ownHandlers) { - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; - $rootScope.comeFrom = 'agents'; - //$location.search('_a',null); - $location.search('tab', 'groups'); - $location.path('/manager'); - }; - - $scope.analyzeAgents = async search => { - try { - await $timeout(200); - $scope.agentsAutoComplete.filters = []; - await $scope.agentsAutoComplete.addFilter('search',search); + assignFilters($scope.tab,id) + $rootScope.completedAgent = true; - if(!$scope.$$phase) $scope.$digest(); - return $scope.agentsAutoComplete.items; - } catch (error) { - errorHandler.handle(error,'Agents'); - if(!$rootScope.$$phase) $rootScope.$digest(); + if(!$scope.$$phase) $scope.$digest(); + return; + } catch (error) { + errorHandler.handle(error,'Agents'); + if(!$rootScope.$$phase) $rootScope.$digest(); + } + }; + + $scope.goGroups = agent => { + $rootScope.globalAgent = agent; + $scope.agentsAutoComplete.reset(); + if($rootScope.ownHandlers) { + for(let h of $rootScope.ownHandlers){ + h._scope.$destroy(); } - } - - //Load + $rootScope.ownHandlers = []; + $rootScope.comeFrom = 'agents'; + //$location.search('_a',null); + $location.search('tab', 'groups'); + $location.path('/manager'); + }; + + $scope.analyzeAgents = async search => { try { - if($scope.tab !== 'configuration'){ - $scope.getAgent(); - } - $scope.agentsAutoComplete.nextPage(''); - } catch (e) { - errorHandler.handle('Unexpected exception loading controller','Agents'); + await $timeout(200); + $scope.agentsAutoComplete.filters = []; + await $scope.agentsAutoComplete.addFilter('search',search); + + if(!$scope.$$phase) $scope.$digest(); + return $scope.agentsAutoComplete.items; + } catch (error) { + errorHandler.handle(error,'Agents'); if(!$rootScope.$$phase) $rootScope.$digest(); } - //Destroy - $scope.$on("$destroy", () => { - $rootScope.rawVisualizations = null; - $scope.agentsAutoComplete.reset(); - if($rootScope.ownHandlers) { - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } + } + + //Load + try { + if($scope.tab !== 'configuration'){ + $scope.getAgent(); + } + $scope.agentsAutoComplete.nextPage(''); + } catch (e) { + errorHandler.handle('Unexpected exception loading controller','Agents'); + if(!$rootScope.$$phase) $rootScope.$digest(); + } + + //Destroy + $scope.$on("$destroy", () => { + $rootScope.rawVisualizations = null; + $scope.agentsAutoComplete.reset(); + if($rootScope.ownHandlers) { + for(let h of $rootScope.ownHandlers){ + h._scope.$destroy(); + } + } + $rootScope.ownHandlers = []; + }); + + //PCI tab + let tabs = []; + genericReq.request('GET', '/api/wazuh-api/pci/all') + .then((data) => { + for(let key in data.data){ + tabs.push({ + "title": key, + "content": data.data[key] + }); } - $rootScope.ownHandlers = []; + }) + .catch(error => { + errorHandler.handle(error,'Agents'); + if(!$rootScope.$$phase) $rootScope.$digest(); }); - //PCI tab - let tabs = []; - genericReq.request('GET', '/api/wazuh-api/pci/all') - .then((data) => { - for(let key in data.data){ - tabs.push({ - "title": key, - "content": data.data[key] - }); - } - }) - .catch(error => { - errorHandler.handle(error,'Agents'); - if(!$rootScope.$$phase) $rootScope.$digest(); - }); + $scope.tabs = tabs; + $scope.selectedIndex = 0; - $scope.tabs = tabs; - $scope.selectedIndex = 0; + $scope.isArray = angular.isArray; - $scope.isArray = angular.isArray; + const getAgent = newAgentId => { - const getAgent = newAgentId => { + // They passed an id + if (newAgentId) { + $location.search('agent', newAgentId); + } - // They passed an id - if (newAgentId) { - $location.search('agent', newAgentId); + }; + + $scope.getAgentConfig = newAgentId => { + getAgent(newAgentId); + firstLoad(); + } + + $scope.goGroup = () => { + $rootScope.globalAgent = $scope.agent; + $rootScope.comeFrom = 'agents'; + $location.search('tab', 'groups'); + $location.path('/manager'); + }; + + const firstLoad = async () => { + try{ + $rootScope.completedAgent = false; + $scope.configurationError = false; + $scope.load = true; + let id; + if ($location.search().agent && !$rootScope.globalAgent) { // There's one in the url + id = $location.search().agent; + } else { // We pick the one in the rootScope + id = $rootScope.globalAgent; + $location.search('agent', id); + delete $rootScope.globalAgent; } + let data = await apiReq.request('GET', `/agents/${id}`, {}); + $scope.agent = data.data.data; + $scope.groupName = $scope.agent.group; - }; + if(!$scope.groupName){ - $scope.getAgentConfig = newAgentId => { - getAgent(newAgentId); - firstLoad(); - } - - $scope.goGroup = () => { - $rootScope.globalAgent = $scope.agent; - $rootScope.comeFrom = 'agents'; - $location.search('tab', 'groups'); - $location.path('/manager'); - }; - - const firstLoad = async () => { - try{ - $rootScope.completedAgent = false; - $scope.configurationError = false; - $scope.load = true; - let id; - if ($location.search().agent && !$rootScope.globalAgent) { // There's one in the url - id = $location.search().agent; - } else { // We pick the one in the rootScope - id = $rootScope.globalAgent; - $location.search('agent', id); - delete $rootScope.globalAgent; - } - let data = await apiReq.request('GET', `/agents/${id}`, {}); - $scope.agent = data.data.data; - $scope.groupName = $scope.agent.group; - - if(!$scope.groupName){ - - $scope.configurationError = true; - $scope.load = false; - if($scope.agent.id === '000'){ - $scope.configurationError = false; - $scope.tab = 'general'; - $scope.switchTab('general'); - } - if(!$scope.$$phase) $scope.$digest(); - return; + $scope.configurationError = true; + $scope.load = false; + if($scope.agent.id === '000'){ + $scope.configurationError = false; + $scope.tab = 'general'; + $scope.switchTab('general'); } + if(!$scope.$$phase) $scope.$digest(); + return; + } - data = await apiReq.request('GET', `/agents/groups/${$scope.groupName}/configuration`, {}); - $scope.groupConfiguration = data.data.data.items[0]; - $scope.rawJSON = beautifier.prettyPrint(data.data.data.items); + data = await apiReq.request('GET', `/agents/groups/${$scope.groupName}/configuration`, {}); + $scope.groupConfiguration = data.data.data.items[0]; + $scope.rawJSON = beautifier.prettyPrint(data.data.data.items); - data = await Promise.all([ - apiReq.request('GET', `/agents/groups?search=${$scope.groupName}`, {}), - apiReq.request('GET', `/agents/groups/${$scope.groupName}`, {}) - ]); + data = await Promise.all([ + apiReq.request('GET', `/agents/groups?search=${$scope.groupName}`, {}), + apiReq.request('GET', `/agents/groups/${$scope.groupName}`, {}) + ]); - let filtered = data[0].data.data.items.filter(item => item.name === $scope.groupName); - $scope.groupMergedSum = (filtered.length) ? filtered[0].merged_sum : 'Unknown'; + let filtered = data[0].data.data.items.filter(item => item.name === $scope.groupName); + $scope.groupMergedSum = (filtered.length) ? filtered[0].merged_sum : 'Unknown'; - filtered = data[1].data.data.items.filter(item => item.id === $scope.agent.id); - $scope.agentMergedSum = (filtered.length) ? filtered[0].merged_sum : 'Unknown'; - $scope.isSynchronized = (($scope.agentMergedSum === $scope.groupMergedSum) && !([$scope.agentMergedSum,$scope.groupMergedSum].includes('Unknown')) ) ? true : false; + filtered = data[1].data.data.items.filter(item => item.id === $scope.agent.id); + $scope.agentMergedSum = (filtered.length) ? filtered[0].merged_sum : 'Unknown'; + $scope.isSynchronized = (($scope.agentMergedSum === $scope.groupMergedSum) && !([$scope.agentMergedSum,$scope.groupMergedSum].includes('Unknown')) ) ? true : false; - $scope.load = false; - $rootScope.completedAgent = true; - if(!$scope.$$phase) $scope.$digest(); - return; - } catch (error){ - errorHandler.handle(error,'Agents'); - if(!$rootScope.$$phase) $rootScope.$digest(); - } - } - /** End of agent configuration */ - if($scope.tab !== 'configuration'){ - $rootScope.rawVisualizations = null; - // Create visualizations for controller's first execution - genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - $rootScope.$broadcast('updateVis'); + $scope.load = false; + + assignFilters($scope.tab,$scope.agent.id) + $rootScope.completedAgent = true; - checkMetrics($scope.tab,'panels'); - }) - .catch(error => errorHandler.handle(error, 'Agents')); + if(!$scope.$$phase) $scope.$digest(); + return; + } catch (error){ + errorHandler.handle(error,'Agents'); + if(!$rootScope.$$phase) $rootScope.$digest(); } - }); + } + /** End of agent configuration */ + if($scope.tab !== 'configuration'){ + $rootScope.rawVisualizations = null; + // Create visualizations for controller's first execution + genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) + .then(data => { + $rootScope.rawVisualizations = data.data.raw; + // Render visualizations + $rootScope.$broadcast('updateVis'); + + checkMetrics($scope.tab,'panels'); + }) + .catch(error => errorHandler.handle(error, 'Agents')); + } +}); diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 11a80595c9..27e591dd74 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -92,9 +92,6 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap } else { // If tab doesn't exist, default it to 'general' $scope.tab = 'general'; $location.search('tab', 'general'); - - // Now we initialize the implicitFilter - $rootScope.currentImplicitFilter = ""; } // This object represents the number of visualizations per tab; used to show a progress bar diff --git a/public/kibana-integrations/kibana-filter-bar.js b/public/kibana-integrations/kibana-filter-bar.js index 9bebab50d2..d020ee5317 100644 --- a/public/kibana-integrations/kibana-filter-bar.js +++ b/public/kibana-integrations/kibana-filter-bar.js @@ -99,14 +99,6 @@ module.directive('filterBarW', function (Private, Promise, getAppState,$location if(queryFilter.getFilters().length) updateFilters(); }); - $scope.$watch('wazuhLoadFilters',() => { - if(queryFilter.getFilters().length) updateFilters(); - }) - - $rootScope.$watch('completedAgent',() => { - if(queryFilter.getFilters().length) updateFilters(); - }) - // when appState changes, update scope's state $scope.$watch(getAppState, function (appState) { $scope.state = appState; diff --git a/public/templates/agents/agents.head b/public/templates/agents/agents.head index 3658ae0728..01024dac71 100644 --- a/public/templates/agents/agents.head +++ b/public/templates/agents/agents.head @@ -74,7 +74,7 @@ - +
From 5fa9c67f2c4a46907d45e7df7e42a3b57146b85e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 11:02:48 +0200 Subject: [PATCH 06/25] Fix filters on agent (in progress) --- public/controllers/agents.js | 15 +++++++++------ public/controllers/overview.js | 6 ++++-- public/kibana-integrations/kibana-discover.js | 13 ++++++++++++- public/kibana-integrations/kibana-filter-bar.js | 5 +++++ public/templates/agents/agents.head | 2 +- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 982b1c7041..969c1fa8a2 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -17,7 +17,7 @@ const app = modules.get('app/wazuh', []); app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, $window) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); - + $rootScope.wzCurrentFilters = []; $rootScope.completedAgent = false; $rootScope.page = 'agents'; $scope.extensions = appState.getExtensions().extensions; @@ -186,7 +186,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $scope.switchTab = tab => { if ($scope.tab === tab) return; - assignFilters(tab); if(tab !== 'configuration') { $rootScope.rawVisualizations = null; // Create current tab visualizations @@ -221,7 +220,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $scope.$watch('tab', () => { $location.search('tab', $scope.tab); - $scope.tabView = 'panels'; if ($rootScope.ownHandlers) { @@ -235,9 +233,15 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r if($scope.tab === 'configuration'){ firstLoad(); + } else { + if($scope.agent) assignFilters($scope.tab,$scope.agent.id); } }); + $scope.$watch('agent',() => { + if($scope.agent) assignFilters($scope.tab,$scope.agent.id); + }) + // Agent data $scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "teal" : "red"; @@ -343,7 +347,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $scope.agent.rootcheck = data[2].data.data; validateRootCheck(); - assignFilters($scope.tab,id) $rootScope.completedAgent = true; if(!$scope.$$phase) $scope.$digest(); @@ -405,6 +408,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } } $rootScope.ownHandlers = []; + $rootScope.wzCurrentFilters = []; }); //PCI tab @@ -497,8 +501,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $scope.isSynchronized = (($scope.agentMergedSum === $scope.groupMergedSum) && !([$scope.agentMergedSum,$scope.groupMergedSum].includes('Unknown')) ) ? true : false; $scope.load = false; - - assignFilters($scope.tab,$scope.agent.id) + $rootScope.completedAgent = true; if(!$scope.$$phase) $scope.$digest(); diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 27e591dd74..9b91c7d2c3 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -17,7 +17,7 @@ const app = modules.get('app/wazuh', []); app.controller('overviewController', function ($scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq,$window) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); - + $rootScope.wzCurrentFilters = []; $rootScope.rawVisualizations = null; $rootScope.page = 'overview'; @@ -223,7 +223,7 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap // Switch tab $scope.switchTab = tab => { if ($scope.tab === tab) return; - assignFilters(tab); + $rootScope.rawVisualizations = null; // Create current tab visualizations @@ -271,6 +271,7 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap $rootScope.ownHandlers = []; $rootScope.loadedVisualizations = []; + assignFilters($scope.tab); }); $scope.$on('$destroy', () => { @@ -282,6 +283,7 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap } $rootScope.ownHandlers = []; + $rootScope.wzCurrentFilters = []; }); // Create visualizations for controller's first execution diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index ab720a8ac0..f38481804a 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -769,11 +769,21 @@ function discoverController( else shouldPersist.push(item) } + console.log('Debug 1') queryFilter.removeAll(); + console.log('Debug 2') + console.log('should',shouldPersist) const currentFilters = $rootScope.wzCurrentFilters; + console.log('current',currentFilters) const total = [...currentFilters,...shouldPersist] + console.log('total',total) queryFilter.addFilters(total) - .then(() => console.log('filters loaded')) + .then(() => { + if($rootScope.page && $rootScope.page === 'agents'){ + console.log('Assign random value') + $rootScope.wzWaitForAgent = new Date().toISOString(); + } + }) .catch(error => console.log(error.message || error)); } @@ -789,6 +799,7 @@ function discoverController( $rootScope.$watch('wzCurrentFilters', () => { console.log('---------------------------------------') console.log($rootScope.wzCurrentFilters); + if(!$rootScope.wzCurrentFilters || !$rootScope.wzCurrentFilters.length) return; console.log('---------------------------------------') loadFilters(); }); diff --git a/public/kibana-integrations/kibana-filter-bar.js b/public/kibana-integrations/kibana-filter-bar.js index d020ee5317..cb629ce73f 100644 --- a/public/kibana-integrations/kibana-filter-bar.js +++ b/public/kibana-integrations/kibana-filter-bar.js @@ -99,6 +99,11 @@ module.directive('filterBarW', function (Private, Promise, getAppState,$location if(queryFilter.getFilters().length) updateFilters(); }); + $rootScope.$watch('wzWaitForAgent',() => { + console.log('Debug wzWaitForAgent') + if(queryFilter.getFilters().length) updateFilters(); + }) + // when appState changes, update scope's state $scope.$watch(getAppState, function (appState) { $scope.state = appState; diff --git a/public/templates/agents/agents.head b/public/templates/agents/agents.head index 01024dac71..3658ae0728 100644 --- a/public/templates/agents/agents.head +++ b/public/templates/agents/agents.head @@ -74,7 +74,7 @@ - +
From 913c7c2c805b392f1ffee557b0807dac89fe5d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 17:12:10 +0200 Subject: [PATCH 07/25] Fix filters (in progress) --- public/controllers/agents.js | 63 +++++++++++-------- public/controllers/overview.js | 52 ++++++++------- public/kibana-integrations/kibana-discover.js | 62 +++++++----------- .../kibana-integrations/kibana-filter-bar.js | 11 +--- .../kibana-visualization.js | 1 - 5 files changed, 89 insertions(+), 100 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 969c1fa8a2..9f7ba50186 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -95,25 +95,32 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r let filters = [] const assignFilters = (tab,agent) => { - filters = []; - - filters.push(filterHandler.managerQuery( - appState.getClusterInfo().status == 'enabled' ? - appState.getClusterInfo().cluster : - appState.getClusterInfo().manager - )) - - if(tab !== 'general'){ - if(tab === 'pci') { - filters.push(filterHandler.pciQuery()) - } else { - filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group)); + try { + filters = []; + + filters.push(filterHandler.managerQuery( + appState.getClusterInfo().status == 'enabled' ? + appState.getClusterInfo().cluster : + appState.getClusterInfo().manager + )) + + if(tab !== 'general'){ + if(tab === 'pci') { + filters.push(filterHandler.pciQuery()) + } else { + filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group)); + } } + + filters.push(filterHandler.agentQuery(agent)); + $rootScope.$emit('wzEventFilters',{filters}); + if(!$rootScope.$$listenerCount['wzEventFilters']){ + $timeout(100) + .then(() => assignFilters(tab,agent)) + } + } catch (error){ + console.log(error.message || error) } - - filters.push(filterHandler.agentQuery(agent)); - $rootScope.wzCurrentFilters = filters; - if(!$rootScope.$$phase) $rootScope.$digest(); } const generateMetric = id => { @@ -185,7 +192,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r // Switch tab $scope.switchTab = tab => { if ($scope.tab === tab) return; - + assignFilters(tab, $scope.agent.id); if(tab !== 'configuration') { $rootScope.rawVisualizations = null; // Create current tab visualizations @@ -219,6 +226,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r }); $scope.$watch('tab', () => { + $location.search('tab', $scope.tab); $scope.tabView = 'panels'; @@ -233,14 +241,9 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r if($scope.tab === 'configuration'){ firstLoad(); - } else { - if($scope.agent) assignFilters($scope.tab,$scope.agent.id); } - }); - $scope.$watch('agent',() => { - if($scope.agent) assignFilters($scope.tab,$scope.agent.id); - }) + }); // Agent data $scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "teal" : "red"; @@ -320,6 +323,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } } + if (id === '000' && $scope.tab === 'configuration') { $scope.tab = 'general'; $scope.switchTab('general'); @@ -348,7 +352,8 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r validateRootCheck(); $rootScope.completedAgent = true; - + assignFilters($scope.tab, id); + //assignFilters($location.search().tab,$location.search().agent); if(!$scope.$$phase) $scope.$digest(); return; } catch (error) { @@ -358,6 +363,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r }; $scope.goGroups = agent => { + $rootScope.wzCurrentFilters = []; $rootScope.globalAgent = agent; $scope.agentsAutoComplete.reset(); if($rootScope.ownHandlers) { @@ -400,6 +406,8 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r //Destroy $scope.$on("$destroy", () => { + delete $rootScope.wzWaitForAgent; + $location.search('_a',null) $rootScope.rawVisualizations = null; $scope.agentsAutoComplete.reset(); if($rootScope.ownHandlers) { @@ -447,10 +455,10 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } $scope.goGroup = () => { + delete $rootScope.wzWaitForAgent; $rootScope.globalAgent = $scope.agent; $rootScope.comeFrom = 'agents'; - $location.search('tab', 'groups'); - $location.path('/manager'); + $location.path('/manager/groups'); }; const firstLoad = async () => { @@ -466,6 +474,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $location.search('agent', id); delete $rootScope.globalAgent; } + let data = await apiReq.request('GET', `/agents/${id}`, {}); $scope.agent = data.data.data; $scope.groupName = $scope.agent.group; diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 9b91c7d2c3..c13c03f559 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -15,9 +15,9 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('overviewController', function ($scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq,$window) { +app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq,$window) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); - $rootScope.wzCurrentFilters = []; + $rootScope.rawVisualizations = null; $rootScope.page = 'overview'; @@ -119,31 +119,38 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap aws : { group: 'amazon' }, virustotal: { group: 'virustotal' } }; - + let filters = [] const assignFilters = tab => { - filters = []; - - filters.push(filterHandler.managerQuery( - appState.getClusterInfo().status == 'enabled' ? - appState.getClusterInfo().cluster : - appState.getClusterInfo().manager - )) - - if(tab !== 'general'){ - if(tab === 'pci') { - filters.push(filterHandler.pciQuery()) - } else { - filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group)); + try{ + + filters = []; + + filters.push(filterHandler.managerQuery( + appState.getClusterInfo().status == 'enabled' ? + appState.getClusterInfo().cluster : + appState.getClusterInfo().manager + )) + + if(tab !== 'general'){ + if(tab === 'pci') { + filters.push(filterHandler.pciQuery()) + } else { + filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group)); + } } + $rootScope.$emit('wzEventFilters',{filters}); + if(!$rootScope.$$listenerCount['wzEventFilters']){ + $timeout(100) + .then(() => assignFilters(tab)) + } + } catch(error) { + console.log(error.message || error) } - - $rootScope.wzCurrentFilters = filters; - if(!$rootScope.$$phase) $rootScope.$digest(); } - assignFilters($scope.tab); + assignFilters($location.search().tab || 'general'); const generateMetric = id => { let html = $(id).html(); @@ -223,7 +230,7 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap // Switch tab $scope.switchTab = tab => { if ($scope.tab === tab) return; - + assignFilters(tab); $rootScope.rawVisualizations = null; // Create current tab visualizations @@ -271,10 +278,10 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap $rootScope.ownHandlers = []; $rootScope.loadedVisualizations = []; - assignFilters($scope.tab); }); $scope.$on('$destroy', () => { + $rootScope.wzEventFilters = []; $rootScope.rawVisualizations = null; if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { @@ -283,7 +290,6 @@ app.controller('overviewController', function ($scope, $location, $rootScope, ap } $rootScope.ownHandlers = []; - $rootScope.wzCurrentFilters = []; }); // Create visualizations for controller's first execution diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index f38481804a..d3592e57c6 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -115,7 +115,9 @@ function discoverController( timefilter, appState, $rootScope, - $location + $location, + getAppState, + globalState ) { const Vis = Private(VisProvider); @@ -757,53 +759,31 @@ function discoverController( ////////////////////////////////////////////////////// WAZUH ////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - const loadFilters = () => { - const filters = queryFilter.getFilters(); - const shouldPersist = []; - for(const item of filters){ - if((item && item.query && item.query.match && item.query.match['rule.groups']) || - (item && item.query && item.query.match && item.query.match['agent.id']) || - (item && item.query && item.query.match && item.query.match['manager.name']) || - (item && item.exists && item.exists.field && item.exists.field === 'rule.pci_dss')) - continue; - else - shouldPersist.push(item) + const loadFilters = wzCurrentFilters => { + const appState = getAppState(); + if(!appState || !globalState){ + $timeout(100) + .then(() => { + console.log('Awaiting app state...') + return loadFilters(wzCurrentFilters) + }) + } else { + $state.filters = []; + + queryFilter.addFilters(wzCurrentFilters) + .then(() => console.log('Filters loaded successfully')) + .catch(error => console.log(error.message || error)); } - console.log('Debug 1') - queryFilter.removeAll(); - console.log('Debug 2') - console.log('should',shouldPersist) - const currentFilters = $rootScope.wzCurrentFilters; - console.log('current',currentFilters) - const total = [...currentFilters,...shouldPersist] - console.log('total',total) - queryFilter.addFilters(total) - .then(() => { - if($rootScope.page && $rootScope.page === 'agents'){ - console.log('Assign random value') - $rootScope.wzWaitForAgent = new Date().toISOString(); - } - }) - .catch(error => console.log(error.message || error)); - } - // Getting the location from the url - $scope.tabView = $location.search().tabView; - $scope.tab = $location.search().tab; - if ($rootScope.page === 'agents') $scope.agentId = $location.search().agent; - // Initial loading of filters - //loadFilters(); + } // Watch for changes in the location - $rootScope.$watch('wzCurrentFilters', () => { - console.log('---------------------------------------') - console.log($rootScope.wzCurrentFilters); - if(!$rootScope.wzCurrentFilters || !$rootScope.wzCurrentFilters.length) return; - console.log('---------------------------------------') - loadFilters(); + $rootScope.$on('wzEventFilters', (evt,parameters) => { + loadFilters(parameters.filters); }); + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/public/kibana-integrations/kibana-filter-bar.js b/public/kibana-integrations/kibana-filter-bar.js index cb629ce73f..04970d8599 100644 --- a/public/kibana-integrations/kibana-filter-bar.js +++ b/public/kibana-integrations/kibana-filter-bar.js @@ -16,7 +16,7 @@ import { uiModules } from 'ui/modules'; import rison from 'rison' const module = uiModules.get('kibana'); -module.directive('filterBarW', function (Private, Promise, getAppState,$location, $rootScope) { +module.directive('filterBarW', function ($timeout, Private, Promise, getAppState,$location, $rootScope) { const mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); const mapFlattenAndWrapFilters = Private(FilterBarLibMapFlattenAndWrapFiltersProvider); const extractTimeFilter = Private(FilterBarLibExtractTimeFilterProvider); @@ -96,14 +96,9 @@ module.directive('filterBarW', function (Private, Promise, getAppState,$location // update the scope filter list on filter changes $scope.$listen(queryFilter, 'update', function () { - if(queryFilter.getFilters().length) updateFilters(); + updateFilters(); }); - $rootScope.$watch('wzWaitForAgent',() => { - console.log('Debug wzWaitForAgent') - if(queryFilter.getFilters().length) updateFilters(); - }) - // when appState changes, update scope's state $scope.$watch(getAppState, function (appState) { $scope.state = appState; @@ -162,7 +157,6 @@ module.directive('filterBarW', function (Private, Promise, getAppState,$location function updateFilters() { const filters = queryFilter.getFilters(); - if(!queryFilter.getFilters().length || ($rootScope.page === 'agents' && !$rootScope.completedAgent)) return; mapAndFlattenFilters(filters).then(function (results) { // used to display the current filters in the state $scope.filters = _.sortBy(results, function (filter) { @@ -170,6 +164,7 @@ module.directive('filterBarW', function (Private, Promise, getAppState,$location }); $scope.$emit('filterbar:updated'); }); + } updateFilters(); diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index 8ae51a0f8d..7be80a2ae7 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -35,7 +35,6 @@ const app = modules.get('apps/webinar_app', []) const myRender = raw => { if (raw && (($rootScope.discoverPendingUpdates && $rootScope.discoverPendingUpdates.length != 0) || $scope.visID.includes('Ruleset') ) ) { // There are pending updates from the discover (which is the one who owns the true app state) - if(!visualization && !rendered && !renderInProgress) { // There's no visualization object -> create it with proper filters renderInProgress = true; From e6d4e82bae96c8582e04ce99c58ec3d2da25d387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 18:32:10 +0200 Subject: [PATCH 08/25] Enabling Discover --- public/controllers/agents.js | 2 +- public/controllers/overview.js | 2 +- public/kibana-integrations/kibana-discover.js | 14 ++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 9f7ba50186..d64fe87f00 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -214,7 +214,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r // We watch the resultState provided by the discover $scope.$watch('tabView', () => { $location.search('tabView', $scope.tabView); - + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { h._scope.$destroy(); diff --git a/public/controllers/overview.js b/public/controllers/overview.js index c13c03f559..d274208580 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -252,7 +252,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo // Watch tabView $scope.$watch('tabView', () => { $location.search('tabView', $scope.tabView); - + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { h._scope.$destroy(); diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index d3592e57c6..eb39cbfdce 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -312,7 +312,7 @@ function discoverController( //////////////////////////////////////////////////////////////////////////// /////////////////////////////// WAZUH /////////////////////////////////// //////////////////////////////////////////////////////////////////////////// - + $rootScope.discoverPendingUpdates = []; $rootScope.discoverPendingUpdates.push($state.query, queryFilter.getFilters()); $rootScope.$broadcast('updateVis', $state.query, queryFilter.getFilters()); @@ -473,7 +473,8 @@ function discoverController( if ($state.query.language && $state.query.language !== query.language) { $state.filters = []; } - + const filters = queryFilter.getFilters(); + if(!filters || !filters.length) return; //////////////////////////////////////////////////////////////////////////// /////////////////////////////// WAZUH /////////////////////////////////// //////////////////////////////////////////////////////////////////////////// @@ -774,16 +775,17 @@ function discoverController( .then(() => console.log('Filters loaded successfully')) .catch(error => console.log(error.message || error)); } - - } - // Watch for changes in the location $rootScope.$on('wzEventFilters', (evt,parameters) => { loadFilters(parameters.filters); }); - + + $scope.tabView = $location.search('tabView') + $rootScope.$on('changeTabView',(evt,parameters) => { + $scope.tabView = parameters.tabView || 'panels' + }) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// From 514b87c2a2563f6b8efa709a69a028e3b22b4b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 18:53:27 +0200 Subject: [PATCH 09/25] Removing updateVis event from init load --- public/controllers/agents.js | 2 +- public/controllers/overview.js | 2 +- public/kibana-integrations/kibana-discover.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index d64fe87f00..20179c5447 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -528,7 +528,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r .then(data => { $rootScope.rawVisualizations = data.data.raw; // Render visualizations - $rootScope.$broadcast('updateVis'); + //$rootScope.$broadcast('updateVis'); checkMetrics($scope.tab,'panels'); }) diff --git a/public/controllers/overview.js b/public/controllers/overview.js index d274208580..f7ec3ecdd8 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -297,7 +297,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo .then(data => { $rootScope.rawVisualizations = data.data.raw; // Render visualizations - $rootScope.$broadcast('updateVis'); + //$rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, $scope.tabView); }) diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index eb39cbfdce..5adc968603 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -782,7 +782,7 @@ function discoverController( }); - $scope.tabView = $location.search('tabView') + $scope.tabView = $location.search().tabView || 'panels' $rootScope.$on('changeTabView',(evt,parameters) => { $scope.tabView = parameters.tabView || 'panels' }) From 1bcc55706f5cbb0dab576abdd1319694174507d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 14 May 2018 19:03:13 +0200 Subject: [PATCH 10/25] Fix agent discover --- public/kibana-integrations/kibana-discover.js | 1 + 1 file changed, 1 insertion(+) diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index 5adc968603..ace0328985 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -785,6 +785,7 @@ function discoverController( $scope.tabView = $location.search().tabView || 'panels' $rootScope.$on('changeTabView',(evt,parameters) => { $scope.tabView = parameters.tabView || 'panels' + $scope.updateQueryAndFetch($state.query); }) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// From a314d2564f2a3a3e5802158ef1e8a1a68d6f708c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 09:39:19 +0200 Subject: [PATCH 11/25] Removed rison dependency --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 007d64bed1..2c065a059f 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "lodash": "3.10.1", "needle": "^2.0.1", "node-cron": "^1.1.2", - "rison": "^0.1.1", "winston": "3.0.0-rc1" } } From 22dfeeb801ddca34e98eb45ccdc5f9467b69ab33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 09:39:35 +0200 Subject: [PATCH 12/25] Emit event only if we have tab and tabView already defined --- public/controllers/agents.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 20179c5447..5f3e588d58 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -527,10 +527,8 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - //$rootScope.$broadcast('updateVis'); - checkMetrics($scope.tab,'panels'); + if($location.search().tab && $location.search().tabView) $rootScope.$emit('updateVis') }) .catch(error => errorHandler.handle(error, 'Agents')); } From 560121b0d298d7aed6de8dcf747f0e192ef63a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 09:39:45 +0200 Subject: [PATCH 13/25] Added missing copyright --- public/controllers/filter-handler.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/public/controllers/filter-handler.js b/public/controllers/filter-handler.js index d239c17cad..c87a87e6f2 100644 --- a/public/controllers/filter-handler.js +++ b/public/controllers/filter-handler.js @@ -1,3 +1,14 @@ +/* + * Wazuh app - Filter handler class + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ export default class FilterHandler { constructor(pattern) { this.pattern = pattern; From 18356c2615d903d592c664c68d45cb8edaf35439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 09:40:07 +0200 Subject: [PATCH 14/25] Emit event only if we have tab and tabView already defined (Overview) --- public/controllers/overview.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/public/controllers/overview.js b/public/controllers/overview.js index f7ec3ecdd8..f95dd9fc5d 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -217,7 +217,6 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo genericReq.request('GET',`/api/wazuh-elastic/create-vis/overview-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { $rootScope.rawVisualizations = data.data.raw; - // Render visualizations $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) @@ -239,12 +238,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo $rootScope.rawVisualizations = data.data.raw; // Render visualizations $rootScope.$broadcast('updateVis'); - checkMetrics(tab, 'panels'); - - // Deleting app state traces in the url - //$location.search('_a', null); - }) .catch(error => errorHandler.handle(error, 'Overview')); }; @@ -296,10 +290,8 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo genericReq.request('GET',`/api/wazuh-elastic/create-vis/overview-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - //$rootScope.$broadcast('updateVis'); - checkMetrics($scope.tab, $scope.tabView); + if($location.search().tab && $location.search().tabView) $rootScope.$emit('updateVis') }) .catch(error => { errorHandler.handle(error, 'Overview'); From 2186573827bb8be73b53ab2f6f88ce70bacbbe01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 09:40:20 +0200 Subject: [PATCH 15/25] Removed useless code --- .gitignore | 7 +------ public/controllers/agents.js | 5 ----- public/kibana-integrations/kibana-discover.js | 11 +++++------ public/kibana-integrations/kibana-filter-bar.js | 5 ++--- public/kibana-integrations/kibana-visualization.js | 6 +++--- .../kibana-template/kibana-discover-template.html | 2 +- 6 files changed, 12 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index d602b4c03f..3f31152b8a 100644 --- a/.gitignore +++ b/.gitignore @@ -58,9 +58,4 @@ typings/ .yarn-integrity # dotenv environment variables file -.env - - -kibana/ - -*.zip \ No newline at end of file +.env \ No newline at end of file diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 5f3e588d58..22f8af3055 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -296,10 +296,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } } - /** Prevents from double agent and come from autocomplete */ - let lastAgent = null; - - $scope.getAgent = async (newAgentId,fromAutocomplete) => { try { $rootScope.completedAgent = false; @@ -337,7 +333,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r // Agent $scope.agent = data[0].data.data; - lastAgent = data[0].data.data.id; if ($scope.agent.os) { $scope.agentOS = $scope.agent.os.name + ' ' + $scope.agent.os.version; } diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index ace0328985..eb8b59cc1b 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -39,12 +39,11 @@ import 'ui/registry/doc_views.js'; import 'plugins/kbn_doc_views/kbn_doc_views.js'; import 'ui/tooltip/tooltip.js'; import moment from 'moment'; -import rison from 'rison-node'; import 'ui/pager_control'; import 'ui/pager'; import { UtilsBrushEventProvider } from 'ui/utils/brush_event'; import { documentationLinks } from 'ui/documentation_links/documentation_links'; -import chrome from 'ui/chrome' + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -315,7 +314,7 @@ function discoverController( $rootScope.discoverPendingUpdates = []; $rootScope.discoverPendingUpdates.push($state.query, queryFilter.getFilters()); - $rootScope.$broadcast('updateVis', $state.query, queryFilter.getFilters()); + $rootScope.$broadcast('updateVis'); $rootScope.$broadcast('fetch'); if($location.search().tab != 'configuration') { $rootScope.loadedVisualizations = []; @@ -473,14 +472,14 @@ function discoverController( if ($state.query.language && $state.query.language !== query.language) { $state.filters = []; } - const filters = queryFilter.getFilters(); - if(!filters || !filters.length) return; //////////////////////////////////////////////////////////////////////////// /////////////////////////////// WAZUH /////////////////////////////////// //////////////////////////////////////////////////////////////////////////// + const filters = queryFilter.getFilters(); + if(!filters || !filters.length) return; $rootScope.discoverPendingUpdates = []; $rootScope.discoverPendingUpdates.push($state.query, queryFilter.getFilters()); - $rootScope.$broadcast('updateVis', $state.query, queryFilter.getFilters()); + $rootScope.$broadcast('updateVis'); $rootScope.$broadcast('fetch'); //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// diff --git a/public/kibana-integrations/kibana-filter-bar.js b/public/kibana-integrations/kibana-filter-bar.js index 04970d8599..8be7eb9d4f 100644 --- a/public/kibana-integrations/kibana-filter-bar.js +++ b/public/kibana-integrations/kibana-filter-bar.js @@ -13,10 +13,10 @@ import { FilterBarLibChangeTimeFilterProvider } from 'ui/filter_bar/lib/change_t import { FilterBarQueryFilterProvider } from 'ui/filter_bar/query_filter'; import { compareFilters } from 'ui/filter_bar/lib/compare_filters'; import { uiModules } from 'ui/modules'; -import rison from 'rison' + const module = uiModules.get('kibana'); -module.directive('filterBarW', function ($timeout, Private, Promise, getAppState,$location, $rootScope) { +module.directive('filterBarW', function (Private, Promise, getAppState) { const mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); const mapFlattenAndWrapFilters = Private(FilterBarLibMapFlattenAndWrapFiltersProvider); const extractTimeFilter = Private(FilterBarLibExtractTimeFilterProvider); @@ -164,7 +164,6 @@ module.directive('filterBarW', function ($timeout, Private, Promise, getAppState }); $scope.$emit('filterbar:updated'); }); - } updateFilters(); diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index 7be80a2ae7..42259680c3 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -38,7 +38,7 @@ const app = modules.get('apps/webinar_app', []) if(!visualization && !rendered && !renderInProgress) { // There's no visualization object -> create it with proper filters renderInProgress = true; - const rawVis = raw.filter(item => item.id === $scope.visID); + const rawVis = raw.filter(item => item && item.id === $scope.visID); wzsavedVisualizations.get($scope.visID,rawVis[0]).then(savedObj => { originalImplicitFilter = savedObj.searchSource.get('query')['query']; visTitle = savedObj.vis.title; @@ -116,12 +116,12 @@ const app = modules.get('apps/webinar_app', []) }; // Listen for changes - $rootScope.$on('updateVis', function (event, query, filters) { + $rootScope.$on('updateVis', () => { if(!$rootScope.$$phase) $rootScope.$digest(); myRender($rootScope.rawVisualizations); }); - var renderComplete = function() { + const renderComplete = () => { rendered = true; if(typeof $rootScope.loadedVisualizations === 'undefined') $rootScope.loadedVisualizations = []; diff --git a/public/templates/kibana-template/kibana-discover-template.html b/public/templates/kibana-template/kibana-discover-template.html index cb87e43a20..8faf68c58f 100644 --- a/public/templates/kibana-template/kibana-discover-template.html +++ b/public/templates/kibana-template/kibana-discover-template.html @@ -5,7 +5,7 @@
-

+

Date: Tue, 15 May 2018 10:59:26 +0200 Subject: [PATCH 16/25] Removed unused array --- public/controllers/overview.js | 1 - 1 file changed, 1 deletion(-) diff --git a/public/controllers/overview.js b/public/controllers/overview.js index f95dd9fc5d..946fc90602 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -275,7 +275,6 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo }); $scope.$on('$destroy', () => { - $rootScope.wzEventFilters = []; $rootScope.rawVisualizations = null; if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { From 5a81ae8a09371309aa7b52c2549fee9caf9cb499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 11:02:21 +0200 Subject: [PATCH 17/25] Fix typo --- public/controllers/agents.js | 8 +------- public/controllers/overview.js | 1 - public/templates/agents/agents.head | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 22f8af3055..bb14c48865 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -17,7 +17,6 @@ const app = modules.get('app/wazuh', []); app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, $window) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); - $rootScope.wzCurrentFilters = []; $rootScope.completedAgent = false; $rootScope.page = 'agents'; $scope.extensions = appState.getExtensions().extensions; @@ -348,7 +347,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $rootScope.completedAgent = true; assignFilters($scope.tab, id); - //assignFilters($location.search().tab,$location.search().agent); + if(!$scope.$$phase) $scope.$digest(); return; } catch (error) { @@ -358,7 +357,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r }; $scope.goGroups = agent => { - $rootScope.wzCurrentFilters = []; $rootScope.globalAgent = agent; $scope.agentsAutoComplete.reset(); if($rootScope.ownHandlers) { @@ -401,7 +399,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r //Destroy $scope.$on("$destroy", () => { - delete $rootScope.wzWaitForAgent; $location.search('_a',null) $rootScope.rawVisualizations = null; $scope.agentsAutoComplete.reset(); @@ -411,7 +408,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } } $rootScope.ownHandlers = []; - $rootScope.wzCurrentFilters = []; }); //PCI tab @@ -450,7 +446,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } $scope.goGroup = () => { - delete $rootScope.wzWaitForAgent; $rootScope.globalAgent = $scope.agent; $rootScope.comeFrom = 'agents'; $location.path('/manager/groups'); @@ -523,7 +518,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r .then(data => { $rootScope.rawVisualizations = data.data.raw; checkMetrics($scope.tab,'panels'); - if($location.search().tab && $location.search().tabView) $rootScope.$emit('updateVis') }) .catch(error => errorHandler.handle(error, 'Agents')); } diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 946fc90602..68ae951c07 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -290,7 +290,6 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo .then(data => { $rootScope.rawVisualizations = data.data.raw; checkMetrics($scope.tab, $scope.tabView); - if($location.search().tab && $location.search().tabView) $rootScope.$emit('updateVis') }) .catch(error => { errorHandler.handle(error, 'Overview'); diff --git a/public/templates/agents/agents.head b/public/templates/agents/agents.head index 3658ae0728..d573130d40 100644 --- a/public/templates/agents/agents.head +++ b/public/templates/agents/agents.head @@ -74,7 +74,7 @@ - +
From 23f2c06d80a76830b6ec0c2556ef0037a9331e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 12:52:09 +0200 Subject: [PATCH 18/25] Removed tab and tabView watchers from Agents and Overview --- public/controllers/agents.js | 95 +++++++------------ public/controllers/overview.js | 82 +++++----------- public/kibana-integrations/kibana-discover.js | 1 - .../kibana-visualization.js | 2 +- .../kibana-discover-template.html | 2 +- 5 files changed, 60 insertions(+), 122 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index bb14c48865..09d510e234 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -168,18 +168,34 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } // Switch subtab - $scope.switchSubtab = subtab => { - if($scope.tabView === subtab) return; + $scope.switchSubtab = (subtab, force = false) => { + if($scope.tabView === subtab && !force) return; + + if ($rootScope.ownHandlers) { + for (let h of $rootScope.ownHandlers) { + h._scope.$destroy(); + } + } + $rootScope.ownHandlers = []; + + $rootScope.rawVisualizations = null; + + $location.search('tabView', subtab); + $scope.tabView = subtab; + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) + + assignFilters($scope.tab, $scope.agent.id); + if(subtab === 'panels' && $scope.tab !== 'configuration'){ + $rootScope.loadedVisualizations = []; + $rootScope.rawVisualizations = null; // Create current tab visualizations genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { $rootScope.rawVisualizations = data.data.raw; - // Render visualizations $rootScope.$broadcast('updateVis'); - checkMetrics($scope.tab, 'panels'); }) .catch(error => errorHandler.handle(error, 'Agents')); @@ -189,60 +205,19 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } // Switch tab - $scope.switchTab = tab => { - if ($scope.tab === tab) return; - assignFilters(tab, $scope.agent.id); - if(tab !== 'configuration') { - $rootScope.rawVisualizations = null; - // Create current tab visualizations - genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - $rootScope.$broadcast('updateVis'); + $scope.switchTab = (tab, force = false) => { + if ($scope.tab === tab && !force) return; - checkMetrics(tab, 'panels'); - - }) - .catch(error => errorHandler.handle(error, 'Agents')); - } - }; - - // Watchers - - // We watch the resultState provided by the discover - $scope.$watch('tabView', () => { - $location.search('tabView', $scope.tabView); - $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; - - $rootScope.loadedVisualizations = []; - }); - - $scope.$watch('tab', () => { - - $location.search('tab', $scope.tab); - $scope.tabView = 'panels'; - - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; - - $rootScope.loadedVisualizations = []; + $location.search('tab', tab); + $scope.tab = tab; if($scope.tab === 'configuration'){ firstLoad(); + } else { + $scope.switchSubtab('panels',force); } + }; - }); // Agent data $scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "teal" : "red"; @@ -346,7 +321,8 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r validateRootCheck(); $rootScope.completedAgent = true; - assignFilters($scope.tab, id); + + $scope.switchTab($scope.tab, true); if(!$scope.$$phase) $scope.$digest(); return; @@ -399,6 +375,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r //Destroy $scope.$on("$destroy", () => { + $rootScope.discoverPendingUpdates = []; $location.search('_a',null) $rootScope.rawVisualizations = null; $scope.agentsAutoComplete.reset(); @@ -503,6 +480,8 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $rootScope.completedAgent = true; + $scope.switchTab($scope.tab, true); + if(!$scope.$$phase) $scope.$digest(); return; } catch (error){ @@ -511,14 +490,4 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } } /** End of agent configuration */ - if($scope.tab !== 'configuration'){ - $rootScope.rawVisualizations = null; - // Create visualizations for controller's first execution - genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - checkMetrics($scope.tab,'panels'); - }) - .catch(error => errorHandler.handle(error, 'Agents')); - } }); diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 68ae951c07..e812c2d2f9 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -150,8 +150,6 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo } } - assignFilters($location.search().tab || 'general'); - const generateMetric = id => { let html = $(id).html(); if (typeof html !== 'undefined' && html.includes(' { - if ($scope.tabView === subtab) return; + $scope.switchSubtab = (subtab,force = false) => { + console.log($scope.tabView,subtab) + if ($scope.tabView === subtab && !force) return; + if ($rootScope.ownHandlers) { + for (let h of $rootScope.ownHandlers) { + h._scope.$destroy(); + } + } + $rootScope.ownHandlers = []; + + $rootScope.rawVisualizations = null; + + $location.search('tabView', subtab); + $scope.tabView = subtab; + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) + + assignFilters($scope.tab); if(subtab === 'panels'){ + $rootScope.loadedVisualizations = []; + $rootScope.rawVisualizations = null; // Create current tab visualizations @@ -227,54 +242,17 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo } // Switch tab - $scope.switchTab = tab => { - if ($scope.tab === tab) return; - assignFilters(tab); - $rootScope.rawVisualizations = null; - - // Create current tab visualizations - genericReq.request('GET',`/api/wazuh-elastic/create-vis/overview-${tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - // Render visualizations - $rootScope.$broadcast('updateVis'); - checkMetrics(tab, 'panels'); - }) - .catch(error => errorHandler.handle(error, 'Overview')); - }; - - // Watch tabView - $scope.$watch('tabView', () => { - $location.search('tabView', $scope.tabView); - $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; - - $rootScope.loadedVisualizations = []; - }); - - // Watch tab - $scope.$watch('tab', () => { + $scope.switchTab = (tab,force = false) => { + if ($scope.tab === tab && !force) return; $location.search('tab', $scope.tab); + $scope.tab = tab; - $scope.tabView = 'panels'; - - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; - - $rootScope.loadedVisualizations = []; - }); + $scope.switchSubtab('panels',true); + }; $scope.$on('$destroy', () => { + $rootScope.discoverPendingUpdates = []; $rootScope.rawVisualizations = null; if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { @@ -285,15 +263,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo $rootScope.ownHandlers = []; }); - // Create visualizations for controller's first execution - genericReq.request('GET',`/api/wazuh-elastic/create-vis/overview-${$scope.tab}/${appState.getCurrentPattern()}`) - .then(data => { - $rootScope.rawVisualizations = data.data.raw; - checkMetrics($scope.tab, $scope.tabView); - }) - .catch(error => { - errorHandler.handle(error, 'Overview'); - }); + $scope.switchTab($scope.tab,true); //PCI tab let tabs = []; diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index eb8b59cc1b..5cbd418668 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -311,7 +311,6 @@ function discoverController( //////////////////////////////////////////////////////////////////////////// /////////////////////////////// WAZUH /////////////////////////////////// //////////////////////////////////////////////////////////////////////////// - $rootScope.discoverPendingUpdates = []; $rootScope.discoverPendingUpdates.push($state.query, queryFilter.getFilters()); $rootScope.$broadcast('updateVis'); diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index 42259680c3..331b1d4174 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -33,7 +33,7 @@ const app = modules.get('apps/webinar_app', []) let renderInProgress = false; const myRender = raw => { - + console.log($scope.visID,$rootScope.discoverPendingUpdates) if (raw && (($rootScope.discoverPendingUpdates && $rootScope.discoverPendingUpdates.length != 0) || $scope.visID.includes('Ruleset') ) ) { // There are pending updates from the discover (which is the one who owns the true app state) if(!visualization && !rendered && !renderInProgress) { // There's no visualization object -> create it with proper filters renderInProgress = true; diff --git a/public/templates/kibana-template/kibana-discover-template.html b/public/templates/kibana-template/kibana-discover-template.html index 8faf68c58f..cb87e43a20 100644 --- a/public/templates/kibana-template/kibana-discover-template.html +++ b/public/templates/kibana-template/kibana-discover-template.html @@ -5,7 +5,7 @@
-

+

Date: Tue, 15 May 2018 13:01:27 +0200 Subject: [PATCH 19/25] Added rendered condition on all agent views --- public/templates/agents/agents-audit.html | 2 +- public/templates/agents/agents-fim.html | 2 +- public/templates/agents/agents-general.html | 2 +- public/templates/agents/agents-oscap.html | 2 +- public/templates/agents/agents-pci.html | 2 +- public/templates/agents/agents-pm.html | 2 +- public/templates/agents/agents-virustotal.html | 2 +- public/templates/agents/agents-vuls.html | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/public/templates/agents/agents-audit.html b/public/templates/agents/agents-audit.html index a91e37ed9a..cdc04f7691 100644 --- a/public/templates/agents/agents-audit.html +++ b/public/templates/agents/agents-audit.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-fim.html b/public/templates/agents/agents-fim.html index 9b3bc16e69..3abf5b6b11 100644 --- a/public/templates/agents/agents-fim.html +++ b/public/templates/agents/agents-fim.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-general.html b/public/templates/agents/agents-general.html index c74b16a74d..1d761276fb 100644 --- a/public/templates/agents/agents-general.html +++ b/public/templates/agents/agents-general.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-oscap.html b/public/templates/agents/agents-oscap.html index 724f274477..3fb1430542 100644 --- a/public/templates/agents/agents-oscap.html +++ b/public/templates/agents/agents-oscap.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-pci.html b/public/templates/agents/agents-pci.html index 1d2832476c..bc74cf5e15 100644 --- a/public/templates/agents/agents-pci.html +++ b/public/templates/agents/agents-pci.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-pm.html b/public/templates/agents/agents-pm.html index 72ffbcbaeb..5aca063e90 100644 --- a/public/templates/agents/agents-pm.html +++ b/public/templates/agents/agents-pm.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-virustotal.html b/public/templates/agents/agents-virustotal.html index 1d876ec4e9..ab55444adf 100644 --- a/public/templates/agents/agents-virustotal.html +++ b/public/templates/agents/agents-virustotal.html @@ -1,4 +1,4 @@ - + diff --git a/public/templates/agents/agents-vuls.html b/public/templates/agents/agents-vuls.html index 8aa6b2545d..c90abe6767 100644 --- a/public/templates/agents/agents-vuls.html +++ b/public/templates/agents/agents-vuls.html @@ -1,4 +1,4 @@ - + From 74c8fad7028dd33a94f2eab383ffa3b32d55e74d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 13:23:32 +0200 Subject: [PATCH 20/25] Added new factory to store visualizations raw instead use rootScope --- public/controllers/agents.js | 10 ++--- public/controllers/overview.js | 13 +++--- public/factories/index.js | 3 +- public/factories/raw-visualizations.js | 41 +++++++++++++++++++ .../kibana-visualization.js | 8 ++-- 5 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 public/factories/raw-visualizations.js diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 09d510e234..a3837afcdd 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -15,7 +15,7 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, $window) { +app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); $rootScope.completedAgent = false; $rootScope.page = 'agents'; @@ -178,7 +178,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } $rootScope.ownHandlers = []; - $rootScope.rawVisualizations = null; + rawVisualizations.removeAll(); $location.search('tabView', subtab); $scope.tabView = subtab; @@ -189,12 +189,12 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r if(subtab === 'panels' && $scope.tab !== 'configuration'){ $rootScope.loadedVisualizations = []; - $rootScope.rawVisualizations = null; + rawVisualizations.removeAll(); // Create current tab visualizations genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { - $rootScope.rawVisualizations = data.data.raw; + rawVisualizations.assignItems(data.data.raw); $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) @@ -377,7 +377,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $scope.$on("$destroy", () => { $rootScope.discoverPendingUpdates = []; $location.search('_a',null) - $rootScope.rawVisualizations = null; + rawVisualizations.removeAll(); $scope.agentsAutoComplete.reset(); if($rootScope.ownHandlers) { for(let h of $rootScope.ownHandlers){ diff --git a/public/controllers/overview.js b/public/controllers/overview.js index e812c2d2f9..d377bdc86c 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -15,10 +15,10 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq,$window) { +app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); - $rootScope.rawVisualizations = null; + rawVisualizations.removeAll(); $rootScope.page = 'overview'; $scope.extensions = appState.getExtensions().extensions; @@ -215,8 +215,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo } $rootScope.ownHandlers = []; - $rootScope.rawVisualizations = null; - + rawVisualizations.removeAll(); $location.search('tabView', subtab); $scope.tabView = subtab; @@ -226,12 +225,12 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo if(subtab === 'panels'){ $rootScope.loadedVisualizations = []; - $rootScope.rawVisualizations = null; + rawVisualizations.removeAll(); // Create current tab visualizations genericReq.request('GET',`/api/wazuh-elastic/create-vis/overview-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { - $rootScope.rawVisualizations = data.data.raw; + rawVisualizations.assignItems(data.data.raw); $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) @@ -253,7 +252,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo $scope.$on('$destroy', () => { $rootScope.discoverPendingUpdates = []; - $rootScope.rawVisualizations = null; + rawVisualizations.removeAll() if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { h._scope.$destroy(); diff --git a/public/factories/index.js b/public/factories/index.js index e93c8f07c8..dfefacab98 100644 --- a/public/factories/index.js +++ b/public/factories/index.js @@ -9,4 +9,5 @@ * * Find more information about this on the LICENSE file. */ -import 'plugins/wazuh/factories/data-handler-composer'; \ No newline at end of file +import 'plugins/wazuh/factories/data-handler-composer'; +import 'plugins/wazuh/factories/raw-visualizations'; \ No newline at end of file diff --git a/public/factories/raw-visualizations.js b/public/factories/raw-visualizations.js new file mode 100644 index 0000000000..6cd940dcac --- /dev/null +++ b/public/factories/raw-visualizations.js @@ -0,0 +1,41 @@ +/* + * Wazuh app - Factory to store visualizations raw content + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ +import * as modules from 'ui/modules' + +const app = modules.get('app/wazuh', []); + +app.factory('rawVisualizations', function() { + let list = []; + + const addItem = item => { + list.push(item); + } + + const assignItems = items => { + list = Array.isArray(items) ? items : []; + } + + const getList = () => { + return list; + } + + const removeAll = () => { + list = []; + } + + return { + addItem : addItem, + getList : getList, + removeAll : removeAll, + assignItems: assignItems + }; +}); \ No newline at end of file diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index 331b1d4174..9d0c83a5b6 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -21,7 +21,7 @@ const app = modules.get('apps/webinar_app', []) visID: '=visId', specificTimeRange: '=specificTimeRange' }, - controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler,Private) { + controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations) { if(!$rootScope.ownHandlers) $rootScope.ownHandlers = []; let originalImplicitFilter = ''; let implicitFilter = ''; @@ -33,7 +33,6 @@ const app = modules.get('apps/webinar_app', []) let renderInProgress = false; const myRender = raw => { - console.log($scope.visID,$rootScope.discoverPendingUpdates) if (raw && (($rootScope.discoverPendingUpdates && $rootScope.discoverPendingUpdates.length != 0) || $scope.visID.includes('Ruleset') ) ) { // There are pending updates from the discover (which is the one who owns the true app state) if(!visualization && !rendered && !renderInProgress) { // There's no visualization object -> create it with proper filters renderInProgress = true; @@ -118,7 +117,10 @@ const app = modules.get('apps/webinar_app', []) // Listen for changes $rootScope.$on('updateVis', () => { if(!$rootScope.$$phase) $rootScope.$digest(); - myRender($rootScope.rawVisualizations); + const rawVis = rawVisualizations.getList(); + if(Array.isArray(rawVis) && rawVis.length){ + myRender(rawVis); + } }); const renderComplete = () => { From 075cf1aefdedb9e58c3e43cfd52013c5512ae303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 14:01:26 +0200 Subject: [PATCH 21/25] Added loaded and tabs factories --- public/controllers/agents.js | 10 ++-- public/controllers/overview.js | 14 +++--- public/factories/index.js | 4 +- public/factories/loaded-visualizations.js | 37 +++++++++++++++ public/factories/tab-visualizations.js | 46 +++++++++++++++++++ public/kibana-integrations/kibana-discover.js | 7 +-- .../kibana-visualization.js | 10 ++-- 7 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 public/factories/loaded-visualizations.js create mode 100644 public/factories/tab-visualizations.js diff --git a/public/controllers/agents.js b/public/controllers/agents.js index a3837afcdd..8ac73f086e 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -15,7 +15,7 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations) { +app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations, loadedVisualizations, tabVisualizations) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); $rootScope.completedAgent = false; $rootScope.page = 'agents'; @@ -68,7 +68,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r virusTotal : '[vis-id="\'Wazuh-App-Agents-Virustotal-Total\'"]' } - $rootScope.tabVisualizations = { + tabVisualizations.assign({ general : 7, fim : 8, pm : 4, @@ -78,7 +78,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r pci : 3, virustotal : 6, configuration: 0 - }; + }); // Object for matching nav items and Wazuh groups const tabFilters = { @@ -187,7 +187,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r assignFilters($scope.tab, $scope.agent.id); if(subtab === 'panels' && $scope.tab !== 'configuration'){ - $rootScope.loadedVisualizations = []; + loadedVisualizations.removeAll(); rawVisualizations.removeAll(); @@ -206,6 +206,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r // Switch tab $scope.switchTab = (tab, force = false) => { + tabVisualizations.setTab(tab); if ($scope.tab === tab && !force) return; $location.search('tab', tab); @@ -378,6 +379,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $rootScope.discoverPendingUpdates = []; $location.search('_a',null) rawVisualizations.removeAll(); + tabVisualizations.removeAll(); $scope.agentsAutoComplete.reset(); if($rootScope.ownHandlers) { for(let h of $rootScope.ownHandlers){ diff --git a/public/controllers/overview.js b/public/controllers/overview.js index d377bdc86c..3c0fe33b0a 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -15,7 +15,7 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations) { +app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations, loadedVisualizations, tabVisualizations) { const filterHandler = new FilterHandler(appState.getCurrentPattern()); rawVisualizations.removeAll(); @@ -95,17 +95,17 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo } // This object represents the number of visualizations per tab; used to show a progress bar - $rootScope.tabVisualizations = { + tabVisualizations.assign({ general : 11, fim : 10, pm : 5, vuls : 8, oscap : 14, audit : 15, - pci : 6, + pci : 7, aws : 10, virustotal: 7 - }; + }); // Object for matching nav items and rules groups const tabFilters = { @@ -223,7 +223,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo assignFilters($scope.tab); if(subtab === 'panels'){ - $rootScope.loadedVisualizations = []; + loadedVisualizations.removeAll(); rawVisualizations.removeAll(); @@ -242,6 +242,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo // Switch tab $scope.switchTab = (tab,force = false) => { + tabVisualizations.setTab(tab); if ($scope.tab === tab && !force) return; $location.search('tab', $scope.tab); @@ -252,7 +253,8 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo $scope.$on('$destroy', () => { $rootScope.discoverPendingUpdates = []; - rawVisualizations.removeAll() + rawVisualizations.removeAll(); + tabVisualizations.removeAll(); if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { h._scope.$destroy(); diff --git a/public/factories/index.js b/public/factories/index.js index dfefacab98..9799397414 100644 --- a/public/factories/index.js +++ b/public/factories/index.js @@ -10,4 +10,6 @@ * Find more information about this on the LICENSE file. */ import 'plugins/wazuh/factories/data-handler-composer'; -import 'plugins/wazuh/factories/raw-visualizations'; \ No newline at end of file +import 'plugins/wazuh/factories/raw-visualizations'; +import 'plugins/wazuh/factories/loaded-visualizations'; +import 'plugins/wazuh/factories/tab-visualizations'; \ No newline at end of file diff --git a/public/factories/loaded-visualizations.js b/public/factories/loaded-visualizations.js new file mode 100644 index 0000000000..495eb2dd2c --- /dev/null +++ b/public/factories/loaded-visualizations.js @@ -0,0 +1,37 @@ +/* + * Wazuh app - Factory to store loaded visualizations + * + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ +import * as modules from 'ui/modules' + +const app = modules.get('app/wazuh', []); + +app.factory('loadedVisualizations', function() { + let list = []; + + const addItem = item => { + list.push(item); + } + + const getList = () => { + return list; + } + + const removeAll = () => { + list = []; + } + + return { + addItem : addItem, + getList : getList, + removeAll : removeAll + }; +}); \ No newline at end of file diff --git a/public/factories/tab-visualizations.js b/public/factories/tab-visualizations.js new file mode 100644 index 0000000000..da670e4057 --- /dev/null +++ b/public/factories/tab-visualizations.js @@ -0,0 +1,46 @@ +/* + * Wazuh app - Factory to store loaded visualizations + * + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ +import * as modules from 'ui/modules' + +const app = modules.get('app/wazuh', []); + +app.factory('tabVisualizations', function() { + let tabVisualizations = {} + let currentTab = ''; + + const setTab = tab => currentTab = tab; + + const getTab = () => { + return currentTab; + } + + const getItem = item => { + return tabVisualizations[item] + } + + const assign = tabs => { + tabVisualizations = tabs; + } + + const removeAll = () => { + tabVisualizations = {}; + } + + return { + getItem : getItem, + removeAll: removeAll, + assign : assign, + setTab : setTab, + getTab : getTab + }; +}); \ No newline at end of file diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index 5cbd418668..18d4a8f98a 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -116,7 +116,8 @@ function discoverController( $rootScope, $location, getAppState, - globalState + globalState, + loadedVisualizations ) { const Vis = Private(VisProvider); @@ -316,7 +317,7 @@ function discoverController( $rootScope.$broadcast('updateVis'); $rootScope.$broadcast('fetch'); if($location.search().tab != 'configuration') { - $rootScope.loadedVisualizations = []; + loadedVisualizations.removeAll(); $rootScope.rendered = false; $rootScope.loadingStatus = "Fetching data..."; } @@ -618,7 +619,7 @@ function discoverController( /////////////////////////////// WAZUH /////////////////////////////////// //////////////////////////////////////////////////////////////////////////// if($location.search().tab != 'configuration') { - $rootScope.loadedVisualizations = []; + loadedVisualizations.removeAll(); $rootScope.rendered = false; $rootScope.loadingStatus = "Fetching data..."; } diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index 9d0c83a5b6..3966e843a1 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -21,7 +21,7 @@ const app = modules.get('apps/webinar_app', []) visID: '=visId', specificTimeRange: '=specificTimeRange' }, - controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations) { + controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations, loadedVisualizations, tabVisualizations) { if(!$rootScope.ownHandlers) $rootScope.ownHandlers = []; let originalImplicitFilter = ''; let implicitFilter = ''; @@ -125,10 +125,10 @@ const app = modules.get('apps/webinar_app', []) const renderComplete = () => { rendered = true; - - if(typeof $rootScope.loadedVisualizations === 'undefined') $rootScope.loadedVisualizations = []; - $rootScope.loadedVisualizations.push(true); - let currentCompleted = Math.round(($rootScope.loadedVisualizations.length / $rootScope.tabVisualizations[$location.search().tab]) * 100); + + loadedVisualizations.addItem(true); + + let currentCompleted = Math.round((loadedVisualizations.getList().length / tabVisualizations.getItem(tabVisualizations.getTab())) * 100); $rootScope.loadingStatus = `Rendering visualizations... ${currentCompleted > 100 ? 100 : currentCompleted} %`; if (currentCompleted >= 100) { From a22da5f840d740aa0b5bf2155283efef4b256148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 16:11:35 +0200 Subject: [PATCH 22/25] Added discover pending updates factory --- public/controllers/agents.js | 34 +++++++++-------- public/controllers/overview.js | 25 ++++++++----- public/factories/discover-pending-updates.js | 37 +++++++++++++++++++ public/factories/index.js | 3 +- public/factories/tab-visualizations.js | 2 +- public/kibana-integrations/kibana-discover.js | 12 +++--- .../kibana-visualization.js | 21 ++++++----- 7 files changed, 92 insertions(+), 42 deletions(-) create mode 100644 public/factories/discover-pending-updates.js diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 8ac73f086e..13846fdca0 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -15,8 +15,14 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('agentsController', function ($timeout, $scope, $location, $q, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations, loadedVisualizations, tabVisualizations) { +app.controller('agentsController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates) { + $location.search('_a',null) const filterHandler = new FilterHandler(appState.getCurrentPattern()); + discoverPendingUpdates.removeAll(); + rawVisualizations.removeAll(); + tabVisualizations.removeAll(); + loadedVisualizations.removeAll(); + $rootScope.completedAgent = false; $rootScope.page = 'agents'; $scope.extensions = appState.getExtensions().extensions; @@ -95,6 +101,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r let filters = [] const assignFilters = (tab,agent) => { try { + console.log(agent) filters = []; filters.push(filterHandler.managerQuery( @@ -178,23 +185,20 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r } $rootScope.ownHandlers = []; + discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); + loadedVisualizations.removeAll(); $location.search('tabView', subtab); $scope.tabView = subtab; - $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) - - assignFilters($scope.tab, $scope.agent.id); if(subtab === 'panels' && $scope.tab !== 'configuration'){ - loadedVisualizations.removeAll(); - - rawVisualizations.removeAll(); - // Create current tab visualizations genericReq.request('GET',`/api/wazuh-elastic/create-vis/agents-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { rawVisualizations.assignItems(data.data.raw); + assignFilters($scope.tab, $scope.agent.id); + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) @@ -215,7 +219,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r if($scope.tab === 'configuration'){ firstLoad(); } else { - $scope.switchSubtab('panels',force); + $scope.switchSubtab('panels', true); } }; @@ -297,7 +301,8 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r if (id === '000' && $scope.tab === 'configuration') { $scope.tab = 'general'; - $scope.switchTab('general'); + discoverPendingUpdates.removeAll(); + $scope.switchTab('general', true); } const data = await Promise.all([ @@ -376,10 +381,10 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r //Destroy $scope.$on("$destroy", () => { - $rootScope.discoverPendingUpdates = []; - $location.search('_a',null) + discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); tabVisualizations.removeAll(); + loadedVisualizations.removeAll(); $scope.agentsAutoComplete.reset(); if($rootScope.ownHandlers) { for(let h of $rootScope.ownHandlers){ @@ -455,7 +460,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r if($scope.agent.id === '000'){ $scope.configurationError = false; $scope.tab = 'general'; - $scope.switchTab('general'); + $scope.switchTab('general', true); } if(!$scope.$$phase) $scope.$digest(); return; @@ -481,8 +486,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $q, $r $scope.load = false; $rootScope.completedAgent = true; - - $scope.switchTab($scope.tab, true); + if($scope.tab !== 'configuration') $scope.switchTab($scope.tab, true); if(!$scope.$$phase) $scope.$digest(); return; diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 3c0fe33b0a..d0449b65a7 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -15,10 +15,13 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations, loadedVisualizations, tabVisualizations) { +app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates) { + $location.search('_a',null) const filterHandler = new FilterHandler(appState.getCurrentPattern()); - + discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); + tabVisualizations.removeAll(); + loadedVisualizations.removeAll(); $rootScope.page = 'overview'; $scope.extensions = appState.getExtensions().extensions; @@ -206,42 +209,43 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo // Switch subtab $scope.switchSubtab = (subtab,force = false) => { - console.log($scope.tabView,subtab) if ($scope.tabView === subtab && !force) return; + if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { h._scope.$destroy(); + console.log(h._scope) } } $rootScope.ownHandlers = []; + discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); + loadedVisualizations.removeAll(); $location.search('tabView', subtab); $scope.tabView = subtab; - $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) - assignFilters($scope.tab); if(subtab === 'panels'){ - loadedVisualizations.removeAll(); - - rawVisualizations.removeAll(); - // Create current tab visualizations genericReq.request('GET',`/api/wazuh-elastic/create-vis/overview-${$scope.tab}/${appState.getCurrentPattern()}`) .then(data => { rawVisualizations.assignItems(data.data.raw); + assignFilters($scope.tab); + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) .catch(error => errorHandler.handle(error, 'Overview')); } else { + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) checkMetrics($scope.tab, subtab); } } // Switch tab $scope.switchTab = (tab,force = false) => { + console.log('Entering switchTab',$scope.tab,tab) tabVisualizations.setTab(tab); if ($scope.tab === tab && !force) return; @@ -252,9 +256,10 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo }; $scope.$on('$destroy', () => { - $rootScope.discoverPendingUpdates = []; + discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); tabVisualizations.removeAll(); + loadedVisualizations.removeAll(); if ($rootScope.ownHandlers) { for (let h of $rootScope.ownHandlers) { h._scope.$destroy(); diff --git a/public/factories/discover-pending-updates.js b/public/factories/discover-pending-updates.js new file mode 100644 index 0000000000..9010a7e2cc --- /dev/null +++ b/public/factories/discover-pending-updates.js @@ -0,0 +1,37 @@ +/* + * Wazuh app - Factory to store pending updates from Discover + * + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ +import * as modules from 'ui/modules' + +const app = modules.get('app/wazuh', []); + +app.factory('discoverPendingUpdates', function() { + let pendingUpdates = []; + + const addItem = (query, filters) => { + pendingUpdates.push(query, filters); + } + + const getList = () => { + return pendingUpdates; + } + + const removeAll = () => { + pendingUpdates = []; + } + + return { + addItem : addItem, + getList : getList, + removeAll : removeAll + }; +}); \ No newline at end of file diff --git a/public/factories/index.js b/public/factories/index.js index 9799397414..dcfe7d427e 100644 --- a/public/factories/index.js +++ b/public/factories/index.js @@ -12,4 +12,5 @@ import 'plugins/wazuh/factories/data-handler-composer'; import 'plugins/wazuh/factories/raw-visualizations'; import 'plugins/wazuh/factories/loaded-visualizations'; -import 'plugins/wazuh/factories/tab-visualizations'; \ No newline at end of file +import 'plugins/wazuh/factories/tab-visualizations'; +import 'plugins/wazuh/factories/discover-pending-updates'; \ No newline at end of file diff --git a/public/factories/tab-visualizations.js b/public/factories/tab-visualizations.js index da670e4057..21af9d2780 100644 --- a/public/factories/tab-visualizations.js +++ b/public/factories/tab-visualizations.js @@ -1,5 +1,5 @@ /* - * Wazuh app - Factory to store loaded visualizations + * Wazuh app - Factory to store visualization tabs * * Copyright (C) 2018 Wazuh, Inc. * diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index 18d4a8f98a..e5ecf83767 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -117,7 +117,8 @@ function discoverController( $location, getAppState, globalState, - loadedVisualizations + loadedVisualizations, + discoverPendingUpdates ) { const Vis = Private(VisProvider); @@ -312,8 +313,8 @@ function discoverController( //////////////////////////////////////////////////////////////////////////// /////////////////////////////// WAZUH /////////////////////////////////// //////////////////////////////////////////////////////////////////////////// - $rootScope.discoverPendingUpdates = []; - $rootScope.discoverPendingUpdates.push($state.query, queryFilter.getFilters()); + discoverPendingUpdates.removeAll() + discoverPendingUpdates.addItem($state.query,queryFilter.getFilters()); $rootScope.$broadcast('updateVis'); $rootScope.$broadcast('fetch'); if($location.search().tab != 'configuration') { @@ -477,8 +478,8 @@ function discoverController( //////////////////////////////////////////////////////////////////////////// const filters = queryFilter.getFilters(); if(!filters || !filters.length) return; - $rootScope.discoverPendingUpdates = []; - $rootScope.discoverPendingUpdates.push($state.query, queryFilter.getFilters()); + discoverPendingUpdates.removeAll() + discoverPendingUpdates.addItem($state.query,queryFilter.getFilters()); $rootScope.$broadcast('updateVis'); $rootScope.$broadcast('fetch'); //////////////////////////////////////////////////////////////////////////// @@ -760,6 +761,7 @@ function discoverController( /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// const loadFilters = wzCurrentFilters => { + console.log(wzCurrentFilters) const appState = getAppState(); if(!appState || !globalState){ $timeout(100) diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index 3966e843a1..b3a6a633b6 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -21,7 +21,7 @@ const app = modules.get('apps/webinar_app', []) visID: '=visId', specificTimeRange: '=specificTimeRange' }, - controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations, loadedVisualizations, tabVisualizations) { + controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates) { if(!$rootScope.ownHandlers) $rootScope.ownHandlers = []; let originalImplicitFilter = ''; let implicitFilter = ''; @@ -33,7 +33,8 @@ const app = modules.get('apps/webinar_app', []) let renderInProgress = false; const myRender = raw => { - if (raw && (($rootScope.discoverPendingUpdates && $rootScope.discoverPendingUpdates.length != 0) || $scope.visID.includes('Ruleset') ) ) { // There are pending updates from the discover (which is the one who owns the true app state) + if (raw && discoverPendingUpdates.getList().length) { // There are pending updates from the discover (which is the one who owns the true app state) + const discoverList = discoverPendingUpdates.getList(); if(!visualization && !rendered && !renderInProgress) { // There's no visualization object -> create it with proper filters renderInProgress = true; @@ -46,21 +47,21 @@ const app = modules.get('apps/webinar_app', []) // There's an original filter if (originalImplicitFilter.length > 0 ) { // And also a pending one -> concatenate them - if ($rootScope.discoverPendingUpdates && typeof $rootScope.discoverPendingUpdates[0].query === 'string' && $rootScope.discoverPendingUpdates[0].query.length > 0) { - implicitFilter = originalImplicitFilter + ' AND ' + $rootScope.discoverPendingUpdates[0].query; + if (typeof discoverList[0].query === 'string' && discoverList[0].query.length > 0) { + implicitFilter = originalImplicitFilter + ' AND ' + discoverList[0].query; } else { // Only the original filter implicitFilter = originalImplicitFilter; } } else { // Other case, use the pending one, if it is empty, it won't matter - implicitFilter = $rootScope.discoverPendingUpdates ? $rootScope.discoverPendingUpdates[0].query : ''; + implicitFilter = discoverList ? discoverList[0].query : ''; } if (visTitle !== 'Wazuh App Overview General Agents status') { // We don't want to filter that visualization as it uses another index-pattern visualization.searchSource .query({ language: 'lucene', query: implicitFilter }) - .set('filter', $rootScope.discoverPendingUpdates ? $rootScope.discoverPendingUpdates[1] : {}); + .set('filter', discoverList.length > 1 ? discoverList[1] : {}); } let params = {}; @@ -94,21 +95,21 @@ const app = modules.get('apps/webinar_app', []) // There's an original filter if (originalImplicitFilter.length > 0 ) { // And also a pending one -> concatenate them - if ($rootScope.discoverPendingUpdates && typeof $rootScope.discoverPendingUpdates[0].query === 'string' && $rootScope.discoverPendingUpdates[0].query.length > 0) { - implicitFilter = originalImplicitFilter + ' AND ' + $rootScope.discoverPendingUpdates[0].query; + if (discoverList[0].query === 'string' && discoverList[0].query.length > 0) { + implicitFilter = originalImplicitFilter + ' AND ' + discoverList[0].query; } else { // Only the original filter implicitFilter = originalImplicitFilter; } } else { // Other case, use the pending one, if it is empty, it won't matter - implicitFilter = $rootScope.discoverPendingUpdates ? $rootScope.discoverPendingUpdates[0].query : ''; + implicitFilter = discoverList ? discoverList[0].query : ''; } if (visTitle !== 'Wazuh App Overview General Agents status') { // We don't want to filter that visualization as it uses another index-pattern visualization.searchSource .query({ language: 'lucene', query: implicitFilter }) - .set('filter', $rootScope.discoverPendingUpdates ? $rootScope.discoverPendingUpdates[1] : {}); + .set('filter', discoverList.length > 1 ? discoverList[1] : {}); } } } From 6e6f5727d482cd3f45bbade1576570b5d742f692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 16:21:44 +0200 Subject: [PATCH 23/25] Vis handlers factory has been added --- public/controllers/agents.js | 25 +++-------- public/controllers/groups.js | 13 +----- public/controllers/manager.js | 21 ++-------- public/controllers/osseclog.js | 7 +--- public/controllers/overview.js | 19 ++------- public/factories/index.js | 3 +- public/factories/vis-handlers.js | 42 +++++++++++++++++++ .../kibana-visualization.js | 6 +-- 8 files changed, 60 insertions(+), 76 deletions(-) create mode 100644 public/factories/vis-handlers.js diff --git a/public/controllers/agents.js b/public/controllers/agents.js index 13846fdca0..f27cdd6261 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -15,9 +15,10 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('agentsController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates) { +app.controller('agentsController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, apiReq, AgentsAutoComplete, errorHandler, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates, visHandlers) { $location.search('_a',null) const filterHandler = new FilterHandler(appState.getCurrentPattern()); + visHandlers.removeAll(); discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); tabVisualizations.removeAll(); @@ -178,13 +179,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS $scope.switchSubtab = (subtab, force = false) => { if($scope.tabView === subtab && !force) return; - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; - + visHandlers.removeAll(); discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); loadedVisualizations.removeAll(); @@ -341,12 +336,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS $scope.goGroups = agent => { $rootScope.globalAgent = agent; $scope.agentsAutoComplete.reset(); - if($rootScope.ownHandlers) { - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + visHandlers.removeAll(); $rootScope.comeFrom = 'agents'; //$location.search('_a',null); $location.search('tab', 'groups'); @@ -386,12 +376,7 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS tabVisualizations.removeAll(); loadedVisualizations.removeAll(); $scope.agentsAutoComplete.reset(); - if($rootScope.ownHandlers) { - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + visHandlers.removeAll(); }); //PCI tab diff --git a/public/controllers/groups.js b/public/controllers/groups.js index 1c34641d9f..cad31f796b 100644 --- a/public/controllers/groups.js +++ b/public/controllers/groups.js @@ -198,12 +198,6 @@ function ($scope, $rootScope, $location, apiReq, Groups, GroupFiles, GroupAgents $scope.groups.reset(); $scope.groupFiles.reset(); $scope.groupAgents.reset(); - if($rootScope.ownHandlers){ - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; reloadWatcher(); }); @@ -220,11 +214,6 @@ app.controller('groupsController', function ($scope,$rootScope) { $scope.groupsMenu = 'preview'; $scope.groupName = ''; $scope.$on("$destroy", () => { - if($rootScope.ownHandlers){ - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + }); }); diff --git a/public/controllers/manager.js b/public/controllers/manager.js index 89fdd07277..aa6b5bcd7d 100644 --- a/public/controllers/manager.js +++ b/public/controllers/manager.js @@ -53,12 +53,7 @@ app.controller('managerController', function ($scope, $rootScope, $routeParams, $scope.setRulesTab = (tab) => $scope.submenuNavItem2 = tab; $scope.$on("$destroy", () => { - if($rootScope.ownHandlers){ - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + }); }); @@ -102,12 +97,7 @@ app.controller('managerStatusController', function ($scope,$rootScope, errorHand }); $scope.$on("$destroy", () => { - if($rootScope.ownHandlers){ - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + }); }); @@ -164,11 +154,6 @@ app.controller('managerConfigurationController', function ($scope, $rootScope, e load(); $scope.$on("$destroy", () => { - if($rootScope.ownHandlers){ - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + }); }); diff --git a/public/controllers/osseclog.js b/public/controllers/osseclog.js index f6d90d983f..7b4799ef75 100644 --- a/public/controllers/osseclog.js +++ b/public/controllers/osseclog.js @@ -91,11 +91,6 @@ app.controller('managerLogController', function ($scope, $rootScope, Logs, apiRe clearInterval(intervalId); } $scope.logs.reset(); - if($rootScope.ownHandlers){ - for(let h of $rootScope.ownHandlers){ - h._scope.$destroy(); - } - } - $rootScope.ownHandlers = []; + }); }); diff --git a/public/controllers/overview.js b/public/controllers/overview.js index d0449b65a7..9141b3bfb9 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -15,7 +15,7 @@ import FilterHandler from './filter-handler' const app = modules.get('app/wazuh', []); -app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates) { +app.controller('overviewController', function ($timeout, $scope, $location, $rootScope, appState, genericReq, errorHandler, apiReq, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates, visHandlers) { $location.search('_a',null) const filterHandler = new FilterHandler(appState.getCurrentPattern()); discoverPendingUpdates.removeAll(); @@ -211,14 +211,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo $scope.switchSubtab = (subtab,force = false) => { if ($scope.tabView === subtab && !force) return; - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - console.log(h._scope) - } - } - $rootScope.ownHandlers = []; - + visHandlers.removeAll(); discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); loadedVisualizations.removeAll(); @@ -260,13 +253,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo rawVisualizations.removeAll(); tabVisualizations.removeAll(); loadedVisualizations.removeAll(); - if ($rootScope.ownHandlers) { - for (let h of $rootScope.ownHandlers) { - h._scope.$destroy(); - } - } - - $rootScope.ownHandlers = []; + visHandlers.removeAll(); }); $scope.switchTab($scope.tab,true); diff --git a/public/factories/index.js b/public/factories/index.js index dcfe7d427e..e48bcc341c 100644 --- a/public/factories/index.js +++ b/public/factories/index.js @@ -13,4 +13,5 @@ import 'plugins/wazuh/factories/data-handler-composer'; import 'plugins/wazuh/factories/raw-visualizations'; import 'plugins/wazuh/factories/loaded-visualizations'; import 'plugins/wazuh/factories/tab-visualizations'; -import 'plugins/wazuh/factories/discover-pending-updates'; \ No newline at end of file +import 'plugins/wazuh/factories/discover-pending-updates'; +import 'plugins/wazuh/factories/vis-handlers'; \ No newline at end of file diff --git a/public/factories/vis-handlers.js b/public/factories/vis-handlers.js new file mode 100644 index 0000000000..24e3239f30 --- /dev/null +++ b/public/factories/vis-handlers.js @@ -0,0 +1,42 @@ +/* + * Wazuh app - Factory to store visualizations handlers + * + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ +import * as modules from 'ui/modules' + +const app = modules.get('app/wazuh', []); + +app.factory('visHandlers', function() { + let list = []; + + const addItem = item => { + list.push(item); + } + + const getList = () => { + return list; + } + + const removeAll = () => { + for(const item of list){ + if(item && item._scope){ + item._scope.$destroy(); + } + } + list = []; + } + + return { + addItem : addItem, + getList : getList, + removeAll : removeAll + }; +}); \ No newline at end of file diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index b3a6a633b6..d73ad1590d 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -21,8 +21,8 @@ const app = modules.get('apps/webinar_app', []) visID: '=visId', specificTimeRange: '=specificTimeRange' }, - controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates) { - if(!$rootScope.ownHandlers) $rootScope.ownHandlers = []; + controller: function VisController($scope, $rootScope, $location, wzsavedVisualizations, genericReq, errorHandler, Private, rawVisualizations, loadedVisualizations, tabVisualizations, discoverPendingUpdates, visHandlers) { + let originalImplicitFilter = ''; let implicitFilter = ''; let visTitle = ''; @@ -80,7 +80,7 @@ const app = modules.get('apps/webinar_app', []) visHandler = loader.embedVisualizationWithSavedObject($(`[vis-id="'${$scope.visID}'"]`), visualization, params); - $rootScope.ownHandlers.push(visHandler); + visHandlers.addItem(visHandler); visHandler.addRenderCompleteListener(renderComplete); }).catch(error => { if(error && error.message && error.message.includes('not locate that index-pattern-field')){ From f477ff757ca04deacfcc17a32b92ffad545e1323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 16:46:52 +0200 Subject: [PATCH 24/25] Destroying useless listeners --- public/controllers/agents.js | 5 ++--- public/controllers/overview.js | 3 +-- public/factories/vis-handlers.js | 3 +++ public/kibana-integrations/kibana-discover.js | 1 - public/kibana-integrations/kibana-visualization.js | 6 +++++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index f27cdd6261..bf9e5dd2e9 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -102,7 +102,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS let filters = [] const assignFilters = (tab,agent) => { try { - console.log(agent) filters = []; filters.push(filterHandler.managerQuery( @@ -178,7 +177,6 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS // Switch subtab $scope.switchSubtab = (subtab, force = false) => { if($scope.tabView === subtab && !force) return; - visHandlers.removeAll(); discoverPendingUpdates.removeAll(); rawVisualizations.removeAll(); @@ -193,12 +191,13 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS .then(data => { rawVisualizations.assignItems(data.data.raw); assignFilters($scope.tab, $scope.agent.id); - $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) + $rootScope.$emit('changeTabView',{tabView:subtab}) $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) .catch(error => errorHandler.handle(error, 'Agents')); } else { + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) checkMetrics($scope.tab, subtab); } } diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 9141b3bfb9..4b88d35291 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -225,7 +225,7 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo .then(data => { rawVisualizations.assignItems(data.data.raw); assignFilters($scope.tab); - $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) + $rootScope.$emit('changeTabView',{tabView:subtab}) $rootScope.$broadcast('updateVis'); checkMetrics($scope.tab, 'panels'); }) @@ -238,7 +238,6 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo // Switch tab $scope.switchTab = (tab,force = false) => { - console.log('Entering switchTab',$scope.tab,tab) tabVisualizations.setTab(tab); if ($scope.tab === tab && !force) return; diff --git a/public/factories/vis-handlers.js b/public/factories/vis-handlers.js index 24e3239f30..0e3fa4995a 100644 --- a/public/factories/vis-handlers.js +++ b/public/factories/vis-handlers.js @@ -30,6 +30,9 @@ app.factory('visHandlers', function() { if(item && item._scope){ item._scope.$destroy(); } + if(item && item._scope && item._scope.savedObj){ + item._scope.savedObj.destroy(); + } } list = []; } diff --git a/public/kibana-integrations/kibana-discover.js b/public/kibana-integrations/kibana-discover.js index e5ecf83767..5a90f6b437 100644 --- a/public/kibana-integrations/kibana-discover.js +++ b/public/kibana-integrations/kibana-discover.js @@ -761,7 +761,6 @@ function discoverController( /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// const loadFilters = wzCurrentFilters => { - console.log(wzCurrentFilters) const appState = getAppState(); if(!appState || !globalState){ $timeout(100) diff --git a/public/kibana-integrations/kibana-visualization.js b/public/kibana-integrations/kibana-visualization.js index d73ad1590d..513ea1b319 100644 --- a/public/kibana-integrations/kibana-visualization.js +++ b/public/kibana-integrations/kibana-visualization.js @@ -116,7 +116,7 @@ const app = modules.get('apps/webinar_app', []) }; // Listen for changes - $rootScope.$on('updateVis', () => { + const updateVisWatcher = $rootScope.$on('updateVis', () => { if(!$rootScope.$$phase) $rootScope.$digest(); const rawVis = rawVisualizations.getList(); if(Array.isArray(rawVis) && rawVis.length){ @@ -124,6 +124,10 @@ const app = modules.get('apps/webinar_app', []) } }); + $scope.$on('$destroy',() => { + updateVisWatcher(); + }) + const renderComplete = () => { rendered = true; From 171a913405b6ce099e8604e7311a69fb726c00a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 15 May 2018 16:55:12 +0200 Subject: [PATCH 25/25] Emitting event whenever come from discover to panels --- public/controllers/agents.js | 5 +++++ public/controllers/overview.js | 5 +++++ .../templates/kibana-template/kibana-discover-template.html | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index bf9e5dd2e9..be2f79fafa 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -183,6 +183,11 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS loadedVisualizations.removeAll(); $location.search('tabView', subtab); + + if(subtab === 'panels' && $scope.tabView === 'discover'){ + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) + } + $scope.tabView = subtab; if(subtab === 'panels' && $scope.tab !== 'configuration'){ diff --git a/public/controllers/overview.js b/public/controllers/overview.js index 4b88d35291..1c535a6d37 100644 --- a/public/controllers/overview.js +++ b/public/controllers/overview.js @@ -217,6 +217,11 @@ app.controller('overviewController', function ($timeout, $scope, $location, $roo loadedVisualizations.removeAll(); $location.search('tabView', subtab); + + if(subtab === 'panels' && $scope.tabView === 'discover'){ + $rootScope.$emit('changeTabView',{tabView:$scope.tabView}) + } + $scope.tabView = subtab; if(subtab === 'panels'){ diff --git a/public/templates/kibana-template/kibana-discover-template.html b/public/templates/kibana-template/kibana-discover-template.html index cb87e43a20..8faf68c58f 100644 --- a/public/templates/kibana-template/kibana-discover-template.html +++ b/public/templates/kibana-template/kibana-discover-template.html @@ -5,7 +5,7 @@
-

+