2015-07-01 13:45:55 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
],
|
2015-09-07 01:57:46 -05:00
|
|
|
function (angular, _) {
|
2015-07-01 13:45:55 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2015-09-06 07:45:12 -05:00
|
|
|
module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv) {
|
2015-07-01 13:45:55 -05:00
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
var target = $scope.target;
|
2015-09-04 02:41:23 -05:00
|
|
|
if (!target) { return; }
|
|
|
|
|
2015-09-05 05:24:14 -05:00
|
|
|
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
|
2015-09-07 09:35:40 -05:00
|
|
|
target.bucketAggs = target.bucketAggs || [{type: 'date_histogram', id: '2', settings: {interval: 'auto'}}];
|
2015-09-07 02:36:56 -05:00
|
|
|
target.timeField = $scope.datasource.timeField;
|
2015-09-03 08:56:41 -05:00
|
|
|
};
|
|
|
|
|
2015-09-04 09:05:47 -05:00
|
|
|
$scope.getFields = function() {
|
2015-09-04 04:17:52 -05:00
|
|
|
return $scope.datasource.metricFindQuery('fields()')
|
|
|
|
.then($scope.transformToSegments(false))
|
|
|
|
.then(null, $scope.handleQueryError);
|
|
|
|
};
|
|
|
|
|
2015-09-04 02:41:23 -05:00
|
|
|
$scope.queryUpdated = function() {
|
2015-09-07 01:57:46 -05:00
|
|
|
var newJson = angular.toJson($scope.datasource.queryBuilder.build($scope.target), true);
|
2015-09-04 02:41:23 -05:00
|
|
|
if (newJson !== $scope.oldQueryRaw) {
|
|
|
|
$scope.rawQueryOld = newJson;
|
|
|
|
$scope.get_data();
|
|
|
|
}
|
2015-09-05 08:41:04 -05:00
|
|
|
|
|
|
|
$scope.appEvent('elastic-query-updated');
|
2015-09-03 04:14:25 -05:00
|
|
|
};
|
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
$scope.transformToSegments = function(addTemplateVars) {
|
|
|
|
return function(results) {
|
|
|
|
var segments = _.map(results, function(segment) {
|
|
|
|
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
|
|
|
|
});
|
2015-07-02 09:54:39 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
if (addTemplateVars) {
|
|
|
|
_.each(templateSrv.variables, function(variable) {
|
|
|
|
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return segments;
|
|
|
|
};
|
2015-07-01 13:45:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.handleQueryError = function(err) {
|
|
|
|
$scope.parserError = err.message || 'Failed to issue metric query';
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2015-09-03 01:18:00 -05:00
|
|
|
$scope.toggleQueryMode = function () {
|
2015-09-04 02:41:23 -05:00
|
|
|
if ($scope.target.rawQuery) {
|
|
|
|
delete $scope.target.rawQuery;
|
|
|
|
} else {
|
2015-09-08 09:59:39 -05:00
|
|
|
$scope.target.rawQuery = angular.toJson($scope.datasource.queryBuilder.build($scope.target), true);
|
2015-09-04 02:41:23 -05:00
|
|
|
}
|
2015-09-03 01:18:00 -05:00
|
|
|
};
|
|
|
|
|
2015-09-02 08:25:40 -05:00
|
|
|
$scope.init();
|
2015-07-01 13:45:55 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|