Skip to content

Commit

Permalink
Merge pull request #1 from jseagull/master
Browse files Browse the repository at this point in the history
new feature of pie: support statistical facet
  • Loading branch information
mountain committed Jun 9, 2013
2 parents 050ee74 + 911c912 commit f8eb479
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 17 deletions.
35 changes: 32 additions & 3 deletions panels/pie/editor.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row-fluid" ng-switch="panel.mode">
<div class="span3">
<label class="small">Mode</label>
<select class="input-small" ng-change="set_mode(panel.mode)" ng-model="panel.mode" ng-options="f for f in ['terms','goal']"></select>
<select class="input-small" ng-change="set_mode(panel.mode)" ng-model="panel.mode" ng-options="f for f in ['terms','statistics','goal']"></select>
</div>
<div ng-switch-when="terms">
<div class="row-fluid">
Expand Down Expand Up @@ -33,11 +33,40 @@
</div>
</div>
</div>
<div ng-switch-when="statistics">
<div class="row-fluid">
<div class="span3">
<form style="margin-bottom: 0px">
<label class="small">Key</label>
<input type="text" style="width:90%" bs-typeahead="fields.list" ng-model="panel.query.field">
</form>
</div>
<div class="span3">
<form style="margin-bottom: 0px">
<label class="small">Value</label>
<input type="text" style="width:90%" bs-typeahead="fields.list" ng-model="panel.query.value">
</form>
</div>
<div class="span5">
<form class="input-append" style="margin-left: 150px">
<label class="small">Query</label>
<input type="text" style="width:80%" ng-model="panel.query.query">
<button class="btn" ng-click="get_data()"><i class="icon-search"></i></button>
</form>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label class="small">Length</label>
<input type="number" style="width:80%" ng-model="panel.size" ng-change="get_data()">
</div>
</div>
</div>
<div ng-switch-when="goal">
<div class="row-fluid">
<div class="span3">
<label class="small">Mode</label>
<select class="input-small" ng-change="set_mode(panel.mode)" ng-model="panel.mode" ng-options="f for f in ['terms','goal']"></select>
<select class="input-small" ng-change="set_mode(panel.mode)" ng-model="panel.mode" ng-options="f for f in ['terms','statistics','goal']"></select>
</div>
<div class="span2">
<form style="margin-bottom: 0px">
Expand Down Expand Up @@ -80,4 +109,4 @@ <h5>Panel Spy</h5>
be accessed by clicking the <i class='icon-eye-open'></i> in the top right
of the panel.
</div>
</div>
</div>
89 changes: 75 additions & 14 deletions panels/pie/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ angular.module('kibana.pie', [])
case 'terms':
$scope.panel.query = {query:"*",field:"_all"};
break;
case 'statistics':
$scope.panel.query = {query:"*",field:"_all",value:null};
break;
case 'goal':
$scope.panel.query = {query:"*",goal:100};
break;
Expand All @@ -98,10 +101,8 @@ angular.module('kibana.pie', [])

$scope.panel.loading = true;
var request = $scope.ejs.Request().indices($scope.index);

// Terms mode
if ($scope.panel.mode == "terms") {
request = request
termsRequest = function(request) {
return request
.facet(ejs.TermsFacet('pie')
.field($scope.panel.query.field || $scope.panel.default_field)
.size($scope.panel['size'])
Expand All @@ -113,8 +114,35 @@ angular.module('kibana.pie', [])
.from($scope.time.from)
.to($scope.time.to)
)))).size(0)
}

$scope.populate_modal(request);
statisticalRequest = function(request, term) {
return request
.facet(ejs.StatisticalFacet('pie')
.field($scope.panel.query.value)
.facetFilter(ejs.QueryFilter(
ejs.FilteredQuery(
ejs.TermQuery($scope.panel.query.field || '*', term),
ejs.RangeFilter($scope.time.field)
.from($scope.time.from)
.to($scope.time.to)
)))).size(0)
}

pushData = function(slice) {
$scope.data.push();
if(!(_.isUndefined($scope.panel.colors))
&& _.isArray($scope.panel.colors)
&& $scope.panel.colors.length > 0) {
slice.color = $scope.panel.colors[k%$scope.panel.colors.length];
}
$scope.data.push(slice)
}

// Terms mode
if ($scope.panel.mode == "terms") {

$scope.populate_modal(termsRequest(request));

var results = request.doSearch();

Expand All @@ -125,18 +153,51 @@ angular.module('kibana.pie', [])
$scope.data = [];
var k = 0;
_.each(results.facets.pie.terms, function(v) {
var slice = { label : v.term, data : v.count };
$scope.data.push();
if(!(_.isUndefined($scope.panel.colors))
&& _.isArray($scope.panel.colors)
&& $scope.panel.colors.length > 0) {
slice.color = $scope.panel.colors[k%$scope.panel.colors.length];
}
$scope.data.push(slice)
var slice = { label : v.term, data : v.count };
pushData(slice)
k = k + 1;
});
$scope.$emit('render');
});
// Statistic mode
} else if ($scope.panel.mode == "statistics") {

$scope.populate_modal(termsRequest(request));

var results = request.doSearch();

// Populate scope when we have results
results.then(function(results) {
$scope.panel.loading = false;
$scope.data = [];
// same as terms
if ($scope.panel.query.value === null) {
$scope.hits = results.hits.total;
var k = 0;
_.each(results.facets.pie.terms, function(v) {
var slice = { label : v.term, data : v.count };
pushData(slice)
k = k + 1;
});
$scope.$emit('render');
// statistical facet
} else {
$scope.hits = 0;
var k = 0;
_.each(results.facets.pie.terms, function(v) {
sub_request = statisticalRequest(request, v.term)
var ret = sub_request.doSearch();
ret.then(function(ret) {
var count = ret.facets.pie.total;
$scope.hits += count;
var slice = { label : v.term, data : count };
pushData(slice)
k = k + 1;
$scope.$emit('render');
});
});
}
});
// Goal mode
} else {
request = request
Expand Down Expand Up @@ -304,4 +365,4 @@ angular.module('kibana.pie', [])

}
};
})
})

0 comments on commit f8eb479

Please sign in to comment.