Commit 52eeefa6 by Torkel Ödegaard

feat(elasticsearch): fields are fetch from mapping instead of docs, you can…

feat(elasticsearch): fields are fetch from mapping instead of docs, you can enter custom value in field options, other fixes, #1034
parent f942ec95
...@@ -20,7 +20,6 @@ function (angular, app, _, $) { ...@@ -20,7 +20,6 @@ function (angular, app, _, $) {
return { return {
scope: { scope: {
segment: "=", segment: "=",
noCustom: "=",
getOptions: "&", getOptions: "&",
onChange: "&", onChange: "&",
}, },
...@@ -48,7 +47,7 @@ function (angular, app, _, $) { ...@@ -48,7 +47,7 @@ function (angular, app, _, $) {
segment.fake = false; segment.fake = false;
segment.expandable = selected.expandable; segment.expandable = selected.expandable;
} }
else if ($scope.noCustom === false) { else if (segment.custom !== 'false') {
segment.value = value; segment.value = value;
segment.html = $sce.trustAsHtml(value); segment.html = $sce.trustAsHtml(value);
segment.expandable = true; segment.expandable = true;
...@@ -83,7 +82,7 @@ function (angular, app, _, $) { ...@@ -83,7 +82,7 @@ function (angular, app, _, $) {
options = _.map($scope.altSegments, function(alt) { return alt.value; }); options = _.map($scope.altSegments, function(alt) { return alt.value; });
// add custom values // add custom values
if ($scope.noCustom === false) { if (segment.custom !== 'false') {
if (!segment.fake && _.indexOf(options, segment.value) === -1) { if (!segment.fake && _.indexOf(options, segment.value) === -1) {
options.unshift(segment.value); options.unshift(segment.value);
} }
...@@ -161,7 +160,7 @@ function (angular, app, _, $) { ...@@ -161,7 +160,7 @@ function (angular, app, _, $) {
.module('grafana.directives') .module('grafana.directives')
.directive('metricSegmentModel', function(uiSegmentSrv, $q) { .directive('metricSegmentModel', function(uiSegmentSrv, $q) {
return { return {
template: '<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()" no-custom="true"></metric-segment>', template: '<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()"></metric-segment>',
restrict: 'E', restrict: 'E',
scope: { scope: {
property: "=", property: "=",
...@@ -175,9 +174,9 @@ function (angular, app, _, $) { ...@@ -175,9 +174,9 @@ function (angular, app, _, $) {
$scope.valueToSegment = function(value) { $scope.valueToSegment = function(value) {
var option = _.findWhere($scope.options, {value: value}); var option = _.findWhere($scope.options, {value: value});
if (option) { if (option) {
return uiSegmentSrv.newSegment({value: option.text, cssClass: attrs.cssClass}); return uiSegmentSrv.newSegment({value: option.text, cssClass: attrs.cssClass, custom: attrs.custom});
} else { } else {
return uiSegmentSrv.newSegment({value: value, cssClass: attrs.cssClass}); return uiSegmentSrv.newSegment({value: value, cssClass: attrs.cssClass, custom: attrs.custom});
} }
}; };
......
...@@ -257,43 +257,19 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) { ...@@ -257,43 +257,19 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) {
var timeFrom = this.translateTime(timeSrv.time.from); var timeFrom = this.translateTime(timeSrv.time.from);
var timeTo = this.translateTime(timeSrv.time.to); var timeTo = this.translateTime(timeSrv.time.to);
var query = { return this._get('/_mapping').then(function(res) {
size: 10,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": timeFrom,
"lte": timeTo
}
}
}
],
}
}
}
}
};
return this._post('/_search?', query).then(function(res) {
var fields = {}; var fields = {};
for (var i = 0; i < res.hits.hits.length; i++) { for (var indexName in res) {
var hit = res.hits.hits[i]; var index = res[indexName];
for (var field in hit) { var mappings = index.mappings;
if (hit.hasOwnProperty(field) && field[0] !== '_') { if (!mappings) { continue; }
fields[field] = 1; for (var typeName in mappings) {
} var properties = mappings[typeName].properties;
} for (var field in properties) {
var prop = properties[field];
if (hit._source) { if (prop.type && field[0] !== '_') {
for (var fieldProp in hit._source) { fields[field] = prop;
if (hit._source.hasOwnProperty(fieldProp)) {
fields[fieldProp] = 1;
} }
} }
} }
......
...@@ -27,6 +27,8 @@ function (angular, _, queryDef) { ...@@ -27,6 +27,8 @@ function (angular, _, queryDef) {
}); });
$scope.validateModel = function() { $scope.validateModel = function() {
$scope.settingsLinkText = '';
if (!$scope.agg.field) { if (!$scope.agg.field) {
$scope.agg.field = 'select field'; $scope.agg.field = 'select field';
} }
...@@ -43,6 +45,7 @@ function (angular, _, queryDef) { ...@@ -43,6 +45,7 @@ function (angular, _, queryDef) {
$scope.onTypeChange = function() { $scope.onTypeChange = function() {
$scope.agg.settings = {}; $scope.agg.settings = {};
$scope.validateModel();
$scope.onChange(); $scope.onChange();
}; };
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<span ng-hide="isFirst">Then by</span> <span ng-hide="isFirst">Then by</span>
</li> </li>
<li> <li>
<metric-segment-model property="agg.type" options="bucketAggTypes" on-change="onChangeInternal()"></metric-segment-model> <metric-segment-model property="agg.type" options="bucketAggTypes" on-change="onChangeInternal()" custom="false"></metric-segment-model>
<metric-segment-model property="agg.field" get-options="getFields()" on-change="onChangeInternal()"></metric-segment> <metric-segment-model property="agg.field" get-options="getFields()" on-change="onChangeInternal()"></metric-segment>
</li> </li>
<li class="tight-form-item tight-form-align" ng-if="settingsLinkText"> <li class="tight-form-item tight-form-align" ng-if="settingsLinkText">
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
Metric Metric
</li> </li>
<li> <li>
<metric-segment-model property="agg.type" options="metricAggTypes" on-change="onTypeChange()"></metric-segment-model> <metric-segment-model property="agg.type" options="metricAggTypes" on-change="onTypeChange()" custom="false"></metric-segment-model>
</li> </li>
<li ng-if="agg.type !== 'count'"> <li ng-if="agg.type !== 'count'">
<metric-segment-model property="agg.field" get-options="getFields()" on-change="onChange()"></metric-segment> <metric-segment-model property="agg.field" get-options="getFields()" on-change="onChange()"></metric-segment>
......
...@@ -24,6 +24,7 @@ function (angular, _) { ...@@ -24,6 +24,7 @@ function (angular, _) {
} }
this.cssClass = options.cssClass; this.cssClass = options.cssClass;
this.custom = options.custom;
this.type = options.type; this.type = options.type;
this.fake = options.fake; this.fake = options.fake;
this.value = options.value; this.value = options.value;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment