eventCustomPerformanceMetrics.tsx 3.0 KB

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