functions.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. };
  43. export const getAlignmentPickerData = ({ valueType, metricKind, perSeriesAligner }, templateSrv) => {
  44. const alignOptions = getAlignmentOptionsByMetric(valueType, metricKind).map(option => ({
  45. ...option,
  46. label: option.text,
  47. }));
  48. if (!alignOptions.some(o => o.value === templateSrv.replace(perSeriesAligner))) {
  49. perSeriesAligner = alignOptions.length > 0 ? alignOptions[0].value : '';
  50. }
  51. return { alignOptions, perSeriesAligner };
  52. };