Skip to content

Commit

Permalink
Merge pull request #3404 from wazuh/implement/try-catch-controller-agent
Browse files Browse the repository at this point in the history
Refactor try catch in Controller/Agent section
  • Loading branch information
pablomarga authored Jun 29, 2021
2 parents 21d3b9c + a4ed923 commit c24a65f
Show file tree
Hide file tree
Showing 7 changed files with 2,317 additions and 2,095 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Changed empty fields in FIM tables and `syscheck.value_name` in discovery now show an empty tag for visual clarity [#3279](https://github.com/wazuh/wazuh-kibana-app/pull/3279)
- Adapted the Mitre tactics and techniques resources to use the API endpoints [#3346](https://github.com/wazuh/wazuh-kibana-app/pull/3346)
- Refactored all try catch strategy on Settings section [#3392](https://github.com/wazuh/wazuh-kibana-app/pull/3392)
- Refactored all try catch strategy on Controller/Agent section [#3398](https://github.com/wazuh/wazuh-kibana-app/issues/3398)

### Fixed

Expand Down
130 changes: 84 additions & 46 deletions public/controllers/agent/agents-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import { ShareAgent } from '../../factories/share-agent';
import { formatUIDate } from '../../react-services/time-service';
import { ErrorHandler } from '../../react-services/error-handler';
import { getDataPlugin, getToasts } from '../../kibana-services';
import { connect } from 'react-redux';
import store from '../../redux/store';
import { UI_LOGGER_LEVELS } from '../../../common/constants';
import { UI_ERROR_SEVERITIES } from '../../react-services/error-orchestrator/types';
import { getErrorOrchestrator } from '../../react-services/common-services';

const errorContext = 'AgentsPreviewController';

export class AgentsPreviewController {
/**
Expand All @@ -32,15 +36,7 @@ export class AgentsPreviewController {
* @param {Object} errorHandler
* @param {Object} csvReq
*/
constructor(
$scope,
$location,
$route,
errorHandler,
csvReq,
commonData,
$window
) {
constructor($scope, $location, $route, errorHandler, csvReq, commonData, $window) {
this.$scope = $scope;
this.genericReq = GenericRequest;
this.$location = $location;
Expand All @@ -62,22 +58,19 @@ export class AgentsPreviewController {
this.api = JSON.parse(AppState.getCurrentAPI()).id;
const loc = this.$location.search();
if ((loc || {}).agent && (loc || {}).agent !== '000') {
this.commonData.setTimefilter( getDataPlugin().timefilter.timefilter.getTime());
this.commonData.setTimefilter(getDataPlugin().timefilter.timefilter.getTime());
return this.showAgent({ id: loc.agent });
}

this.isClusterEnabled =
AppState.getClusterInfo() &&
AppState.getClusterInfo().status === 'enabled';

this.isClusterEnabled = AppState.getClusterInfo() && AppState.getClusterInfo().status === 'enabled';
this.loading = true;
this.osPlatforms = [];
this.versions = [];
this.groups = [];
this.nodes = [];
this.mostActiveAgent = {
name: '',
id: ''
id: '',
};
this.prevSearch = false;

Expand All @@ -95,16 +88,14 @@ export class AgentsPreviewController {
} else {
this.hasAgents = true;
}

// Watcher for URL params
this.$scope.$watch('submenuNavItem', () => {
this.$location.search('tab', this.submenuNavItem);
});

this.$scope.$on('wazuhFetched', evt => {
this.$scope.$on('wazuhFetched', (evt) => {
evt.stopPropagation();
});

this.registerAgentsProps = {
addNewAgent: flag => this.addNewAgent(flag),
hasAgents: this.hasAgents,
Expand All @@ -126,26 +117,19 @@ export class AgentsPreviewController {
this.downloadCsv(filters);
this.$scope.$applyAsync();
},
showAgent: agent => {
showAgent: (agent) => {
this.showAgent(agent);
this.$scope.$applyAsync();
},
getMostActive: async () => {
return await this.getMostActive();
},
clickAction: (item, openAction = false) => {
clickAction(
item,
openAction,
instance,
this.shareAgent,
this.$location,
this.$scope
);
clickAction(item, openAction, instance, this.shareAgent, this.$location, this.$scope);
this.$scope.$applyAsync();
},
formatUIDate: date => formatUIDate(date),
summary: this.summary
formatUIDate: (date) => formatUIDate(date),
summary: this.summary,
};
//Load
this.load();
Expand Down Expand Up @@ -175,18 +159,25 @@ export class AgentsPreviewController {
*/
async downloadCsv(filters) {
try {
ErrorHandler.info(
'Your download should begin automatically...',
'CSV'
);
ErrorHandler.info('Your download should begin automatically...', 'CSV');
const output = await this.csvReq.fetch('/agents', this.api, filters);
const blob = new Blob([output], { type: 'text/csv' }); // eslint-disable-line

FileSaver.saveAs(blob, 'agents.csv');

return;
} catch (error) {
ErrorHandler.handle(error, 'Download CSV');
const options = {
context: errorContext,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
error: {
error: error,
message: error.message || error,
title: `Error exporting CSV: ${error.message || error}`,
},
};
getErrorOrchestrator().handleError(options);
}
return;
}
Expand All @@ -195,21 +186,38 @@ export class AgentsPreviewController {
try {
const data = await this.genericReq.request(
'GET',
`/elastic/top/${this.firstUrlParam}/${this.secondUrlParam}/agent.name/${this.pattern}?agentsList=${store.getState().appStateReducers.allowedAgents.toString()}`
`/elastic/top/${this.firstUrlParam}/${this.secondUrlParam}/agent.name/${
this.pattern
}?agentsList=${store.getState().appStateReducers.allowedAgents.toString()}`
);
this.mostActiveAgent.name = data.data.data;
const info = await this.genericReq.request(
'GET',
`/elastic/top/${this.firstUrlParam}/${this.secondUrlParam}/agent.id/${this.pattern}?agentsList=${store.getState().appStateReducers.allowedAgents.toString()}`
`/elastic/top/${this.firstUrlParam}/${this.secondUrlParam}/agent.id/${
this.pattern
}?agentsList=${store.getState().appStateReducers.allowedAgents.toString()}`
);
if (info.data.data === '' && this.mostActiveAgent.name !== '') {
this.mostActiveAgent.id = '000';
} else {
this.mostActiveAgent.id = info.data.data;
}
return this.mostActiveAgent;
} catch (error) {
getToasts().addDanger({title: 'An error occurred while trying to get the most active agent', text: error.message || error });
} catch (error) {
const options = {
context: errorContext,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `An error occurred while trying to get the most active agent: ${
error.message || error
}`,
},
};
getErrorOrchestrator().handleError(options);
}
}

Expand All @@ -219,14 +227,23 @@ export class AgentsPreviewController {
async load() {
try {
this.errorInit = false;

const clusterInfo = AppState.getClusterInfo();
this.firstUrlParam =
clusterInfo.status === 'enabled' ? 'cluster' : 'manager';
this.firstUrlParam = clusterInfo.status === 'enabled' ? 'cluster' : 'manager';
this.secondUrlParam = clusterInfo[this.firstUrlParam];
this.pattern = (await getDataPlugin().indexPatterns.get(AppState.getCurrentPattern())).title;
} catch (error) {
this.errorInit = ErrorHandler.handle(error, '', { silent: true });
const options = {
context: errorContext,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.CRITICAL,
store: true,
error: {
error: error,
message: error.message || error,
title: error.message || error,
},
};
getErrorOrchestrator().handleError(options);
}
this.loading = false;
this.$scope.$applyAsync();
Expand All @@ -250,14 +267,24 @@ export class AgentsPreviewController {
try {
const result = await this.genericReq.request('GET', '/hosts/apis');
const entries = result.data || [];
const host = entries.filter(e => {
const host = entries.filter((e) => {
return e.id == this.api;
});
const url = host[0].url;
const numToClean = url.startsWith('https://') ? 8 : 7;
return url.substr(numToClean);
} catch (error) {
return false;
const options = {
context: errorContext,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.UI,
error: {
error: error,
message: error.message || error,
title: `Could not get the Wazuh API address: ${error.message || error}`,
},
};
getErrorOrchestrator().handleError(options);
}
}

Expand All @@ -268,8 +295,19 @@ export class AgentsPreviewController {
try {
const data = await WzRequest.apiReq('GET', '//', {});
const result = ((data || {}).data || {}).data || {};
return result.api_version
return result.api_version;
} catch (error) {
const options = {
context: errorContext,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
error: {
error: error,
message: error.message || error,
title: `Could not get the Wazuh version: ${error.message || error}`,
},
};
getErrorOrchestrator().handleError(options);
return version;
}
}
Expand Down
Loading

0 comments on commit c24a65f

Please sign in to comment.