123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- import {useCallback, useMemo} from 'react';
- import styled from '@emotion/styled';
- import Count from 'sentry/components/count';
- import GridEditable, {
- COL_WIDTH_UNDEFINED,
- GridColumnOrder,
- } from 'sentry/components/gridEditable';
- import PerformanceDuration from 'sentry/components/performanceDuration';
- import {ArrayLinks} from 'sentry/components/profiling/arrayLinks';
- import {t} from 'sentry/locale';
- import {Project} from 'sentry/types';
- import {SuspectFunction} from 'sentry/types/profiling/core';
- import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
- import {Container, NumberContainer} from 'sentry/utils/discover/styles';
- import {getShortEventId} from 'sentry/utils/events';
- import {generateProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
- import {renderTableHead} from 'sentry/utils/profiling/tableRenderer';
- import {useLocation} from 'sentry/utils/useLocation';
- import useOrganization from 'sentry/utils/useOrganization';
- interface FunctionsTableProps {
- analyticsPageSource: 'performance_transaction' | 'profiling_transaction';
- error: string | null;
- functions: SuspectFunction[];
- isLoading: boolean;
- project: Project | undefined;
- sort: string;
- }
- function FunctionsTable(props: FunctionsTableProps) {
- const location = useLocation();
- const organization = useOrganization();
- const sort = useMemo(() => {
- let column = props.sort;
- let order: 'asc' | 'desc' = 'asc' as const;
- if (props.sort.startsWith('-')) {
- column = props.sort.substring(1);
- order = 'desc' as const;
- }
- if (!SORTABLE_COLUMNS.has(column as any)) {
- column = 'p99';
- }
- return {
- key: column as TableColumnKey,
- order,
- };
- }, [props.sort]);
- const functions: TableDataRow[] = useMemo(() => {
- const project = props.project;
- if (!project) {
- return [];
- }
- return props.functions.map(func => {
- const {worst, examples, ...rest} = func;
- const allExamples = examples.filter(example => example !== worst);
- allExamples.unshift(worst);
- return {
- ...rest,
- examples: allExamples.map(example => {
- const profileId = example.replaceAll('-', '');
- return {
- value: getShortEventId(profileId),
- onClick: () =>
- trackAdvancedAnalyticsEvent('profiling_views.go_to_flamegraph', {
- organization,
- source: `${props.analyticsPageSource}.suspect_functions_table`,
- }),
- target: generateProfileFlamechartRouteWithQuery({
- orgSlug: organization.slug,
- projectSlug: project.slug,
- profileId,
- query: {
- // specify the frame to focus, the flamegraph will switch
- // to the appropriate thread when these are specified
- frameName: func.name,
- framePackage: func.package,
- },
- }),
- };
- }),
- };
- });
- }, [organization, props.project, props.functions, props.analyticsPageSource]);
- const generateSortLink = useCallback(
- (column: TableColumnKey) => {
- if (!SORTABLE_COLUMNS.has(column)) {
- return () => undefined;
- }
- const direction =
- sort.key !== column ? 'desc' : sort.order === 'desc' ? 'asc' : 'desc';
- return () => ({
- ...location,
- query: {
- ...location.query,
- functionsSort: `${direction === 'desc' ? '-' : ''}${column}`,
- },
- });
- },
- [location, sort]
- );
- return (
- <GridEditable
- isLoading={props.isLoading}
- error={props.error}
- data={functions}
- columnOrder={COLUMN_ORDER.map(key => COLUMNS[key])}
- columnSortBy={[]}
- grid={{
- renderHeadCell: renderTableHead({
- currentSort: sort,
- rightAlignedColumns: RIGHT_ALIGNED_COLUMNS,
- sortableColumns: SORTABLE_COLUMNS,
- generateSortLink,
- }),
- renderBodyCell: renderFunctionsTableCell,
- }}
- location={location}
- />
- );
- }
- const RIGHT_ALIGNED_COLUMNS = new Set<TableColumnKey>(['p75', 'p99', 'count']);
- const SORTABLE_COLUMNS = RIGHT_ALIGNED_COLUMNS;
- function renderFunctionsTableCell(
- column: TableColumn,
- dataRow: TableDataRow,
- rowIndex: number,
- columnIndex: number
- ) {
- return (
- <ProfilingFunctionsTableCell
- column={column}
- dataRow={dataRow}
- rowIndex={rowIndex}
- columnIndex={columnIndex}
- />
- );
- }
- interface ProfilingFunctionsTableCellProps {
- column: TableColumn;
- columnIndex: number;
- dataRow: TableDataRow;
- rowIndex: number;
- }
- const EmptyValueContainer = styled('span')`
- color: ${p => p.theme.gray300};
- `;
- function ProfilingFunctionsTableCell({
- column,
- dataRow,
- }: ProfilingFunctionsTableCellProps) {
- const value = dataRow[column.key];
- switch (column.key) {
- case 'count':
- return (
- <NumberContainer>
- <Count value={value} />
- </NumberContainer>
- );
- case 'p75':
- case 'p99':
- return (
- <NumberContainer>
- <PerformanceDuration nanoseconds={value} abbreviation />
- </NumberContainer>
- );
- case 'examples':
- return <ArrayLinks items={value} />;
- case 'name':
- case 'package':
- const name = value || <EmptyValueContainer>{t('Unknown')}</EmptyValueContainer>;
- return <Container>{name}</Container>;
- default:
- return <Container>{value}</Container>;
- }
- }
- type TableColumnKey = keyof Omit<SuspectFunction, 'fingerprint' | 'worst'>;
- type TableDataRow = Record<TableColumnKey, any>;
- type TableColumn = GridColumnOrder<TableColumnKey>;
- const COLUMN_ORDER: TableColumnKey[] = [
- 'name',
- 'package',
- 'count',
- 'p75',
- 'p99',
- 'examples',
- ];
- const COLUMNS: Record<Exclude<TableColumnKey, 'p95'>, TableColumn> = {
- name: {
- key: 'name',
- name: t('Name'),
- width: COL_WIDTH_UNDEFINED,
- },
- package: {
- key: 'package',
- name: t('Package'),
- width: COL_WIDTH_UNDEFINED,
- },
- path: {
- key: 'path',
- name: t('Path'),
- width: COL_WIDTH_UNDEFINED,
- },
- p75: {
- key: 'p75',
- name: t('P75 Total Duration'),
- width: COL_WIDTH_UNDEFINED,
- },
- p99: {
- key: 'p99',
- name: t('P99 Total Duration'),
- width: COL_WIDTH_UNDEFINED,
- },
- count: {
- key: 'count',
- name: t('Total Occurrences'),
- width: COL_WIDTH_UNDEFINED,
- },
- examples: {
- key: 'examples',
- name: t('Example Profiles'),
- width: COL_WIDTH_UNDEFINED,
- },
- };
- export {FunctionsTable};
|