2015-07-01 13:45:55 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
'./queryBuilder',
|
|
|
|
],
|
|
|
|
function (angular, _, ElasticQueryBuilder) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2015-09-02 10:45:41 -05:00
|
|
|
module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv, $q) {
|
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-03 01:18:00 -05:00
|
|
|
target.timeField = target.timeField || '@timestamp';
|
2015-09-05 02:05:09 -05:00
|
|
|
target.metrics = target.metrics || [{ type: 'count' }];
|
2015-09-04 09:05:47 -05:00
|
|
|
target.bucketAggs = target.bucketAggs || [];
|
|
|
|
target.bucketAggs = [
|
|
|
|
{
|
|
|
|
type: 'terms',
|
|
|
|
field: '@hostname'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'date_histogram',
|
|
|
|
field: '@timestamp'
|
|
|
|
},
|
|
|
|
];
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-03 01:18:00 -05:00
|
|
|
$scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
|
2015-09-03 04:14:25 -05:00
|
|
|
|
2015-09-03 08:56:41 -05:00
|
|
|
$scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
|
|
|
|
$scope.resetSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- reset --'});
|
2015-09-04 02:41:23 -05:00
|
|
|
|
|
|
|
$scope.queryBuilder = new ElasticQueryBuilder(target);
|
|
|
|
$scope.rawQueryOld = angular.toJson($scope.queryBuilder.build($scope.target), true);
|
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-05 03:14:21 -05:00
|
|
|
console.log('qery updated');
|
2015-09-04 02:41:23 -05:00
|
|
|
var newJson = angular.toJson($scope.queryBuilder.build($scope.target), true);
|
|
|
|
if (newJson !== $scope.oldQueryRaw) {
|
|
|
|
$scope.rawQueryOld = newJson;
|
|
|
|
$scope.get_data();
|
|
|
|
}
|
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 }));
|
|
|
|
});
|
|
|
|
}
|
2015-08-31 07:25:05 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
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 {
|
|
|
|
$scope.target.rawQuery = $scope.rawQueryOld;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|