Commit 26b1cc5d by Erik Sundell

stackdriver: join resource and metric labels. split them in values and keys

parent 69137e67
import has from 'lodash/has';
import isString from 'lodash/isString';
import { alignmentPeriods } from './constants';
import { MetricFindQueryTypes } from './types';
......@@ -12,9 +11,10 @@ export default class StackdriverMetricFindQuery {
switch (query.selectedQueryType) {
case MetricFindQueryTypes.MetricTypes:
return this.handleMetricTypesQuery(query);
case MetricFindQueryTypes.MetricLabels:
case MetricFindQueryTypes.ResourceLabels:
return this.handleLabelQuery(query);
case MetricFindQueryTypes.LabelKeys:
return this.handleLabelKeysQuery(query);
case MetricFindQueryTypes.LabelValues:
return this.handleLabelValuesQuery(query);
case MetricFindQueryTypes.ResourceTypes:
return this.handleResourceTypeQuery(query);
case MetricFindQueryTypes.Aligners:
......@@ -44,16 +44,33 @@ export default class StackdriverMetricFindQuery {
}));
}
async handleLabelQuery({ selectedQueryType, selectedMetricType, labelKey }) {
async handleLabelKeysQuery({ selectedQueryType, selectedMetricType, labelKey }) {
if (!selectedMetricType) {
return [];
}
const refId = 'handleLabelsQueryType';
const refId = 'handleLabelKeysQuery';
const response = await this.datasource.getLabels(selectedMetricType, refId);
if (!has(response, `meta.${selectedQueryType}.${labelKey}`)) {
const labelKeys = response.meta
? [...Object.keys(response.meta.resourceLabels), ...Object.keys(response.meta.metricLabels)]
: [];
return labelKeys.map(this.toFindQueryResult);
}
async handleLabelValuesQuery({ selectedQueryType, selectedMetricType, labelKey }) {
if (!selectedMetricType) {
return [];
}
return response.meta[selectedQueryType][labelKey].map(this.toFindQueryResult);
const refId = 'handleLabelValuesQuery';
const response = await this.datasource.getLabels(selectedMetricType, refId);
let values = [];
if (response.meta && response.meta.metricLabels && response.meta.metricLabels.hasOwnProperty(labelKey)) {
values = response.meta.metricLabels[labelKey];
} else if (response.meta && response.meta.resourceLabels && response.meta.resourceLabels.hasOwnProperty(labelKey)) {
values = response.meta.resourceLabels[labelKey];
}
return values.map(this.toFindQueryResult);
}
async handleResourceTypeQuery({ selectedMetricType }) {
......@@ -62,7 +79,7 @@ export default class StackdriverMetricFindQuery {
}
const refId = 'handleResourceTypeQueryQueryType';
const response = await this.datasource.getLabels(selectedMetricType, refId);
return response.meta.resourceTypes.map(this.toFindQueryResult);
return response.meta.resourceTypes ? response.meta.resourceTypes.map(this.toFindQueryResult) : [];
}
async handleAlignersQuery({ selectedMetricType }) {
......
......@@ -34,9 +34,9 @@ describe('VariableQueryEditor', () => {
describe('and an existing variable is edited', () => {
it('should trigger new query using the saved query type', done => {
props.query = { selectedQueryType: MetricFindQueryTypes.MetricLabels };
props.query = { selectedQueryType: MetricFindQueryTypes.LabelKeys };
props.onChange = (query, definition) => {
expect(definition).toBe('Stackdriver - Metric Labels');
expect(definition).toBe('Stackdriver - Labels Keys');
done();
};
renderer.create(<StackdriverVariableQueryEditor {...props} />).toJSON();
......
......@@ -8,8 +8,8 @@ import { MetricFindQueryTypes, VariableQueryData } from '../types';
export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryProps, VariableQueryData> {
queryTypes: Array<{ value: string; name: string }> = [
{ value: MetricFindQueryTypes.MetricTypes, name: 'Metric Types' },
{ value: MetricFindQueryTypes.MetricLabels, name: 'Metric Labels' },
{ value: MetricFindQueryTypes.ResourceLabels, name: 'Resource Labels' },
{ value: MetricFindQueryTypes.LabelKeys, name: 'Label Keys' },
{ value: MetricFindQueryTypes.LabelValues, name: 'Label Values' },
{ value: MetricFindQueryTypes.ResourceTypes, name: 'Resource Types' },
{ value: MetricFindQueryTypes.Aggregations, name: 'Aggregations' },
{ value: MetricFindQueryTypes.Aligners, name: 'Aligners' },
......@@ -57,7 +57,7 @@ export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryP
metricTypes,
selectedMetricType,
metricDescriptors,
...await this.getLabels(selectedMetricType),
...await this.getLabelValues(selectedMetricType),
};
this.setState(state);
}
......@@ -65,7 +65,7 @@ export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryP
async handleQueryTypeChange(event) {
const state: any = {
selectedQueryType: event.target.value,
...await this.getLabels(this.state.selectedMetricType, event.target.value),
...await this.getLabelValues(this.state.selectedMetricType, event.target.value),
};
this.setState(state);
}
......@@ -80,13 +80,13 @@ export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryP
selectedService: event.target.value,
metricTypes,
selectedMetricType,
...await this.getLabels(selectedMetricType),
...await this.getLabelValues(selectedMetricType),
};
this.setState(state);
}
async onMetricTypeChange(event) {
const state: any = { selectedMetricType: event.target.value, ...await this.getLabels(event.target.value) };
const state: any = { selectedMetricType: event.target.value, ...await this.getLabelValues(event.target.value) };
this.setState(state);
}
......@@ -100,16 +100,12 @@ export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryP
this.props.onChange(queryModel, `Stackdriver - ${query.name}`);
}
isLabelQuery(queryType) {
return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
}
async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
async getLabelValues(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
let result = { labels: this.state.labels, labelKey: this.state.labelKey };
if (selectedMetricType && this.isLabelQuery(selectedQueryType)) {
if (selectedMetricType && selectedQueryType === MetricFindQueryTypes.LabelValues) {
const refId = 'StackdriverVariableQueryEditor';
const response = await this.props.datasource.getLabels(selectedMetricType, refId);
const labels = Object.keys(response.meta[selectedQueryType]);
const labels = [...Object.keys(response.meta.resourceLabels), ...Object.keys(response.meta.metricLabels)];
const labelKey = labels.some(l => l === this.state.labelKey) ? this.state.labelKey : labels[0];
result = { labels, labelKey };
}
......@@ -127,8 +123,8 @@ export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryP
label="Services"
/>
);
case MetricFindQueryTypes.MetricLabels:
case MetricFindQueryTypes.ResourceLabels:
case MetricFindQueryTypes.LabelKeys:
case MetricFindQueryTypes.LabelValues:
case MetricFindQueryTypes.ResourceTypes:
return (
<React.Fragment>
......@@ -144,16 +140,12 @@ export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryP
onValueChange={e => this.onMetricTypeChange(e)}
label="Metric Types"
/>
{queryType !== MetricFindQueryTypes.ResourceTypes && (
{queryType === MetricFindQueryTypes.LabelValues && (
<SimpleSelect
value={this.state.labelKey}
options={this.state.labels.map(l => ({ value: l, name: l }))}
onValueChange={e => this.onLabelKeyChange(e)}
label={
this.state.selectedQueryType === MetricFindQueryTypes.ResourceLabels
? 'Resource Label Key'
: 'Metric Label Key'
}
label="Label Keys"
/>
)}
</React.Fragment>
......
export enum MetricFindQueryTypes {
MetricTypes = 'metricTypes',
MetricLabels = 'metricLabels',
ResourceLabels = 'resourceLabels',
LabelKeys = 'labelKeys',
LabelValues = 'labelValues',
ResourceTypes = 'resourceTypes',
Aggregations = 'aggregations',
Aligners = 'aligners',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment