wazuh-kibana-app/public/services/reporting.js

164 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-06-12 13:22:37 +00:00
/*
* Wazuh app - Reporting service
2019-01-14 16:36:47 +00:00
* Copyright (C) 2015-2019 Wazuh, Inc.
2018-06-12 13:22:37 +00:00
*
* 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.
*/
2018-09-11 09:52:54 +00:00
import $ from 'jquery';
import moment from 'moment';
2018-06-12 13:22:37 +00:00
2018-09-11 09:52:54 +00:00
export class ReportingService {
2018-09-10 08:32:49 +00:00
constructor(
$rootScope,
vis2png,
rawVisualizations,
visHandlers,
genericReq,
errorHandler,
wazuhConfig
2018-09-10 08:32:49 +00:00
) {
this.$rootScope = $rootScope;
this.vis2png = vis2png;
this.rawVisualizations = rawVisualizations;
this.visHandlers = visHandlers;
this.genericReq = genericReq;
this.errorHandler = errorHandler;
this.wazuhConfig = wazuhConfig;
}
removeAgentStatusVis(idArray) {
2019-07-22 14:13:52 +00:00
const monitoringEnabled = this.wazuhConfig.getConfig()[
'wazuh.monitoring.enabled'
];
if (!monitoringEnabled) {
const visArray = idArray.filter(vis => {
return vis !== 'Wazuh-App-Overview-General-Agents-status';
});
return visArray;
}
return idArray;
2018-09-10 08:32:49 +00:00
}
async startVis2Png(tab, isAgents = false, syscollectorFilters = null) {
try {
if (this.vis2png.isWorking()) {
this.errorHandler.handle('Report in progress', 'Reporting', true);
return;
}
this.$rootScope.reportBusy = true;
this.$rootScope.reportStatus = 'Generating report...0%';
this.$rootScope.$applyAsync();
2018-09-10 08:32:49 +00:00
this.vis2png.clear();
2018-06-12 13:22:37 +00:00
let idArray = [];
if (tab === 'general') {
2019-07-22 14:13:52 +00:00
idArray = this.removeAgentStatusVis(
this.rawVisualizations.getList().map(item => item.id)
);
} else {
idArray = this.rawVisualizations.getList().map(item => item.id);
}
2018-06-12 13:22:37 +00:00
2018-09-10 08:32:49 +00:00
for (const item of idArray) {
const tmpHTMLElement = $(`#${item}`);
this.vis2png.assignHTMLItem(item, tmpHTMLElement);
}
2018-06-12 13:22:37 +00:00
2018-09-10 08:32:49 +00:00
const appliedFilters = this.visHandlers.getAppliedFilters(
syscollectorFilters
);
2018-06-12 13:22:37 +00:00
2018-09-10 08:32:49 +00:00
const array = await this.vis2png.checkArray(idArray);
const name = `wazuh-${
isAgents ? 'agents' : 'overview'
2019-07-22 14:13:52 +00:00
}-${tab}-${(Date.now() / 1000) | 0}.pdf`;
const browserTimezone = moment.tz.guess(true);
2018-06-12 13:22:37 +00:00
2018-09-10 08:32:49 +00:00
const data = {
array,
name,
title: isAgents ? `Agents ${tab}` : `Overview ${tab}`,
filters: appliedFilters.filters,
time: appliedFilters.time,
searchBar: appliedFilters.searchBar,
tables: appliedFilters.tables,
tab,
section: isAgents ? 'agents' : 'overview',
isAgents,
browserTimezone
2018-09-10 08:32:49 +00:00
};
2018-06-12 13:22:37 +00:00
2018-11-02 17:23:00 +00:00
await this.genericReq.request('POST', '/reports', data);
2018-06-12 13:22:37 +00:00
2018-09-10 08:32:49 +00:00
this.$rootScope.reportBusy = false;
this.$rootScope.reportStatus = false;
this.$rootScope.$applyAsync();
2018-09-10 08:32:49 +00:00
this.errorHandler.info(
2018-12-18 16:14:30 +00:00
'Success. Go to Wazuh > Management > Reporting',
2018-09-10 08:32:49 +00:00
'Reporting'
);
2018-06-12 13:22:37 +00:00
2018-09-10 08:32:49 +00:00
return;
} catch (error) {
this.$rootScope.reportBusy = false;
this.$rootScope.reportStatus = false;
2019-03-26 20:37:23 +00:00
this.errorHandler.handle(error.message || error);
2018-08-20 07:00:50 +00:00
}
2018-09-10 08:32:49 +00:00
}
2019-08-07 08:31:59 +00:00
async startConfigReport(obj, type, components) {
try {
this.$rootScope.reportBusy = true;
this.$rootScope.reportStatus = 'Generating PDF document...';
this.$rootScope.$applyAsync();
const docType =
type === 'agentConfig'
? `wazuh-agent-${obj.id}`
: `wazuh-group-${obj.name}`;
const name = `${docType}-configuration-${(Date.now() / 1000) | 0}.pdf`;
const browserTimezone = moment.tz.guess(true);
const data = {
array: [],
name,
filters: [
type === 'agentConfig' ? { agent: obj.id } : { group: obj.name }
],
time: '',
searchBar: '',
tables: [],
tab: type,
browserTimezone,
components
};
await this.genericReq.request('POST', '/reports', data);
this.$rootScope.reportBusy = false;
this.$rootScope.reportStatus = false;
this.$rootScope.$applyAsync();
this.errorHandler.info(
'Success. Go to Wazuh > Management > Reporting',
'Reporting'
);
return;
} catch (error) {
this.$rootScope.reportBusy = false;
this.$rootScope.reportStatus = false;
this.errorHandler.handle(error.message || error);
this.$rootScope.$applyAsync();
}
}
2018-08-20 07:00:50 +00:00
}