spanFrequencyBox.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type {Theme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import type {
  4. AggregateSpanType,
  5. GapSpanType,
  6. } from 'sentry/components/events/interfaces/spans/types';
  7. import {Tooltip} from 'sentry/components/tooltip';
  8. import {t, tct} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {formatPercentage} from 'sentry/utils/formatters';
  11. export const FREQUENCY_BOX_WIDTH = 40;
  12. type Props = {
  13. span: AggregateSpanType | GapSpanType;
  14. };
  15. // Colors are copied from tagsHeatMap.tsx, as they are not available on the theme
  16. const purples = ['#D1BAFC', '#9282F3', '#6056BA', '#313087', '#021156'];
  17. export function SpanFrequencyBox({span}: Props) {
  18. if (span.type === 'gap') {
  19. return (
  20. <StyledBox>
  21. <Tooltip isHoverable title={t('This frequency of this span is unknown')}>
  22. {'—'}
  23. </Tooltip>
  24. </StyledBox>
  25. );
  26. }
  27. const {frequency, count, total} = span;
  28. return (
  29. <StyledBox frequency={frequency ?? 0}>
  30. <Tooltip
  31. isHoverable
  32. title={tct('This span occurred in [x] out of [total] events aggregated', {
  33. x: count,
  34. total,
  35. })}
  36. >
  37. {formatPercentage(frequency ?? 0, 0)}
  38. </Tooltip>
  39. </StyledBox>
  40. );
  41. }
  42. function getBoxColors(theme: Theme, frequency?: number) {
  43. if (!frequency || frequency >= 0.9) {
  44. return `
  45. background: ${purples[3]};
  46. color: ${theme.white};
  47. `;
  48. }
  49. if (frequency >= 0.7) {
  50. return `
  51. background: ${purples[2]};
  52. color: ${theme.white};
  53. `;
  54. }
  55. if (frequency >= 0.5) {
  56. return `
  57. background: ${purples[1]};
  58. color: ${theme.black};
  59. `;
  60. }
  61. if (frequency >= 0.3) {
  62. return `
  63. background: ${purples[0]};
  64. color: ${theme.black};
  65. `;
  66. }
  67. return `
  68. background: ${theme.white};
  69. color: ${theme.black};
  70. `;
  71. }
  72. const StyledBox = styled('div')<{frequency?: number}>`
  73. display: flex;
  74. justify-content: right;
  75. align-items: center;
  76. height: 100%;
  77. width: ${FREQUENCY_BOX_WIDTH}px;
  78. border-left: 1px solid ${p => p.theme.gray200};
  79. border-right: 1px solid ${p => p.theme.gray200};
  80. padding-right: ${space(1)};
  81. font-size: ${p => p.theme.fontSizeExtraSmall};
  82. ${p => getBoxColors(p.theme, p.frequency)}
  83. z-index: 9;
  84. `;