2014-03-01 05:02:55 -06:00
|
|
|
define([
|
2014-11-03 01:56:13 -06:00
|
|
|
'angular',
|
|
|
|
'lodash'
|
2014-03-01 05:02:55 -06:00
|
|
|
],
|
2014-11-03 01:56:13 -06:00
|
|
|
function (angular, _) {
|
2014-03-01 05:02:55 -06:00
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.controllers');
|
2014-03-01 05:02:55 -06:00
|
|
|
|
2015-05-15 04:38:22 -05:00
|
|
|
module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv) {
|
|
|
|
|
|
|
|
$scope.functionList = [
|
|
|
|
'count', 'mean', 'sum', 'min',
|
|
|
|
'max', 'mode', 'distinct', 'median',
|
|
|
|
'derivative', 'stddev', 'first', 'last',
|
|
|
|
'difference'
|
|
|
|
];
|
|
|
|
|
|
|
|
$scope.functionMenu = _.map($scope.functionList, function(func) {
|
|
|
|
return { text: func, click: "changeFunction('" + func + "');" };
|
|
|
|
});
|
2014-03-01 05:02:55 -06:00
|
|
|
|
|
|
|
$scope.init = function() {
|
2015-05-15 04:38:22 -05:00
|
|
|
var target = $scope.target;
|
|
|
|
target.function = target.function || 'mean';
|
2014-09-04 07:08:31 -05:00
|
|
|
|
2015-05-15 04:38:22 -05:00
|
|
|
if (!target.measurement) {
|
|
|
|
$scope.measurementSegment = MetricSegment.newSelectMeasurement();
|
|
|
|
} else {
|
|
|
|
$scope.measurementSegment = new MetricSegment(target.measurement);
|
|
|
|
}
|
|
|
|
};
|
2014-06-06 23:38:33 -05:00
|
|
|
|
2015-05-15 04:38:22 -05:00
|
|
|
$scope.changeFunction = function(func) {
|
|
|
|
$scope.target.function = func;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.measurementChanged = function() {
|
|
|
|
$scope.target.measurement = $scope.measurementSegment.value;
|
|
|
|
console.log('measurement updated', $scope.target.measurement);
|
|
|
|
$scope.$parent.get_data();
|
2014-03-02 11:24:15 -06:00
|
|
|
};
|
|
|
|
|
2015-03-25 05:07:12 -05:00
|
|
|
$scope.toggleQueryMode = function () {
|
|
|
|
$scope.target.rawQuery = !$scope.target.rawQuery;
|
2014-03-28 00:13:11 -05:00
|
|
|
};
|
|
|
|
|
2015-03-25 05:07:12 -05:00
|
|
|
$scope.moveMetricQuery = function(fromIndex, toIndex) {
|
|
|
|
_.move($scope.panel.targets, fromIndex, toIndex);
|
2014-03-28 00:13:11 -05:00
|
|
|
};
|
|
|
|
|
2015-03-25 05:07:12 -05:00
|
|
|
$scope.duplicate = function() {
|
|
|
|
var clone = angular.copy($scope.target);
|
|
|
|
$scope.panel.targets.push(clone);
|
2014-03-02 11:24:15 -06:00
|
|
|
};
|
|
|
|
|
2015-05-15 04:38:22 -05:00
|
|
|
$scope.getMeasurements = function () {
|
|
|
|
// 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);
|
|
|
|
//
|
|
|
|
console.log('get measurements');
|
|
|
|
return $scope.datasource.metricFindQuery('SHOW MEASUREMENTS', 'MEASUREMENTS').then(function(results) {
|
2015-03-25 05:07:12 -05:00
|
|
|
console.log('get alt segments: response', results);
|
2015-05-15 04:38:22 -05:00
|
|
|
var measurements = _.map(results, function(segment) {
|
2015-03-25 05:07:12 -05:00
|
|
|
return new MetricSegment({ value: segment.text, expandable: segment.expandable });
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(templateSrv.variables, function(variable) {
|
2015-05-15 04:38:22 -05:00
|
|
|
measurements.unshift(new MetricSegment({
|
2015-03-25 05:07:12 -05:00
|
|
|
type: 'template',
|
|
|
|
value: '$' + variable.name,
|
|
|
|
expandable: true,
|
|
|
|
}));
|
|
|
|
});
|
2015-05-15 04:38:22 -05:00
|
|
|
|
|
|
|
return measurements;
|
2015-03-25 05:07:12 -05:00
|
|
|
}, function(err) {
|
|
|
|
$scope.parserError = err.message || 'Failed to issue metric query';
|
2015-05-15 04:38:22 -05:00
|
|
|
return [];
|
2015-03-25 05:07:12 -05:00
|
|
|
});
|
2014-06-06 03:56:23 -05:00
|
|
|
};
|
|
|
|
|
2015-03-25 05:07:12 -05:00
|
|
|
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;
|
2014-03-05 05:43:44 -06:00
|
|
|
}
|
2015-03-25 05:07:12 -05:00
|
|
|
|
|
|
|
this.fake = options.fake;
|
|
|
|
this.value = options.value;
|
|
|
|
this.type = options.type;
|
|
|
|
this.expandable = options.expandable;
|
|
|
|
this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:38:22 -05:00
|
|
|
MetricSegment.newSelectMeasurement = function() {
|
|
|
|
return new MetricSegment({value: 'select measurement', fake: true});
|
2014-03-01 05:02:55 -06:00
|
|
|
};
|
|
|
|
|
2015-03-25 05:07:12 -05:00
|
|
|
MetricSegment.newSelectTag = function() {
|
|
|
|
return new MetricSegment({value: 'select tag', fake: true});
|
2014-11-03 01:56:13 -06:00
|
|
|
};
|
|
|
|
|
2015-03-25 05:07:12 -05:00
|
|
|
MetricSegment.newSelectTagValue = function() {
|
|
|
|
return new MetricSegment({value: 'select tag value', fake: true});
|
2014-03-01 05:02:55 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-03-28 00:13:11 -05:00
|
|
|
});
|