From a0b8c4acba8eb9d07cec7fd0630206e90c69d8f7 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Mon, 29 Oct 2018 16:27:53 +0100 Subject: [PATCH] stackdriver: add aligner query --- .../stackdriver/StackdriverMetricFindQuery.ts | 17 ++++++++++++++++- .../components/TemplateQueryComponent.tsx | 11 +++++++++++ .../plugins/datasource/stackdriver/functions.ts | 9 +++++++++ .../stackdriver/query_aggregation_ctrl.ts | 9 ++------- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts index 83fe2094a82..7568c853848 100644 --- a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts +++ b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts @@ -1,4 +1,8 @@ -import { extractServicesFromMetricDescriptors, getMetricTypesByService } from './functions'; +import { + extractServicesFromMetricDescriptors, + getMetricTypesByService, + getAlignmentOptionsByMetric, +} from './functions'; import has from 'lodash/has'; export default class StackdriverMetricFindQuery { @@ -15,6 +19,8 @@ export default class StackdriverMetricFindQuery { return this.handleLabelQueryType(query); case 'resourceTypes': return this.handleResourceType(query); + case 'alignerns': + return this.handleAlignersType(query); default: return []; } @@ -85,4 +91,13 @@ export default class StackdriverMetricFindQuery { return []; } } + + async handleAlignersType({ metricType }) { + if (!metricType) { + return []; + } + const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName); + const { valueType, metricKind } = metricDescriptors.find(m => m.type === metricType); + return getAlignmentOptionsByMetric(valueType, metricKind); + } } diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx index 3d1bef0aa15..d68864d5a34 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx @@ -133,6 +133,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent ); + case 'alignerns': + return ( + + + + + ); default: return ''; } diff --git a/public/app/plugins/datasource/stackdriver/functions.ts b/public/app/plugins/datasource/stackdriver/functions.ts index 15e84f050e8..8b3551d153a 100644 --- a/public/app/plugins/datasource/stackdriver/functions.ts +++ b/public/app/plugins/datasource/stackdriver/functions.ts @@ -1,6 +1,15 @@ +import { alignOptions } from './constants'; import uniqBy from 'lodash/uniqBy'; export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy(metricDescriptors, 'service'); export const getMetricTypesByService = (metricDescriptors, service) => metricDescriptors.filter(m => m.service === service); + +export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => { + return !metricValueType + ? [] + : alignOptions.filter(i => { + return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1; + }); +}; diff --git a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts index 6cd6c805463..1340aa7f690 100644 --- a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts @@ -1,6 +1,7 @@ import coreModule from 'app/core/core_module'; import _ from 'lodash'; import * as options from './constants'; +import { getAlignmentOptionsByMetric } from './functions'; import kbn from 'app/core/utils/kbn'; export class StackdriverAggregation { @@ -41,13 +42,7 @@ export class StackdriverAggregationCtrl { } setAlignOptions() { - this.alignOptions = !this.target.valueType - ? [] - : options.alignOptions.filter(i => { - return ( - i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1 - ); - }); + this.alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind); if (!this.alignOptions.find(o => o.value === this.target.aggregation.perSeriesAligner)) { this.target.aggregation.perSeriesAligner = this.alignOptions.length > 0 ? this.alignOptions[0].value : ''; }