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