metricField.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import Button from 'sentry/components/button';
  5. import FormField from 'sentry/components/forms/formField';
  6. import {Organization} from 'sentry/types';
  7. import {
  8. Aggregation,
  9. AGGREGATIONS,
  10. explodeFieldString,
  11. generateFieldAsString,
  12. } from 'sentry/utils/discover/fields';
  13. import {
  14. AlertType,
  15. hideParameterSelectorSet,
  16. hidePrimarySelectorSet,
  17. } from 'sentry/views/alerts/wizard/options';
  18. import {QueryField} from 'sentry/views/eventsV2/table/queryField';
  19. import {FieldValueKind} from 'sentry/views/eventsV2/table/types';
  20. import {generateFieldOptions} from 'sentry/views/eventsV2/utils';
  21. import {
  22. errorFieldConfig,
  23. getWizardAlertFieldConfig,
  24. OptionConfig,
  25. transactionFieldConfig,
  26. } from './constants';
  27. import {Dataset} from './types';
  28. type Props = Omit<FormField['props'], 'children'> & {
  29. organization: Organization;
  30. alertType?: AlertType;
  31. /**
  32. * Optionally set a width for each column of selector
  33. */
  34. columnWidth?: number;
  35. inFieldLabels?: boolean;
  36. };
  37. export const getFieldOptionConfig = ({
  38. dataset,
  39. alertType,
  40. }: {
  41. dataset: Dataset;
  42. alertType?: AlertType;
  43. }) => {
  44. let config: OptionConfig;
  45. let hidePrimarySelector = false;
  46. let hideParameterSelector = false;
  47. if (alertType) {
  48. config = getWizardAlertFieldConfig(alertType, dataset);
  49. hidePrimarySelector = hidePrimarySelectorSet.has(alertType);
  50. hideParameterSelector = hideParameterSelectorSet.has(alertType);
  51. } else {
  52. config = dataset === Dataset.ERRORS ? errorFieldConfig : transactionFieldConfig;
  53. }
  54. const aggregations = Object.fromEntries<Aggregation>(
  55. config.aggregations.map(key => {
  56. // TODO(scttcper): Temporary hack for default value while we handle the translation of user
  57. if (key === 'count_unique') {
  58. const agg = AGGREGATIONS[key] as Aggregation;
  59. agg.getFieldOverrides = () => {
  60. return {defaultValue: 'tags[sentry:user]'};
  61. };
  62. return [key, agg];
  63. }
  64. return [key, AGGREGATIONS[key]];
  65. })
  66. );
  67. const fieldKeys = config.fields.map(key => {
  68. // XXX(epurkhiser): Temporary hack while we handle the translation of user ->
  69. // tags[sentry:user].
  70. if (key === 'user') {
  71. return 'tags[sentry:user]';
  72. }
  73. return key;
  74. });
  75. const {measurementKeys} = config;
  76. return {
  77. fieldOptionsConfig: {aggregations, fieldKeys, measurementKeys},
  78. hidePrimarySelector,
  79. hideParameterSelector,
  80. };
  81. };
  82. const MetricField = ({
  83. organization,
  84. columnWidth,
  85. inFieldLabels,
  86. alertType,
  87. ...props
  88. }: Props) => (
  89. <FormField {...props}>
  90. {({onChange, value, model, disabled}) => {
  91. const dataset = model.getValue('dataset');
  92. const {fieldOptionsConfig, hidePrimarySelector, hideParameterSelector} =
  93. getFieldOptionConfig({
  94. dataset: dataset as Dataset,
  95. alertType,
  96. });
  97. const fieldOptions = generateFieldOptions({organization, ...fieldOptionsConfig});
  98. const fieldValue = explodeFieldString(value ?? '');
  99. const fieldKey =
  100. fieldValue?.kind === FieldValueKind.FUNCTION
  101. ? `function:${fieldValue.function[0]}`
  102. : '';
  103. const selectedField = fieldOptions[fieldKey]?.value;
  104. const numParameters: number =
  105. selectedField?.kind === FieldValueKind.FUNCTION
  106. ? selectedField.meta.parameters.length
  107. : 0;
  108. const parameterColumns =
  109. numParameters - (hideParameterSelector ? 1 : 0) - (hidePrimarySelector ? 1 : 0);
  110. return (
  111. <Fragment>
  112. <StyledQueryField
  113. filterPrimaryOptions={option => option.value.kind === FieldValueKind.FUNCTION}
  114. fieldOptions={fieldOptions}
  115. fieldValue={fieldValue}
  116. onChange={v => onChange(generateFieldAsString(v), {})}
  117. columnWidth={columnWidth}
  118. gridColumns={parameterColumns + 1}
  119. inFieldLabels={inFieldLabels}
  120. shouldRenderTag={false}
  121. disabled={disabled}
  122. hideParameterSelector={hideParameterSelector}
  123. hidePrimarySelector={hidePrimarySelector}
  124. />
  125. </Fragment>
  126. );
  127. }}
  128. </FormField>
  129. );
  130. const StyledQueryField = styled(QueryField)<{gridColumns: number; columnWidth?: number}>`
  131. ${p =>
  132. p.columnWidth &&
  133. css`
  134. width: ${p.gridColumns * p.columnWidth}px;
  135. `}
  136. `;
  137. const PresetButton = styled(Button)<{disabled: boolean}>`
  138. ${p =>
  139. p.disabled &&
  140. css`
  141. color: ${p.theme.textColor};
  142. &:hover,
  143. &:focus {
  144. color: ${p.theme.textColor};
  145. }
  146. `}
  147. `;
  148. PresetButton.defaultProps = {
  149. priority: 'link',
  150. borderless: true,
  151. };
  152. export default MetricField;