2013-04-20 12:55:15 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
## Table
|
|
|
|
|
|
|
|
|
|
A paginated table of events matching a query
|
|
|
|
|
|
|
|
|
|
### Parameters
|
|
|
|
|
* query :: A string representing then current query
|
|
|
|
|
* size :: Number of events per page to show
|
|
|
|
|
* pages :: Number of pages to show. size * pages = number of cached events.
|
|
|
|
|
Bigger = more memory usage byh the browser
|
|
|
|
|
* offset :: Position from which to start in the array of hits
|
|
|
|
|
* sort :: An array with 2 elements. sort[0]: field, sort[1]: direction ('asc' or 'desc')
|
|
|
|
|
* style :: hash of css properties
|
|
|
|
|
* fields :: columns to show in table
|
2013-05-24 01:45:59 +08:00
|
|
|
* overflow :: 'height' or 'min-height' controls wether the row will expand (min-height) to
|
|
|
|
|
to fit the table, or if the table will scroll to fit the row (height)
|
2013-04-20 12:55:15 +08:00
|
|
|
* sortable :: Allow sorting?
|
|
|
|
|
* spyable :: Show the 'eye' icon that reveals the last ES query for this panel
|
|
|
|
|
### Group Events
|
|
|
|
|
#### Sends
|
|
|
|
|
* table_documents :: An array containing all of the documents in the table.
|
|
|
|
|
Only used by the fields panel so far.
|
|
|
|
|
#### Receives
|
|
|
|
|
* time :: An object containing the time range to use and the index(es) to query
|
|
|
|
|
* query :: An Array of queries, even if its only one
|
|
|
|
|
* sort :: An array with 2 elements. sort[0]: field, sort[1]: direction ('asc' or 'desc')
|
|
|
|
|
* selected_fields :: An array of fields to show
|
|
|
|
|
*/
|
|
|
|
|
|
2013-01-31 12:39:57 +08:00
|
|
|
angular.module('kibana.table', [])
|
2013-07-15 07:15:20 +08:00
|
|
|
.controller('table', function($rootScope, $scope, eventBus, fields, query, dashboard, filterSrv) {
|
2013-01-31 12:39:57 +08:00
|
|
|
|
|
|
|
|
// Set and populate defaults
|
|
|
|
|
var _d = {
|
2013-06-18 01:34:18 +08:00
|
|
|
status : "Stable",
|
2013-01-31 12:39:57 +08:00
|
|
|
query : "*",
|
2013-03-31 02:20:20 +08:00
|
|
|
size : 100, // Per page
|
|
|
|
|
pages : 5, // Pages available
|
2013-02-19 01:53:25 +08:00
|
|
|
offset : 0,
|
2013-02-06 05:30:08 +08:00
|
|
|
sort : ['@timestamp','desc'],
|
|
|
|
|
group : "default",
|
2013-03-31 02:20:20 +08:00
|
|
|
style : {'font-size': '9pt'},
|
2013-05-24 01:45:59 +08:00
|
|
|
overflow: 'height',
|
2013-02-07 05:14:17 +08:00
|
|
|
fields : [],
|
2013-05-18 05:21:25 +08:00
|
|
|
highlight : [],
|
2013-03-21 12:27:07 +08:00
|
|
|
sortable: true,
|
2013-05-09 22:13:38 +08:00
|
|
|
header : true,
|
|
|
|
|
paging : true,
|
|
|
|
|
spyable: true
|
2013-02-06 05:30:08 +08:00
|
|
|
}
|
|
|
|
|
_.defaults($scope.panel,_d)
|
|
|
|
|
|
|
|
|
|
$scope.init = function () {
|
2013-02-08 06:05:55 +08:00
|
|
|
|
|
|
|
|
$scope.set_listeners($scope.panel.group)
|
2013-05-24 01:45:59 +08:00
|
|
|
|
2013-02-08 06:05:55 +08:00
|
|
|
// Now that we're all setup, request the time from our group
|
|
|
|
|
eventBus.broadcast($scope.$id,$scope.panel.group,"get_time")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.set_listeners = function(group) {
|
2013-07-12 08:05:50 +08:00
|
|
|
$scope.$on('refresh',function(){$scope.get_data()})
|
2013-02-08 06:05:55 +08:00
|
|
|
eventBus.register($scope,'sort', function(event,sort){
|
2013-02-07 05:14:17 +08:00
|
|
|
$scope.panel.sort = _.clone(sort);
|
2013-02-13 07:25:39 +08:00
|
|
|
$scope.get_data();
|
2013-02-07 05:14:17 +08:00
|
|
|
});
|
2013-02-08 06:05:55 +08:00
|
|
|
eventBus.register($scope,'selected_fields', function(event, fields) {
|
2013-02-07 05:14:17 +08:00
|
|
|
$scope.panel.fields = _.clone(fields)
|
|
|
|
|
});
|
2013-05-14 04:31:59 +08:00
|
|
|
eventBus.register($scope,'table_documents', function(event, docs) {
|
2013-07-12 08:05:50 +08:00
|
|
|
query.list[query.ids[0]].query = docs.query;
|
2013-05-14 04:31:59 +08:00
|
|
|
$scope.data = docs.docs;
|
|
|
|
|
});
|
2013-01-31 12:39:57 +08:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 05:14:17 +08:00
|
|
|
$scope.set_sort = function(field) {
|
|
|
|
|
if($scope.panel.sort[0] === field)
|
|
|
|
|
$scope.panel.sort[1] = $scope.panel.sort[1] == 'asc' ? 'desc' : 'asc';
|
|
|
|
|
else
|
|
|
|
|
$scope.panel.sort[0] = field;
|
2013-02-13 07:25:39 +08:00
|
|
|
$scope.get_data();
|
2013-02-01 11:23:34 +08:00
|
|
|
}
|
|
|
|
|
|
2013-02-08 06:05:55 +08:00
|
|
|
$scope.toggle_field = function(field) {
|
|
|
|
|
if (_.indexOf($scope.panel.fields,field) > -1)
|
|
|
|
|
$scope.panel.fields = _.without($scope.panel.fields,field)
|
|
|
|
|
else
|
|
|
|
|
$scope.panel.fields.push(field)
|
2013-02-26 00:32:54 +08:00
|
|
|
broadcast_results();
|
2013-02-08 06:05:55 +08:00
|
|
|
}
|
|
|
|
|
|
2013-05-18 05:21:25 +08:00
|
|
|
$scope.toggle_highlight = function(field) {
|
|
|
|
|
if (_.indexOf($scope.panel.highlight,field) > -1)
|
|
|
|
|
$scope.panel.highlight = _.without($scope.panel.highlight,field)
|
|
|
|
|
else
|
|
|
|
|
$scope.panel.highlight.push(field)
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 12:41:56 +08:00
|
|
|
$scope.toggle_details = function(row) {
|
|
|
|
|
row.kibana = row.kibana || {};
|
|
|
|
|
row.kibana.details = !row.kibana.details ? $scope.without_kibana(row) : false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 01:53:25 +08:00
|
|
|
$scope.page = function(page) {
|
|
|
|
|
$scope.panel.offset = page*$scope.panel.size
|
|
|
|
|
$scope.get_data();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 04:40:14 +08:00
|
|
|
$scope.build_search = function(field,value,negate) {
|
2013-07-15 07:15:20 +08:00
|
|
|
var query = (negate ? '-':'+')+field+":\""+value+"\""
|
|
|
|
|
filterSrv.set({type:'querystring',query:query})
|
2013-02-19 12:41:56 +08:00
|
|
|
$scope.panel.offset = 0;
|
2013-07-15 07:15:20 +08:00
|
|
|
dashboard.refresh();
|
2013-02-19 12:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
2013-03-31 02:20:20 +08:00
|
|
|
$scope.get_data = function(segment,query_id) {
|
2013-04-06 04:01:32 +08:00
|
|
|
$scope.panel.error = false;
|
|
|
|
|
|
2013-02-05 08:05:36 +08:00
|
|
|
// Make sure we have everything for the request to complete
|
2013-07-15 07:15:20 +08:00
|
|
|
if(dashboard.indices.length == 0) {
|
2013-02-05 08:05:36 +08:00
|
|
|
return
|
2013-07-15 07:15:20 +08:00
|
|
|
}
|
2013-02-28 01:00:39 +08:00
|
|
|
|
|
|
|
|
$scope.panel.loading = true;
|
2013-01-31 12:39:57 +08:00
|
|
|
|
2013-03-31 02:20:20 +08:00
|
|
|
var _segment = _.isUndefined(segment) ? 0 : segment
|
2013-04-01 12:29:15 +08:00
|
|
|
$scope.segment = _segment;
|
2013-03-31 02:20:20 +08:00
|
|
|
|
2013-07-15 07:15:20 +08:00
|
|
|
var request = $scope.ejs.Request().indices(dashboard.indices[_segment])
|
2013-07-12 08:05:50 +08:00
|
|
|
|
|
|
|
|
var boolQuery = ejs.BoolQuery();
|
|
|
|
|
_.each(query.list,function(q) {
|
|
|
|
|
boolQuery = boolQuery.should(ejs.QueryStringQuery(q.query || '*'))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
request = request.query(
|
|
|
|
|
ejs.FilteredQuery(
|
|
|
|
|
boolQuery,
|
2013-07-15 07:15:20 +08:00
|
|
|
filterSrv.getBoolFilter(filterSrv.ids)
|
|
|
|
|
))
|
2013-05-18 05:21:25 +08:00
|
|
|
.highlight(
|
|
|
|
|
ejs.Highlight($scope.panel.highlight)
|
|
|
|
|
.fragmentSize(2147483647) // Max size of a 32bit unsigned int
|
|
|
|
|
.preTags('@start-highlight@')
|
|
|
|
|
.postTags('@end-highlight@')
|
|
|
|
|
)
|
2013-03-31 02:20:20 +08:00
|
|
|
.size($scope.panel.size*$scope.panel.pages)
|
2013-03-12 08:13:47 +08:00
|
|
|
.sort($scope.panel.sort[0],$scope.panel.sort[1]);
|
|
|
|
|
|
|
|
|
|
$scope.populate_modal(request)
|
|
|
|
|
|
2013-04-06 03:11:47 +08:00
|
|
|
var results = request.doSearch()
|
2013-01-31 12:39:57 +08:00
|
|
|
|
|
|
|
|
// Populate scope when we have results
|
|
|
|
|
results.then(function(results) {
|
2013-02-28 01:00:39 +08:00
|
|
|
$scope.panel.loading = false;
|
|
|
|
|
|
2013-03-31 02:20:20 +08:00
|
|
|
if(_segment === 0) {
|
2013-04-02 06:12:07 +08:00
|
|
|
$scope.hits = 0;
|
2013-03-31 02:20:20 +08:00
|
|
|
$scope.data = [];
|
|
|
|
|
query_id = $scope.query_id = new Date().getTime()
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-06 04:01:32 +08:00
|
|
|
// Check for error and abort if found
|
2013-04-06 03:11:47 +08:00
|
|
|
if(!(_.isUndefined(results.error))) {
|
|
|
|
|
$scope.panel.error = $scope.parse_error(results.error);
|
2013-02-01 11:23:34 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2013-03-31 02:20:20 +08:00
|
|
|
|
|
|
|
|
// Check that we're still on the same query, if not stop
|
|
|
|
|
if($scope.query_id === query_id) {
|
|
|
|
|
$scope.data= $scope.data.concat(_.map(results.hits.hits, function(hit) {
|
2013-05-18 05:21:25 +08:00
|
|
|
return {
|
|
|
|
|
_source : flatten_json(hit['_source']),
|
|
|
|
|
highlight : flatten_json(hit['highlight']||{})
|
|
|
|
|
}
|
2013-03-31 02:20:20 +08:00
|
|
|
}));
|
|
|
|
|
|
2013-04-02 06:12:07 +08:00
|
|
|
$scope.hits += results.hits.total;
|
|
|
|
|
|
2013-03-31 02:20:20 +08:00
|
|
|
// Sort the data
|
|
|
|
|
$scope.data = _.sortBy($scope.data, function(v){
|
2013-05-18 05:21:25 +08:00
|
|
|
return v._source[$scope.panel.sort[0]]
|
2013-03-31 02:20:20 +08:00
|
|
|
});
|
2013-04-02 07:52:18 +08:00
|
|
|
|
2013-03-31 02:20:20 +08:00
|
|
|
// Reverse if needed
|
|
|
|
|
if($scope.panel.sort[1] == 'desc')
|
|
|
|
|
$scope.data.reverse();
|
|
|
|
|
|
|
|
|
|
// Keep only what we need for the set
|
|
|
|
|
$scope.data = $scope.data.slice(0,$scope.panel.size * $scope.panel.pages)
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This breaks, use $scope.data for this
|
2013-05-18 05:21:25 +08:00
|
|
|
$scope.all_fields = get_all_fields(_.pluck($scope.data,'_source'));
|
2013-02-20 07:27:07 +08:00
|
|
|
broadcast_results();
|
2013-03-31 02:20:20 +08:00
|
|
|
|
|
|
|
|
// If we're not sorting in reverse chrono order, query every index for
|
|
|
|
|
// size*pages results
|
|
|
|
|
// Otherwise, only get size*pages results then stop querying
|
2013-07-15 07:15:20 +08:00
|
|
|
if($scope.data.length < $scope.panel.size*$scope.panel.pages
|
|
|
|
|
//($scope.data.length < $scope.panel.size*$scope.panel.pages
|
|
|
|
|
// || !(($scope.panel.sort[0] === $scope.time.field) && $scope.panel.sort[1] === 'desc'))
|
|
|
|
|
&& _segment+1 < dashboard.indices.length
|
2013-03-31 02:20:20 +08:00
|
|
|
) {
|
|
|
|
|
$scope.get_data(_segment+1,$scope.query_id)
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-31 12:39:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-12 08:13:47 +08:00
|
|
|
$scope.populate_modal = function(request) {
|
|
|
|
|
$scope.modal = {
|
|
|
|
|
title: "Table Inspector",
|
|
|
|
|
body : "<h5>Last Elasticsearch Query</h5><pre>"+
|
2013-05-07 02:23:07 +08:00
|
|
|
'curl -XGET '+config.elasticsearch+'/'+$scope.index+"/_search?pretty -d'\n"+
|
2013-03-12 08:13:47 +08:00
|
|
|
angular.toJson(JSON.parse(request.toString()),true)+
|
|
|
|
|
"'</pre>",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 12:41:56 +08:00
|
|
|
$scope.without_kibana = function (row) {
|
2013-05-18 05:21:25 +08:00
|
|
|
return {
|
|
|
|
|
_source : row._source,
|
|
|
|
|
highlight : row.highlight
|
|
|
|
|
}
|
2013-02-19 12:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
2013-02-08 06:05:55 +08:00
|
|
|
// Broadcast a list of all fields. Note that receivers of field array
|
|
|
|
|
// events should be able to receive from multiple sources, merge, dedupe
|
|
|
|
|
// and sort on the fly if needed.
|
2013-02-20 07:27:07 +08:00
|
|
|
function broadcast_results() {
|
2013-02-08 06:05:55 +08:00
|
|
|
eventBus.broadcast($scope.$id,$scope.panel.group,"fields", {
|
|
|
|
|
all : $scope.all_fields,
|
|
|
|
|
sort : $scope.panel.sort,
|
|
|
|
|
active: $scope.panel.fields
|
|
|
|
|
});
|
2013-02-20 07:27:07 +08:00
|
|
|
eventBus.broadcast($scope.$id,$scope.panel.group,"table_documents",
|
2013-05-14 04:31:59 +08:00
|
|
|
{
|
2013-07-12 08:05:50 +08:00
|
|
|
query: query.list[query.ids[0]].query,
|
2013-05-18 05:21:25 +08:00
|
|
|
docs : _.pluck($scope.data,'_source'),
|
2013-05-14 04:31:59 +08:00
|
|
|
index: $scope.index
|
|
|
|
|
});
|
2013-02-08 06:05:55 +08:00
|
|
|
}
|
|
|
|
|
|
2013-05-18 05:21:25 +08:00
|
|
|
$scope.set_refresh = function (state) {
|
|
|
|
|
$scope.refresh = state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.close_edit = function() {
|
|
|
|
|
if($scope.refresh)
|
|
|
|
|
$scope.get_data();
|
|
|
|
|
$scope.refresh = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.filter('highlight', function() {
|
|
|
|
|
return function(text) {
|
2013-06-11 20:43:58 +08:00
|
|
|
if (!_.isUndefined(text) && !_.isNull(text) && text.toString().length > 0) {
|
2013-05-18 05:21:25 +08:00
|
|
|
return text.toString().
|
|
|
|
|
replace(/&/g, '&').
|
|
|
|
|
replace(/</g, '<').
|
|
|
|
|
replace(/>/g, '>').
|
2013-05-29 20:15:40 +08:00
|
|
|
replace(/\r?\n/g, '<br/>').
|
2013-05-18 05:21:25 +08:00
|
|
|
replace(/@start-highlight@/g, '<code class="highlight">').
|
|
|
|
|
replace(/@end-highlight@/g, '</code>')
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2013-05-29 20:15:40 +08:00
|
|
|
});
|