wazuh-kibana-app/public/factories/vis-handlers.js

132 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-05-15 14:21:44 +00:00
/*
* 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.
*/
2018-06-15 10:42:57 +00:00
import { uiModules } from 'ui/modules'
2018-06-14 12:53:46 +00:00
import dateMath from '@kbn/datemath';
2018-06-15 10:42:57 +00:00
const app = uiModules.get('app/wazuh', []);
2018-05-15 14:21:44 +00:00
app.factory('visHandlers', function() {
let list = [];
const addItem = item => {
list.push(item);
2018-06-28 07:15:06 +00:00
};
2018-05-15 14:21:44 +00:00
const getList = () => {
return list;
2018-06-28 07:15:06 +00:00
};
2018-05-15 14:21:44 +00:00
const getAppliedFilters = syscollector => {
const appliedFilters = {};
2018-06-28 07:15:06 +00:00
if(syscollector){
Object.assign(appliedFilters, {
filters: syscollector,
time:{
from: 'now-1d/d',
to: 'now'
},
searchBar: false,
tables:[]
});
return appliedFilters;
}
2018-06-28 07:15:06 +00:00
// Check raw response from all rendered tables
const tables = list.filter(item => item._scope &&
item._scope.savedObj &&
item._scope.savedObj.vis &&
item._scope.savedObj.vis._state &&
item._scope.savedObj.vis._state.type === 'table'
)
2018-06-28 14:12:31 +00:00
.map(item => {
const columns = [];
for(const agg of item._scope.savedObj._source.visState.aggs){
if(agg.type === 'count') continue;
if(agg.params && agg.params.customLabel){
columns.push(agg.params.customLabel);
} else {
columns.push('Column');
}
}
columns.push('Count');
2018-06-28 14:12:31 +00:00
return (item._scope &&
item._scope.savedObj &&
item._scope.savedObj.vis &&
item._scope.savedObj.vis.searchSource &&
item._scope.savedObj.vis.searchSource.rawResponse) ?
{
rawResponse: item._scope.savedObj.vis.searchSource.rawResponse,
title: (item._scope && item._scope.savedObj && item._scope.savedObj.title) ?
item._scope.savedObj.title :
'Table',
columns
} :
false;
});
2018-06-28 07:15:06 +00:00
2018-05-29 15:42:22 +00:00
if(list && list.length) {
2018-06-28 07:15:06 +00:00
// Parse applied filters for the first visualization
const filters = list[0]._scope.savedObj.vis.API.queryFilter.getFilters();
2018-06-28 07:15:06 +00:00
// Parse current time range
2018-05-29 15:42:22 +00:00
const { from, to } = list[0]._scope.savedObj.vis.API.timeFilter.time;
2018-06-28 07:15:06 +00:00
Object.assign(appliedFilters, {
2018-05-29 15:42:22 +00:00
filters,
time:{
from: dateMath.parse(from),
to: dateMath.parse(to)
},
searchBar: list[0] && list[0]._scope && list[0]._scope.appState && list[0]._scope.appState.query && list[0]._scope.appState.query.query ?
list[0]._scope.appState.query.query :
2018-06-28 07:15:06 +00:00
false,
tables
});
2018-05-29 15:42:22 +00:00
}
2018-05-29 15:42:22 +00:00
return appliedFilters;
2018-06-28 07:15:06 +00:00
};
2018-05-29 15:42:22 +00:00
2018-05-18 09:47:29 +00:00
const hasData = () => {
for(const item of list) {
if(item && item._scope && item._scope.savedObj && item._scope.savedObj.searchSource &&
item._scope.savedObj.searchSource.rawResponse &&
item._scope.savedObj.searchSource.rawResponse.hits &&
item._scope.savedObj.searchSource.rawResponse.hits.total){
return true;
}
}
return false;
2018-06-28 07:15:06 +00:00
};
2018-05-18 09:47:29 +00:00
2018-05-15 14:21:44 +00:00
const removeAll = () => {
for(const item of list){
if(item && item._scope){
item._scope.$destroy();
}
2018-05-15 14:46:52 +00:00
if(item && item._scope && item._scope.savedObj){
item._scope.savedObj.destroy();
}
2018-05-15 14:21:44 +00:00
}
list = [];
2018-06-28 07:15:06 +00:00
};
2018-05-15 14:21:44 +00:00
return {
2018-05-29 15:42:22 +00:00
addItem,
getList,
removeAll,
hasData,
getAppliedFilters
2018-05-15 14:21:44 +00:00
};
});