fieldRenderers.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. import {browserHistory} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import partial from 'lodash/partial';
  5. import Count from 'sentry/components/count';
  6. import Duration from 'sentry/components/duration';
  7. import FileSize from 'sentry/components/fileSize';
  8. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  9. import UserBadge from 'sentry/components/idBadge/userBadge';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import {RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
  12. import {pickBarColor, toPercent} from 'sentry/components/performance/waterfall/utils';
  13. import Tooltip from 'sentry/components/tooltip';
  14. import UserMisery from 'sentry/components/userMisery';
  15. import Version from 'sentry/components/version';
  16. import {t} from 'sentry/locale';
  17. import {AvatarProject, Organization, Project} from 'sentry/types';
  18. import {defined, isUrl} from 'sentry/utils';
  19. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  20. import EventView, {EventData, MetaType} from 'sentry/utils/discover/eventView';
  21. import {
  22. AGGREGATIONS,
  23. getAggregateAlias,
  24. getSpanOperationName,
  25. isEquation,
  26. isRelativeSpanOperationBreakdownField,
  27. SPAN_OP_BREAKDOWN_FIELDS,
  28. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  29. } from 'sentry/utils/discover/fields';
  30. import {getShortEventId} from 'sentry/utils/events';
  31. import {formatFloat, formatPercentage} from 'sentry/utils/formatters';
  32. import getDynamicText from 'sentry/utils/getDynamicText';
  33. import Projects from 'sentry/utils/projects';
  34. import {
  35. filterToLocationQuery,
  36. SpanOperationBreakdownFilter,
  37. stringToFilter,
  38. } from 'sentry/views/performance/transactionSummary/filter';
  39. import ArrayValue from './arrayValue';
  40. import {
  41. BarContainer,
  42. Container,
  43. FieldDateTime,
  44. FieldShortId,
  45. FlexContainer,
  46. NumberContainer,
  47. OverflowLink,
  48. UserIcon,
  49. VersionContainer,
  50. } from './styles';
  51. import TeamKeyTransactionField from './teamKeyTransactionField';
  52. /**
  53. * Types, functions and definitions for rendering fields in discover results.
  54. */
  55. export type RenderFunctionBaggage = {
  56. location: Location;
  57. organization: Organization;
  58. eventView?: EventView;
  59. unit?: string;
  60. };
  61. type FieldFormatterRenderFunction = (
  62. field: string,
  63. data: EventData,
  64. baggage?: RenderFunctionBaggage
  65. ) => React.ReactNode;
  66. type FieldFormatterRenderFunctionPartial = (
  67. data: EventData,
  68. baggage: RenderFunctionBaggage
  69. ) => React.ReactNode;
  70. type FieldFormatter = {
  71. isSortable: boolean;
  72. renderFunc: FieldFormatterRenderFunction;
  73. };
  74. type FieldFormatters = {
  75. array: FieldFormatter;
  76. boolean: FieldFormatter;
  77. date: FieldFormatter;
  78. duration: FieldFormatter;
  79. integer: FieldFormatter;
  80. number: FieldFormatter;
  81. percentage: FieldFormatter;
  82. size: FieldFormatter;
  83. string: FieldFormatter;
  84. };
  85. export type FieldTypes = keyof FieldFormatters;
  86. const EmptyValueContainer = styled('span')`
  87. color: ${p => p.theme.gray300};
  88. `;
  89. const emptyValue = <EmptyValueContainer>{t('(no value)')}</EmptyValueContainer>;
  90. const emptyStringValue = <EmptyValueContainer>{t('(empty string)')}</EmptyValueContainer>;
  91. export function nullableValue(value: string | null): string | React.ReactElement {
  92. switch (value) {
  93. case null:
  94. return emptyValue;
  95. case '':
  96. return emptyStringValue;
  97. default:
  98. return value;
  99. }
  100. }
  101. export const SIZE_UNITS = {
  102. bit: 1 / 8,
  103. byte: 1,
  104. kibibyte: 1024,
  105. mebibyte: 1024 ** 2,
  106. gibibyte: 1024 ** 3,
  107. tebibyte: 1024 ** 4,
  108. pebibyte: 1024 ** 5,
  109. exbibyte: 1024 ** 6,
  110. kilobyte: 1000,
  111. megabyte: 1000 ** 2,
  112. gigabyte: 1000 ** 3,
  113. terabyte: 1000 ** 4,
  114. petabyte: 1000 ** 5,
  115. exabyte: 1000 ** 6,
  116. };
  117. export const ABYTE_UNITS = [
  118. 'kilobyte',
  119. 'megabyte',
  120. 'gigabyte',
  121. 'terabyte',
  122. 'petabyte',
  123. 'exabyte',
  124. ];
  125. export const DURATION_UNITS = {
  126. nanosecond: 1 / 1000 ** 2,
  127. microsecond: 1 / 1000,
  128. millisecond: 1,
  129. second: 1000,
  130. minute: 1000 * 60,
  131. hour: 1000 * 60 * 60,
  132. day: 1000 * 60 * 60 * 24,
  133. week: 1000 * 60 * 60 * 24 * 7,
  134. };
  135. export const PERCENTAGE_UNITS = ['ratio', 'percent'];
  136. /**
  137. * A mapping of field types to their rendering function.
  138. * This mapping is used when a field is not defined in SPECIAL_FIELDS
  139. * and the field is not being coerced to a link.
  140. *
  141. * This mapping should match the output sentry.utils.snuba:get_json_type
  142. */
  143. export const FIELD_FORMATTERS: FieldFormatters = {
  144. boolean: {
  145. isSortable: true,
  146. renderFunc: (field, data) => {
  147. const value = data[field] ? t('true') : t('false');
  148. return <Container>{value}</Container>;
  149. },
  150. },
  151. date: {
  152. isSortable: true,
  153. renderFunc: (field, data) => (
  154. <Container>
  155. {data[field]
  156. ? getDynamicText({
  157. value: <FieldDateTime date={data[field]} year seconds timeZone />,
  158. fixed: 'timestamp',
  159. })
  160. : emptyValue}
  161. </Container>
  162. ),
  163. },
  164. duration: {
  165. isSortable: true,
  166. renderFunc: (field, data, baggage) => {
  167. const {unit} = baggage ?? {};
  168. return (
  169. <NumberContainer>
  170. {typeof data[field] === 'number' ? (
  171. <Duration
  172. seconds={(data[field] * ((unit && DURATION_UNITS[unit]) ?? 1)) / 1000}
  173. fixedDigits={2}
  174. abbreviation
  175. />
  176. ) : (
  177. emptyValue
  178. )}
  179. </NumberContainer>
  180. );
  181. },
  182. },
  183. integer: {
  184. isSortable: true,
  185. renderFunc: (field, data) => (
  186. <NumberContainer>
  187. {typeof data[field] === 'number' ? <Count value={data[field]} /> : emptyValue}
  188. </NumberContainer>
  189. ),
  190. },
  191. number: {
  192. isSortable: true,
  193. renderFunc: (field, data) => (
  194. <NumberContainer>
  195. {typeof data[field] === 'number' ? formatFloat(data[field], 4) : emptyValue}
  196. </NumberContainer>
  197. ),
  198. },
  199. percentage: {
  200. isSortable: true,
  201. renderFunc: (field, data) => (
  202. <NumberContainer>
  203. {typeof data[field] === 'number' ? formatPercentage(data[field]) : emptyValue}
  204. </NumberContainer>
  205. ),
  206. },
  207. size: {
  208. isSortable: true,
  209. renderFunc: (field, data, baggage) => {
  210. const {unit} = baggage ?? {};
  211. return (
  212. <NumberContainer>
  213. {unit && SIZE_UNITS[unit] && typeof data[field] === 'number' ? (
  214. <FileSize
  215. bytes={data[field] * SIZE_UNITS[unit]}
  216. base={ABYTE_UNITS.includes(unit) ? 10 : 2}
  217. />
  218. ) : (
  219. emptyValue
  220. )}
  221. </NumberContainer>
  222. );
  223. },
  224. },
  225. string: {
  226. isSortable: true,
  227. renderFunc: (field, data) => {
  228. // Some fields have long arrays in them, only show the tail of the data.
  229. const value = Array.isArray(data[field])
  230. ? data[field].slice(-1)
  231. : defined(data[field])
  232. ? data[field]
  233. : emptyValue;
  234. if (isUrl(value)) {
  235. return (
  236. <Container>
  237. <ExternalLink href={value} data-test-id="group-tag-url">
  238. {value}
  239. </ExternalLink>
  240. </Container>
  241. );
  242. }
  243. return <Container>{nullableValue(value)}</Container>;
  244. },
  245. },
  246. array: {
  247. isSortable: true,
  248. renderFunc: (field, data) => {
  249. const value = Array.isArray(data[field]) ? data[field] : [data[field]];
  250. return <ArrayValue value={value} />;
  251. },
  252. },
  253. };
  254. type SpecialFieldRenderFunc = (
  255. data: EventData,
  256. baggage: RenderFunctionBaggage
  257. ) => React.ReactNode;
  258. type SpecialField = {
  259. renderFunc: SpecialFieldRenderFunc;
  260. sortField: string | null;
  261. };
  262. type SpecialFields = {
  263. 'count_unique(user)': SpecialField;
  264. 'error.handled': SpecialField;
  265. id: SpecialField;
  266. issue: SpecialField;
  267. 'issue.id': SpecialField;
  268. project: SpecialField;
  269. release: SpecialField;
  270. team_key_transaction: SpecialField;
  271. 'timestamp.to_day': SpecialField;
  272. 'timestamp.to_hour': SpecialField;
  273. trace: SpecialField;
  274. 'trend_percentage()': SpecialField;
  275. user: SpecialField;
  276. 'user.display': SpecialField;
  277. };
  278. /**
  279. * "Special fields" either do not map 1:1 to an single column in the event database,
  280. * or they require custom UI formatting that can't be handled by the datatype formatters.
  281. */
  282. const SPECIAL_FIELDS: SpecialFields = {
  283. id: {
  284. sortField: 'id',
  285. renderFunc: data => {
  286. const id: string | unknown = data?.id;
  287. if (typeof id !== 'string') {
  288. return null;
  289. }
  290. return <Container>{getShortEventId(id)}</Container>;
  291. },
  292. },
  293. trace: {
  294. sortField: 'trace',
  295. renderFunc: data => {
  296. const id: string | unknown = data?.trace;
  297. if (typeof id !== 'string') {
  298. return null;
  299. }
  300. return <Container>{getShortEventId(id)}</Container>;
  301. },
  302. },
  303. 'issue.id': {
  304. sortField: 'issue.id',
  305. renderFunc: (data, {organization}) => {
  306. const target = {
  307. pathname: `/organizations/${organization.slug}/issues/${data['issue.id']}/`,
  308. };
  309. return (
  310. <Container>
  311. <OverflowLink to={target} aria-label={data['issue.id']}>
  312. {data['issue.id']}
  313. </OverflowLink>
  314. </Container>
  315. );
  316. },
  317. },
  318. issue: {
  319. sortField: null,
  320. renderFunc: (data, {organization}) => {
  321. const issueID = data['issue.id'];
  322. if (!issueID) {
  323. return (
  324. <Container>
  325. <FieldShortId shortId={`${data.issue}`} />
  326. </Container>
  327. );
  328. }
  329. const target = {
  330. pathname: `/organizations/${organization.slug}/issues/${issueID}/`,
  331. };
  332. return (
  333. <Container>
  334. <OverflowLink to={target} aria-label={issueID}>
  335. <FieldShortId shortId={`${data.issue}`} />
  336. </OverflowLink>
  337. </Container>
  338. );
  339. },
  340. },
  341. project: {
  342. sortField: 'project',
  343. renderFunc: (data, {organization}) => {
  344. let slugs: string[] | undefined = undefined;
  345. let projectIds: number[] | undefined = undefined;
  346. if (typeof data.project === 'number') {
  347. projectIds = [data.project];
  348. } else {
  349. slugs = [data.project];
  350. }
  351. return (
  352. <Container>
  353. <Projects orgId={organization.slug} slugs={slugs} projectIds={projectIds}>
  354. {({projects}) => {
  355. let project: Project | AvatarProject | undefined;
  356. if (typeof data.project === 'number') {
  357. project = projects.find(p => p.id === data.project.toString());
  358. } else {
  359. project = projects.find(p => p.slug === data.project);
  360. }
  361. return (
  362. <ProjectBadge
  363. project={project ? project : {slug: data.project}}
  364. avatarSize={16}
  365. />
  366. );
  367. }}
  368. </Projects>
  369. </Container>
  370. );
  371. },
  372. },
  373. user: {
  374. sortField: 'user',
  375. renderFunc: data => {
  376. if (data.user) {
  377. const [key, value] = data.user.split(':');
  378. const userObj = {
  379. id: '',
  380. name: '',
  381. email: '',
  382. username: '',
  383. ip_address: '',
  384. };
  385. userObj[key] = value;
  386. const badge = <UserBadge user={userObj} hideEmail avatarSize={16} />;
  387. return <Container>{badge}</Container>;
  388. }
  389. return <Container>{emptyValue}</Container>;
  390. },
  391. },
  392. 'user.display': {
  393. sortField: 'user.display',
  394. renderFunc: data => {
  395. if (data['user.display']) {
  396. const userObj = {
  397. id: '',
  398. name: data['user.display'],
  399. email: '',
  400. username: '',
  401. ip_address: '',
  402. };
  403. const badge = <UserBadge user={userObj} hideEmail avatarSize={16} />;
  404. return <Container>{badge}</Container>;
  405. }
  406. return <Container>{emptyValue}</Container>;
  407. },
  408. },
  409. 'count_unique(user)': {
  410. sortField: 'count_unique(user)',
  411. renderFunc: data => {
  412. const count = data.count_unique_user ?? data['count_unique(user)'];
  413. if (typeof count === 'number') {
  414. return (
  415. <FlexContainer>
  416. <NumberContainer>
  417. <Count value={count} />
  418. </NumberContainer>
  419. <UserIcon size="20" />
  420. </FlexContainer>
  421. );
  422. }
  423. return <Container>{emptyValue}</Container>;
  424. },
  425. },
  426. release: {
  427. sortField: 'release',
  428. renderFunc: data =>
  429. data.release ? (
  430. <VersionContainer>
  431. <Version version={data.release} anchor={false} tooltipRawVersion truncate />
  432. </VersionContainer>
  433. ) : (
  434. <Container>{emptyValue}</Container>
  435. ),
  436. },
  437. 'error.handled': {
  438. sortField: 'error.handled',
  439. renderFunc: data => {
  440. const values = data['error.handled'];
  441. // Transactions will have null, and default events have no handled attributes.
  442. if (values === null || values?.length === 0) {
  443. return <Container>{emptyValue}</Container>;
  444. }
  445. const value = Array.isArray(values) ? values : [values];
  446. return (
  447. <Container>
  448. {value.every(v => [1, null].includes(v)) ? 'true' : 'false'}
  449. </Container>
  450. );
  451. },
  452. },
  453. team_key_transaction: {
  454. sortField: null,
  455. renderFunc: (data, {organization}) => (
  456. <Container>
  457. <TeamKeyTransactionField
  458. isKeyTransaction={(data.team_key_transaction ?? 0) !== 0}
  459. organization={organization}
  460. projectSlug={data.project}
  461. transactionName={data.transaction}
  462. />
  463. </Container>
  464. ),
  465. },
  466. 'trend_percentage()': {
  467. sortField: 'trend_percentage()',
  468. renderFunc: data => (
  469. <NumberContainer>
  470. {typeof data.trend_percentage === 'number'
  471. ? formatPercentage(data.trend_percentage - 1)
  472. : emptyValue}
  473. </NumberContainer>
  474. ),
  475. },
  476. 'timestamp.to_hour': {
  477. sortField: 'timestamp.to_hour',
  478. renderFunc: data => (
  479. <Container>
  480. {getDynamicText({
  481. value: <FieldDateTime date={data['timestamp.to_hour']} year timeZone />,
  482. fixed: 'timestamp.to_hour',
  483. })}
  484. </Container>
  485. ),
  486. },
  487. 'timestamp.to_day': {
  488. sortField: 'timestamp.to_day',
  489. renderFunc: data => (
  490. <Container>
  491. {getDynamicText({
  492. value: <FieldDateTime date={data['timestamp.to_day']} dateOnly year utc />,
  493. fixed: 'timestamp.to_day',
  494. })}
  495. </Container>
  496. ),
  497. },
  498. };
  499. type SpecialFunctionFieldRenderer = (
  500. fieldName: string
  501. ) => (data: EventData, baggage: RenderFunctionBaggage) => React.ReactNode;
  502. type SpecialFunctions = {
  503. user_misery: SpecialFunctionFieldRenderer;
  504. };
  505. /**
  506. * "Special functions" are functions whose values either do not map 1:1 to a single column,
  507. * or they require custom UI formatting that can't be handled by the datatype formatters.
  508. */
  509. const SPECIAL_FUNCTIONS: SpecialFunctions = {
  510. user_misery: fieldName => data => {
  511. const userMiseryField = fieldName;
  512. if (!(userMiseryField in data)) {
  513. return <NumberContainer>{emptyValue}</NumberContainer>;
  514. }
  515. const userMisery = data[userMiseryField];
  516. if (userMisery === null || isNaN(userMisery)) {
  517. return <NumberContainer>{emptyValue}</NumberContainer>;
  518. }
  519. const projectThresholdConfig = 'project_threshold_config';
  520. let countMiserableUserField: string = '';
  521. let miseryLimit: number | undefined = parseInt(
  522. userMiseryField.split('(').pop()?.slice(0, -1) || '',
  523. 10
  524. );
  525. if (isNaN(miseryLimit)) {
  526. countMiserableUserField = 'count_miserable(user)';
  527. if (projectThresholdConfig in data) {
  528. miseryLimit = data[projectThresholdConfig][1];
  529. } else {
  530. miseryLimit = undefined;
  531. }
  532. } else {
  533. countMiserableUserField = `count_miserable(user,${miseryLimit})`;
  534. }
  535. const uniqueUsers = data['count_unique(user)'];
  536. let miserableUsers: number | undefined;
  537. if (countMiserableUserField in data) {
  538. const countMiserableMiseryLimit = parseInt(
  539. userMiseryField.split('(').pop()?.slice(0, -1) || '',
  540. 10
  541. );
  542. miserableUsers =
  543. countMiserableMiseryLimit === miseryLimit ||
  544. (isNaN(countMiserableMiseryLimit) && projectThresholdConfig)
  545. ? data[countMiserableUserField]
  546. : undefined;
  547. }
  548. return (
  549. <BarContainer>
  550. <UserMisery
  551. bars={10}
  552. barHeight={20}
  553. miseryLimit={miseryLimit}
  554. totalUsers={uniqueUsers}
  555. userMisery={userMisery}
  556. miserableUsers={miserableUsers}
  557. />
  558. </BarContainer>
  559. );
  560. },
  561. };
  562. /**
  563. * Get the sort field name for a given field if it is special or fallback
  564. * to the generic type formatter.
  565. */
  566. export function getSortField(
  567. field: string,
  568. tableMeta: MetaType | undefined
  569. ): string | null {
  570. if (SPECIAL_FIELDS.hasOwnProperty(field)) {
  571. return SPECIAL_FIELDS[field as keyof typeof SPECIAL_FIELDS].sortField;
  572. }
  573. if (!tableMeta) {
  574. return field;
  575. }
  576. if (isEquation(field)) {
  577. return field;
  578. }
  579. for (const alias in AGGREGATIONS) {
  580. if (field.startsWith(alias)) {
  581. return AGGREGATIONS[alias].isSortable ? field : null;
  582. }
  583. }
  584. const fieldType = tableMeta[field];
  585. if (FIELD_FORMATTERS.hasOwnProperty(fieldType)) {
  586. return FIELD_FORMATTERS[fieldType as keyof typeof FIELD_FORMATTERS].isSortable
  587. ? field
  588. : null;
  589. }
  590. return null;
  591. }
  592. const isDurationValue = (data: EventData, field: string): boolean => {
  593. return field in data && typeof data[field] === 'number';
  594. };
  595. const spanOperationRelativeBreakdownRenderer = (
  596. data: EventData,
  597. {location, organization, eventView}: RenderFunctionBaggage
  598. ): React.ReactNode => {
  599. const sumOfSpanTime = SPAN_OP_BREAKDOWN_FIELDS.reduce(
  600. (prev, curr) => (isDurationValue(data, curr) ? prev + data[curr] : prev),
  601. 0
  602. );
  603. const cumulativeSpanOpBreakdown = Math.max(sumOfSpanTime, data['transaction.duration']);
  604. if (
  605. SPAN_OP_BREAKDOWN_FIELDS.every(field => !isDurationValue(data, field)) ||
  606. cumulativeSpanOpBreakdown === 0
  607. ) {
  608. return FIELD_FORMATTERS.duration.renderFunc(SPAN_OP_RELATIVE_BREAKDOWN_FIELD, data);
  609. }
  610. let otherPercentage = 1;
  611. let orderedSpanOpsBreakdownFields;
  612. const sortingOnField = eventView?.sorts?.[0]?.field;
  613. if (sortingOnField && (SPAN_OP_BREAKDOWN_FIELDS as string[]).includes(sortingOnField)) {
  614. orderedSpanOpsBreakdownFields = [
  615. sortingOnField,
  616. ...SPAN_OP_BREAKDOWN_FIELDS.filter(op => op !== sortingOnField),
  617. ];
  618. } else {
  619. orderedSpanOpsBreakdownFields = SPAN_OP_BREAKDOWN_FIELDS;
  620. }
  621. return (
  622. <RelativeOpsBreakdown>
  623. {orderedSpanOpsBreakdownFields.map(field => {
  624. if (!isDurationValue(data, field)) {
  625. return null;
  626. }
  627. const operationName = getSpanOperationName(field) ?? 'op';
  628. const spanOpDuration: number = data[field];
  629. const widthPercentage = spanOpDuration / cumulativeSpanOpBreakdown;
  630. otherPercentage = otherPercentage - widthPercentage;
  631. if (widthPercentage === 0) {
  632. return null;
  633. }
  634. return (
  635. <div key={operationName} style={{width: toPercent(widthPercentage || 0)}}>
  636. <Tooltip
  637. title={
  638. <div>
  639. <div>{operationName}</div>
  640. <div>
  641. <Duration
  642. seconds={spanOpDuration / 1000}
  643. fixedDigits={2}
  644. abbreviation
  645. />
  646. </div>
  647. </div>
  648. }
  649. containerDisplayMode="block"
  650. >
  651. <RectangleRelativeOpsBreakdown
  652. spanBarHatch={false}
  653. style={{
  654. backgroundColor: pickBarColor(operationName),
  655. cursor: 'pointer',
  656. }}
  657. onClick={event => {
  658. event.stopPropagation();
  659. const filter = stringToFilter(operationName);
  660. if (filter === SpanOperationBreakdownFilter.None) {
  661. return;
  662. }
  663. trackAdvancedAnalyticsEvent(
  664. 'performance_views.relative_breakdown.selection',
  665. {
  666. action: filter,
  667. organization,
  668. }
  669. );
  670. browserHistory.push({
  671. pathname: location.pathname,
  672. query: {
  673. ...location.query,
  674. ...filterToLocationQuery(filter),
  675. },
  676. });
  677. }}
  678. />
  679. </Tooltip>
  680. </div>
  681. );
  682. })}
  683. <div key="other" style={{width: toPercent(otherPercentage || 0)}}>
  684. <Tooltip title={<div>{t('Other')}</div>} containerDisplayMode="block">
  685. <OtherRelativeOpsBreakdown spanBarHatch={false} />
  686. </Tooltip>
  687. </div>
  688. </RelativeOpsBreakdown>
  689. );
  690. };
  691. const RelativeOpsBreakdown = styled('div')`
  692. position: relative;
  693. display: flex;
  694. `;
  695. const RectangleRelativeOpsBreakdown = styled(RowRectangle)`
  696. position: relative;
  697. width: 100%;
  698. `;
  699. const OtherRelativeOpsBreakdown = styled(RectangleRelativeOpsBreakdown)`
  700. background-color: ${p => p.theme.gray100};
  701. `;
  702. /**
  703. * Get the field renderer for the named field and metadata
  704. *
  705. * @param {String} field name
  706. * @param {object} metadata mapping.
  707. * @param {boolean} isAlias convert the name with getAggregateAlias
  708. * @returns {Function}
  709. */
  710. export function getFieldRenderer(
  711. field: string,
  712. meta: MetaType,
  713. isAlias: boolean = true
  714. ): FieldFormatterRenderFunctionPartial {
  715. if (SPECIAL_FIELDS.hasOwnProperty(field)) {
  716. return SPECIAL_FIELDS[field].renderFunc;
  717. }
  718. if (isRelativeSpanOperationBreakdownField(field)) {
  719. return spanOperationRelativeBreakdownRenderer;
  720. }
  721. const fieldName = isAlias ? getAggregateAlias(field) : field;
  722. const fieldType = meta[fieldName];
  723. for (const alias in SPECIAL_FUNCTIONS) {
  724. if (fieldName.startsWith(alias)) {
  725. return SPECIAL_FUNCTIONS[alias](fieldName);
  726. }
  727. }
  728. if (FIELD_FORMATTERS.hasOwnProperty(fieldType)) {
  729. return partial(FIELD_FORMATTERS[fieldType].renderFunc, fieldName);
  730. }
  731. return partial(FIELD_FORMATTERS.string.renderFunc, fieldName);
  732. }
  733. type FieldTypeFormatterRenderFunctionPartial = (
  734. data: EventData,
  735. baggage?: RenderFunctionBaggage
  736. ) => React.ReactNode;
  737. /**
  738. * Get the field renderer for the named field only based on its type from the given
  739. * metadata.
  740. *
  741. * @param {String} field name
  742. * @param {object} metadata mapping.
  743. * @param {boolean} isAlias convert the name with getAggregateAlias
  744. * @returns {Function}
  745. */
  746. export function getFieldFormatter(
  747. field: string,
  748. meta: MetaType,
  749. isAlias: boolean = true
  750. ): FieldTypeFormatterRenderFunctionPartial {
  751. const fieldName = isAlias ? getAggregateAlias(field) : field;
  752. const fieldType = meta[fieldName];
  753. if (FIELD_FORMATTERS.hasOwnProperty(fieldType)) {
  754. return partial(FIELD_FORMATTERS[fieldType].renderFunc, fieldName);
  755. }
  756. return partial(FIELD_FORMATTERS.string.renderFunc, fieldName);
  757. }