eventCustomPerformanceMetrics.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import styled from '@emotion/styled';
  2. import {Location} from 'history';
  3. import {SectionHeading} from 'sentry/components/charts/styles';
  4. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  5. import Panel from 'sentry/components/panels/panel';
  6. import {IconEllipsis} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import {Organization} from 'sentry/types';
  10. import {Event} from 'sentry/types/event';
  11. import EventView from 'sentry/utils/discover/eventView';
  12. import {
  13. DURATION_UNITS,
  14. FIELD_FORMATTERS,
  15. PERCENTAGE_UNITS,
  16. SIZE_UNITS,
  17. } from 'sentry/utils/discover/fieldRenderers';
  18. import {isCustomMeasurement} from 'sentry/views/dashboards/utils';
  19. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  20. export enum EventDetailPageSource {
  21. PERFORMANCE = 'performance',
  22. DISCOVER = 'discover',
  23. }
  24. type Props = {
  25. event: Event;
  26. location: Location;
  27. organization: Organization;
  28. isHomepage?: boolean;
  29. source?: EventDetailPageSource;
  30. };
  31. function isNotMarkMeasurement(field: string) {
  32. return !field.startsWith('mark.');
  33. }
  34. export default function EventCustomPerformanceMetrics({
  35. event,
  36. location,
  37. organization,
  38. source,
  39. isHomepage,
  40. }: Props) {
  41. const measurementNames = Object.keys(event.measurements ?? {})
  42. .filter(name => isCustomMeasurement(`measurements.${name}`))
  43. .filter(isNotMarkMeasurement)
  44. .sort();
  45. if (measurementNames.length === 0) {
  46. return null;
  47. }
  48. return (
  49. <Container>
  50. <SectionHeading>{t('Custom Performance Metrics')}</SectionHeading>
  51. <Measurements>
  52. {measurementNames.map(name => {
  53. return (
  54. <EventCustomPerformanceMetric
  55. key={name}
  56. event={event}
  57. name={name}
  58. location={location}
  59. organization={organization}
  60. source={source}
  61. isHomepage={isHomepage}
  62. />
  63. );
  64. })}
  65. </Measurements>
  66. </Container>
  67. );
  68. }
  69. type EventCustomPerformanceMetricProps = Props & {
  70. name: string;
  71. };
  72. export function getFieldTypeFromUnit(unit) {
  73. if (unit) {
  74. if (DURATION_UNITS[unit]) {
  75. return 'duration';
  76. }
  77. if (SIZE_UNITS[unit]) {
  78. return 'size';
  79. }
  80. if (PERCENTAGE_UNITS.includes(unit)) {
  81. return 'percentage';
  82. }
  83. if (unit === 'none') {
  84. return 'integer';
  85. }
  86. return 'string';
  87. }
  88. return 'number';
  89. }
  90. function EventCustomPerformanceMetric({
  91. event,
  92. name,
  93. location,
  94. organization,
  95. source,
  96. isHomepage,
  97. }: EventCustomPerformanceMetricProps) {
  98. const {value, unit} = event.measurements?.[name] ?? {};
  99. if (value === null) {
  100. return null;
  101. }
  102. const fieldType = getFieldTypeFromUnit(unit);
  103. const renderValue = fieldType === 'string' ? `${value} ${unit}` : value;
  104. const rendered = fieldType
  105. ? FIELD_FORMATTERS[fieldType].renderFunc(
  106. name,
  107. {[name]: renderValue},
  108. {location, organization, unit}
  109. )
  110. : renderValue;
  111. function generateLinkWithQuery(query: string) {
  112. const eventView = EventView.fromLocation(location);
  113. eventView.query = query;
  114. switch (source) {
  115. case EventDetailPageSource.PERFORMANCE:
  116. return transactionSummaryRouteWithQuery({
  117. orgSlug: organization.slug,
  118. transaction: event.title,
  119. projectID: event.projectID,
  120. query: {query},
  121. });
  122. case EventDetailPageSource.DISCOVER:
  123. default:
  124. return eventView.getResultsViewUrlTarget(organization.slug, isHomepage);
  125. }
  126. }
  127. // Some custom perf metrics have units.
  128. // These custom perf metrics need to be adjusted to the correct value.
  129. let customMetricValue = value;
  130. if (typeof value === 'number' && unit && customMetricValue) {
  131. if (Object.keys(SIZE_UNITS).includes(unit)) {
  132. customMetricValue *= SIZE_UNITS[unit];
  133. } else if (Object.keys(DURATION_UNITS).includes(unit)) {
  134. customMetricValue *= DURATION_UNITS[unit];
  135. }
  136. }
  137. return (
  138. <StyledPanel>
  139. <div>
  140. <div>{name}</div>
  141. <ValueRow>
  142. <Value>{rendered}</Value>
  143. </ValueRow>
  144. </div>
  145. <StyledDropdownMenuControl
  146. items={[
  147. {
  148. key: 'includeEvents',
  149. label: t('Show events with this value'),
  150. to: generateLinkWithQuery(`measurements.${name}:${customMetricValue}`),
  151. },
  152. {
  153. key: 'excludeEvents',
  154. label: t('Hide events with this value'),
  155. to: generateLinkWithQuery(`!measurements.${name}:${customMetricValue}`),
  156. },
  157. {
  158. key: 'includeGreaterThanEvents',
  159. label: t('Show events with values greater than'),
  160. to: generateLinkWithQuery(`measurements.${name}:>${customMetricValue}`),
  161. },
  162. {
  163. key: 'includeLessThanEvents',
  164. label: t('Show events with values less than'),
  165. to: generateLinkWithQuery(`measurements.${name}:<${customMetricValue}`),
  166. },
  167. ]}
  168. triggerProps={{
  169. 'aria-label': t('Widget actions'),
  170. size: 'xs',
  171. borderless: true,
  172. showChevron: false,
  173. icon: <IconEllipsis direction="down" size="sm" />,
  174. }}
  175. position="bottom-end"
  176. />
  177. </StyledPanel>
  178. );
  179. }
  180. const Measurements = styled('div')`
  181. display: grid;
  182. grid-column-gap: ${space(1)};
  183. `;
  184. const Container = styled('div')`
  185. font-size: ${p => p.theme.fontSizeMedium};
  186. margin-bottom: ${space(4)};
  187. `;
  188. const StyledPanel = styled(Panel)`
  189. padding: ${space(1)} ${space(1.5)};
  190. margin-bottom: ${space(1)};
  191. display: flex;
  192. `;
  193. const ValueRow = styled('div')`
  194. display: flex;
  195. align-items: center;
  196. `;
  197. const Value = styled('span')`
  198. font-size: ${p => p.theme.fontSizeExtraLarge};
  199. `;
  200. const StyledDropdownMenuControl = styled(DropdownMenu)`
  201. margin-left: auto;
  202. `;