eventCustomPerformanceMetrics.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import styled from '@emotion/styled';
  2. import {Location} from 'history';
  3. import {SectionHeading} from 'sentry/components/charts/styles';
  4. import {Panel} from 'sentry/components/panels';
  5. import {t} from 'sentry/locale';
  6. import space from 'sentry/styles/space';
  7. import {Organization} from 'sentry/types';
  8. import {Event} from 'sentry/types/event';
  9. import {
  10. DURATION_UNITS,
  11. FIELD_FORMATTERS,
  12. PERCENTAGE_UNITS,
  13. SIZE_UNITS,
  14. } from 'sentry/utils/discover/fieldRenderers';
  15. import {isCustomMeasurement} from 'sentry/views/dashboardsV2/utils';
  16. type Props = {
  17. event: Event;
  18. location: Location;
  19. organization: Organization;
  20. };
  21. export default function EventCustomPerformanceMetrics({
  22. event,
  23. location,
  24. organization,
  25. }: Props) {
  26. const measurementNames = Object.keys(event.measurements ?? {})
  27. .filter(name => isCustomMeasurement(`measurements.${name}`))
  28. .sort();
  29. if (measurementNames.length === 0) {
  30. return null;
  31. }
  32. return (
  33. <Container>
  34. <SectionHeading>{t('Custom Performance Metrics')}</SectionHeading>
  35. <Measurements>
  36. {measurementNames.map(name => {
  37. return (
  38. <EventCustomPerformanceMetric
  39. key={name}
  40. event={event}
  41. name={name}
  42. location={location}
  43. organization={organization}
  44. />
  45. );
  46. })}
  47. </Measurements>
  48. </Container>
  49. );
  50. }
  51. type EventCustomPerformanceMetricProps = Props & {
  52. name: string;
  53. };
  54. function getFieldTypeFromUnit(unit) {
  55. if (unit) {
  56. if (DURATION_UNITS[unit]) {
  57. return 'duration';
  58. }
  59. if (SIZE_UNITS[unit]) {
  60. return 'size';
  61. }
  62. if (PERCENTAGE_UNITS.includes(unit)) {
  63. return 'percentage';
  64. }
  65. if (unit === 'none') {
  66. return 'integer';
  67. }
  68. }
  69. return 'number';
  70. }
  71. function EventCustomPerformanceMetric({
  72. event,
  73. name,
  74. location,
  75. organization,
  76. }: EventCustomPerformanceMetricProps) {
  77. const {value, unit} = event.measurements?.[name] ?? {};
  78. if (value === null) {
  79. return null;
  80. }
  81. const fieldType = getFieldTypeFromUnit(unit);
  82. const rendered = fieldType
  83. ? FIELD_FORMATTERS[fieldType].renderFunc(
  84. name,
  85. {[name]: value},
  86. {location, organization, unit}
  87. )
  88. : value;
  89. return (
  90. <StyledPanel>
  91. <div>{name}</div>
  92. <ValueRow>
  93. <Value>{rendered}</Value>
  94. </ValueRow>
  95. </StyledPanel>
  96. );
  97. }
  98. const Measurements = styled('div')`
  99. display: grid;
  100. grid-column-gap: ${space(1)};
  101. `;
  102. const Container = styled('div')`
  103. font-size: ${p => p.theme.fontSizeMedium};
  104. margin-bottom: ${space(4)};
  105. `;
  106. const StyledPanel = styled(Panel)`
  107. padding: ${space(1)} ${space(1.5)};
  108. margin-bottom: ${space(1)};
  109. `;
  110. const ValueRow = styled('div')`
  111. display: flex;
  112. align-items: center;
  113. `;
  114. const Value = styled('span')`
  115. font-size: ${p => p.theme.fontSizeExtraLarge};
  116. `;