Skip to content

Commit

Permalink
Add index pattern selector to discover, closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashid Khan committed Apr 13, 2014
1 parent a94a48a commit 0781efc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 61 deletions.
122 changes: 68 additions & 54 deletions src/kibana/apps/discover/controllers/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ define(function (require) {
resolve: {
savedSearch: function (savedSearches, $route) {
return savedSearches.get($route.current.params.id);
},
patternList: function (es, configFile) {
// TODO: This is inefficient because it pulls down all of the cached mappings for every
// configured pattern instead of only the currently selected one.
return es.search({
index: configFile.kibanaIndex,
type: 'mapping',
size: 50000,
body: {
query: {match_all: {}},
}
})
.then(function (res) {
return res.hits.hits;
});
}
}
});
Expand All @@ -34,19 +49,26 @@ define(function (require) {
{ display: 'Yearly', val: 'yearly' }
];

app.controller('discover', function ($scope, config, $q, $route, savedSearches, courier, createNotifier, $location, state) {
app.controller('discover', function ($scope, config, $q, $route, savedSearches, courier, createNotifier, $location,
state, es, configFile) {
var notify = createNotifier({
location: 'Discover'
});

// the saved savedSearch
var savedSearch = $route.current.locals.savedSearch;

// the actual courier.SearchSource
var searchSource = savedSearch.searchSource;

/* Manage state & url state */
var initialQuery = searchSource.get('query');

function init() {
setFields(_.findLast($scope.opts.patternList, {_id: $scope.opts.index})._source);
updateDataSource();
}

function loadState() {
$scope.state = state.get();
$scope.state = _.defaults($scope.state, {
Expand All @@ -65,8 +87,8 @@ define(function (require) {
maxSummaryLength: 100,
// Index to match
index: config.get('defaultIndex'),
timefield: '@timestamp',
savedSearch: savedSearch
savedSearch: savedSearch,
patternList: $route.current.locals.patternList
};

$scope.opts.saveDataSource = function () {
Expand Down Expand Up @@ -95,12 +117,16 @@ define(function (require) {
$scope.fetch();
}
});
// the index to use when they don't specify one
$scope.$watch('opts.index', function (val) {
if (!val) return;
updateDataSource();
$scope.fetch();
});

// Bind a result handler. Any time scope.fetch() is executed this gets called
// with the results
searchSource.onResults().then(function onResults(resp) {
if (!$scope.fields) getFields();

$scope.rows = resp.hits.hits;
$scope.chart = {rows: [{columns: [{
label: 'Events over time',
Expand Down Expand Up @@ -161,12 +187,15 @@ define(function (require) {
if ($scope.opts.index !== searchSource.get('index')) {
// set the index on the savedSearch
searchSource.index($scope.opts.index);
delete $scope.fields;
delete $scope.columns;

setFields(_.findLast($scope.opts.patternList, {_id: $scope.opts.index})._source);

// clear the columns and fields, then refetch when we do a savedSearch
//$scope.state.columns = $scope.fields = null;
}

if (!$scope.fields) getFields();

var sort = {};
sort[$scope.state.sort[0]] = $scope.state.sort[1];

Expand All @@ -177,16 +206,20 @@ define(function (require) {
query: $scope.state.query
}
})
.sort([sort])
.sort([sort]);

if (!!$scope.opts.timefield) {
searchSource
.aggs({
events: {
date_histogram: {
field: '@timestamp',
field: $scope.opts.timefield,
interval: '12h',
format: 'yyyy-MM-dd'
}
}
});
}
}

$scope.fetch = function () {
Expand All @@ -209,56 +242,45 @@ define(function (require) {
return obj;
}

var activeGetFields;
function getFields() {
var defer = $q.defer();

if (activeGetFields) {
activeGetFields.then(function () {
defer.resolve();
});
return;
}

function setFields(fields) {
var currentState = _.transform($scope.fields || [], function (current, field) {
current[field.name] = {
display: field.display
};
}, {});

searchSource
.getFields()
.then(function (fields) {
if (!fields) return;

if (!fields) return;

var columnObjects = arrayToKeys($scope.state.columns);
var columnObjects = arrayToKeys($scope.state.columns);

$scope.fields = [];
$scope.state.columns = $scope.state.columns || [];
$scope.fields = [];
$scope.state.columns = $scope.state.columns || [];

// Inject source into list;
$scope.fields.push({name: '_source', type: 'source', display: false});
// Inject source into list;
$scope.fields.push({name: '_source', type: 'source', display: false});

_(fields)
.keys()
.sort()
.each(function (name) {
var field = fields[name];
field.name = name;
_(fields)
.keys()
.sort()
.each(function (name) {
var field = fields[name];
field.name = name;

_.defaults(field, currentState[name]);
$scope.fields.push(_.defaults(field, {display: columnObjects[name] || false}));
});
_.defaults(field, currentState[name]);
$scope.fields.push(_.defaults(field, {display: columnObjects[name] || false}));
});

refreshColumns();
// TODO: timefield should be associated with the index pattern, this is a hack
// to pick the first date field and use it.
var timefields = _.find($scope.fields, {type: 'date'});
if (!!timefields) {
$scope.opts.timefield = timefields.name;
} else {
delete $scope.opts.timefield;
}

defer.resolve();
}, defer.reject);
refreshColumns();

return defer.promise.then(function () {
activeGetFields = null;
});
}

// TODO: On array fields, negating does not negate the combination, rather all terms
Expand Down Expand Up @@ -291,14 +313,6 @@ define(function (require) {
refreshColumns();
};

$scope.refreshFieldList = function () {
searchSource.clearFieldCache(function () {
getFields().then(function () {
$scope.fetch();
});
});
};

function refreshColumns() {
// Get all displayed field names;
var fields = _.pluck(_.filter($scope.fields, function (field) {
Expand All @@ -325,7 +339,7 @@ define(function (require) {
return str;
};

updateDataSource();
init();
$scope.$emit('application.load');
});
});
2 changes: 1 addition & 1 deletion src/kibana/apps/discover/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</disc-field-chooser>
</div>
<div class="col-md-10">
<discover-timechart data="chart" height="150"></discover-timechart>
<discover-timechart data="chart" height="150" ng-if="opts.timefield"></discover-timechart>
<div class="discover-table"
fixed-scroll='table'
fixed-scroll-trigger="state.columns">
Expand Down
20 changes: 14 additions & 6 deletions src/kibana/apps/discover/partials/settings.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<label class="control-label">Index</label>
<input class="form-control" ng-model="opts.index">
</div>
<div class="col-md-6">
<div class="col-md-9">
<div class="form-group">
<label class="control-label">Name</label>
<label class="control-label">Save Search</label>
<input ng-model="opts.savedSearch.title" class="form-control">
</div>
<div class="form-group">
Expand All @@ -15,5 +11,17 @@
</button>
</div>
</div>
<div class="col-md-3">
<label class="control-label">Index Pattern <a class="small" ng-href="#/settings/indices/{{opts.index}}">Configure Pattern</a>
</label>
<select
class="form-control"
ng-model="opts.index"
ng-options="obj._id as obj._id for obj in opts.patternList">
</select>
<small>
Time field: <strong>{{opts.timefield || 'not configured'}}</strong>
</small>
</div>
</div>
</div>

0 comments on commit 0781efc

Please sign in to comment.