common.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import styled from '@emotion/styled';
  2. import {t} from 'sentry/locale';
  3. import {getDuration} from 'sentry/utils/formatters';
  4. import {TextAlignRight} from 'sentry/views/starfish/components/textAlign';
  5. type Props = {
  6. duration: number;
  7. p95: number;
  8. containerProps?: React.DetailedHTMLProps<
  9. React.HTMLAttributes<HTMLSpanElement>,
  10. HTMLSpanElement
  11. >;
  12. };
  13. export function DurationComparisonCell({duration, p95, containerProps}: Props) {
  14. const diff = duration - p95;
  15. if (isNearBaseline(duration, p95)) {
  16. return <TextAlignRight {...containerProps}>{t('Near baseline')}</TextAlignRight>;
  17. }
  18. const readableDiff = getDuration(diff / 1000, 2, true, true);
  19. const labelString = diff > 0 ? `+${readableDiff} above` : `${readableDiff} below`;
  20. return (
  21. <ComparisonLabel {...containerProps} value={diff}>
  22. {labelString}
  23. </ComparisonLabel>
  24. );
  25. }
  26. export const isNearBaseline = (duration: number, p95: number) => {
  27. const maxDiff = 0.03 * p95;
  28. const diff = Math.abs(duration - p95);
  29. return diff < maxDiff;
  30. };
  31. const ComparisonLabel = styled('span')<{value: number}>`
  32. color: ${p =>
  33. p.value === 0 ? p.theme.subText : p.value < 0 ? p.theme.green400 : p.theme.red400};
  34. text-align: right;
  35. `;