functions.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { chunk, initial, startCase, uniqBy } from 'lodash';
  2. import { getTemplateSrv, TemplateSrv } from '@grafana/runtime';
  3. import { AGGREGATIONS, ALIGNMENTS, SYSTEM_LABELS } from './constants';
  4. import CloudMonitoringDatasource from './datasource';
  5. import { AlignmentTypes, MetricDescriptor, MetricKind, PreprocessorType, ValueTypes } from './types';
  6. const templateSrv: TemplateSrv = getTemplateSrv();
  7. export const extractServicesFromMetricDescriptors = (metricDescriptors: MetricDescriptor[]) =>
  8. uniqBy(metricDescriptors, 'service');
  9. export const getMetricTypesByService = (metricDescriptors: MetricDescriptor[], service: string) =>
  10. metricDescriptors.filter((m: MetricDescriptor) => m.service === service);
  11. export const getMetricTypes = (
  12. metricDescriptors: MetricDescriptor[],
  13. metricType: string,
  14. interpolatedMetricType: string,
  15. selectedService: string
  16. ) => {
  17. const metricTypes = getMetricTypesByService(metricDescriptors, selectedService).map((m) => ({
  18. value: m.type,
  19. name: m.displayName,
  20. }));
  21. const metricTypeExistInArray = metricTypes.some(
  22. (m: { value: string; name: string }) => m.value === interpolatedMetricType
  23. );
  24. const metricTypeByService = metricTypes.length ? metricTypes[0].value : '';
  25. const selectedMetricType = metricTypeExistInArray ? metricType : metricTypeByService;
  26. return {
  27. metricTypes,
  28. selectedMetricType,
  29. };
  30. };
  31. export const getAlignmentOptionsByMetric = (
  32. metricValueType: string,
  33. metricKind: string,
  34. preprocessor?: PreprocessorType
  35. ) => {
  36. if (preprocessor && preprocessor === PreprocessorType.Rate) {
  37. metricKind = MetricKind.GAUGE;
  38. }
  39. return !metricValueType
  40. ? []
  41. : ALIGNMENTS.filter((i) => {
  42. return (
  43. i.valueTypes.indexOf(metricValueType as ValueTypes) !== -1 &&
  44. i.metricKinds.indexOf(metricKind as MetricKind) !== -1
  45. );
  46. });
  47. };
  48. export const getAggregationOptionsByMetric = (valueType: ValueTypes, metricKind: MetricKind) => {
  49. return !metricKind
  50. ? []
  51. : AGGREGATIONS.filter((i) => {
  52. return i.valueTypes.indexOf(valueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
  53. });
  54. };
  55. export const getLabelKeys = async (
  56. datasource: CloudMonitoringDatasource,
  57. selectedMetricType: string,
  58. projectName: string
  59. ) => {
  60. const refId = 'handleLabelKeysQuery';
  61. const labels = await datasource.getLabels(selectedMetricType, refId, projectName);
  62. return [...Object.keys(labels), ...SYSTEM_LABELS];
  63. };
  64. export const getAlignmentPickerData = (
  65. valueType: string | undefined = ValueTypes.DOUBLE,
  66. metricKind: string | undefined = MetricKind.GAUGE,
  67. perSeriesAligner: string | undefined = AlignmentTypes.ALIGN_MEAN,
  68. preprocessor?: PreprocessorType
  69. ) => {
  70. const alignOptions = getAlignmentOptionsByMetric(valueType!, metricKind!, preprocessor!).map((option) => ({
  71. ...option,
  72. label: option.text,
  73. }));
  74. if (!alignOptions.some((o: { value: string }) => o.value === templateSrv.replace(perSeriesAligner))) {
  75. perSeriesAligner = alignOptions.length > 0 ? alignOptions[0].value : AlignmentTypes.ALIGN_MEAN;
  76. }
  77. return { alignOptions, perSeriesAligner };
  78. };
  79. export const labelsToGroupedOptions = (groupBys: string[]) => {
  80. const groups = groupBys.reduce((acc: any, curr: string) => {
  81. const arr = curr.split('.').map(startCase);
  82. const group = (arr.length === 2 ? arr : initial(arr)).join(' ');
  83. const option = {
  84. value: curr,
  85. label: curr,
  86. };
  87. if (acc[group]) {
  88. acc[group] = [...acc[group], option];
  89. } else {
  90. acc[group] = [option];
  91. }
  92. return acc;
  93. }, {});
  94. return Object.entries(groups).map(([label, options]) => ({ label, options, expanded: true }), []);
  95. };
  96. export const stringArrayToFilters = (filterArray: string[]) =>
  97. chunk(filterArray, 4).map(([key, operator, value, condition = 'AND']) => ({
  98. key,
  99. operator,
  100. value,
  101. condition,
  102. }));