Merge pull request #766 from wazuh/issue-661

Refactor agents autocomplete. Deprecated old data handler
This commit is contained in:
Javier Castro 2018-08-01 20:09:31 +02:00 committed by GitHub
commit 5192a1fd37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 33 additions and 370 deletions

View File

@ -12,7 +12,8 @@
// Require CSS
import './less/loader';
import { uiModules } from 'ui/modules'
import { uiModules } from 'ui/modules';
// Set up Wazuh app
const app = uiModules.get('app/wazuh', ['ngCookies', 'ngMaterial']);
@ -26,28 +27,18 @@ app.config(['$httpProvider', function($httpProvider) {
// Font Awesome, Kibana UI framework and others
import './utils/fontawesome/css/font-awesome.min.css';
import './utils/when-scrolled/when-scrolled.js';
// Dev tools
import './utils/codemirror/codemirror.css'
import './utils/codemirror/foldgutter.css'
import './utils/codemirror/ttcn.css'
import './utils/codemirror/javascript.js'
import './utils/codemirror/brace-fold.js'
import './utils/codemirror/foldcode.js'
import './utils/codemirror/foldgutter.js'
import './utils/codemirror/search-cursor.js'
import './utils/codemirror/mark-selection.js'
import './utils/codemirror'
// Material
import './../node_modules/angular-material/angular-material.css';
import './../node_modules/angular-aria/angular-aria.js';
import './../node_modules/angular-animate/angular-animate.js';
import './../node_modules/angular-material/angular-material.js';
import 'angular-material/angular-material.css';
import 'angular-aria/angular-aria';
import 'angular-animate/angular-animate';
import 'angular-material/angular-material';
// Cookies
import './../node_modules/angular-cookies/angular-cookies.min.js';
import 'angular-cookies/angular-cookies';
import 'ui/autoload/all';
import 'ui/chrome';

View File

@ -17,6 +17,7 @@ import TabNames from '../utils/tab-names';
import { metricsAudit, metricsVulnerability, metricsScap, metricsCiscat, metricsVirustotal } from '../utils/agents-metrics';
import * as FileSaver from '../services/file-saver';
import DataFactory from '../services/data-factory';
import TabDescription from '../../server/reporting/tab-description';
const app = uiModules.get('app/wazuh', []);
@ -30,7 +31,9 @@ function (
reportingService, visFactoryService, csvReq,
wzTableFilter
) {
const agentFactory = new DataFactory(apiReq,'/agents');
$scope.TabDescription = TabDescription;
$rootScope.reportStatus = false;
$location.search('_a',null)
@ -41,8 +44,6 @@ function (
const extensions = appState.getExtensions(currentApi);
$scope.extensions = extensions;
$scope.agentsAutoComplete = AgentsAutoComplete;
$scope.tabView = commonData.checkTabViewLocation();
$scope.tab = commonData.checkTabLocation();
@ -156,7 +157,7 @@ function (
$scope.agent.syscheck = result;
}
$scope.getAgent = async (newAgentId,fromAutocomplete) => {
$scope.getAgent = async newAgentId => {
try {
$scope.load = true;
changeAgent = true;
@ -214,25 +215,19 @@ function (
};
$scope.goGroups = agent => {
$scope.agentsAutoComplete.reset();
visFactoryService.clearAll()
shareAgent.setAgent(agent)
$location.search('tab', 'groups');
$location.path('/manager');
};
$scope.analyzeAgents = async search => {
try {
await $timeout(200);
$scope.agentsAutoComplete.filters = [];
await $scope.agentsAutoComplete.addFilter('search',search);
$scope.analyzeAgents = search => {
agentFactory.items = [];
agentFactory.addFilter('search',search);
return agentFactory.fetch({nonull: true})
.then(data => data.items)
.catch(error => errorHandler.handle(error,'Agents'))
if(!$scope.$$phase) $scope.$digest();
return $scope.agentsAutoComplete.items;
} catch (error) {
errorHandler.handle(error,'Agents');
}
return;
}
$scope.downloadCsv = async data_path => {
@ -256,7 +251,6 @@ function (
//Destroy
$scope.$on("$destroy", () => {
visFactoryService.clearAll()
$scope.agentsAutoComplete.reset();
});
// PCI and GDPR requirements
@ -353,7 +347,6 @@ function (
//Load
try {
$scope.getAgent();
$scope.agentsAutoComplete.nextPage('');
} catch (e) {
errorHandler.handle('Unexpected exception loading controller','Agents');
}

View File

@ -1,47 +0,0 @@
/*
* Wazuh app - File for data handler factory definitions
* 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 { uiModules } from 'ui/modules'
const app = uiModules.get('app/wazuh', []);
/**
* Main function to build a data handler factory.
* @param {*} DataHandler
* @param {*} path
*/
const compose = (DataHandler, path) => {
let dataHandler = new DataHandler();
dataHandler.path = path;
return dataHandler;
};
const agents = DataHandler => compose(DataHandler, '/agents');
const simple = DataHandler => new DataHandler();
app
.factory('AgentsAutoComplete', agents)
.directive('lazyLoadData', function($compile) {
return {
link: (scope, el, attrs) => {
let now = new Date().getTime();
let rep = angular.element(document.getElementsByClassName('md-virtual-repeat-scroller'));
rep.on('scroll', evt => {
if (rep[0].scrollTop + rep[0].offsetHeight >= rep[0].scrollHeight)
if (new Date().getTime() - now > 100) {
now = new Date().getTime();
scope.$apply(() => scope.$eval(attrs.lazyLoadData));
}
});
}
};
});

View File

@ -79,7 +79,7 @@ export default class DataFactory {
// Ignore manager as an agent, once the team solves this issue, review this line
if(this.path === '/agents') this.items = this.items.filter(item => item.id !== '000');
if(remaining > 0 )this.items.push(...Array(remaining).fill(null));
if((!options || !options.nonull) && remaining > 0 ) this.items.push(...Array(remaining).fill(null));
const end = new Date();
const elapsed = (end - start) / 1000;

View File

@ -1,259 +0,0 @@
/*
* Wazuh app - Data handler service
* 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 { uiModules } from 'ui/modules'
const app = uiModules.get('app/wazuh', []);
app.factory('DataHandler', function ($q, apiReq,errorHandler) {
class DataHandler {
constructor() {
this.items = [];
this.filters = [];
this.path = '';
this.offset = 0;
this.sortValue = '';
this.initial = true;
this.initialBatch = 40;
this.regularBatch = 15;
this.busy = false;
this.end = false;
this.onlyParents = false;
this.ruleID = null;
this.decoderPosition = null;
}
toggleOnlyParents(value){
this.reset();
this.onlyParents = !value;
return this.nextPage();
}
nextPage () {
if(this.totalItems && this.items && this.totalItems > this.items.length){
this.end = false;
}
if (this.busy || this.end) {
return Promise.resolve(true);
}
const deferred = $q.defer();
this.busy = true;
let requestData;
if (this.initial) {
requestData = {
offset: this.offset,
limit: this.initialBatch
};
this.initial = false;
} else {
requestData = {
offset: this.offset,
limit: this.regularBatch
};
}
if(this.sortValue){
requestData.sort = this.sortDir ? '-' + this.sortValue : this.sortValue;
}
for(let filter of this.filters){
if (filter.value !== '') requestData[filter.name] = filter.value;
}
if(this.offset !== 0 && this.offset >= this.totalItems){
this.end = true;
this.busy = false;
return Promise.resolve(true);
}
const path = this.onlyParents ? this.path + '/parents' : this.path;
apiReq.request('GET', path, requestData)
.then(data => {
if (data.data.data === 0){
this.busy = false;
deferred.resolve(false);
}
this.totalItems = data.data.data.totalItems;
let items = data.data.data.items;
for (let i = 0,len = items.length; i < len; i++) {
this.items.push(items[i]);
this.items[i].selected = false;
}
// Prevents from any manager
if (this.path === '/agents') {
const filteredAgents = this.items.filter(item => item && item.id !== '000');
this.items = filteredAgents;
}
this.offset += items.length;
if (this.offset >= this.totalItems) this.end = true;
if (data.data.data !== 0){
if(this.path === '/agents/groups'){
let filtered = this.items.filter((elem, index, self) => self.findIndex(
(t) => {return (t.merged_sum === elem.merged_sum)}) === index);
if(filtered.length !== this.items.length) this.items = filtered;
}
// Remove the current decoder (by its position) from the list of related decoders
if (this.decoderPosition || this.decoderPosition === 0) {
const filteredDecoders = this.items.filter(item => item.position !== this.decoderPosition);
this.items = filteredDecoders;
}
deferred.resolve(true);
}
this.busy = false;
})
.catch(error => {
this.busy = false;
errorHandler.handle(error,'Datahandler factory');
});
return deferred.promise;
}
getFilter (filterName) {
let filtered = this.filters.filter(element => element.name === filterName);
return (filtered.length !== 0) ? filtered[0].value : null;
}
hasFilter (filterName) {
let filtered = this.filters.filter(element => element.name === filterName);
return filtered.length !== 0;
}
addFilter (filterName, value) {
this.removeFilter(filterName, false);
this.filters.push({
name: filterName,
value: value
});
return this.search();
}
///////////////////////////////////////////////////////////////
// Only used by agents preview, don't use for any thing more //
///////////////////////////////////////////////////////////////
addMultipleFilters (arrayOfFilters) {
for(const filter of arrayOfFilters) this.removeFilter(filter.name,false)
this.filters.push(...arrayOfFilters);
return this.search();
}
removeFilter (filterName, search) {
this.end = false;
if(search) this.filters = this.filters.filter(filter => filterName !== filter.name && filter.value !== search);
else this.filters = this.filters.filter(filter => filterName !== filter.name);
if (search) this.search();
}
removeAllFilters () {
for (let filter of this.filters) {
this.removeFilter(filter.name, true);
}
// this.filters = [];
// return this.search();
}
delete (name, index) {
apiReq.request('DELETE', this.path, {})
.then(function (data) {
this.items.splice(index, 1);
}.bind(this))
.catch(error => errorHandler.handle(error,'Datahandler factory'));
}
search () {
if (this.busy) {
return Promise.resolve(true);
}
this.busy = true;
const deferred = $q.defer();
let requestData;
this.end = false;
//this.sortValue = '';
requestData = {
offset: 0,
limit: this.initialBatch
};
if(this.sortValue){
requestData.sort = this.sortDir ? '-' + this.sortValue : this.sortValue;
}
let isUnknown = false;
for(let filter of this.filters){
if (filter.value !== '' && filter.value !== 'Unknown') requestData[filter.name] = filter.value;
if (filter.value === 'Unknown') isUnknown = true;
}
apiReq.request('GET', this.path, requestData)
.then(data => {
this.items = [];
let items = data.data.data.items;
this.totalItems = data.data.data.totalItems;
for (let i = 0,len = items.length; i < len; i++) {
this.items.push(items[i]);
this.items[i].selected = false;
}
if(isUnknown){
this.items = this.items.filter(item => typeof item.os === 'undefined');
}
// Remove the current rule (by its ID) from the list of related rules
if (this.ruleID) {
const filteredRules = this.items.filter(item => item.id !== this.ruleID);
this.items = filteredRules;
}
// Prevents from any manager
if (this.path === '/agents') {
const filteredAgents = this.items.filter(item => item && item.id !== '000');
this.items = filteredAgents;
}
this.offset = items.length;
deferred.resolve(true);
this.busy = false;
})
.catch(error => {
this.busy = false;
errorHandler.handle(error,'Datahandler factory');
});
return deferred.promise;
}
sort(by) {
this.sortValue = by;
this.sortDir = !this.sortDir;
return this.search();
}
reset() {
this.items = [];
this.filters = [];
this.offset = 0;
this.sortValue = '';
this.initial = true;
this.end = false;
this.busy = false;
this.ruleID = null;
this.decoderPosition = null;
this.onlyParents = false;
}
}
return DataHandler;
});

View File

@ -13,7 +13,6 @@ import './error-handler'
import './theming';
import './api-request'
import './generic-request'
import './data-handler'
import './app-state'
import './api-tester'
import './pattern-handler'

View File

@ -62,7 +62,7 @@
<!-- Agent autocomplete selector section -->
<div flex="30" class="wz-margin-8-no-left">
<md-autocomplete id="agentsAutocomplete" flex class="wz-autocomplete wz-select-input"
<md-autocomplete flex class="wz-autocomplete wz-select-input"
md-no-cache="true"
md-select-on-match="false"
md-selected-item="_swpagent"
@ -74,8 +74,7 @@
md-no-asterisk="false"
md-menu-class="autocomplete-custom-agents-bar"
md-search-text-change="analyzeAgents(searchTerm)"
placeholder="Search by name, ID or IP address"
lazy-load-data="agentsAutoComplete.nextPage('')">
placeholder="Search by name, ID or IP address">
<md-item-template>
<span class="item-title">
<span><strong md-highlight-text="search" md-highlight-flags="i"> {{agentAutoComplete.name}} ({{agentAutoComplete.id}}) </strong></span>

View File

@ -0,0 +1,12 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
import './codemirror.css'
import './foldgutter.css'
import './ttcn.css'
import './javascript.js'
import './brace-fold.js'
import './foldcode.js'
import './foldgutter.js'
import './search-cursor.js'
import './mark-selection.js'

View File

@ -1,25 +0,0 @@
/*
* Wazuh app - whenScrolled directive definition
* 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 { uiModules } from 'ui/modules';
const app = uiModules.get('app/wazuh', []);
app.directive('whenScrolled', function() {
return function(scope, elm, attr) {
var raw = elm[0];
elm.bind('scroll', function() {
if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight - 150) {
scope.$apply(attr.whenScrolled);
}
});
};
});