functions.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import uniqBy from 'lodash/uniqBy';
  2. import { alignOptions, aggOptions } from './constants';
  3. export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy(metricDescriptors, 'service');
  4. export const getMetricTypesByService = (metricDescriptors, service) =>
  5. metricDescriptors.filter(m => m.service === service);
  6. export const getMetricTypes = (metricDescriptors, metricType, interpolatedMetricType, selectedService) => {
  7. const metricTypes = getMetricTypesByService(metricDescriptors, selectedService).map(m => ({
  8. value: m.type,
  9. name: m.displayName,
  10. }));
  11. const metricTypeExistInArray = metricTypes.some(m => m.value === interpolatedMetricType);
  12. const selectedMetricType = metricTypeExistInArray ? metricType : metricTypes[0].value;
  13. return {
  14. metricTypes,
  15. selectedMetricType,
  16. };
  17. };
  18. export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => {
  19. return !metricValueType
  20. ? []
  21. : alignOptions.filter(i => {
  22. return i.valueTypes.indexOf(metricValueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
  23. });
  24. };
  25. export const getAggregationOptionsByMetric = (valueType, metricKind) => {
  26. return !metricKind
  27. ? []
  28. : aggOptions.filter(i => {
  29. return i.valueTypes.indexOf(valueType) !== -1 && i.metricKinds.indexOf(metricKind) !== -1;
  30. });
  31. };
  32. export const getLabelKeys = async (datasource, selectedMetricType) => {
  33. const refId = 'handleLabelKeysQuery';
  34. const response = await datasource.getLabels(selectedMetricType, refId);
  35. const labelKeys = response.meta
  36. ? [
  37. ...Object.keys(response.meta.resourceLabels).map(l => `resource.label.${l}`),
  38. ...Object.keys(response.meta.metricLabels).map(l => `metric.label.${l}`),
  39. ]
  40. : [];
  41. return labelKeys;
  42. };