eventCustomPerformanceMetrics.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 FeatureBadge from 'sentry/components/featureBadge';
  6. import {Panel} from 'sentry/components/panels';
  7. import {IconEllipsis} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import space from 'sentry/styles/space';
  10. import {Organization} from 'sentry/types';
  11. import {Event} from 'sentry/types/event';
  12. import EventView from 'sentry/utils/discover/eventView';
  13. import {
  14. DURATION_UNITS,
  15. FIELD_FORMATTERS,
  16. PERCENTAGE_UNITS,
  17. SIZE_UNITS,
  18. } from 'sentry/utils/discover/fieldRenderers';
  19. import {isCustomMeasurement} from 'sentry/views/dashboardsV2/utils';
  20. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  21. export enum EventDetailPageSource {
  22. PERFORMANCE = 'performance',
  23. DISCOVER = 'discover',
  24. }
  25. type Props = {
  26. event: Event;
  27. location: Location;
  28. organization: Organization;
  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. }: Props) {
  40. const measurementNames = Object.keys(event.measurements ?? {})
  41. .filter(name => isCustomMeasurement(`measurements.${name}`))
  42. .filter(isNotMarkMeasurement)
  43. .sort();
  44. if (measurementNames.length === 0) {
  45. return null;
  46. }
  47. return (
  48. <Container>
  49. <SectionHeading>{t('Custom Performance Metrics')}</SectionHeading>
  50. <FeatureBadge type="beta" />
  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. />
  62. );
  63. })}
  64. </Measurements>
  65. </Container>
  66. );
  67. }
  68. type EventCustomPerformanceMetricProps = Props & {
  69. name: string;
  70. };
  71. function getFieldTypeFromUnit(unit) {
  72. if (unit) {
  73. if (DURATION_UNITS[unit]) {
  74. return 'duration';
  75. }
  76. if (SIZE_UNITS[unit]) {
  77. return 'size';
  78. }
  79. if (PERCENTAGE_UNITS.includes(unit)) {
  80. return 'percentage';
  81. }
  82. if (unit === 'none') {
  83. return 'integer';
  84. }
  85. }
  86. return 'number';
  87. }
  88. function EventCustomPerformanceMetric({
  89. event,
  90. name,
  91. location,
  92. organization,
  93. source,
  94. }: EventCustomPerformanceMetricProps) {
  95. const {value, unit} = event.measurements?.[name] ?? {};
  96. if (value === null) {
  97. return null;
  98. }
  99. const fieldType = getFieldTypeFromUnit(unit);
  100. const rendered = fieldType
  101. ? FIELD_FORMATTERS[fieldType].renderFunc(
  102. name,
  103. {[name]: value},
  104. {location, organization, unit}
  105. )
  106. : value;
  107. function generateLinkWithQuery(query: string) {
  108. const eventView = EventView.fromSavedQuery({
  109. query,
  110. fields: [],
  111. id: undefined,
  112. name: '',
  113. projects: [],
  114. version: 1,
  115. });
  116. switch (source) {
  117. case EventDetailPageSource.PERFORMANCE:
  118. return transactionSummaryRouteWithQuery({
  119. orgSlug: organization.slug,
  120. transaction: event.title,
  121. projectID: event.projectID,
  122. query: {query},
  123. });
  124. case EventDetailPageSource.DISCOVER:
  125. default:
  126. return eventView.getResultsViewUrlTarget(organization.slug);
  127. }
  128. }
  129. return (
  130. <StyledPanel>
  131. <div>
  132. <div>{name}</div>
  133. <ValueRow>
  134. <Value>{rendered}</Value>
  135. </ValueRow>
  136. </div>
  137. <StyledDropdownMenuControl
  138. items={[
  139. {
  140. key: 'includeEvents',
  141. label: t('Show events with this value'),
  142. to: generateLinkWithQuery(`measurements.${name}:${value}`),
  143. },
  144. {
  145. key: 'excludeEvents',
  146. label: t('Hide events with this value'),
  147. to: generateLinkWithQuery(`!measurements.${name}:${value}`),
  148. },
  149. {
  150. key: 'includeGreaterThanEvents',
  151. label: t('Show events with values greater than'),
  152. to: generateLinkWithQuery(`measurements.${name}:>${value}`),
  153. },
  154. {
  155. key: 'includeLessThanEvents',
  156. label: t('Show events with values less than'),
  157. to: generateLinkWithQuery(`measurements.${name}:<${value}`),
  158. },
  159. ]}
  160. triggerProps={{
  161. 'aria-label': t('Widget actions'),
  162. size: 'xs',
  163. borderless: true,
  164. showChevron: false,
  165. icon: <IconEllipsis direction="down" size="sm" />,
  166. }}
  167. placement="bottom right"
  168. />
  169. </StyledPanel>
  170. );
  171. }
  172. const Measurements = styled('div')`
  173. display: grid;
  174. grid-column-gap: ${space(1)};
  175. `;
  176. const Container = styled('div')`
  177. font-size: ${p => p.theme.fontSizeMedium};
  178. margin-bottom: ${space(4)};
  179. `;
  180. const StyledPanel = styled(Panel)`
  181. padding: ${space(1)} ${space(1.5)};
  182. margin-bottom: ${space(1)};
  183. display: flex;
  184. `;
  185. const ValueRow = styled('div')`
  186. display: flex;
  187. align-items: center;
  188. `;
  189. const Value = styled('span')`
  190. font-size: ${p => p.theme.fontSizeExtraLarge};
  191. `;
  192. const StyledDropdownMenuControl = styled(DropdownMenuControl)`
  193. margin-left: auto;
  194. `;