Skip to content

Commit

Permalink
Add translator_index and translator_id to terms panel
Browse files Browse the repository at this point in the history
Added two options which control an extra query into the index defined by
translator_index to lookup the document with translator_id. This document is
then used as a translator to process the incoming terms and assign readable
labels to easy recognition.
  • Loading branch information
t0mas committed Apr 7, 2014
1 parent 2581d31 commit eea0035
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/app/panels/terms/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ <h5>View Options</h5>
</div>
</div>
</div>
<div class="editor-row">
<div class="section">
<h5>Translator Options</h5>
<div class="editor-option">
<label class="small">
Translator Index
<i class="icon-question-sign" bs-tooltip="'Lookup a translator document by ID in this index to map terms to display labels'"></i>
</label>
<input type="text" ng-model="panel.translator_index" ng-change="set_refresh(true)">
</div>
<div class="editor-option">
<label class="small">
Translator Id
<i class="icon-question-sign" bs-tooltip="'Use this document ID to lookup the translator document'"></i>
</label>
<input type="text" class="input-small" ng-model="panel.translator_id" ng-change="set_refresh(true)">
</div>
</div>
</div>

46 changes: 40 additions & 6 deletions src/app/panels/terms/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function (angular, app, _, $, kbn) {
var module = angular.module('kibana.panels.terms', []);
app.useModule(module);

module.controller('terms', function($scope, querySrv, dashboard, filterSrv, fields) {
module.controller('terms', function($scope, querySrv, dashboard, filterSrv, fields, $q) {
$scope.panelMeta = {
modals : [
{
Expand Down Expand Up @@ -197,24 +197,49 @@ function (angular, app, _, $, kbn) {
// Populate the inspector panel
$scope.inspector = angular.toJson(JSON.parse(request.toString()),true);

// Execute the results query
results = request.doSearch();

// Populate scope when we have results
results.then(function(results) {
// Get the translators if enabled
var translators;
if(!_.isEmpty($scope.panel.translator_index) && !_.isEmpty($scope.panel.translator_id)) {
translators = $scope.ejs.Request()
.indices($scope.panel.translator_index)
.query($scope.ejs.IdsQuery($scope.panel.translator_id))
.doSearch();
} else {
// If we don't need translators we will provide a pre-resolved promise
var emptyTranslatorDeferred = $q.defer();
translators = emptyTranslatorDeferred.promise;
emptyTranslatorDeferred.resolve({});
}

// Populate scope when we have all results
$q.all([results, translators]).then(function(combinedResults) {
// Process results as usual
results = combinedResults[0];
$scope.panelMeta.loading = false;
if($scope.panel.tmode === 'terms') {
$scope.hits = results.hits.total;
}

$scope.results = results;

// Now also process translators
translators = combinedResults[1];
if(!_.isEmpty(translators) && translators.hits.total > 0) {
$scope.translator = translators.hits.hits[0]._source;
} else {
$scope.translator = {};
}

$scope.$emit('render');
});
};

$scope.build_search = function(term,negate) {
if(_.isUndefined(term.meta)) {
filterSrv.set({type:'terms',field:$scope.field,value:term.label,
filterSrv.set({type:'terms',field:$scope.field,value:term.term,
mandate:(negate ? 'mustNot':'must')});
} else if(term.meta === 'missing') {
filterSrv.set({type:'exists',field:$scope.field,
Expand Down Expand Up @@ -272,10 +297,10 @@ function (angular, app, _, $, kbn) {
_.each(scope.results.facets.terms.terms, function(v) {
var slice;
if(scope.panel.tmode === 'terms') {
slice = { label : v.term, data : [[k,v.count]], actions: true};
slice = { term : v.term, label : v.term, data : [[k,v.count]], actions: true};
}
if(scope.panel.tmode === 'terms_stats') {
slice = { label : v.term, data : [[k,v[scope.panel.tstat]]], actions: true};
slice = { term : v.term, label : v.term, data : [[k,v[scope.panel.tstat]]], actions: true};
}
scope.data.push(slice);
k = k + 1;
Expand Down Expand Up @@ -306,6 +331,15 @@ function (angular, app, _, $, kbn) {
chartData = scope.panel.other ? chartData :
_.without(chartData,_.findWhere(chartData,{meta:'other'}));

// Translate data when a translator is available
if(!_.isEmpty(scope.translator)) {
_.forEach(chartData, function(data) {
if(!_.isEmpty(scope.translator[data.term])) {
data.label = scope.translator[data.term];
}
})
}

// Populate element.
require(['jquery.flot.pie'], function(){
// Populate element
Expand Down

0 comments on commit eea0035

Please sign in to comment.