eventCustomPerformanceMetrics.tsx 5.7 KB

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