123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- import {CSSProperties, useState} from 'react';
- import {useTheme} from '@emotion/react';
- import styled from '@emotion/styled';
- import {Location} from 'history';
- import Badge from 'sentry/components/badge';
- import GridEditable, {GridColumnHeader} from 'sentry/components/gridEditable';
- import Link from 'sentry/components/links/link';
- import {CHART_PALETTE} from 'sentry/constants/chartPalette';
- import {space} from 'sentry/styles/space';
- import {Series} from 'sentry/types/echarts';
- import {getDuration} from 'sentry/utils/formatters';
- import {SortableHeader} from 'sentry/views/starfish/components/sortableHeader';
- import Sparkline, {
- generateHorizontalLine,
- } from 'sentry/views/starfish/components/sparkline';
- type Props = {
- isDataLoading: boolean;
- location: Location;
- onSelect: (row: DataRow, rowIndex: number) => void;
- columns?: any;
- data?: DataRow[];
- noP95?: boolean;
- onSortChange?: ({direction, sortHeader}: MainTableSort) => void;
- p95asNumber?: boolean;
- selectedRow?: DataRow;
- };
- type Sort<T> = {
- direction: 'desc' | 'asc' | undefined;
- sortHeader: T | undefined;
- };
- export type DataRow = {
- action: string;
- count: number;
- data_keys: Array<string>;
- data_values: Array<string>;
- description: string;
- domain: string;
- epm: number;
- firstSeen: string;
- formatted_desc: string;
- group_id: string;
- lastSeen: string;
- newish: number;
- p50: number;
- p50_trend: Series;
- p75: number;
- p95: number;
- p95_trend: Series;
- retired: number;
- throughput: Series;
- total_time: number;
- transactions: number;
- };
- export type Keys =
- | 'description'
- | 'domain'
- | 'p50'
- | 'p95'
- | 'epm'
- | 'p95'
- | 'transactions'
- | 'total_time';
- export type TableColumnHeader = GridColumnHeader<Keys>;
- export type MainTableSort = Sort<TableColumnHeader>;
- export function similarity(value: string, other: string): number {
- // If they're identical we don't care
- if (value === other || other === undefined || value === undefined) {
- return -1;
- }
- const short_words = value.length < other.length ? value.split(' ') : other.split(' ');
- const long_words = value.length > other.length ? value.split(' ') : other.split(' ');
- const total = long_words.length;
- let count = 0;
- while (long_words.length > 0) {
- const word = long_words.pop();
- if (word && short_words.includes(word)) {
- count += 1;
- short_words.splice(short_words.indexOf(word), 1);
- }
- }
- return count / total;
- }
- function renderBadge(row, selectedRow) {
- const similar = similarity(selectedRow?.description, row.description) > 0.8;
- const newish = row?.newish === 1;
- const retired = row?.retired === 1;
- let response: React.ReactNode | null = null;
- if (similar) {
- if (newish && selectedRow.newish !== 1) {
- response = (
- <span>
- <StyledBadge type="new" text="new" />
- <StyledBadge type="alpha" text="similar" />
- </span>
- );
- } else if (retired && selectedRow.retired !== 1) {
- response = (
- <span>
- <StyledBadge type="warning" text="old" />
- <StyledBadge type="alpha" text="similar" />
- </span>
- );
- } else {
- response = <StyledBadge type="alpha" text="similar" />;
- }
- } else if (newish) {
- response = <StyledBadge type="new" text="new" />;
- } else if (retired) {
- response = <StyledBadge type="warning" text="old" />;
- }
- return response;
- }
- export default function DatabaseTableView({
- location,
- data,
- onSelect,
- p95asNumber,
- noP95,
- onSortChange,
- selectedRow,
- isDataLoading,
- columns,
- }: Props) {
- const [sort, setSort] = useState<{
- direction: 'desc' | 'asc' | undefined;
- sortHeader: TableColumnHeader | undefined;
- }>({direction: undefined, sortHeader: undefined});
- const theme = useTheme();
- let COLUMN_ORDER: TableColumnHeader[] = [
- {
- key: 'description',
- name: 'Query',
- width: 550,
- },
- {
- key: 'domain',
- name: 'Table',
- width: 175,
- },
- {
- key: 'epm',
- name: 'Throughput (SPM)',
- width: 175,
- },
- {
- key: 'p50',
- name: 'P50',
- width: 175,
- },
- {
- key: 'p95',
- name: 'P95',
- width: !p95asNumber ? 175 : undefined,
- },
- {
- key: 'transactions',
- name: 'transactions',
- },
- {
- key: 'total_time',
- name: 'Total Time',
- },
- ];
- if (noP95) {
- COLUMN_ORDER = COLUMN_ORDER.filter(col => col.key !== 'p95');
- }
- function onSortClick(col: TableColumnHeader) {
- let direction: 'desc' | 'asc' | undefined = undefined;
- if (!sort.direction || col.key !== sort.sortHeader?.key) {
- direction = 'desc';
- } else if (sort.direction === 'desc') {
- direction = 'asc';
- }
- if (onSortChange) {
- setSort({direction, sortHeader: col});
- onSortChange({direction, sortHeader: col});
- }
- }
- function renderHeadCell(col: TableColumnHeader): React.ReactNode {
- const sortableKeys: Keys[] = [
- 'p50',
- 'p95',
- 'epm',
- 'total_time',
- 'domain',
- 'transactions',
- ];
- if (sortableKeys.includes(col.key)) {
- const isBeingSorted = col.key === sort.sortHeader?.key;
- const direction = isBeingSorted ? sort.direction : undefined;
- return (
- <SortableHeader
- onClick={() => onSortClick(col)}
- direction={direction}
- title={col.name}
- />
- );
- }
- return <span>{col.name}</span>;
- }
- function renderBodyCell(
- column: TableColumnHeader,
- row: DataRow,
- rowIndex: number
- ): React.ReactNode {
- const {key} = column;
- const isSelectedRow = selectedRow?.group_id === row.group_id;
- const rowStyle: CSSProperties | undefined = isSelectedRow
- ? {fontWeight: 'bold'}
- : undefined;
- if (key === 'description') {
- const value = row.description;
- return (
- <Link onClick={() => onSelect(row, rowIndex)} to="" style={rowStyle}>
- {value.substring(0, 30)}
- {value.length > 30 ? '...' : ''}
- {value.length > 30 ? value.substring(value.length - 30) : ''}
- {renderBadge(row, selectedRow)}
- </Link>
- );
- }
- if (key === 'epm') {
- const horizontalLine = generateHorizontalLine('', row.epm, theme);
- return (
- <GraphRow>
- <span style={rowStyle}>{row.epm.toFixed(2)}</span>
- <Graphline>
- <Sparkline
- color={CHART_PALETTE[3][0]}
- series={row.throughput}
- markLine={horizontalLine}
- width={column.width ? column.width - column.width / 5 - 50 : undefined}
- />
- </Graphline>
- </GraphRow>
- );
- }
- if (key === 'p50') {
- const horizontalLine = generateHorizontalLine('', row.p50, theme);
- return (
- <GraphRow>
- <span style={rowStyle}>{getDuration(row.p50 / 1000, 2, true)}</span>
- <Graphline>
- <Sparkline
- color={CHART_PALETTE[3][1]}
- series={row.p50_trend}
- markLine={horizontalLine}
- width={column.width ? column.width - column.width / 5 - 50 : undefined}
- />
- </Graphline>
- </GraphRow>
- );
- }
- if (key === 'p95') {
- const horizontalLine = generateHorizontalLine('', row.p95, theme);
- return (
- <GraphRow>
- <span style={rowStyle}>{getDuration(row.p95 / 1000, 2, true)}</span>
- <Graphline>
- {!p95asNumber && (
- <Sparkline
- color={CHART_PALETTE[3][2]}
- series={row.p95_trend}
- markLine={horizontalLine}
- width={column.width ? column.width - column.width / 5 - 50 : undefined}
- />
- )}
- </Graphline>
- </GraphRow>
- );
- }
- if (key === 'total_time') {
- return <span style={rowStyle}>{getDuration(row.total_time / 1000, 2, true)}</span>;
- }
- return <span style={rowStyle}>{row[key]}</span>;
- }
- return (
- <GridEditable
- isLoading={isDataLoading}
- data={data as any}
- columnOrder={columns ?? COLUMN_ORDER}
- columnSortBy={[]}
- grid={{
- renderHeadCell,
- renderBodyCell,
- }}
- location={location}
- />
- );
- }
- const StyledBadge = styled(Badge)`
- margin-left: ${space(0.75)};
- `;
- const Graphline = styled('div')`
- margin-left: auto;
- `;
- const GraphRow = styled('div')`
- display: inline-flex;
- `;
|