123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- import {Fragment, PureComponent} from 'react';
- import styled from '@emotion/styled';
- import type {Location, LocationDescriptor} from 'history';
- import SortLink from 'sentry/components/gridEditable/sortLink';
- import Link from 'sentry/components/links/link';
- import LoadingIndicator from 'sentry/components/loadingIndicator';
- import {PanelTable} from 'sentry/components/panels/panelTable';
- import QuestionTooltip from 'sentry/components/questionTooltip';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import type {Organization} from 'sentry/types/organization';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import type {TableData, TableDataRow} from 'sentry/utils/discover/discoverQuery';
- import type {MetaType} from 'sentry/utils/discover/eventView';
- import type EventView from 'sentry/utils/discover/eventView';
- import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
- import type {Alignments} from 'sentry/utils/discover/fields';
- import {fieldAlignment, getAggregateAlias} from 'sentry/utils/discover/fields';
- import ViewReplayLink from 'sentry/utils/discover/viewReplayLink';
- import {isEmptyObject} from 'sentry/utils/object/isEmptyObject';
- import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
- import type {Actions} from 'sentry/views/discover/table/cellAction';
- import CellAction from 'sentry/views/discover/table/cellAction';
- import type {TableColumn} from 'sentry/views/discover/table/types';
- import {GridCell, GridCellNumber} from 'sentry/views/performance/styles';
- import type {TrendsDataEvents} from 'sentry/views/performance/trends/types';
- type Props = {
- columnOrder: TableColumn<React.ReactText>[];
- eventView: EventView;
- isLoading: boolean;
- location: Location;
- organization: Organization;
- tableData: TableData | TrendsDataEvents | null;
- useAggregateAlias: boolean;
- generateLink?: Record<
- string,
- (
- organization: Organization,
- tableRow: TableDataRow,
- location: Location
- ) => LocationDescriptor
- >;
- handleCellAction?: (
- c: TableColumn<React.ReactText>
- ) => (a: Actions, v: React.ReactText) => void;
- referrer?: string;
- titles?: string[];
- };
- class TransactionsTable extends PureComponent<Props> {
- getTitles() {
- const {eventView, titles} = this.props;
- return titles ?? eventView.getFields();
- }
- renderHeader() {
- const {tableData, columnOrder} = this.props;
- const tableMeta = tableData?.meta;
- const generateSortLink = () => undefined;
- const tableTitles = this.getTitles();
- const headers = tableTitles.map((title, index) => {
- const column = columnOrder[index];
- const align: Alignments = fieldAlignment(column.name, column.type, tableMeta);
- if (column.key === 'span_ops_breakdown.relative') {
- return (
- <HeadCellContainer key={index}>
- <SortLink
- align={align}
- title={
- title === t('operation duration') ? (
- <Fragment>
- {title}
- <StyledIconQuestion
- size="xs"
- position="top"
- title={t(
- `Span durations are summed over the course of an entire transaction. Any overlapping spans are only counted once.`
- )}
- />
- </Fragment>
- ) : (
- title
- )
- }
- direction={undefined}
- canSort={false}
- generateSortLink={generateSortLink}
- />
- </HeadCellContainer>
- );
- }
- return (
- <HeadCellContainer key={index}>
- <SortLink
- align={align}
- title={title}
- direction={undefined}
- canSort={false}
- generateSortLink={generateSortLink}
- />
- </HeadCellContainer>
- );
- });
- return headers;
- }
- renderRow(
- row: TableDataRow,
- rowIndex: number,
- columnOrder: TableColumn<React.ReactText>[],
- tableMeta: MetaType
- ): React.ReactNode[] {
- const {
- eventView,
- organization,
- location,
- generateLink,
- handleCellAction,
- titles,
- useAggregateAlias,
- referrer,
- } = this.props;
- const fields = eventView.getFields();
- if (titles?.length) {
- // Slice to match length of given titles
- columnOrder = columnOrder.slice(0, titles.length);
- }
- const resultsRow = columnOrder.map((column, index) => {
- const field = String(column.key);
- // TODO add a better abstraction for this in fieldRenderers.
- const fieldName = useAggregateAlias ? getAggregateAlias(field) : field;
- const fieldType = tableMeta[fieldName];
- const fieldRenderer = getFieldRenderer(field, tableMeta, useAggregateAlias);
- let rendered = fieldRenderer(row, {organization, location});
- const target = generateLink?.[field]?.(organization, row, location);
- if (target && !isEmptyObject(target)) {
- if (fields[index] === 'replayId') {
- rendered = (
- <ViewReplayLink replayId={row.replayId} to={target}>
- {rendered}
- </ViewReplayLink>
- );
- } else if (fields[index] === 'profile.id') {
- rendered = (
- <Link
- data-test-id={`view-${fields[index]}`}
- to={target}
- onClick={getProfileAnalyticsHandler(organization, referrer)}
- >
- {rendered}
- </Link>
- );
- } else {
- rendered = (
- <Link data-test-id={`view-${fields[index]}`} to={target}>
- {rendered}
- </Link>
- );
- }
- }
- const isNumeric = ['integer', 'number', 'duration'].includes(fieldType);
- const key = `${rowIndex}:${column.key}:${index}`;
- rendered = isNumeric ? (
- <GridCellNumber data-test-id="grid-cell">{rendered}</GridCellNumber>
- ) : (
- <GridCell data-test-id="grid-cell">{rendered}</GridCell>
- );
- if (handleCellAction) {
- rendered = (
- <CellAction
- column={column}
- dataRow={row}
- handleCellAction={handleCellAction(column)}
- >
- {rendered}
- </CellAction>
- );
- }
- return <BodyCellContainer key={key}>{rendered}</BodyCellContainer>;
- });
- return resultsRow;
- }
- renderResults() {
- const {isLoading, tableData, columnOrder} = this.props;
- let cells: React.ReactNode[] = [];
- if (isLoading) {
- return cells;
- }
- if (!tableData || !tableData.meta || !tableData.data) {
- return cells;
- }
- tableData.data.forEach((row, i: number) => {
- // Another check to appease tsc
- if (!tableData.meta) {
- return;
- }
- cells = cells.concat(this.renderRow(row, i, columnOrder, tableData.meta));
- });
- return cells;
- }
- render() {
- const {isLoading, tableData} = this.props;
- const hasResults = tableData?.meta && tableData.data?.length > 0;
- // Custom set the height so we don't have layout shift when results are loaded.
- const loader = <LoadingIndicator style={{margin: '70px auto'}} />;
- return (
- <VisuallyCompleteWithData
- id="TransactionsTable"
- hasData={hasResults}
- isLoading={isLoading}
- >
- <PanelTable
- data-test-id="transactions-table"
- isEmpty={!hasResults}
- emptyMessage={t('No transactions found')}
- headers={this.renderHeader()}
- isLoading={isLoading}
- disablePadding
- loader={loader}
- >
- {this.renderResults()}
- </PanelTable>
- </VisuallyCompleteWithData>
- );
- }
- }
- function getProfileAnalyticsHandler(organization: Organization, referrer?: string) {
- return () => {
- let source;
- if (referrer === 'performance.transactions_summary') {
- source = 'performance.transactions_summary.overview';
- } else {
- source = 'discover.transactions_table';
- }
- trackAnalytics('profiling_views.go_to_flamegraph', {
- organization,
- source,
- });
- };
- }
- const HeadCellContainer = styled('div')`
- padding: ${space(2)};
- `;
- const BodyCellContainer = styled('div')`
- padding: ${space(1)} ${space(2)};
- ${p => p.theme.overflowEllipsis};
- `;
- const StyledIconQuestion = styled(QuestionTooltip)`
- position: relative;
- top: 1px;
- left: 4px;
- `;
- export default TransactionsTable;
|