123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- import {Fragment, useMemo, useRef} from 'react';
- import styled from '@emotion/styled';
- import EmptyStateWarning from 'sentry/components/emptyStateWarning';
- import {GridResizer} from 'sentry/components/gridEditable/styles';
- import Link from 'sentry/components/links/link';
- import LoadingIndicator from 'sentry/components/loadingIndicator';
- import Pagination from 'sentry/components/pagination';
- import {Tooltip} from 'sentry/components/tooltip';
- import {CHART_PALETTE} from 'sentry/constants/chartPalette';
- import {IconArrow} from 'sentry/icons/iconArrow';
- import {IconStack} from 'sentry/icons/iconStack';
- import {IconWarning} from 'sentry/icons/iconWarning';
- import {t} from 'sentry/locale';
- import {defined} from 'sentry/utils';
- import {
- fieldAlignment,
- parseFunction,
- prettifyParsedFunction,
- } from 'sentry/utils/discover/fields';
- import {useLocation} from 'sentry/utils/useLocation';
- import useProjects from 'sentry/utils/useProjects';
- import {
- Table,
- TableBody,
- TableBodyCell,
- TableHead,
- TableHeadCell,
- TableHeadCellContent,
- TableRow,
- TableStatus,
- useTableStyles,
- } from 'sentry/views/explore/components/table';
- import {
- useExploreGroupBys,
- useExploreQuery,
- useExploreSortBys,
- useSetExploreSortBys,
- } from 'sentry/views/explore/contexts/pageParamsContext';
- import {useSpanTags} from 'sentry/views/explore/contexts/spanTagsContext';
- import type {AggregatesTableResult} from 'sentry/views/explore/hooks/useExploreAggregatesTable';
- import {TOP_EVENTS_LIMIT, useTopEvents} from 'sentry/views/explore/hooks/useTopEvents';
- import {viewSamplesTarget} from 'sentry/views/explore/utils';
- import {FieldRenderer} from './fieldRenderer';
- interface AggregatesTableProps {
- aggregatesTableResult: AggregatesTableResult;
- }
- export function AggregatesTable({aggregatesTableResult}: AggregatesTableProps) {
- const location = useLocation();
- const {projects} = useProjects();
- const topEvents = useTopEvents();
- const groupBys = useExploreGroupBys();
- const {result, eventView, fields} = aggregatesTableResult;
- const sorts = useExploreSortBys();
- const setSorts = useSetExploreSortBys();
- const query = useExploreQuery();
- const columns = useMemo(() => eventView.getColumns(), [eventView]);
- const tableRef = useRef<HTMLTableElement>(null);
- const {initialTableStyles, onResizeMouseDown} = useTableStyles(fields, tableRef, {
- minimumColumnWidth: 50,
- prefixColumnWidth: 'min-content',
- });
- const meta = result.meta ?? {};
- const numberTags = useSpanTags('number');
- const stringTags = useSpanTags('string');
- return (
- <Fragment>
- <Table ref={tableRef} styles={initialTableStyles}>
- <TableHead>
- <TableRow>
- <TableHeadCell isFirst={false}>
- <TableHeadCellContent />
- </TableHeadCell>
- {fields.map((field, i) => {
- // Hide column names before alignment is determined
- if (result.isPending) {
- return <TableHeadCell key={i} isFirst={i === 0} />;
- }
- let label = field;
- const fieldType = meta.fields?.[field];
- const align = fieldAlignment(field, fieldType);
- const tag = stringTags[field] ?? numberTags[field] ?? null;
- if (tag) {
- label = tag.name;
- }
- const func = parseFunction(field);
- if (func) {
- label = prettifyParsedFunction(func);
- }
- const direction = sorts.find(s => s.field === field)?.kind;
- function updateSort() {
- const kind = direction === 'desc' ? 'asc' : 'desc';
- setSorts([{field, kind}]);
- }
- return (
- <TableHeadCell align={align} key={i} isFirst={i === 0}>
- <TableHeadCellContent onClick={updateSort}>
- <Tooltip showOnlyOnOverflow title={label}>
- {label}
- </Tooltip>
- {defined(direction) && (
- <IconArrow
- size="xs"
- direction={
- direction === 'desc'
- ? 'down'
- : direction === 'asc'
- ? 'up'
- : undefined
- }
- />
- )}
- </TableHeadCellContent>
- {i !== fields.length - 1 && (
- <GridResizer
- dataRows={
- !result.isError && !result.isPending && result.data
- ? result.data.length
- : 0
- }
- onMouseDown={e => onResizeMouseDown(e, i)}
- />
- )}
- </TableHeadCell>
- );
- })}
- </TableRow>
- </TableHead>
- <TableBody>
- {result.isPending ? (
- <TableStatus>
- <LoadingIndicator />
- </TableStatus>
- ) : result.isError ? (
- <TableStatus>
- <IconWarning data-test-id="error-indicator" color="gray300" size="lg" />
- </TableStatus>
- ) : result.isFetched && result.data?.length ? (
- result.data?.map((row, i) => {
- const target = viewSamplesTarget(location, query, groupBys, row, {
- projects,
- });
- return (
- <TableRow key={i}>
- <TableBodyCell>
- {topEvents && i < topEvents && <TopResultsIndicator index={i} />}
- <Tooltip title={t('View Samples')} containerDisplayMode="flex">
- <StyledLink to={target}>
- <IconStack />
- </StyledLink>
- </Tooltip>
- </TableBodyCell>
- {fields.map((field, j) => {
- return (
- <TableBodyCell key={j}>
- <FieldRenderer
- column={columns[j]!}
- data={row}
- unit={meta?.units?.[field]}
- meta={meta}
- />
- </TableBodyCell>
- );
- })}
- </TableRow>
- );
- })
- ) : (
- <TableStatus>
- <EmptyStateWarning>
- <p>{t('No spans found')}</p>
- </EmptyStateWarning>
- </TableStatus>
- )}
- </TableBody>
- </Table>
- <Pagination pageLinks={result.pageLinks} />
- </Fragment>
- );
- }
- const TopResultsIndicator = styled('div')<{index: number}>`
- position: absolute;
- left: -1px;
- width: 9px;
- height: 16px;
- border-radius: 0 3px 3px 0;
- background-color: ${p => {
- return CHART_PALETTE[TOP_EVENTS_LIMIT - 1]![p.index];
- }};
- `;
- const StyledLink = styled(Link)`
- display: flex;
- `;
|