|
@@ -1,7 +1,8 @@
|
|
|
import coreModule from 'app/core/core_module';
|
|
import coreModule from 'app/core/core_module';
|
|
|
import _ from 'lodash';
|
|
import _ from 'lodash';
|
|
|
import { FilterSegments } from './filter_segments';
|
|
import { FilterSegments } from './filter_segments';
|
|
|
-import appEvents from 'app/core/app_events';
|
|
|
|
|
|
|
+import { QueryMeta } from './query_ctrl';
|
|
|
|
|
+// import appEvents from 'app/core/app_events';
|
|
|
|
|
|
|
|
export class StackdriverFilter {
|
|
export class StackdriverFilter {
|
|
|
/** @ngInject */
|
|
/** @ngInject */
|
|
@@ -12,8 +13,9 @@ export class StackdriverFilter {
|
|
|
controllerAs: 'ctrl',
|
|
controllerAs: 'ctrl',
|
|
|
restrict: 'E',
|
|
restrict: 'E',
|
|
|
scope: {
|
|
scope: {
|
|
|
|
|
+ labelData: '<',
|
|
|
|
|
+ loading: '<',
|
|
|
target: '=',
|
|
target: '=',
|
|
|
- datasource: '=',
|
|
|
|
|
refresh: '&',
|
|
refresh: '&',
|
|
|
hideGroupBys: '<',
|
|
hideGroupBys: '<',
|
|
|
},
|
|
},
|
|
@@ -28,41 +30,26 @@ export class StackdriverFilterCtrl {
|
|
|
|
|
|
|
|
defaultRemoveGroupByValue = '-- remove group by --';
|
|
defaultRemoveGroupByValue = '-- remove group by --';
|
|
|
resourceTypeValue = 'resource.type';
|
|
resourceTypeValue = 'resource.type';
|
|
|
- loadLabelsPromise: Promise<any>;
|
|
|
|
|
|
|
|
|
|
metricDescriptors: any[];
|
|
metricDescriptors: any[];
|
|
|
metrics: any[];
|
|
metrics: any[];
|
|
|
- metricGroups: any[];
|
|
|
|
|
services: any[];
|
|
services: any[];
|
|
|
groupBySegments: any[];
|
|
groupBySegments: any[];
|
|
|
filterSegments: FilterSegments;
|
|
filterSegments: FilterSegments;
|
|
|
removeSegment: any;
|
|
removeSegment: any;
|
|
|
target: any;
|
|
target: any;
|
|
|
- datasource: any;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ labelData: QueryMeta;
|
|
|
|
|
+ loading: Promise<any>;
|
|
|
|
|
|
|
|
/** @ngInject */
|
|
/** @ngInject */
|
|
|
constructor(private $scope, private uiSegmentSrv, private templateSrv, private $rootScope) {
|
|
constructor(private $scope, private uiSegmentSrv, private templateSrv, private $rootScope) {
|
|
|
- this.datasource = $scope.datasource;
|
|
|
|
|
this.target = $scope.target;
|
|
this.target = $scope.target;
|
|
|
this.metricDescriptors = [];
|
|
this.metricDescriptors = [];
|
|
|
this.metrics = [];
|
|
this.metrics = [];
|
|
|
- this.metricGroups = [];
|
|
|
|
|
this.services = [];
|
|
this.services = [];
|
|
|
|
|
|
|
|
- this.getCurrentProject()
|
|
|
|
|
- .then(this.loadMetricDescriptors.bind(this))
|
|
|
|
|
- .then(this.getLabels.bind(this));
|
|
|
|
|
-
|
|
|
|
|
this.initSegments($scope.hideGroupBys);
|
|
this.initSegments($scope.hideGroupBys);
|
|
|
- this.handleMetricTypeChange = this.handleMetricTypeChange.bind(this);
|
|
|
|
|
- this.handleServiceChange = this.handleServiceChange.bind(this);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- handleMetricTypeChange(value) {
|
|
|
|
|
- this.target.metricType = value;
|
|
|
|
|
- this.setMetricType();
|
|
|
|
|
- this.$scope.refresh();
|
|
|
|
|
- this.getLabels();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
initSegments(hideGroupBys: boolean) {
|
|
initSegments(hideGroupBys: boolean) {
|
|
@@ -84,129 +71,34 @@ export class StackdriverFilterCtrl {
|
|
|
this.filterSegments.buildSegmentModel();
|
|
this.filterSegments.buildSegmentModel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async getCurrentProject() {
|
|
|
|
|
- return new Promise(async (resolve, reject) => {
|
|
|
|
|
- try {
|
|
|
|
|
- if (!this.target.defaultProject || this.target.defaultProject === 'loading project...') {
|
|
|
|
|
- this.target.defaultProject = await this.datasource.getDefaultProject();
|
|
|
|
|
- }
|
|
|
|
|
- resolve(this.target.defaultProject);
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- appEvents.emit('ds-request-error', error);
|
|
|
|
|
- reject();
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async loadMetricDescriptors() {
|
|
|
|
|
- if (this.target.defaultProject !== 'loading project...') {
|
|
|
|
|
- this.metricDescriptors = await this.datasource.getMetricTypes(this.target.defaultProject);
|
|
|
|
|
- this.services = this.getServicesList();
|
|
|
|
|
- this.metrics = this.getMetricsList();
|
|
|
|
|
- this.metricGroups = this.getMetricGroups();
|
|
|
|
|
- return this.metricDescriptors;
|
|
|
|
|
- } else {
|
|
|
|
|
- return [];
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- getServicesList() {
|
|
|
|
|
- const services = this.metricDescriptors.map(m => ({
|
|
|
|
|
- value: m.service,
|
|
|
|
|
- label: _.startCase(m.serviceShortName),
|
|
|
|
|
- }));
|
|
|
|
|
-
|
|
|
|
|
- return services.length > 0 ? _.uniqBy(services, s => s.value) : [];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- getMetricGroups() {
|
|
|
|
|
- return [
|
|
|
|
|
- this.getTemplateVariablesGroup(),
|
|
|
|
|
- {
|
|
|
|
|
- label: 'Metrics',
|
|
|
|
|
- options: this.getMetricsList(),
|
|
|
|
|
- },
|
|
|
|
|
- ];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- getTemplateVariablesGroup() {
|
|
|
|
|
- return {
|
|
|
|
|
- label: 'Template Variables',
|
|
|
|
|
- options: this.templateSrv.variables.map(v => ({
|
|
|
|
|
- label: `$${v.name}`,
|
|
|
|
|
- value: `$${v.name}`,
|
|
|
|
|
- })),
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- getMetricsList() {
|
|
|
|
|
- const metricsByService = this.metricDescriptors.filter(m => m.service === this.target.service).map(m => ({
|
|
|
|
|
- service: m.service,
|
|
|
|
|
- value: m.type,
|
|
|
|
|
- label: m.displayName,
|
|
|
|
|
- description: m.description,
|
|
|
|
|
- }));
|
|
|
|
|
-
|
|
|
|
|
- if (
|
|
|
|
|
- metricsByService.length > 0 &&
|
|
|
|
|
- !metricsByService.some(m => m.value === this.templateSrv.replace(this.target.metricType))
|
|
|
|
|
- ) {
|
|
|
|
|
- this.target.metricType = metricsByService[0].value;
|
|
|
|
|
- }
|
|
|
|
|
- return metricsByService;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async getLabels() {
|
|
|
|
|
- this.loadLabelsPromise = new Promise(async resolve => {
|
|
|
|
|
- try {
|
|
|
|
|
- if (this.target.metricType) {
|
|
|
|
|
- const { meta } = await this.datasource.getLabels(this.target.metricType, this.target.refId);
|
|
|
|
|
- this.metricLabels = meta.metricLabels;
|
|
|
|
|
- this.resourceLabels = meta.resourceLabels;
|
|
|
|
|
- this.resourceTypes = meta.resourceTypes;
|
|
|
|
|
- resolve();
|
|
|
|
|
- } else {
|
|
|
|
|
- resolve();
|
|
|
|
|
- }
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- if (error.data && error.data.message) {
|
|
|
|
|
- console.log(error.data.message);
|
|
|
|
|
- } else {
|
|
|
|
|
- console.log(error);
|
|
|
|
|
- }
|
|
|
|
|
- appEvents.emit('alert-error', ['Error', 'Error loading metric labels for ' + this.target.metricType]);
|
|
|
|
|
- resolve();
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- handleServiceChange(service) {
|
|
|
|
|
- this.target.service = service;
|
|
|
|
|
- this.metrics = this.getMetricsList();
|
|
|
|
|
- this.metricGroups = this.getMetricGroups();
|
|
|
|
|
- this.setMetricType();
|
|
|
|
|
- this.getLabels();
|
|
|
|
|
- if (!this.metrics.some(m => m.value === this.target.metricType)) {
|
|
|
|
|
- this.target.metricType = '';
|
|
|
|
|
- } else {
|
|
|
|
|
- this.$scope.refresh();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- setMetricType() {
|
|
|
|
|
- const { valueType, metricKind, unit } = this.metricDescriptors.find(
|
|
|
|
|
- m => m.type === this.templateSrv.replace(this.target.metricType)
|
|
|
|
|
- );
|
|
|
|
|
- this.target.unit = unit;
|
|
|
|
|
- this.target.valueType = valueType;
|
|
|
|
|
- this.target.metricKind = metricKind;
|
|
|
|
|
- this.$rootScope.$broadcast('metricTypeChanged');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // async getLabels() {
|
|
|
|
|
+ // this.loadLabelsPromise = new Promise(async resolve => {
|
|
|
|
|
+ // try {
|
|
|
|
|
+ // if (this.target.metricType) {
|
|
|
|
|
+ // const { meta } = await this.datasource.getLabels(this.target.metricType, this.target.refId);
|
|
|
|
|
+ // this.$scope.labelData.metricLabels = meta.metricLabels;
|
|
|
|
|
+ // this.$scope.labelData.resourceLabels = meta.resourceLabels;
|
|
|
|
|
+ // this.$scope.labelData.resourceTypes = meta.resourceTypes;
|
|
|
|
|
+ // resolve();
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // resolve();
|
|
|
|
|
+ // }
|
|
|
|
|
+ // } catch (error) {
|
|
|
|
|
+ // if (error.data && error.data.message) {
|
|
|
|
|
+ // console.log(error.data.message);
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // console.log(error);
|
|
|
|
|
+ // }
|
|
|
|
|
+ // appEvents.emit('alert-error', ['Error', 'Error loading metric labels for ' + this.target.metricType]);
|
|
|
|
|
+ // resolve();
|
|
|
|
|
+ // }
|
|
|
|
|
+ // });
|
|
|
|
|
+ // }
|
|
|
|
|
|
|
|
async createLabelKeyElements() {
|
|
async createLabelKeyElements() {
|
|
|
- await this.loadLabelsPromise;
|
|
|
|
|
|
|
+ await this.$scope.loading;
|
|
|
|
|
|
|
|
- let elements = Object.keys(this.metricLabels || {}).map(l => {
|
|
|
|
|
|
|
+ let elements = Object.keys(this.$scope.labelData.metricLabels || {}).map(l => {
|
|
|
return this.uiSegmentSrv.newSegment({
|
|
return this.uiSegmentSrv.newSegment({
|
|
|
value: `metric.label.${l}`,
|
|
value: `metric.label.${l}`,
|
|
|
expandable: false,
|
|
expandable: false,
|
|
@@ -215,7 +107,7 @@ export class StackdriverFilterCtrl {
|
|
|
|
|
|
|
|
elements = [
|
|
elements = [
|
|
|
...elements,
|
|
...elements,
|
|
|
- ...Object.keys(this.resourceLabels || {}).map(l => {
|
|
|
|
|
|
|
+ ...Object.keys(this.$scope.labelData.resourceLabels || {}).map(l => {
|
|
|
return this.uiSegmentSrv.newSegment({
|
|
return this.uiSegmentSrv.newSegment({
|
|
|
value: `resource.label.${l}`,
|
|
value: `resource.label.${l}`,
|
|
|
expandable: false,
|
|
expandable: false,
|
|
@@ -223,7 +115,7 @@ export class StackdriverFilterCtrl {
|
|
|
}),
|
|
}),
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- if (this.resourceTypes && this.resourceTypes.length > 0) {
|
|
|
|
|
|
|
+ if (this.$scope.labelData.resourceTypes && this.$scope.labelData.resourceTypes.length > 0) {
|
|
|
elements = [
|
|
elements = [
|
|
|
...elements,
|
|
...elements,
|
|
|
this.uiSegmentSrv.newSegment({
|
|
this.uiSegmentSrv.newSegment({
|
|
@@ -288,28 +180,33 @@ export class StackdriverFilterCtrl {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async getFilters(segment, index) {
|
|
async getFilters(segment, index) {
|
|
|
- const hasNoFilterKeys = this.metricLabels && Object.keys(this.metricLabels).length === 0;
|
|
|
|
|
|
|
+ const hasNoFilterKeys =
|
|
|
|
|
+ this.$scope.labelData.metricLabels && Object.keys(this.$scope.labelData.metricLabels).length === 0;
|
|
|
return this.filterSegments.getFilters(segment, index, hasNoFilterKeys);
|
|
return this.filterSegments.getFilters(segment, index, hasNoFilterKeys);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
getFilterValues(index) {
|
|
getFilterValues(index) {
|
|
|
const filterKey = this.templateSrv.replace(this.filterSegments.filterSegments[index - 2].value);
|
|
const filterKey = this.templateSrv.replace(this.filterSegments.filterSegments[index - 2].value);
|
|
|
- if (!filterKey || !this.metricLabels || Object.keys(this.metricLabels).length === 0) {
|
|
|
|
|
|
|
+ if (
|
|
|
|
|
+ !filterKey ||
|
|
|
|
|
+ !this.$scope.labelData.metricLabels ||
|
|
|
|
|
+ Object.keys(this.$scope.labelData.metricLabels).length === 0
|
|
|
|
|
+ ) {
|
|
|
return [];
|
|
return [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const shortKey = filterKey.substring(filterKey.indexOf('.label.') + 7);
|
|
const shortKey = filterKey.substring(filterKey.indexOf('.label.') + 7);
|
|
|
|
|
|
|
|
- if (filterKey.startsWith('metric.label.') && this.metricLabels.hasOwnProperty(shortKey)) {
|
|
|
|
|
- return this.metricLabels[shortKey];
|
|
|
|
|
|
|
+ if (filterKey.startsWith('metric.label.') && this.$scope.labelData.metricLabels.hasOwnProperty(shortKey)) {
|
|
|
|
|
+ return this.$scope.labelData.metricLabels[shortKey];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (filterKey.startsWith('resource.label.') && this.resourceLabels.hasOwnProperty(shortKey)) {
|
|
|
|
|
- return this.resourceLabels[shortKey];
|
|
|
|
|
|
|
+ if (filterKey.startsWith('resource.label.') && this.$scope.labelData.resourceLabels.hasOwnProperty(shortKey)) {
|
|
|
|
|
+ return this.$scope.labelData.resourceLabels[shortKey];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (filterKey === this.resourceTypeValue) {
|
|
if (filterKey === this.resourceTypeValue) {
|
|
|
- return this.resourceTypes;
|
|
|
|
|
|
|
+ return this.$scope.labelData.resourceTypes;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
return [];
|