functions.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import uniqBy from 'lodash/uniqBy';
  2. import { alignOptions, aggOptions, ValueTypes, MetricKind } from './constants';
  3. import { TemplateSrv } from 'app/features/templating/template_srv';
  4. import { StackdriverQuery } from './types';
  5. export const extractServicesFromMetricDescriptors = (metricDescriptors: any) => uniqBy(metricDescriptors, 'service');
  6. export const getMetricTypesByService = (metricDescriptors: any, service: any) =>
  7. metricDescriptors.filter((m: any) => m.service === service);
  8. export const getMetricTypes = (
  9. metricDescriptors: any[],
  10. metricType: string,
  11. interpolatedMetricType: any,
  12. selectedService: any
  13. ) => {
  14. const metricTypes = getMetricTypesByService(metricDescriptors, selectedService).map((m: any) => ({
  15. value: m.type,
  16. name: m.displayName,
  17. }));
  18. const metricTypeExistInArray = metricTypes.some((m: any) => m.value === interpolatedMetricType);
  19. const selectedMetricType = metricTypeExistInArray ? metricType : metricTypes[0].value;
  20. return {
  21. metricTypes,
  22. selectedMetricType,
  23. };
  24. };
  25. export const getAlignmentOptionsByMetric = (metricValueType: any, metricKind: any) => {
  26. return !metricValueType
  27. ? []
  28. : alignOptions.filter(i => {
  29. return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
  30. });
  31. };
  32. export const getAggregationOptionsByMetric = (valueType: ValueTypes, metricKind: MetricKind) => {
  33. return !metricKind
  34. ? []
  35. : aggOptions.filter(i => {
  36. return i.valueTypes.indexOf(valueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
  37. });
  38. };
  39. export const getLabelKeys = async (datasource: any, selectedMetricType: any) => {
  40. const refId = 'handleLabelKeysQuery';
  41. const response = await datasource.getLabels(selectedMetricType, refId);
  42. const labelKeys = response.meta
  43. ? [
  44. ...Object.keys(response.meta.resourceLabels).map(l => `resource.label.${l}`),
  45. ...Object.keys(response.meta.metricLabels).map(l => `metric.label.${l}`),
  46. ]
  47. : [];
  48. return labelKeys;
  49. };
  50. export const getAlignmentPickerData = (
  51. { valueType, metricKind, perSeriesAligner }: Partial<StackdriverQuery>,
  52. templateSrv: TemplateSrv
  53. ) => {
  54. const options = getAlignmentOptionsByMetric(valueType, metricKind).map(option => ({
  55. ...option,
  56. label: option.text,
  57. }));
  58. const alignOptions = [
  59. {
  60. label: 'Alignment options',
  61. expanded: true,
  62. options,
  63. },
  64. ];
  65. if (!options.some(o => o.value === templateSrv.replace(perSeriesAligner))) {
  66. perSeriesAligner = options.length > 0 ? options[0].value : '';
  67. }
  68. return { alignOptions, perSeriesAligner };
  69. };