123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- import {useState} from 'react';
- import {Theme, useTheme} from '@emotion/react';
- import styled from '@emotion/styled';
- import keyBy from 'lodash/keyBy';
- import moment from 'moment';
- import Badge from 'sentry/components/badge';
- import {Button} from 'sentry/components/button';
- import ButtonBar from 'sentry/components/buttonBar';
- import MarkLine from 'sentry/components/charts/components/markLine';
- import {IconChevron} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {Series, SeriesDataUnit} from 'sentry/types/echarts';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import Chart from 'sentry/views/starfish/components/chart';
- import Detail from 'sentry/views/starfish/components/detailPanel';
- import ProfileView from 'sentry/views/starfish/modules/databaseModule/panel/profileView';
- import QueryTransactionTable, {
- PanelSort,
- } from 'sentry/views/starfish/modules/databaseModule/panel/queryTransactionTable';
- import SimilarQueryView from 'sentry/views/starfish/modules/databaseModule/panel/similarQueryView';
- import {
- useQueryPanelEventCount,
- useQueryPanelGraph,
- useQueryPanelSparklines,
- useQueryPanelTable,
- useQueryTransactionByTPMAndP75,
- } from 'sentry/views/starfish/modules/databaseModule/queries';
- import {queryToSeries} from 'sentry/views/starfish/modules/databaseModule/utils';
- import {getDateFilters} from 'sentry/views/starfish/utils/dates';
- import {zeroFillSeries} from 'sentry/views/starfish/utils/zeroFillSeries';
- import {DataRow, MainTableSort} from '../databaseTableView';
- const INTERVAL = 12;
- const SPARKLINE_INTERVAL = 24;
- type DbQueryDetailProps = {
- isDataLoading: boolean;
- mainTableSort: MainTableSort;
- onRowChange: (row: DataRow | undefined) => void;
- row: DataRow;
- nextRow?: DataRow;
- prevRow?: DataRow;
- transaction?: string;
- };
- export type TransactionListDataRow = {
- count: number;
- frequency: number;
- group_id: string;
- p75: number;
- transaction: string;
- uniqueEvents: number;
- };
- export default function QueryDetail({
- row,
- nextRow,
- prevRow,
- isDataLoading,
- onClose,
- onRowChange,
- mainTableSort,
- transaction,
- }: Partial<DbQueryDetailProps> & {
- isDataLoading: boolean;
- mainTableSort: MainTableSort;
- onClose: () => void;
- onRowChange: (row: DataRow) => void;
- }) {
- return (
- <Detail detailKey={row?.description} onClose={onClose}>
- {row && (
- <QueryDetailBody
- mainTableSort={mainTableSort}
- onRowChange={onRowChange}
- isDataLoading={isDataLoading}
- row={row}
- nextRow={nextRow}
- prevRow={prevRow}
- transaction={transaction}
- />
- )}
- </Detail>
- );
- }
- function QueryDetailBody({
- row,
- nextRow,
- prevRow,
- onRowChange,
- transaction,
- isDataLoading: isRowLoading,
- }: DbQueryDetailProps) {
- const theme = useTheme();
- const pageFilter = usePageFilters();
- const {startTime, endTime} = getDateFilters(pageFilter);
- const isNew = row.newish === 1;
- const isOld = row.retired === 1;
- const [sort, setSort] = useState<PanelSort>({
- direction: undefined,
- sortHeader: undefined,
- });
- const {isLoading, data: graphData} = useQueryPanelGraph(row, INTERVAL);
- const {isLoading: isTableLoading, data: tableData} = useQueryPanelTable(
- row,
- sort.sortHeader?.key,
- sort.direction,
- transaction
- );
- const {isLoading: isSparklinesLoading, data: sparklineData} = useQueryPanelSparklines(
- row,
- sort.sortHeader?.key,
- sort.direction,
- SPARKLINE_INTERVAL,
- transaction
- );
- const {isLoading: isP75GraphLoading, data: transactionGraphData} =
- useQueryTransactionByTPMAndP75(
- tableData.map(d => d.transaction).splice(0, 5),
- SPARKLINE_INTERVAL
- );
- const {isLoading: isEventCountLoading, data: eventCountData} =
- useQueryPanelEventCount(row);
- const isDataLoading =
- isLoading ||
- isTableLoading ||
- isEventCountLoading ||
- isRowLoading ||
- isP75GraphLoading ||
- isSparklinesLoading;
- const eventCountMap = keyBy(eventCountData, 'transaction');
- const mergedTableData: TransactionListDataRow[] = tableData.map(data => {
- const tableTransaction = data.transaction;
- const eventData = eventCountMap[tableTransaction];
- if (eventData?.uniqueEvents) {
- const frequency = data.count / eventData.uniqueEvents;
- return {...data, frequency, ...eventData} as TransactionListDataRow;
- }
- return data as TransactionListDataRow;
- });
- const [countSeries, p75Series] = throughputQueryToChartData(
- graphData,
- startTime,
- endTime
- );
- const spmTransactionSeries = queryToSeries(
- sparklineData,
- 'transaction',
- 'spm',
- startTime,
- endTime,
- SPARKLINE_INTERVAL
- );
- const spanp50TransactionSeries = queryToSeries(
- sparklineData,
- 'transaction',
- 'p50',
- startTime,
- endTime,
- SPARKLINE_INTERVAL
- );
- const tpmTransactionSeries = queryToSeries(
- transactionGraphData,
- 'group',
- 'epm()',
- startTime,
- endTime,
- SPARKLINE_INTERVAL
- );
- const p50TransactionSeries = queryToSeries(
- transactionGraphData,
- 'group',
- 'p50(transaction.duration)',
- startTime,
- endTime,
- SPARKLINE_INTERVAL
- );
- const markLine =
- spmTransactionSeries?.[0]?.data && (isNew || isOld)
- ? generateMarkLine(
- isNew ? 'First Seen' : 'Last Seen',
- isNew ? row.firstSeen : row.lastSeen,
- spmTransactionSeries[0].data,
- theme
- )
- : undefined;
- return (
- <div>
- <FlexRowContainer>
- <FlexRowItem>
- <h2>{t('Query Detail')}</h2>
- </FlexRowItem>
- <FlexRowItem>
- <SimplePagination
- disableLeft={!prevRow}
- disableRight={!nextRow}
- onLeftClick={() => onRowChange(prevRow)}
- onRightClick={() => onRowChange(nextRow)}
- />
- </FlexRowItem>
- </FlexRowContainer>
- <FlexRowContainer>
- <FlexRowItem>
- <SubHeader>
- {t('First Seen')}
- {row.newish === 1 && <Badge type="new" text="new" />}
- </SubHeader>
- <SubSubHeader>{row.firstSeen}</SubSubHeader>
- </FlexRowItem>
- <FlexRowItem>
- <SubHeader>
- {t('Last Seen')}
- {row.retired === 1 && <Badge type="warning" text="old" />}
- </SubHeader>
- <SubSubHeader>{row.lastSeen}</SubSubHeader>
- </FlexRowItem>
- <FlexRowItem>
- <SubHeader>{t('Total Time')}</SubHeader>
- <SubSubHeader>{row.total_time.toFixed(2)}ms</SubSubHeader>
- </FlexRowItem>
- </FlexRowContainer>
- <SubHeader>{t('Query Description')}</SubHeader>
- <FormattedCode>{highlightSql(row.formatted_desc, row)}</FormattedCode>
- <FlexRowContainer>
- <FlexRowItem>
- <SubHeader>{t('Throughput')}</SubHeader>
- <SubSubHeader>{row.epm.toFixed(3)}</SubSubHeader>
- <Chart
- statsPeriod="24h"
- height={140}
- data={[countSeries]}
- start=""
- end=""
- loading={isDataLoading}
- utc={false}
- stacked
- isLineChart
- disableXAxis
- hideYAxisSplitLine
- />
- </FlexRowItem>
- <FlexRowItem>
- <SubHeader>{t('Duration (P75)')}</SubHeader>
- <SubSubHeader>{row.p75.toFixed(3)}ms</SubSubHeader>
- <Chart
- statsPeriod="24h"
- height={140}
- data={[p75Series]}
- start=""
- end=""
- loading={isDataLoading}
- utc={false}
- chartColors={[theme.charts.getColorPalette(4)[3]]}
- stacked
- isLineChart
- disableXAxis
- hideYAxisSplitLine
- />
- </FlexRowItem>
- </FlexRowContainer>
- <QueryTransactionTable
- isDataLoading={isDataLoading}
- onClickSort={s => setSort(s)}
- row={row}
- sort={sort}
- tableData={mergedTableData}
- spmData={spmTransactionSeries}
- tpmData={tpmTransactionSeries}
- spanP50Data={spanp50TransactionSeries}
- txnP50Data={p50TransactionSeries}
- markLine={markLine}
- />
- <FlexRowContainer>
- <FlexRowItem>
- <SubHeader>{t('Example Profile')}</SubHeader>
- <ProfileView
- spanHash={row.group_id}
- transactionNames={tableData.map(d => d.transaction)}
- />
- </FlexRowItem>
- </FlexRowContainer>
- <FlexRowContainer>
- <FlexRowItem>
- <SubHeader>{t('Similar Queries')}</SubHeader>
- <SimilarQueryView mainTableRow={row} />
- </FlexRowItem>
- </FlexRowContainer>
- </div>
- );
- }
- type SimplePaginationProps = {
- disableLeft?: boolean;
- disableRight?: boolean;
- onLeftClick?: () => void;
- onRightClick?: () => void;
- };
- function SimplePagination(props: SimplePaginationProps) {
- return (
- <ButtonBar merged>
- <Button
- icon={<IconChevron direction="left" size="sm" />}
- aria-label={t('Previous')}
- disabled={props.disableLeft}
- onClick={props.onLeftClick}
- />
- <Button
- icon={<IconChevron direction="right" size="sm" />}
- aria-label={t('Next')}
- onClick={props.onRightClick}
- disabled={props.disableRight}
- />
- </ButtonBar>
- );
- }
- export const highlightSql = (description: string, queryDetail: DataRow) => {
- let acc = '';
- return description.split('').map((token, i) => {
- acc += token;
- let final: string | React.ReactElement | null = null;
- if (acc === queryDetail.action) {
- final = <Operation key={i}>{queryDetail.action} </Operation>;
- } else if (acc === queryDetail.domain) {
- final = <Domain key={i}>{queryDetail.domain} </Domain>;
- } else if (
- ['FROM', 'INNER', 'JOIN', 'WHERE', 'ON', 'AND', 'NOT', 'NULL', 'IS'].includes(acc)
- ) {
- final = <Keyword key={i}>{acc}</Keyword>;
- } else if (['(', ')'].includes(acc)) {
- final = <Bracket key={i}>{acc}</Bracket>;
- } else if (token === ' ' || token === '\n' || description[i + 1] === ')') {
- final = acc;
- } else if (i === description.length - 1) {
- final = acc;
- }
- if (final) {
- acc = '';
- const result = final;
- final = null;
- return result;
- }
- return null;
- });
- };
- function generateMarkLine(
- title: string,
- position: string,
- data: SeriesDataUnit[],
- theme: Theme
- ) {
- const index = data.findIndex(item => {
- return (
- Math.abs(moment.duration(moment(item.name).diff(moment(position))).asSeconds()) <
- 86400
- );
- });
- return {
- seriesName: title,
- type: 'line',
- color: theme.blue300,
- data: [],
- xAxisIndex: 0,
- yAxisIndex: 0,
- markLine: MarkLine({
- silent: true,
- animation: false,
- lineStyle: {color: theme.blue300, type: 'dotted'},
- data: [
- {
- xAxis: index,
- },
- ],
- label: {
- show: false,
- },
- }),
- };
- }
- const throughputQueryToChartData = (
- data: any,
- startTime: moment.Moment,
- endTime: moment.Moment
- ): Series[] => {
- const countSeries: Series = {seriesName: 'count()', data: [] as any[]};
- const p75Series: Series = {seriesName: 'p75()', data: [] as any[]};
- data.forEach(({count, p75, interval}: any) => {
- countSeries.data.push({value: count, name: interval});
- p75Series.data.push({value: p75, name: interval});
- });
- return [
- zeroFillSeries(countSeries, moment.duration(INTERVAL, 'hours'), startTime, endTime),
- zeroFillSeries(p75Series, moment.duration(INTERVAL, 'hours'), startTime, endTime),
- ];
- };
- const SubHeader = styled('h3')`
- color: ${p => p.theme.gray300};
- font-size: ${p => p.theme.fontSizeLarge};
- `;
- const SubSubHeader = styled('h4')`
- margin: 0;
- font-weight: normal;
- `;
- const FlexRowContainer = styled('div')`
- display: flex;
- & > div:last-child {
- padding-right: ${space(1)};
- }
- padding-bottom: ${space(2)};
- `;
- const FlexRowItem = styled('div')`
- padding-right: ${space(4)};
- flex: 1;
- `;
- const FormattedCode = styled('div')`
- padding: ${space(1)};
- margin-bottom: ${space(3)};
- background: ${p => p.theme.backgroundSecondary};
- border-radius: ${p => p.theme.borderRadius};
- overflow-x: auto;
- white-space: pre;
- `;
- const Operation = styled('b')`
- color: ${p => p.theme.blue400};
- `;
- const Domain = styled('b')`
- color: ${p => p.theme.green400};
- margin-right: -${space(0.5)};
- `;
- const Keyword = styled('b')`
- color: ${p => p.theme.yellow400};
- `;
- const Bracket = styled('b')`
- color: ${p => p.theme.pink400};
- `;
|