grafana/public/app/plugins/datasource/elasticsearch/query_ctrl.ts

93 lines
2.2 KiB
TypeScript
Raw Normal View History

///<reference path="../../../headers/common.d.ts" />
import "./bucket_agg";
import "./metric_agg";
import angular from "angular";
import _ from "lodash";
import * as queryDef from "./query_def";
import { QueryCtrl } from "app/plugins/sdk";
export class ElasticQueryCtrl extends QueryCtrl {
static templateUrl = "partials/query.editor.html";
esVersion: any;
rawQueryOld: string;
/** @ngInject **/
constructor($scope, $injector, private $rootScope, private uiSegmentSrv) {
super($scope, $injector);
this.esVersion = this.datasource.esVersion;
this.queryUpdated();
}
getFields(type) {
var jsonStr = angular.toJson({ find: "fields", type: type });
return this.datasource
.metricFindQuery(jsonStr)
.then(this.uiSegmentSrv.transformToSegments(false))
.catch(this.handleQueryError.bind(this));
}
queryUpdated() {
var newJson = angular.toJson(
this.datasource.queryBuilder.build(this.target),
true
);
2017-06-16 03:56:24 +08:00
if (this.rawQueryOld && newJson !== this.rawQueryOld) {
this.refresh();
}
2017-06-16 03:56:24 +08:00
this.rawQueryOld = newJson;
this.$rootScope.appEvent("elastic-query-updated");
}
getCollapsedText() {
2016-04-27 16:16:04 +08:00
var metricAggs = this.target.metrics;
var bucketAggs = this.target.bucketAggs;
var metricAggTypes = queryDef.getMetricAggTypes(this.esVersion);
var bucketAggTypes = queryDef.bucketAggTypes;
var text = "";
2016-04-27 16:16:04 +08:00
if (this.target.query) {
text += "Query: " + this.target.query + ", ";
2016-04-27 16:16:04 +08:00
}
text += "Metrics: ";
2016-04-27 16:16:04 +08:00
_.each(metricAggs, (metric, index) => {
var aggDef = _.find(metricAggTypes, { value: metric.type });
text += aggDef.text + "(";
2016-04-27 16:16:04 +08:00
if (aggDef.requiresField) {
text += metric.field;
}
text += "), ";
2016-04-27 16:16:04 +08:00
});
_.each(bucketAggs, (bucketAgg, index) => {
if (index === 0) {
text += " Group by: ";
2016-04-27 16:16:04 +08:00
}
var aggDef = _.find(bucketAggTypes, { value: bucketAgg.type });
text += aggDef.text + "(";
2016-04-27 16:16:04 +08:00
if (aggDef.requiresField) {
text += bucketAgg.field;
}
text += "), ";
2016-04-27 16:16:04 +08:00
});
if (this.target.alias) {
text += "Alias: " + this.target.alias;
2016-04-27 16:16:04 +08:00
}
return text;
}
handleQueryError(err) {
this.error = err.message || "Failed to issue metric query";
return [];
}
}