From 4ab278dca49b9e940c4e26ec8655c49cc3600eb2 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Fri, 26 Oct 2018 15:57:01 +0200 Subject: [PATCH] stackdriver: add metric labels query --- .../stackdriver/StackdriverMetricFindQuery.ts | 17 ++++++++ .../components/MetricLabelKeySelector.tsx | 26 ++++++++++++ .../components/MetricTypeSelector.tsx | 2 +- .../components/TemplateQueryComponent.tsx | 40 +++++++++++++++++-- 4 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 public/app/plugins/datasource/stackdriver/components/MetricLabelKeySelector.tsx diff --git a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts index abe88131cc7..1e8d1807f86 100644 --- a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts +++ b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts @@ -1,4 +1,5 @@ import { extractServicesFromMetricDescriptors, getMetricTypesByService } from './functions'; +import has from 'lodash/has'; export default class StackdriverMetricFindQuery { constructor(private datasource) {} @@ -9,6 +10,8 @@ export default class StackdriverMetricFindQuery { return this.handleServiceQueryType(); case 'metricTypes': return this.handleMetricTypesQueryType(query); + case 'metricLabels': + return this.handleMetricLabelsQueryType(query); default: return []; } @@ -33,4 +36,18 @@ export default class StackdriverMetricFindQuery { expandable: true, })); } + + async handleMetricLabelsQueryType({ metricType, metricLabelKey }) { + if (!metricType || !metricLabelKey) { + return []; + } + const refId = 'handleMetricLabelsQueryType'; + const response = await this.datasource.getLabels(metricType, refId); + return has(response, `meta.metricLabels.${metricLabelKey}`) + ? response.meta.metricLabels[metricLabelKey].map(s => ({ + text: s, + expandable: true, + })) + : []; + } } diff --git a/public/app/plugins/datasource/stackdriver/components/MetricLabelKeySelector.tsx b/public/app/plugins/datasource/stackdriver/components/MetricLabelKeySelector.tsx new file mode 100644 index 00000000000..72d1e10e330 --- /dev/null +++ b/public/app/plugins/datasource/stackdriver/components/MetricLabelKeySelector.tsx @@ -0,0 +1,26 @@ +import React, { SFC } from 'react'; + +interface Props { + onMetricLabelKeyChange: any; + metricLabels: any; + metricLabelKey: string; +} + +const MetricLabelKeySelector: SFC = props => { + return ( +
+ Metric Labels +
+ +
+
+ ); +}; + +export default MetricLabelKeySelector; diff --git a/public/app/plugins/datasource/stackdriver/components/MetricTypeSelector.tsx b/public/app/plugins/datasource/stackdriver/components/MetricTypeSelector.tsx index 9aa3f9de6cb..530512defcb 100644 --- a/public/app/plugins/datasource/stackdriver/components/MetricTypeSelector.tsx +++ b/public/app/plugins/datasource/stackdriver/components/MetricTypeSelector.tsx @@ -14,7 +14,7 @@ const MetricTypeSelector: SFC = props => { } return getMetricTypesByService(props.metricDescriptors, props.selectedService).map(m => ({ - value: m.service, + value: m.type, name: m.displayName, })); }; diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx index 95cd9cd6c64..a9e1cbc9fc2 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx @@ -1,23 +1,37 @@ import React, { PureComponent } from 'react'; -// import StackdriverDatasource from '../datasource'; import ServiceSelector from './ServiceSelector'; import MetricTypeSelector from './MetricTypeSelector'; +import MetricLabelKeySelector from './MetricLabelKeySelector'; import { TemplateQueryProps } from 'app/types/plugins'; import defaultsDeep from 'lodash/defaultsDeep'; +import has from 'lodash/has'; export class StackdriverTemplateQueryComponent extends PureComponent { queryTypes: Array<{ value: string; name: string }> = [ { value: 'services', name: 'Services' }, { value: 'metricTypes', name: 'Metric Types' }, - { value: 'metricLabels', name: 'Metric labels For Metric Type' }, + { value: 'metricLabels', name: 'Metric Labels' }, + { value: 'resourceLabels', name: 'Resource Labels' }, + { value: 'resourceTypes', name: 'Resource Types' }, + { value: 'aggregations', name: 'Aggregations' }, + { value: 'alignerns', name: 'Aligners' }, + { value: 'alignmentPeriods', name: 'Alignment Periods' }, ]; - defaults = { type: undefined, metricDescriptors: [], service: undefined, metricType: undefined }; + defaults = { + type: undefined, + metricDescriptors: [], + service: undefined, + metricType: undefined, + metricLabels: [], + metricLabelKey: undefined, + }; constructor(props: TemplateQueryProps) { super(props); this.handleQueryTypeChange = this.handleQueryTypeChange.bind(this); this.onServiceChange = this.onServiceChange.bind(this); this.onMetricTypeChange = this.onMetricTypeChange.bind(this); + this.onMetricLabelKeyChange = this.onMetricLabelKeyChange.bind(this); this.state = defaultsDeep(this.props.query, this.defaults); } @@ -26,6 +40,14 @@ export class StackdriverTemplateQueryComponent extends PureComponent + ); default: