metricField.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, {FormFieldProps} 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/discover/table/queryField';
  19. import {FieldValueKind} from 'sentry/views/discover/table/types';
  20. import {generateFieldOptions} from 'sentry/views/discover/utils';
  21. import {
  22. errorFieldConfig,
  23. getWizardAlertFieldConfig,
  24. OptionConfig,
  25. transactionFieldConfig,
  26. } from './constants';
  27. import {Dataset} from './types';
  28. type Props = Omit<FormFieldProps, '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. function MetricField({
  83. organization,
  84. columnWidth,
  85. inFieldLabels,
  86. alertType,
  87. ...props
  88. }: Props) {
  89. return (
  90. <FormField {...props}>
  91. {({onChange, value, model, disabled}) => {
  92. const dataset = model.getValue('dataset');
  93. const {fieldOptionsConfig, hidePrimarySelector, hideParameterSelector} =
  94. getFieldOptionConfig({
  95. dataset: dataset as Dataset,
  96. alertType,
  97. });
  98. const fieldOptions = generateFieldOptions({organization, ...fieldOptionsConfig});
  99. const fieldValue = explodeFieldString(value ?? '');
  100. const fieldKey =
  101. fieldValue?.kind === FieldValueKind.FUNCTION
  102. ? `function:${fieldValue.function[0]}`
  103. : '';
  104. const selectedField = fieldOptions[fieldKey]?.value;
  105. const numParameters: number =
  106. selectedField?.kind === FieldValueKind.FUNCTION
  107. ? selectedField.meta.parameters.length
  108. : 0;
  109. const parameterColumns =
  110. numParameters - (hideParameterSelector ? 1 : 0) - (hidePrimarySelector ? 1 : 0);
  111. return (
  112. <Fragment>
  113. <StyledQueryField
  114. filterPrimaryOptions={option =>
  115. option.value.kind === FieldValueKind.FUNCTION
  116. }
  117. fieldOptions={fieldOptions}
  118. fieldValue={fieldValue}
  119. onChange={v => onChange(generateFieldAsString(v), {})}
  120. columnWidth={columnWidth}
  121. gridColumns={parameterColumns + 1}
  122. inFieldLabels={inFieldLabels}
  123. shouldRenderTag={false}
  124. disabled={disabled}
  125. hideParameterSelector={hideParameterSelector}
  126. hidePrimarySelector={hidePrimarySelector}
  127. />
  128. </Fragment>
  129. );
  130. }}
  131. </FormField>
  132. );
  133. }
  134. const StyledQueryField = styled(QueryField)<{gridColumns: number; columnWidth?: number}>`
  135. ${p =>
  136. p.columnWidth &&
  137. css`
  138. width: ${p.gridColumns * p.columnWidth}px;
  139. `}
  140. `;
  141. const PresetButton = styled(Button)<{disabled: boolean}>`
  142. ${p =>
  143. p.disabled &&
  144. css`
  145. color: ${p.theme.textColor};
  146. &:hover,
  147. &:focus {
  148. color: ${p.theme.textColor};
  149. }
  150. `}
  151. `;
  152. PresetButton.defaultProps = {
  153. priority: 'link',
  154. borderless: true,
  155. };
  156. export default MetricField;