grafana/public/app/plugins/datasource/stackdriver/functions.ts
Erik Sundell 260239d98b
Stackdriver: Support meta labels (#21373)
* Rewrite angular segments for filter and group by in react

* wip: refactoring

* Update metric find queries

* Remove old maps used to create labels - use one map for all types instead

* Use value as label (again) for filters ang groupby

* Remove old filter

* Remove not used code

* Fixes after pr feedback

* Fix broken tests and add new metadata tests

* Add index file to make imports cleaner

* Cleanup. Remove old angular filter code

* Fix broken tests

* Use type switching instead of if statements

* Use globals for regex

* Updates after pr feedback

* Make sure it's possible to filter using the same key multiple times

* Replace metric select with segment component

* Pass template vars as props

* Refactor meta labels code

* Reorder template variables

* Fix broken tests

* Reset metric value when changing service

* Fix lint issue.

* Make tests independant of element order

* Include kubernetes.io in regex

* Add instruction in help section
2020-01-17 12:25:47 +01:00

85 lines
3.0 KiB
TypeScript

import _ from 'lodash';
import { alignOptions, aggOptions, ValueTypes, MetricKind, systemLabels } from './constants';
import { SelectableValue } from '@grafana/data';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { StackdriverQuery } from './types';
export const extractServicesFromMetricDescriptors = (metricDescriptors: any) => _.uniqBy(metricDescriptors, 'service');
export const getMetricTypesByService = (metricDescriptors: any, service: any) =>
metricDescriptors.filter((m: any) => m.service === service);
export const getMetricTypes = (
metricDescriptors: any[],
metricType: string,
interpolatedMetricType: any,
selectedService: any
) => {
const metricTypes = getMetricTypesByService(metricDescriptors, selectedService).map((m: any) => ({
value: m.type,
name: m.displayName,
}));
const metricTypeExistInArray = metricTypes.some((m: any) => m.value === interpolatedMetricType);
const selectedMetricType = metricTypeExistInArray ? metricType : metricTypes[0].value;
return {
metricTypes,
selectedMetricType,
};
};
export const getAlignmentOptionsByMetric = (metricValueType: any, metricKind: any) => {
return !metricValueType
? []
: alignOptions.filter(i => {
return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
});
};
export const getAggregationOptionsByMetric = (valueType: ValueTypes, metricKind: MetricKind) => {
return !metricKind
? []
: aggOptions.filter(i => {
return i.valueTypes.indexOf(valueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
});
};
export const getLabelKeys = async (datasource: any, selectedMetricType: any) => {
const refId = 'handleLabelKeysQuery';
const labels = await datasource.getLabels(selectedMetricType, refId);
return [...Object.keys(labels), ...systemLabels];
};
export const getAlignmentPickerData = (
{ valueType, metricKind, perSeriesAligner }: Partial<StackdriverQuery>,
templateSrv: TemplateSrv
) => {
const alignOptions = getAlignmentOptionsByMetric(valueType, metricKind).map(option => ({
...option,
label: option.text,
}));
if (!alignOptions.some(o => o.value === templateSrv.replace(perSeriesAligner))) {
perSeriesAligner = alignOptions.length > 0 ? alignOptions[0].value : '';
}
return { alignOptions, perSeriesAligner };
};
export const labelsToGroupedOptions = (groupBys: string[]) => {
const groups = groupBys.reduce((acc: any, curr: string) => {
const arr = curr.split('.').map(_.startCase);
const group = (arr.length === 2 ? arr : _.initial(arr)).join(' ');
const option = {
value: curr,
label: curr,
};
if (acc[group]) {
acc[group] = [...acc[group], option];
} else {
acc[group] = [option];
}
return acc;
}, {});
return Object.entries(groups).map(([label, options]) => ({ label, options, expanded: true }), []);
};
export const toOption = (value: string) => ({ label: value, value } as SelectableValue<string>);