grafana/public/app/plugins/datasource/stackdriver/functions.ts

49 lines
1.7 KiB
TypeScript
Raw Normal View History

import uniqBy from 'lodash/uniqBy';
2018-10-31 20:55:40 +08:00
import { alignOptions, aggOptions } from './constants';
export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy(metricDescriptors, 'service');
export const getMetricTypesByService = (metricDescriptors, service) =>
metricDescriptors.filter(m => m.service === service);
2018-10-29 23:27:53 +08:00
export const getMetricTypes = (metricDescriptors, metricType, interpolatedMetricType, selectedService) => {
2018-10-31 20:36:41 +08:00
const metricTypes = getMetricTypesByService(metricDescriptors, selectedService).map(m => ({
2018-10-31 20:07:57 +08:00
value: m.type,
name: m.displayName,
}));
const metricTypeExistInArray = metricTypes.some(m => m.value === interpolatedMetricType);
2018-11-01 17:14:28 +08:00
const selectedMetricType = metricTypeExistInArray ? metricType : metricTypes[0].value;
2018-10-31 20:07:57 +08:00
return {
metricTypes,
2018-10-31 20:36:41 +08:00
selectedMetricType,
2018-10-31 20:07:57 +08:00
};
};
2018-10-29 23:27:53 +08:00
export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => {
return !metricValueType
? []
: alignOptions.filter(i => {
return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
});
};
2018-10-29 23:57:07 +08:00
export const getAggregationOptionsByMetric = (valueType, metricKind) => {
return !metricKind
? []
: aggOptions.filter(i => {
return i.valueTypes.indexOf(valueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
});
};
export const getLabelKeys = async (datasource, selectedMetricType) => {
const refId = 'handleLabelKeysQuery';
const response = await datasource.getLabels(selectedMetricType, refId);
const labelKeys = response.meta
? [
...Object.keys(response.meta.resourceLabels).map(l => `resource.label.${l}`),
...Object.keys(response.meta.metricLabels).map(l => `metric.label.${l}`),
]
: [];
return labelKeys;
};