thresholdsStep.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. onThresholdChange: (maxKey: ThresholdMaxKeys, value: string) => void;
  18. onUnitChange: (unit: string) => void;
  19. thresholdsConfig: ThresholdsConfig | null;
  20. dataType?: string;
  21. dataUnit?: string;
  22. errors?: ThresholdErrors;
  23. };
  24. type ThresholdRowProp = {
  25. color: string;
  26. maxInputProps: NumberFieldProps;
  27. minInputProps: NumberFieldProps;
  28. unitOptions: Array<{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. export function Thresholds({
  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. <ThresholdsContainer>
  157. {thresholdRowProps.map((props, index) => (
  158. <ThresholdRow
  159. {...props}
  160. onThresholdChange={onThresholdChange}
  161. onUnitChange={onUnitChange}
  162. key={index}
  163. />
  164. ))}
  165. </ThresholdsContainer>
  166. );
  167. }
  168. function ThresholdsStep({
  169. thresholdsConfig,
  170. onThresholdChange,
  171. onUnitChange,
  172. errors,
  173. dataType = '',
  174. dataUnit = '',
  175. }: ThresholdsStepProps) {
  176. return (
  177. <BuildStep
  178. title={t('Set thresholds')}
  179. description={tct(
  180. '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].',
  181. {
  182. thresholdValues: <HighlightedText>(green: 100, yellow: 200)</HighlightedText>,
  183. greenRange: <HighlightedText>[0 - 100]</HighlightedText>,
  184. yellowRange: <HighlightedText>(100 - 200]</HighlightedText>,
  185. redValue: <HighlightedText>200</HighlightedText>,
  186. }
  187. )}
  188. >
  189. <Thresholds
  190. thresholdsConfig={thresholdsConfig}
  191. onThresholdChange={onThresholdChange}
  192. onUnitChange={onUnitChange}
  193. errors={errors}
  194. dataType={dataType}
  195. dataUnit={dataUnit}
  196. />
  197. </BuildStep>
  198. );
  199. }
  200. const ThresholdRowWrapper = styled('div')`
  201. display: flex;
  202. align-items: center;
  203. gap: ${space(2)};
  204. `;
  205. const ThresholdsContainer = styled('div')`
  206. display: flex;
  207. flex-direction: column;
  208. gap: ${space(2)};
  209. margin-top: ${space(1)};
  210. ${FieldWrapper} {
  211. padding: 0;
  212. border-bottom: none;
  213. }
  214. `;
  215. const StyledNumberField = styled(NumberField)`
  216. width: 200px;
  217. `;
  218. const StyledSelectField = styled(SelectField)`
  219. min-width: 150px;
  220. `;
  221. export const HighlightedText = styled('span')`
  222. font-family: ${p => p.theme.text.familyMono};
  223. color: ${p => p.theme.pink300};
  224. `;
  225. export default ThresholdsStep;