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
|
|
|
|
2018-05-29 15:42:22 +00:00
|
|
|
const getAppliedFilters = () => {
|
|
|
|
let 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.type &&
|
|
|
|
item._scope.savedObj.vis.type.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');
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-05-29 15:42:22 +00:00
|
|
|
appliedFilters = {
|
|
|
|
filters,
|
|
|
|
time:{
|
|
|
|
from: dateMath.parse(from),
|
|
|
|
to: dateMath.parse(to)
|
2018-06-01 11:50:57 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
};
|
|
|
|
});
|