mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge branch 'elasticsearch-datasource' of https://github.com/aheinz-sg/grafana into elastic_ds
This commit is contained in:
@@ -4,9 +4,11 @@ define([
|
||||
'config',
|
||||
'kbn',
|
||||
'moment',
|
||||
'./queryBuilder',
|
||||
'./queryCtrl',
|
||||
'./directives'
|
||||
],
|
||||
function (angular, _, config, kbn, moment) {
|
||||
function (angular, _, config, kbn, moment, ElasticQueryBuilder) {
|
||||
'use strict';
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
@@ -292,8 +294,48 @@ function (angular, _, config, kbn, moment) {
|
||||
});
|
||||
};
|
||||
|
||||
ElasticDatasource.prototype.testDatasource = function() {
|
||||
var query = JSON.stringify();
|
||||
return this._post('/_search?search_type=count', query).then(function() {
|
||||
return { status: "success", message: "Data source is working", title: "Success" };
|
||||
});
|
||||
};
|
||||
|
||||
ElasticDatasource.prototype.query = function(options) {
|
||||
var queryBuilder = new ElasticQueryBuilder;
|
||||
var query = queryBuilder.build(options.targets);
|
||||
query = query.replace(/\$interval/g, options.interval);
|
||||
query = query.replace(/\$rangeFrom/g, options.range.from);
|
||||
query = query.replace(/\$rangeTo/g, options.range.to);
|
||||
query = query.replace(/\$maxDataPoints/g, options.maxDataPoints);
|
||||
query = templateSrv.replace(query, options.scopedVars);
|
||||
return this._post('/_search?search_type=count', query).then(this._getTimeSeries);
|
||||
};
|
||||
|
||||
ElasticDatasource.prototype._getTimeSeries = function(results) {
|
||||
var _aggregation2timeSeries = function(aggregation) {
|
||||
var datapoints = aggregation.date_histogram.buckets.map(function(entry) {
|
||||
return [entry.stats.avg, entry.key];
|
||||
});
|
||||
return { target: aggregation.key, datapoints: datapoints };
|
||||
};
|
||||
var data = [];
|
||||
|
||||
if (results && results.aggregations) {
|
||||
for (var target in results.aggregations) {
|
||||
if (!results.aggregations.hasOwnProperty(target)) {
|
||||
continue;
|
||||
}
|
||||
if (results.aggregations[target].date_histogram && results.aggregations[target].date_histogram.buckets) {
|
||||
data.push(_aggregation2timeSeries(results.aggregations[target]));
|
||||
} else if (results.aggregations[target].terms && results.aggregations[target].terms.buckets) {
|
||||
[].push.apply(data, results.aggregations[target].terms.buckets.map(_aggregation2timeSeries));
|
||||
}
|
||||
}
|
||||
}
|
||||
return { data: data };
|
||||
};
|
||||
|
||||
return ElasticDatasource;
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -6,6 +6,14 @@ function (angular) {
|
||||
|
||||
var module = angular.module('grafana.directives');
|
||||
|
||||
module.directive('metricQueryEditorElasticsearch', function() {
|
||||
return {controller: 'ElasticQueryCtrl', templateUrl: 'app/plugins/datasource/elasticsearch/partials/query.editor.html'};
|
||||
});
|
||||
|
||||
module.directive('metricQueryOptionsElasticsearch', function() {
|
||||
return {templateUrl: 'app/plugins/datasource/elasticsearch/partials/query.options.html'};
|
||||
});
|
||||
|
||||
module.directive('annotationsQueryEditorElasticsearch', function() {
|
||||
return {templateUrl: 'app/plugins/datasource/elasticsearch/partials/annotations.editor.html'};
|
||||
});
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
<div class="editor-row">
|
||||
|
||||
<div ng-repeat="target in panel.targets" ng-controller="ElasticQueryCtrl" ng-init="init()" ng-class="{'tight-form-disabled': target.hide}" class="tight-form-container-no-item-borders" style="margin-bottom: 10px">
|
||||
<div class="tight-form">
|
||||
<ul class="tight-form-list pull-right">
|
||||
<li ng-show="parserError" class="tight-form-item">
|
||||
<a bs-tooltip="parserError" style="color: rgb(229, 189, 28)" role="menuitem">
|
||||
<i class="fa fa-warning"></i>
|
||||
</a>
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
<a class="pointer" tabindex="1" ng-click="toggleQueryMode()">
|
||||
<i class="fa fa-pencil"></i>
|
||||
</a>
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
<div class="dropdown">
|
||||
<a class="pointer dropdown-toggle"
|
||||
data-toggle="dropdown"
|
||||
tabindex="1">
|
||||
<i class="fa fa-bars"></i>
|
||||
</a>
|
||||
<ul class="dropdown-menu pull-right" role="menu">
|
||||
<li role="menuitem">
|
||||
<a tabindex="1"
|
||||
ng-click="duplicate()">
|
||||
Duplicate
|
||||
</a>
|
||||
</li>
|
||||
<li role="menuitem">
|
||||
<a tabindex="1"
|
||||
ng-click="moveMetricQuery($index, $index-1)">
|
||||
Move up
|
||||
</a>
|
||||
</li>
|
||||
<li role="menuitem">
|
||||
<a tabindex="1"
|
||||
ng-click="moveMetricQuery($index, $index+1)">
|
||||
Move down
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</li>
|
||||
<li class="tight-form-item last">
|
||||
<a class="pointer" tabindex="1" ng-click="removeDataQuery(target)">
|
||||
<i class="fa fa-remove"></i>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="tight-form-list">
|
||||
<li>
|
||||
<a class="tight-form-item"
|
||||
ng-click="target.hide = !target.hide; get_data();"
|
||||
role="menuitem">
|
||||
<i class="fa fa-eye"></i>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<input type="text" class="tight-form-clear-input" style="width: 80%" ng-model="target.query" focus-me="target.rawQuery" spellcheck='false' ng-model-onblur ng-change="get_data()" ng-show="target.rawQuery"/>
|
||||
|
||||
<ul class="tight-form-list" role="menu" ng-hide="target.rawQuery">
|
||||
<li class="tight-form-item query-keyword" style="width: 75px;">
|
||||
Function
|
||||
</li>
|
||||
<li class="dropdown tight-form-item">
|
||||
<a gf-dropdown="functionMenu" class="dropdown-toggle" data-toggle="dropdown">
|
||||
{{target.function}}<span>(value)</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
|
||||
<div class="tight-form" ng-hide="target.rawQuery">
|
||||
<ul class="tight-form-list">
|
||||
<li class="tight-form-item">
|
||||
<i class="fa fa-eye invisible"></i>
|
||||
</li>
|
||||
<li class="tight-form-item query-keyword" style="width: 75px;">
|
||||
Key Field
|
||||
</li>
|
||||
<li>
|
||||
<metric-segment segment="keyFieldSegment" on-value-changed="keyFieldChanged()"></metric-segment>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
|
||||
<div class="tight-form" ng-hide="target.rawQuery">
|
||||
<ul class="tight-form-list">
|
||||
<li class="tight-form-item">
|
||||
<i class="fa fa-eye invisible"></i>
|
||||
</li>
|
||||
<li class="tight-form-item query-keyword" style="width: 75px;">
|
||||
Value Field
|
||||
</li>
|
||||
<li>
|
||||
<metric-segment segment="valueFieldSegment" on-value-changed="valueFieldChanged()"></metric-segment>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
|
||||
<div class="tight-form" ng-hide="target.rawQuery">
|
||||
<ul class="tight-form-list">
|
||||
<li class="tight-form-item">
|
||||
<i class="fa fa-eye invisible"></i>
|
||||
</li>
|
||||
|
||||
<li class="tight-form-item query-keyword" style="width: 75px;">
|
||||
Term
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<metric-segment segment="termKeySegment" on-value-changed="termKeySegmentChanged()"></metric-segment>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<span class="tight-form-item">:</span>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<metric-segment segment="termValueSegment" on-value-changed="termValueSegmentChanged()"></metric-segment>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
|
||||
<div class="tight-form" ng-hide="target.rawQuery">
|
||||
<ul class="tight-form-list">
|
||||
<li class="tight-form-item">
|
||||
<i class="fa fa-eye invisible"></i>
|
||||
</li>
|
||||
<li class="tight-form-item query-keyword" style="width: 75px;">
|
||||
Group By
|
||||
</li>
|
||||
<li>
|
||||
<metric-segment segment="groupByFieldSegment" on-value-changed="groupByFieldChanged()"></metric-segment>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,86 @@
|
||||
<section class="grafana-metric-options">
|
||||
<div class="tight-form">
|
||||
<ul class="tight-form-list">
|
||||
<li class="tight-form-item tight-form-item-icon">
|
||||
<i class="fa fa-wrench"></i>
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
Group by time interval
|
||||
</li>
|
||||
<li>
|
||||
<input type="text" class="input-medium tight-form-input" ng-model="panel.interval" ng-blur="get_data();"
|
||||
spellcheck='false' placeholder="example: >10s">
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
<i class="fa fa-question-circle" bs-tooltip="'Set a low limit by having a greater sign: example: >60s'" data-placement="right"></i>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
|
||||
<div class="tight-form">
|
||||
<ul class="tight-form-list">
|
||||
<li class="tight-form-item tight-form-item-icon">
|
||||
<i class="fa fa-info-circle"></i>
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
<a ng-click="toggleEditorHelp(1);" bs-tooltip="'click to show helpful info'" data-placement="bottom">
|
||||
alias patterns
|
||||
</a>
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
<a ng-click="toggleEditorHelp(2)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
|
||||
stacking & and fill
|
||||
</a>
|
||||
</li>
|
||||
<li class="tight-form-item">
|
||||
<a ng-click="toggleEditorHelp(3)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
|
||||
group by time
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="editor-row">
|
||||
<div class="pull-left" style="margin-top: 30px;">
|
||||
|
||||
<div class="grafana-info-box span6" ng-if="editorHelpIndex === 1">
|
||||
<h5>Alias patterns</h5>
|
||||
<ul>
|
||||
<li>$m = replaced with measurement name</li>
|
||||
<li>$measurement = replaced with measurement name</li>
|
||||
<li>$tag_hostname = replaced with the value of the hostname tag</li>
|
||||
<li>You can also use [[tag_hostname]] pattern replacement syntax</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="grafana-info-box span6" ng-if="editorHelpIndex === 2">
|
||||
<h5>Stacking and fill</h5>
|
||||
<ul>
|
||||
<li>When stacking is enabled it important that points align</li>
|
||||
<li>If there are missing points for one series it can cause gaps or missing bars</li>
|
||||
<li>You must use fill(0), and select a group by time low limit</li>
|
||||
<li>Use the group by time option below your queries and specify for example >10s if your metrics are written every 10 seconds</li>
|
||||
<li>This will insert zeros for series that are missing measurements and will make stacking work properly</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="grafana-info-box span6" ng-if="editorHelpIndex === 3">
|
||||
<h5>Group by time</h5>
|
||||
<ul>
|
||||
<li>Group by time is important, otherwise the query could return many thousands of datapoints that will slow down Grafana</li>
|
||||
<li>Leave the group by time field empty for each query and it will be calculated based on time range and pixel width of the graph</li>
|
||||
<li>If you use fill(0) or fill(null) set a low limit for the auto group by time interval</li>
|
||||
<li>The low limit can only be set in the group by time option below your queries</li>
|
||||
<li>You set a low limit by adding a greater sign before the interval</li>
|
||||
<li>Example: >60s if you write metrics to ElasticDB every 60 seconds</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
73
public/app/plugins/datasource/elasticsearch/queryBuilder.js
Normal file
73
public/app/plugins/datasource/elasticsearch/queryBuilder.js
Normal file
@@ -0,0 +1,73 @@
|
||||
define([
|
||||
],
|
||||
function () {
|
||||
'use strict';
|
||||
|
||||
function ElasticQueryBuilder() {
|
||||
}
|
||||
|
||||
ElasticQueryBuilder.prototype.build = function(targets) {
|
||||
var query = {
|
||||
"aggs": {},
|
||||
"size": "$maxDataPoints"
|
||||
};
|
||||
var self = this;
|
||||
targets.forEach(function(target) {
|
||||
if (!target.hide) {
|
||||
query["aggs"][target.termKey + "_" + target.termValue] = {
|
||||
"filter": {
|
||||
"and": [
|
||||
self._buildRangeFilter(target),
|
||||
self._buildTermFilter(target)
|
||||
]
|
||||
},
|
||||
"aggs": {
|
||||
"date_histogram": {
|
||||
"date_histogram": {
|
||||
"interval": target.interval || "$interval",
|
||||
"field": target.keyField,
|
||||
"min_doc_count": 0,
|
||||
},
|
||||
"aggs": {
|
||||
"stats": {
|
||||
"stats": {
|
||||
"field": target.valueField
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if (target.groupByField) {
|
||||
query["aggs"][target.termKey + "_" + target.termValue]["aggs"] = {
|
||||
"terms": {
|
||||
"terms": {
|
||||
"field": target.groupByField
|
||||
},
|
||||
"aggs": query["aggs"][target.termKey + "_" + target.termValue]["aggs"]
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
query = JSON.stringify(query);
|
||||
return query;
|
||||
};
|
||||
|
||||
ElasticQueryBuilder.prototype._buildRangeFilter = function(target) {
|
||||
var filter = {"range":{}};
|
||||
filter["range"][target.keyField] = {
|
||||
"gte": "$rangeFrom",
|
||||
"lte": "$rangeTo"
|
||||
};
|
||||
return filter;
|
||||
};
|
||||
|
||||
ElasticQueryBuilder.prototype._buildTermFilter = function(target) {
|
||||
var filter = {"term":{}};
|
||||
filter["term"][target.termKey] = target.termValue;
|
||||
return filter;
|
||||
};
|
||||
|
||||
return ElasticQueryBuilder;
|
||||
});
|
||||
339
public/app/plugins/datasource/elasticsearch/queryCtrl.js
Normal file
339
public/app/plugins/datasource/elasticsearch/queryCtrl.js
Normal file
@@ -0,0 +1,339 @@
|
||||
define([
|
||||
'angular',
|
||||
'lodash',
|
||||
'./queryBuilder',
|
||||
],
|
||||
function (angular, _, ElasticQueryBuilder) {
|
||||
'use strict';
|
||||
|
||||
var module = angular.module('grafana.controllers');
|
||||
|
||||
module.controller('ElasticQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) {
|
||||
|
||||
$scope.functionList = ['count', 'min', 'max', 'total', 'mean'];
|
||||
|
||||
$scope.functionMenu = _.map($scope.functionList, function(func) {
|
||||
return { text: func, click: "changeFunction('" + func + "');" };
|
||||
});
|
||||
|
||||
$scope.init = function() {
|
||||
var target = $scope.target;
|
||||
target.function = target.function || 'mean';
|
||||
target.tags = target.tags || [];
|
||||
target.groupByTags = target.groupByTags || [];
|
||||
|
||||
$scope.queryBuilder = new ElasticQueryBuilder(target);
|
||||
|
||||
if (!target.keyField) {
|
||||
target.keyField = '@timestamp';
|
||||
}
|
||||
$scope.keyFieldSegment = new MetricSegment({value: target.keyField});
|
||||
|
||||
if (!target.valueField) {
|
||||
target.valueField = 'metric';
|
||||
}
|
||||
$scope.valueFieldSegment = new MetricSegment({value: target.valueField});
|
||||
|
||||
if (!target.termKey) {
|
||||
target.termKey = 'service.raw';
|
||||
}
|
||||
$scope.termKeySegment = new MetricSegment({value: target.termKey});
|
||||
|
||||
if (!target.termValue) {
|
||||
target.termValue = 'cpu-average/cpu-user';
|
||||
}
|
||||
$scope.termValueSegment = new MetricSegment({value: target.termValue});
|
||||
|
||||
if (!target.groupByField) {
|
||||
target.groupByField = 'host.raw';
|
||||
}
|
||||
$scope.groupByFieldSegment = new MetricSegment({value: target.groupByField});
|
||||
|
||||
if (!target.measurement) {
|
||||
$scope.measurementSegment = MetricSegment.newSelectMeasurement();
|
||||
} else {
|
||||
$scope.measurementSegment = new MetricSegment(target.measurement);
|
||||
}
|
||||
|
||||
$scope.tagSegments = [];
|
||||
_.each(target.tags, function(tag) {
|
||||
if (tag.condition) {
|
||||
$scope.tagSegments.push(MetricSegment.newCondition(tag.condition));
|
||||
}
|
||||
$scope.tagSegments.push(new MetricSegment({value: tag.key, type: 'key', cssClass: 'query-segment-key' }));
|
||||
$scope.tagSegments.push(new MetricSegment.newOperator("="));
|
||||
$scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value', cssClass: 'query-segment-value'}));
|
||||
});
|
||||
|
||||
$scope.fixTagSegments();
|
||||
|
||||
$scope.groupBySegments = [];
|
||||
_.each(target.groupByTags, function(tag) {
|
||||
$scope.groupBySegments.push(new MetricSegment(tag));
|
||||
});
|
||||
|
||||
$scope.groupBySegments.push(MetricSegment.newPlusButton());
|
||||
|
||||
$scope.removeTagFilterSegment = new MetricSegment({fake: true, value: '-- remove tag filter --'});
|
||||
$scope.removeGroupBySegment = new MetricSegment({fake: true, value: '-- remove group by --'});
|
||||
};
|
||||
|
||||
$scope.valueFieldChanged = function() {
|
||||
$scope.target.valueField = $scope.valueFieldSegment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.keyFieldChanged = function() {
|
||||
$scope.target.keyField = $scope.keyFieldSegment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.termValueSegmentChanged = function() {
|
||||
$scope.target.termValue = $scope.termValueSegment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.termKeySegmentChanged = function() {
|
||||
$scope.target.termKey = $scope.termKeySegment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.groupByFieldChanged = function() {
|
||||
$scope.target.groupBy = $scope.groupByFieldSegment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.fixTagSegments = function() {
|
||||
var count = $scope.tagSegments.length;
|
||||
var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
|
||||
|
||||
if (!lastSegment || lastSegment.type !== 'plus-button') {
|
||||
$scope.tagSegments.push(MetricSegment.newPlusButton());
|
||||
}
|
||||
};
|
||||
|
||||
$scope.groupByTagUpdated = function(segment, index) {
|
||||
if (segment.value === $scope.removeGroupBySegment.value) {
|
||||
$scope.target.groupByTags.splice(index, 1);
|
||||
$scope.groupBySegments.splice(index, 1);
|
||||
$scope.$parent.get_data();
|
||||
return;
|
||||
}
|
||||
|
||||
if (index === $scope.groupBySegments.length-1) {
|
||||
$scope.groupBySegments.push(MetricSegment.newPlusButton());
|
||||
}
|
||||
|
||||
segment.type = 'group-by-key';
|
||||
segment.fake = false;
|
||||
|
||||
$scope.target.groupByTags[index] = segment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.changeFunction = function(func) {
|
||||
$scope.target.function = func;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.measurementChanged = function() {
|
||||
$scope.target.measurement = $scope.measurementSegment.value;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.toggleQueryMode = function () {
|
||||
$scope.target.rawQuery = !$scope.target.rawQuery;
|
||||
};
|
||||
|
||||
$scope.moveMetricQuery = function(fromIndex, toIndex) {
|
||||
_.move($scope.panel.targets, fromIndex, toIndex);
|
||||
};
|
||||
|
||||
$scope.duplicate = function() {
|
||||
var clone = angular.copy($scope.target);
|
||||
$scope.panel.targets.push(clone);
|
||||
};
|
||||
|
||||
$scope.getMeasurements = function () {
|
||||
var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
|
||||
return $scope.datasource.metricFindQuery(query)
|
||||
.then($scope.transformToSegments)
|
||||
.then($scope.addTemplateVariableSegments)
|
||||
.then(null, $scope.handleQueryError);
|
||||
};
|
||||
|
||||
$scope.handleQueryError = function(err) {
|
||||
$scope.parserError = err.message || 'Failed to issue metric query';
|
||||
return [];
|
||||
};
|
||||
|
||||
$scope.transformToSegments = function(results) {
|
||||
return _.map(results, function(segment) {
|
||||
return new MetricSegment({ value: segment.text, expandable: segment.expandable });
|
||||
});
|
||||
};
|
||||
|
||||
$scope.addTemplateVariableSegments = function(segments) {
|
||||
_.each(templateSrv.variables, function(variable) {
|
||||
segments.unshift(new MetricSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
|
||||
});
|
||||
return segments;
|
||||
};
|
||||
|
||||
$scope.getTagsOrValues = function(segment, index) {
|
||||
var query;
|
||||
|
||||
if (segment.type === 'key' || segment.type === 'plus-button') {
|
||||
query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
|
||||
} else if (segment.type === 'value') {
|
||||
query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
|
||||
} else if (segment.type === 'condition') {
|
||||
return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
|
||||
}
|
||||
else {
|
||||
return $q.when([]);
|
||||
}
|
||||
|
||||
return $scope.datasource.metricFindQuery(query)
|
||||
.then($scope.transformToSegments)
|
||||
.then($scope.addTemplateVariableSegments)
|
||||
.then(function(results) {
|
||||
if (segment.type === 'key') {
|
||||
results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
|
||||
}
|
||||
return results;
|
||||
})
|
||||
.then(null, $scope.handleQueryError);
|
||||
};
|
||||
|
||||
$scope.getGroupByTagSegments = function(segment) {
|
||||
var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
|
||||
|
||||
return $scope.datasource.metricFindQuery(query)
|
||||
.then($scope.transformToSegments)
|
||||
.then($scope.addTemplateVariableSegments)
|
||||
.then(function(results) {
|
||||
if (segment.type !== 'plus-button') {
|
||||
results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
|
||||
}
|
||||
return results;
|
||||
})
|
||||
.then(null, $scope.handleQueryError);
|
||||
};
|
||||
|
||||
$scope.tagSegmentUpdated = function(segment, index) {
|
||||
$scope.tagSegments[index] = segment;
|
||||
|
||||
// handle remove tag condition
|
||||
if (segment.value === $scope.removeTagFilterSegment.value) {
|
||||
$scope.tagSegments.splice(index, 3);
|
||||
if ($scope.tagSegments.length === 0) {
|
||||
$scope.tagSegments.push(MetricSegment.newPlusButton());
|
||||
} else if ($scope.tagSegments.length > 2) {
|
||||
$scope.tagSegments.splice(Math.max(index-1, 0), 1);
|
||||
if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
|
||||
$scope.tagSegments.push(MetricSegment.newPlusButton());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (segment.type === 'plus-button') {
|
||||
if (index > 2) {
|
||||
$scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
|
||||
}
|
||||
$scope.tagSegments.push(MetricSegment.newOperator('='));
|
||||
$scope.tagSegments.push(MetricSegment.newFake('select tag value', 'value', 'query-segment-value'));
|
||||
segment.type = 'key';
|
||||
segment.cssClass = 'query-segment-key';
|
||||
}
|
||||
|
||||
if ((index+1) === $scope.tagSegments.length) {
|
||||
$scope.tagSegments.push(MetricSegment.newPlusButton());
|
||||
}
|
||||
}
|
||||
|
||||
$scope.rebuildTargetTagConditions();
|
||||
};
|
||||
|
||||
$scope.rebuildTargetTagConditions = function() {
|
||||
var tags = [];
|
||||
var tagIndex = 0;
|
||||
_.each($scope.tagSegments, function(segment2, index) {
|
||||
if (segment2.type === 'key') {
|
||||
if (tags.length === 0) {
|
||||
tags.push({});
|
||||
}
|
||||
tags[tagIndex].key = segment2.value;
|
||||
}
|
||||
else if (segment2.type === 'value') {
|
||||
tags[tagIndex].value = segment2.value;
|
||||
$scope.tagSegments[index-1] = $scope.getTagValueOperator(segment2.value);
|
||||
}
|
||||
else if (segment2.type === 'condition') {
|
||||
tags.push({ condition: segment2.value });
|
||||
tagIndex += 1;
|
||||
}
|
||||
});
|
||||
|
||||
$scope.target.tags = tags;
|
||||
$scope.$parent.get_data();
|
||||
};
|
||||
|
||||
$scope.getTagValueOperator = function(tagValue) {
|
||||
if (tagValue[0] === '/' && tagValue[tagValue.length - 1] === '/') {
|
||||
return MetricSegment.newOperator('=~');
|
||||
}
|
||||
|
||||
return MetricSegment.newOperator('=');
|
||||
};
|
||||
|
||||
function MetricSegment(options) {
|
||||
if (options === '*' || options.value === '*') {
|
||||
this.value = '*';
|
||||
this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
|
||||
this.expandable = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_.isString(options)) {
|
||||
this.value = options;
|
||||
this.html = $sce.trustAsHtml(this.value);
|
||||
return;
|
||||
}
|
||||
|
||||
this.cssClass = options.cssClass;
|
||||
this.type = options.type;
|
||||
this.fake = options.fake;
|
||||
this.value = options.value;
|
||||
this.type = options.type;
|
||||
this.expandable = options.expandable;
|
||||
this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
|
||||
}
|
||||
|
||||
MetricSegment.newSelectMeasurement = function() {
|
||||
return new MetricSegment({value: 'select measurement', fake: true});
|
||||
};
|
||||
|
||||
MetricSegment.newFake = function(text, type, cssClass) {
|
||||
return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
|
||||
};
|
||||
|
||||
MetricSegment.newCondition = function(condition) {
|
||||
return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
|
||||
};
|
||||
|
||||
MetricSegment.newOperator = function(op) {
|
||||
return new MetricSegment({value: op, type: 'operator', cssClass: 'query-segment-operator' });
|
||||
};
|
||||
|
||||
MetricSegment.newPlusButton = function() {
|
||||
return new MetricSegment({fake: true, html: '<i class="fa fa-plus "></i>', type: 'plus-button' });
|
||||
};
|
||||
|
||||
MetricSegment.newSelectTagValue = function() {
|
||||
return new MetricSegment({value: 'select tag value', fake: true});
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user