123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- import * as React from 'react';
- import * as ReactRouter from 'react-router';
- import {Location, LocationDescriptorObject} from 'history';
- import {addSuccessMessage} from 'app/actionCreators/indicator';
- import {openModal} from 'app/actionCreators/modal';
- import {fetchLegacyKeyTransactionsCount} from 'app/actionCreators/performance';
- import GuideAnchor from 'app/components/assistant/guideAnchor';
- import GridEditable, {COL_WIDTH_UNDEFINED, GridColumn} from 'app/components/gridEditable';
- import SortLink from 'app/components/gridEditable/sortLink';
- import Link from 'app/components/links/link';
- import Pagination from 'app/components/pagination';
- import Tooltip from 'app/components/tooltip';
- import {IconStar} from 'app/icons';
- import {tct} from 'app/locale';
- import {Organization, Project} from 'app/types';
- import {defined} from 'app/utils';
- import {trackAnalyticsEvent} from 'app/utils/analytics';
- import DiscoverQuery, {TableData, TableDataRow} from 'app/utils/discover/discoverQuery';
- import EventView, {EventData, isFieldSortable} from 'app/utils/discover/eventView';
- import {getFieldRenderer} from 'app/utils/discover/fieldRenderers';
- import {fieldAlignment, getAggregateAlias} from 'app/utils/discover/fields';
- import {tokenizeSearch} from 'app/utils/tokenizeSearch';
- import CellAction, {Actions, updateQuery} from 'app/views/eventsV2/table/cellAction';
- import {TableColumn} from 'app/views/eventsV2/table/types';
- import TransactionThresholdModal, {
- modalCss,
- TransactionThresholdMetric,
- } from './transactionSummary/transactionThresholdModal';
- import {transactionSummaryRouteWithQuery} from './transactionSummary/utils';
- import {COLUMN_TITLES} from './data';
- export function getProjectID(
- eventData: EventData,
- projects: Project[]
- ): string | undefined {
- const projectSlug = (eventData?.project as string) || undefined;
- if (typeof projectSlug === undefined) {
- return undefined;
- }
- const project = projects.find(currentProject => currentProject.slug === projectSlug);
- if (!project) {
- return undefined;
- }
- return project.id;
- }
- type Props = {
- eventView: EventView;
- organization: Organization;
- location: Location;
- setError: (msg: string | undefined) => void;
- summaryConditions: string;
- projects: Project[];
- columnTitles?: string[];
- };
- type State = {
- widths: number[];
- keyedTransactions: number | null;
- transaction: string | undefined;
- transactionThreshold: number | undefined;
- transactionThresholdMetric: TransactionThresholdMetric | undefined;
- };
- class Table extends React.Component<Props, State> {
- state: State = {
- widths: [],
- keyedTransactions: null,
- transaction: undefined,
- transactionThreshold: undefined,
- transactionThresholdMetric: undefined,
- };
- componentDidMount() {
- this.fetchKeyTransactionCount();
- }
- async fetchKeyTransactionCount() {
- const {organization} = this.props;
- try {
- const count = await fetchLegacyKeyTransactionsCount(organization.slug);
- this.setState({keyedTransactions: count});
- } catch (error) {
- this.setState({keyedTransactions: null});
- }
- }
- handleCellAction = (column: TableColumn<keyof TableDataRow>, dataRow: TableDataRow) => {
- return (action: Actions, value: React.ReactText) => {
- const {eventView, location, organization, projects} = this.props;
- trackAnalyticsEvent({
- eventKey: 'performance_views.overview.cellaction',
- eventName: 'Performance Views: Cell Action Clicked',
- organization_id: parseInt(organization.id, 10),
- action,
- });
- if (action === Actions.EDIT_THRESHOLD) {
- const project_threshold = dataRow.project_threshold_config;
- const transactionName = dataRow.transaction as string;
- const projectID = getProjectID(dataRow, projects);
- openModal(
- modalProps => (
- <TransactionThresholdModal
- {...modalProps}
- organization={organization}
- transactionName={transactionName}
- eventView={eventView}
- project={projectID}
- transactionThreshold={project_threshold[1]}
- transactionThresholdMetric={project_threshold[0]}
- onApply={(threshold, metric) => {
- if (
- threshold !== project_threshold[1] ||
- metric !== project_threshold[0]
- ) {
- this.setState({
- transaction: transactionName,
- transactionThreshold: threshold,
- transactionThresholdMetric: metric,
- });
- }
- addSuccessMessage(
- tct('[transactionName] updated successfully', {
- transactionName,
- })
- );
- }}
- />
- ),
- {modalCss, backdrop: 'static'}
- );
- return;
- }
- const searchConditions = tokenizeSearch(eventView.query);
- // remove any event.type queries since it is implied to apply to only transactions
- searchConditions.removeTag('event.type');
- updateQuery(searchConditions, action, column, value);
- ReactRouter.browserHistory.push({
- pathname: location.pathname,
- query: {
- ...location.query,
- cursor: undefined,
- query: searchConditions.formatString(),
- },
- });
- };
- };
- renderBodyCell(
- tableData: TableData | null,
- column: TableColumn<keyof TableDataRow>,
- dataRow: TableDataRow
- ): React.ReactNode {
- const {eventView, organization, projects, location} = this.props;
- if (!tableData || !tableData.meta) {
- return dataRow[column.key];
- }
- const tableMeta = tableData.meta;
- const field = String(column.key);
- const fieldRenderer = getFieldRenderer(field, tableMeta);
- const rendered = fieldRenderer(dataRow, {organization, location});
- const allowActions = [
- Actions.ADD,
- Actions.EXCLUDE,
- Actions.SHOW_GREATER_THAN,
- Actions.SHOW_LESS_THAN,
- ];
- if (organization.features.includes('project-transaction-threshold-override')) {
- allowActions.push(Actions.EDIT_THRESHOLD);
- }
- if (field === 'transaction') {
- const projectID = getProjectID(dataRow, projects);
- const summaryView = eventView.clone();
- if (dataRow['http.method']) {
- summaryView.additionalConditions.setTagValues('http.method', [
- dataRow['http.method'] as string,
- ]);
- }
- summaryView.query = summaryView.getQueryWithAdditionalConditions();
- const target = transactionSummaryRouteWithQuery({
- orgSlug: organization.slug,
- transaction: String(dataRow.transaction) || '',
- query: summaryView.generateQueryStringObject(),
- projectID,
- });
- return (
- <CellAction
- column={column}
- dataRow={dataRow}
- handleCellAction={this.handleCellAction(column, dataRow)}
- allowActions={allowActions}
- >
- <Link to={target} onClick={this.handleSummaryClick}>
- {rendered}
- </Link>
- </CellAction>
- );
- }
- if (field.startsWith('key_transaction')) {
- // don't display per cell actions for key_transaction
- return rendered;
- }
- if (field.startsWith('team_key_transaction')) {
- // don't display per cell actions for team_key_transaction
- return rendered;
- }
- const fieldName = getAggregateAlias(field);
- const value = dataRow[fieldName];
- if (tableMeta[fieldName] === 'integer' && defined(value) && value > 999) {
- return (
- <Tooltip
- title={value.toLocaleString()}
- containerDisplayMode="block"
- position="right"
- >
- <CellAction
- column={column}
- dataRow={dataRow}
- handleCellAction={this.handleCellAction(column, dataRow)}
- allowActions={allowActions}
- >
- {rendered}
- </CellAction>
- </Tooltip>
- );
- }
- return (
- <CellAction
- column={column}
- dataRow={dataRow}
- handleCellAction={this.handleCellAction(column, dataRow)}
- allowActions={allowActions}
- >
- {rendered}
- </CellAction>
- );
- }
- renderBodyCellWithData = (tableData: TableData | null) => {
- return (
- column: TableColumn<keyof TableDataRow>,
- dataRow: TableDataRow
- ): React.ReactNode => this.renderBodyCell(tableData, column, dataRow);
- };
- onSortClick(currentSortKind?: string, currentSortField?: string) {
- const {organization} = this.props;
- trackAnalyticsEvent({
- eventKey: 'performance_views.landingv2.transactions.sort',
- eventName: 'Performance Views: Landing Transactions Sorted',
- organization_id: parseInt(organization.id, 10),
- field: currentSortField,
- direction: currentSortKind,
- });
- }
- renderHeadCell(
- tableMeta: TableData['meta'],
- column: TableColumn<keyof TableDataRow>,
- title: React.ReactNode
- ): React.ReactNode {
- const {eventView, location, organization} = this.props;
- const align = fieldAlignment(column.name, column.type, tableMeta);
- const field = {field: column.name, width: column.width};
- function generateSortLink(): LocationDescriptorObject | undefined {
- if (!tableMeta) {
- return undefined;
- }
- const nextEventView = eventView.sortOnField(field, tableMeta);
- const queryStringObject = nextEventView.generateQueryStringObject();
- return {
- ...location,
- query: {...location.query, sort: queryStringObject.sort},
- };
- }
- const currentSort = eventView.sortForField(field, tableMeta);
- const canSort = isFieldSortable(field, tableMeta);
- const currentSortKind = currentSort ? currentSort.kind : undefined;
- const currentSortField = currentSort ? currentSort.field : undefined;
- const sortLink = (
- <SortLink
- align={align}
- title={title || field.field}
- direction={currentSortKind}
- canSort={canSort}
- generateSortLink={generateSortLink}
- onClick={() => this.onSortClick(currentSortKind, currentSortField)}
- />
- );
- if (field.field.startsWith('user_misery')) {
- return (
- <GuideAnchor
- target="project_transaction_threshold"
- position="top"
- disabled={!organization.features.includes('project-transaction-threshold')}
- >
- {sortLink}
- </GuideAnchor>
- );
- }
- return sortLink;
- }
- renderHeadCellWithMeta = (tableMeta: TableData['meta']) => {
- const columnTitles = this.props.columnTitles ?? COLUMN_TITLES;
- return (column: TableColumn<keyof TableDataRow>, index: number): React.ReactNode =>
- this.renderHeadCell(tableMeta, column, columnTitles[index]);
- };
- renderPrependCellWithData = (tableData: TableData | null) => {
- const {eventView} = this.props;
- const {keyedTransactions} = this.state;
- const keyTransactionColumn = eventView
- .getColumns()
- .find((col: TableColumn<React.ReactText>) => col.name === 'key_transaction');
- const teamKeyTransactionColumn = eventView
- .getColumns()
- .find((col: TableColumn<React.ReactText>) => col.name === 'team_key_transaction');
- return (isHeader: boolean, dataRow?: any) => {
- if (keyTransactionColumn) {
- if (isHeader) {
- const star = (
- <IconStar
- key="keyTransaction"
- color="yellow300"
- isSolid
- data-test-id="key-transaction-header"
- />
- );
- return [this.renderHeadCell(tableData?.meta, keyTransactionColumn, star)];
- } else {
- return [this.renderBodyCell(tableData, keyTransactionColumn, dataRow)];
- }
- } else if (teamKeyTransactionColumn) {
- if (isHeader) {
- const star = (
- <GuideAnchor
- target="team_key_transaction_header"
- position="top"
- disabled={keyedTransactions === null} // wait for the legacy counts to load
- >
- <GuideAnchor
- target="team_key_transaction_existing"
- position="top"
- disabled={!keyedTransactions}
- >
- <IconStar
- key="keyTransaction"
- color="yellow300"
- isSolid
- data-test-id="team-key-transaction-header"
- />
- </GuideAnchor>
- </GuideAnchor>
- );
- return [this.renderHeadCell(tableData?.meta, teamKeyTransactionColumn, star)];
- } else {
- return [this.renderBodyCell(tableData, teamKeyTransactionColumn, dataRow)];
- }
- }
- return [];
- };
- };
- handleSummaryClick = () => {
- const {organization} = this.props;
- trackAnalyticsEvent({
- eventKey: 'performance_views.overview.navigate.summary',
- eventName: 'Performance Views: Overview view summary',
- organization_id: parseInt(organization.id, 10),
- });
- };
- handleResizeColumn = (columnIndex: number, nextColumn: GridColumn) => {
- const widths: number[] = [...this.state.widths];
- widths[columnIndex] = nextColumn.width
- ? Number(nextColumn.width)
- : COL_WIDTH_UNDEFINED;
- this.setState({widths});
- };
- getSortedEventView() {
- const {eventView} = this.props;
- return eventView.withSorts([
- {
- field: 'team_key_transaction',
- kind: 'desc',
- },
- ...eventView.sorts,
- ]);
- }
- render() {
- const {eventView, organization, location, setError} = this.props;
- const {widths, transaction, transactionThreshold, transactionThresholdMetric} =
- this.state;
- const columnOrder = eventView
- .getColumns()
- // remove key_transactions from the column order as we'll be rendering it
- // via a prepended column
- .filter(
- (col: TableColumn<React.ReactText>) =>
- col.name !== 'key_transaction' &&
- col.name !== 'team_key_transaction' &&
- !col.name.startsWith('count_miserable') &&
- col.name !== 'project_threshold_config'
- )
- .map((col: TableColumn<React.ReactText>, i: number) => {
- if (typeof widths[i] === 'number') {
- return {...col, width: widths[i]};
- }
- return col;
- });
- const sortedEventView = this.getSortedEventView();
- const columnSortBy = sortedEventView.getSorts();
- const prependColumnWidths = ['max-content'];
- return (
- <div>
- <DiscoverQuery
- eventView={sortedEventView}
- orgSlug={organization.slug}
- location={location}
- setError={setError}
- referrer="api.performance.landing-table"
- transactionName={transaction}
- transactionThreshold={transactionThreshold}
- transactionThresholdMetric={transactionThresholdMetric}
- >
- {({pageLinks, isLoading, tableData}) => (
- <React.Fragment>
- <GridEditable
- isLoading={isLoading}
- data={tableData ? tableData.data : []}
- columnOrder={columnOrder}
- columnSortBy={columnSortBy}
- grid={{
- onResizeColumn: this.handleResizeColumn,
- renderHeadCell: this.renderHeadCellWithMeta(tableData?.meta) as any,
- renderBodyCell: this.renderBodyCellWithData(tableData) as any,
- renderPrependColumns: this.renderPrependCellWithData(tableData) as any,
- prependColumnWidths,
- }}
- location={location}
- />
- <Pagination pageLinks={pageLinks} />
- </React.Fragment>
- )}
- </DiscoverQuery>
- </div>
- );
- }
- }
- export default Table;
|