2015-07-02 02:45:55 +08:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
],
|
2015-11-23 21:18:21 +08:00
|
|
|
function (angular) {
|
2015-07-02 02:45:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2015-11-23 21:18:21 +08:00
|
|
|
module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv) {
|
2015-07-02 02:45:55 +08:00
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
var target = $scope.target;
|
2015-09-04 15:41:23 +08:00
|
|
|
if (!target) { return; }
|
|
|
|
|
2015-09-05 18:24:14 +08:00
|
|
|
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
|
2015-09-07 22:35:40 +08:00
|
|
|
target.bucketAggs = target.bucketAggs || [{type: 'date_histogram', id: '2', settings: {interval: 'auto'}}];
|
2015-09-07 15:36:56 +08:00
|
|
|
target.timeField = $scope.datasource.timeField;
|
2015-09-03 21:56:41 +08:00
|
|
|
};
|
|
|
|
|
2015-09-22 15:31:58 +08:00
|
|
|
$scope.getFields = function(type) {
|
|
|
|
var jsonStr = angular.toJson({find: 'fields', type: type});
|
|
|
|
return $scope.datasource.metricFindQuery(jsonStr)
|
2015-11-23 21:18:21 +08:00
|
|
|
.then(uiSegmentSrv.transformToSegments(false))
|
2015-09-04 17:17:52 +08:00
|
|
|
.then(null, $scope.handleQueryError);
|
|
|
|
};
|
|
|
|
|
2015-09-04 15:41:23 +08:00
|
|
|
$scope.queryUpdated = function() {
|
2015-09-07 14:57:46 +08:00
|
|
|
var newJson = angular.toJson($scope.datasource.queryBuilder.build($scope.target), true);
|
2015-09-04 15:41:23 +08:00
|
|
|
if (newJson !== $scope.oldQueryRaw) {
|
|
|
|
$scope.rawQueryOld = newJson;
|
|
|
|
$scope.get_data();
|
|
|
|
}
|
2015-09-05 21:41:04 +08:00
|
|
|
|
|
|
|
$scope.appEvent('elastic-query-updated');
|
2015-09-03 17:14:25 +08:00
|
|
|
};
|
|
|
|
|
2015-07-02 02:45:55 +08:00
|
|
|
$scope.handleQueryError = function(err) {
|
|
|
|
$scope.parserError = err.message || 'Failed to issue metric query';
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2015-09-03 14:18:00 +08:00
|
|
|
$scope.toggleQueryMode = function () {
|
2015-09-04 15:41:23 +08:00
|
|
|
if ($scope.target.rawQuery) {
|
|
|
|
delete $scope.target.rawQuery;
|
|
|
|
} else {
|
2015-09-08 22:59:39 +08:00
|
|
|
$scope.target.rawQuery = angular.toJson($scope.datasource.queryBuilder.build($scope.target), true);
|
2015-09-04 15:41:23 +08:00
|
|
|
}
|
2015-09-03 14:18:00 +08:00
|
|
|
};
|
|
|
|
|
2015-09-02 21:25:40 +08:00
|
|
|
$scope.init();
|
2015-07-02 02:45:55 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|