Commit da833cbc by Torkel Ödegaard

Small progress on influxdb 0.9 query editor, #1525

parent 9268ecf3
...@@ -37,12 +37,14 @@ function (angular, app, _, $) { ...@@ -37,12 +37,14 @@ function (angular, app, _, $) {
if (selected) { if (selected) {
segment.value = selected.value; segment.value = selected.value;
segment.html = selected.html; segment.html = selected.html;
segment.fake = false;
segment.expandable = selected.expandable; segment.expandable = selected.expandable;
} }
else { else {
segment.value = value; segment.value = value;
segment.html = $sce.trustAsHtml(value); segment.html = $sce.trustAsHtml(value);
segment.expandable = true; segment.expandable = true;
segment.fake = false;
} }
$scope.segmentValueChanged(segment, $scope.$index); $scope.segmentValueChanged(segment, $scope.$index);
}); });
...@@ -71,7 +73,7 @@ function (angular, app, _, $) { ...@@ -71,7 +73,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 (segment.value !== 'select metric' && _.indexOf(options, segment.value) === -1) { if (!segment.fake && _.indexOf(options, segment.value) === -1) {
options.unshift(segment.value); options.unshift(segment.value);
} }
......
...@@ -113,7 +113,7 @@ function (angular, _, config, gfunc, Parser) { ...@@ -113,7 +113,7 @@ function (angular, _, config, gfunc, Parser) {
function checkOtherSegments(fromIndex) { function checkOtherSegments(fromIndex) {
if (fromIndex === 0) { if (fromIndex === 0) {
$scope.segments.push(new MetricSegment('select metric')); $scope.segments.push(MetricSegment.newSelectMetric());
return; return;
} }
...@@ -123,13 +123,13 @@ function (angular, _, config, gfunc, Parser) { ...@@ -123,13 +123,13 @@ function (angular, _, config, gfunc, Parser) {
if (segments.length === 0) { if (segments.length === 0) {
if (path !== '') { if (path !== '') {
$scope.segments = $scope.segments.splice(0, fromIndex); $scope.segments = $scope.segments.splice(0, fromIndex);
$scope.segments.push(new MetricSegment('select metric')); $scope.segments.push(MetricSegment.newSelectMetric());
} }
return; return;
} }
if (segments[0].expandable) { if (segments[0].expandable) {
if ($scope.segments.length === fromIndex) { if ($scope.segments.length === fromIndex) {
$scope.segments.push(new MetricSegment('select metric')); $scope.segments.push(MetricSegment.newSelectMetric());
} }
else { else {
return checkOtherSegments(fromIndex + 1); return checkOtherSegments(fromIndex + 1);
...@@ -238,7 +238,7 @@ function (angular, _, config, gfunc, Parser) { ...@@ -238,7 +238,7 @@ function (angular, _, config, gfunc, Parser) {
$scope.moveAliasFuncLast(); $scope.moveAliasFuncLast();
$scope.smartlyHandleNewAliasByNode(newFunc); $scope.smartlyHandleNewAliasByNode(newFunc);
if ($scope.segments.length === 1 && $scope.segments[0].value === 'select metric') { if ($scope.segments.length === 1 && $scope.segments[0].fake) {
$scope.segments = []; $scope.segments = [];
} }
...@@ -298,18 +298,17 @@ function (angular, _, config, gfunc, Parser) { ...@@ -298,18 +298,17 @@ function (angular, _, config, gfunc, Parser) {
return; return;
} }
if (_.isString(options)) { this.fake = options.fake;
this.value = options;
this.html = $sce.trustAsHtml(this.value);
return;
}
this.value = options.value; this.value = options.value;
this.type = options.type; this.type = options.type;
this.expandable = options.expandable; this.expandable = options.expandable;
this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value)); this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
} }
MetricSegment.newSelectMetric = function() {
return new MetricSegment({value: 'select metric', fake: true});
};
}); });
module.directive('focusMe', function($timeout, $parse) { module.directive('focusMe', function($timeout, $parse) {
......
...@@ -36,7 +36,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -36,7 +36,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
var timeFilter = getTimeFilter(options); var timeFilter = getTimeFilter(options);
var promises = _.map(options.targets, function(target) { var promises = _.map(options.targets, function(target) {
if (target.hide || !((target.series && target.column) || target.query)) { if (target.hide || !target.query) {
return []; return [];
} }
...@@ -73,40 +73,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -73,40 +73,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
}); });
}; };
InfluxDatasource.prototype.listColumns = function(seriesName) { InfluxDatasource.prototype.metricFindQuery = function (query, queryType) {
seriesName = templateSrv.replace(seriesName);
if(!seriesName.match('^/.*/') && !seriesName.match(/^merge\(.*\)/)) {
seriesName = '"' + seriesName+ '"';
}
return this._seriesQuery('select * from ' + seriesName + ' limit 1').then(function(data) {
if (!data) {
return [];
}
return data[0].columns.map(function(item) {
return /^\w+$/.test(item) ? item : ('"' + item + '"');
});
});
};
InfluxDatasource.prototype.listSeries = function(query) {
// wrap in regex
if (query && query.length > 0 && query[0] !== '/') {
query = '/' + query + '/';
}
return this._seriesQuery('SHOW MEASUREMENTS').then(function(data) {
if (!data || data.length === 0) {
return [];
}
return _.map(data[0].points, function(point) {
return point[1];
});
});
};
InfluxDatasource.prototype.metricFindQuery = function (query) {
var interpolated; var interpolated;
try { try {
interpolated = templateSrv.replace(query); interpolated = templateSrv.replace(query);
...@@ -115,17 +82,30 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -115,17 +82,30 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
return $q.reject(err); return $q.reject(err);
} }
return this._seriesQuery(interpolated) console.log('metricFindQuery called with: ' + [query, queryType].join(', '));
.then(function (results) {
if (!results || results.length === 0) { return []; }
return _.map(results[0].points, function (metric) { return this._seriesQuery(interpolated, queryType).then(function (results) {
return { if (!results || results.results.length === 0) { return []; }
text: metric[1],
expandable: false var influxResults = results.results[0];
}; if (!influxResults.series) {
}); return [];
}); }
console.log('metric find query response', results);
var series = influxResults.series[0];
switch (queryType) {
case 'MEASUREMENTS':
return _.map(series.values, function(value) { return { text: value[0], expandable: true }; });
case 'TAG_KEYS':
var tagKeys = _.flatten(series.values);
return _.map(tagKeys, function(tagKey) { return { text: tagKey, expandable: true }; });
case 'TAG_VALUES':
var tagValues = _.flatten(series.values);
return _.map(tagValues, function(tagValue) { return { text: tagValue, expandable: true }; });
}
});
}; };
function retry(deferred, callback, delay) { function retry(deferred, callback, delay) {
...@@ -143,9 +123,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -143,9 +123,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
} }
InfluxDatasource.prototype._seriesQuery = function(query) { InfluxDatasource.prototype._seriesQuery = function(query) {
return this._influxRequest('GET', '/query', { return this._influxRequest('GET', '/query', {q: query});
q: query,
});
}; };
InfluxDatasource.prototype._influxRequest = function(method, url, data) { InfluxDatasource.prototype._influxRequest = function(method, url, data) {
......
...@@ -7,102 +7,153 @@ function (angular, _) { ...@@ -7,102 +7,153 @@ function (angular, _) {
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var seriesList = null; module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) {
module.controller('InfluxQueryCtrl', function($scope, $timeout) {
$scope.init = function() { $scope.init = function() {
var target = $scope.target; $scope.segments = $scope.target.segments || [];
target.function = target.function || 'mean';
target.column = target.column || 'value';
// backward compatible correction of schema
if (target.condition_value) {
target.condition = target.condition_key + ' ' + target.condition_op + ' ' + target.condition_value;
delete target.condition_key;
delete target.condition_op;
delete target.condition_value;
}
if (target.groupby_field_add === false) {
target.groupby_field = '';
delete target.groupby_field_add;
}
$scope.rawQuery = true; $scope.functionsSelect = [
$scope.functions = [
'count', 'mean', 'sum', 'min', 'count', 'mean', 'sum', 'min',
'max', 'mode', 'distinct', 'median', 'max', 'mode', 'distinct', 'median',
'derivative', 'stddev', 'first', 'last', 'derivative', 'stddev', 'first', 'last',
'difference' 'difference'
]; ];
$scope.operators = ['=', '=~', '>', '<', '!~', '<>']; checkOtherSegments(0);
$scope.oldSeries = target.series;
$scope.$on('typeahead-updated', function() {
$timeout($scope.get_data);
});
}; };
$scope.showQuery = function () { $scope.toggleQueryMode = function () {
$scope.target.rawQuery = true; $scope.target.rawQuery = !$scope.target.rawQuery;
}; };
$scope.hideQuery = function () { $scope.moveMetricQuery = function(fromIndex, toIndex) {
$scope.target.rawQuery = false; _.move($scope.panel.targets, fromIndex, toIndex);
}; };
// Cannot use typeahead and ng-change on blur at the same time $scope.duplicate = function() {
$scope.seriesBlur = function() { var clone = angular.copy($scope.target);
if ($scope.oldSeries !== $scope.target.series) { $scope.panel.targets.push(clone);
$scope.oldSeries = $scope.target.series;
$scope.columnList = null;
$scope.get_data();
}
}; };
$scope.changeFunction = function(func) { $scope.getAltSegments = function (index) {
$scope.target.function = func; $scope.altSegments = [];
$scope.get_data();
var measurement = $scope.segments[0].value;
var queryType, query;
if (index === 0) {
queryType = 'MEASUREMENTS';
query = 'SHOW MEASUREMENTS';
} else if (index % 2 === 1) {
queryType = 'TAG_KEYS';
query = 'SHOW TAG KEYS FROM ' + measurement;
} else {
queryType = 'TAG_VALUES';
query = "SHOW TAG VALUES FROM " + measurement + " WITH KEY = " + $scope.segments[$scope.segments.length - 2].value;
}
console.log('getAltSegments: query' , query);
return $scope.datasource.metricFindQuery(query, queryType).then(function(results) {
console.log('get alt segments: response', results);
$scope.altSegments = _.map(results, function(segment) {
return new MetricSegment({ value: segment.text, expandable: segment.expandable });
});
_.each(templateSrv.variables, function(variable) {
$scope.altSegments.unshift(new MetricSegment({
type: 'template',
value: '$' + variable.name,
expandable: true,
}));
});
}, function(err) {
$scope.parserError = err.message || 'Failed to issue metric query';
});
}; };
// called outside of digest $scope.segmentValueChanged = function (segment, segmentIndex) {
$scope.listColumns = function(query, callback) { delete $scope.parserError;
if (!$scope.columnList) {
$scope.$apply(function() { if (segment.expandable) {
$scope.datasource.listColumns($scope.target.series).then(function(columns) { return checkOtherSegments(segmentIndex + 1).then(function () {
$scope.columnList = columns; setSegmentFocus(segmentIndex + 1);
callback(columns); $scope.targetChanged();
});
}); });
} }
else { else {
return $scope.columnList; $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
} }
setSegmentFocus(segmentIndex + 1);
$scope.targetChanged();
}; };
$scope.listSeries = function(query, callback) { $scope.targetChanged = function() {
if (query !== '') { if ($scope.parserError) {
seriesList = []; return;
$scope.datasource.listSeries(query).then(function(series) {
seriesList = series;
callback(seriesList);
});
} }
else {
return seriesList; $scope.$parent.get_data();
};
function checkOtherSegments(fromIndex) {
if (fromIndex === 0) {
$scope.segments.push(MetricSegment.newSelectMetric());
return;
}
if ($scope.segments.length === 0) {
throw('should always have a scope segment?');
}
if (_.last($scope.segments).fake) {
return $q.when([]);
} else if ($scope.segments.length % 2 === 1) {
$scope.segments.push(MetricSegment.newSelectTag());
return $q.when([]);
} else {
$scope.segments.push(MetricSegment.newSelectTagValue());
return $q.when([]);
}
}
function setSegmentFocus(segmentIndex) {
_.each($scope.segments, function(segment, index) {
segment.focus = segmentIndex === index;
});
}
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.fake = options.fake;
this.value = options.value;
this.type = options.type;
this.expandable = options.expandable;
this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
}
MetricSegment.newSelectMetric = function() {
return new MetricSegment({value: 'select metric', fake: true});
}; };
$scope.moveMetricQuery = function(fromIndex, toIndex) { MetricSegment.newSelectTag = function() {
_.move($scope.panel.targets, fromIndex, toIndex); return new MetricSegment({value: 'select tag', fake: true});
}; };
$scope.duplicate = function() { MetricSegment.newSelectTagValue = function() {
var clone = angular.copy($scope.target); return new MetricSegment({value: 'select tag value', fake: true});
$scope.panel.targets.push(clone);
}; };
}); });
......
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