thresholdsStep.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import styled from '@emotion/styled';
  2. import CircleIndicator from 'sentry/components/circleIndicator';
  3. import FieldWrapper from 'sentry/components/forms/fieldGroup/fieldWrapper';
  4. import type {NumberFieldProps} from 'sentry/components/forms/fields/numberField';
  5. import NumberField from 'sentry/components/forms/fields/numberField';
  6. import type {SelectFieldProps} from 'sentry/components/forms/fields/selectField';
  7. import SelectField from 'sentry/components/forms/fields/selectField';
  8. import {t, tct} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import theme from 'sentry/utils/theme';
  11. import {getThresholdUnitSelectOptions} from 'sentry/views/dashboards/utils';
  12. import {BuildStep} from '../buildStep';
  13. type ThresholdErrors = {
  14. [K in ThresholdMaxKeys]?: string;
  15. };
  16. type ThresholdsStepProps = {
  17. errors: ThresholdErrors;
  18. onThresholdChange: (maxKey: ThresholdMaxKeys, value: string) => void;
  19. onUnitChange: (unit: string) => void;
  20. thresholdsConfig: ThresholdsConfig | null;
  21. dataType?: string;
  22. dataUnit?: string;
  23. };
  24. type ThresholdRowProp = {
  25. color: string;
  26. maxInputProps: NumberFieldProps;
  27. minInputProps: NumberFieldProps;
  28. unitOptions: {label: string; value: string}[];
  29. unitSelectProps: SelectFieldProps<any>;
  30. maxKey?: ThresholdMaxKeys;
  31. onThresholdChange?: (maxKey: ThresholdMaxKeys, value: string) => void;
  32. onUnitChange?: (maxKey: ThresholdMaxKeys, value: string) => void;
  33. };
  34. export enum ThresholdMaxKeys {
  35. MAX_1 = 'max1',
  36. MAX_2 = 'max2',
  37. }
  38. type ThresholdMaxValues = {
  39. [K in ThresholdMaxKeys]?: number;
  40. };
  41. export type ThresholdsConfig = {
  42. max_values: ThresholdMaxValues;
  43. unit: string | null;
  44. };
  45. const WIDGET_INDICATOR_SIZE = 15;
  46. function ThresholdRow({
  47. color,
  48. minInputProps,
  49. maxInputProps,
  50. onThresholdChange,
  51. onUnitChange,
  52. maxKey,
  53. unitOptions,
  54. unitSelectProps,
  55. }: ThresholdRowProp) {
  56. const handleChange = (val: string) => {
  57. if (onThresholdChange && maxKey) {
  58. onThresholdChange(maxKey, val);
  59. }
  60. };
  61. return (
  62. <ThresholdRowWrapper>
  63. <CircleIndicator color={color} size={WIDGET_INDICATOR_SIZE} />
  64. <StyledNumberField {...minInputProps} inline={false} disabled />
  65. {t('to')}
  66. <StyledNumberField onChange={handleChange} {...maxInputProps} inline={false} />
  67. {unitOptions.length > 0 && (
  68. <StyledSelectField
  69. {...unitSelectProps}
  70. onChange={onUnitChange}
  71. options={unitOptions}
  72. inline={false}
  73. />
  74. )}
  75. </ThresholdRowWrapper>
  76. );
  77. }
  78. function ThresholdsStep({
  79. thresholdsConfig,
  80. onThresholdChange,
  81. onUnitChange,
  82. errors,
  83. dataType = '',
  84. dataUnit = '',
  85. }: ThresholdsStepProps) {
  86. const maxOneValue = thresholdsConfig?.max_values[ThresholdMaxKeys.MAX_1] ?? '';
  87. const maxTwoValue = thresholdsConfig?.max_values[ThresholdMaxKeys.MAX_2] ?? '';
  88. const unit = thresholdsConfig?.unit ?? dataUnit;
  89. const unitOptions = ['duration', 'rate'].includes(dataType)
  90. ? getThresholdUnitSelectOptions(dataType)
  91. : [];
  92. const thresholdRowProps: ThresholdRowProp[] = [
  93. {
  94. maxKey: ThresholdMaxKeys.MAX_1,
  95. minInputProps: {
  96. name: 'firstMinimum',
  97. value: 0,
  98. 'aria-label': 'First Minimum',
  99. },
  100. maxInputProps: {
  101. name: 'firstMaximum',
  102. value: maxOneValue,
  103. 'aria-label': 'First Maximum',
  104. error: errors?.max1,
  105. },
  106. color: theme.green300,
  107. unitOptions,
  108. unitSelectProps: {
  109. name: 'First unit select',
  110. value: unit,
  111. },
  112. },
  113. {
  114. maxKey: ThresholdMaxKeys.MAX_2,
  115. minInputProps: {
  116. name: 'secondMinimum',
  117. value: maxOneValue,
  118. 'aria-label': 'Second Minimum',
  119. },
  120. maxInputProps: {
  121. name: 'secondMaximum',
  122. value: maxTwoValue,
  123. 'aria-label': 'Second Maximum',
  124. error: errors?.max2,
  125. },
  126. color: theme.yellow300,
  127. unitOptions,
  128. unitSelectProps: {
  129. name: 'Second unit select',
  130. value: unit,
  131. disabled: true,
  132. },
  133. },
  134. {
  135. minInputProps: {
  136. name: 'thirdMinimum',
  137. value: maxTwoValue,
  138. 'aria-label': 'Third Minimum',
  139. },
  140. maxInputProps: {
  141. name: 'thirdMaximum',
  142. disabled: true,
  143. placeholder: t('No max'),
  144. 'aria-label': 'Third Maximum',
  145. },
  146. color: theme.red300,
  147. unitOptions,
  148. unitSelectProps: {
  149. name: 'Third unit select',
  150. value: unit,
  151. disabled: true,
  152. },
  153. },
  154. ];
  155. return (
  156. <BuildStep
  157. title={t('Set thresholds')}
  158. description={tct(
  159. 'Set thresholds to identify problematic widgets. For example: setting the max values, [thresholdValues] will display a green indicator for results in the range [greenRange], a yellow indicator for results in the range [yellowRange] and a red indicator for results above [redValue].',
  160. {
  161. thresholdValues: <HighlightedText>(green: 100, yellow: 200)</HighlightedText>,
  162. greenRange: <HighlightedText>[0 - 100]</HighlightedText>,
  163. yellowRange: <HighlightedText>(100 - 200]</HighlightedText>,
  164. redValue: <HighlightedText>200</HighlightedText>,
  165. }
  166. )}
  167. >
  168. <ThresholdsContainer>
  169. {thresholdRowProps.map((props, index) => (
  170. <ThresholdRow
  171. {...props}
  172. onThresholdChange={onThresholdChange}
  173. onUnitChange={onUnitChange}
  174. key={index}
  175. />
  176. ))}
  177. </ThresholdsContainer>
  178. </BuildStep>
  179. );
  180. }
  181. const ThresholdRowWrapper = styled('div')`
  182. display: flex;
  183. align-items: center;
  184. gap: ${space(2)};
  185. `;
  186. const ThresholdsContainer = styled('div')`
  187. display: flex;
  188. flex-direction: column;
  189. gap: ${space(2)};
  190. margin-top: ${space(1)};
  191. ${FieldWrapper} {
  192. padding: 0;
  193. border-bottom: none;
  194. }
  195. `;
  196. const StyledNumberField = styled(NumberField)`
  197. width: 200px;
  198. `;
  199. const StyledSelectField = styled(SelectField)`
  200. min-width: 150px;
  201. `;
  202. const HighlightedText = styled('span')`
  203. font-family: ${p => p.theme.text.familyMono};
  204. color: ${p => p.theme.pink300};
  205. `;
  206. export default ThresholdsStep;