123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- import {useState} from 'react';
- import {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 TimeSince from 'sentry/components/timeSince';
- import Version from 'sentry/components/version';
- import VersionHoverCard from 'sentry/components/versionHoverCard';
- import {IconChevron} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {Series} from 'sentry/types/echarts';
- import useOrganization from 'sentry/utils/useOrganization';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import Chart from 'sentry/views/starfish/components/chart';
- import Detail from 'sentry/views/starfish/components/detailPanel';
- import {FormattedCode} from 'sentry/views/starfish/components/formattedCode';
- import {generateMarkLine} from 'sentry/views/starfish/components/sparkline';
- 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 {
- useQueryExampleTransaction,
- useQueryGetEvent,
- useQueryPanelEventCount,
- useQueryPanelGraph,
- useQueryPanelSparklines,
- useQueryPanelTable,
- useQueryTransactionByTPMAndDuration,
- } 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;
- example: string;
- 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 organization = useOrganization();
- 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} =
- useQueryTransactionByTPMAndDuration(
- tableData.map(d => d.transaction).splice(0, 5),
- SPARKLINE_INTERVAL
- );
- const {isLoading: isEventCountLoading, data: eventCountData} =
- useQueryPanelEventCount(row);
- const {isLoading: isExampleLoading, data: exampleTransaction} =
- useQueryExampleTransaction(row);
- const {isLoading: isFirstExampleLoading, data: firstSeenExample} = useQueryGetEvent(
- exampleTransaction?.[0]?.first
- );
- const {isLoading: isLastExampleLoading, data: lastSeenExample} = useQueryGetEvent(
- exampleTransaction?.[0]?.latest
- );
- const isDataLoading =
- isLoading ||
- isTableLoading ||
- isEventCountLoading ||
- isRowLoading ||
- isP75GraphLoading ||
- isExampleLoading ||
- isFirstExampleLoading ||
- isLastExampleLoading ||
- 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, p50Series, p95Series] = 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>
- {Math.abs(moment(row.firstSeen).diff(startTime, 'minutes')) < 720 ? (
- <SubSubHeader>
- More than <TimeSince date={row.firstSeen} />{' '}
- </SubSubHeader>
- ) : (
- <span>
- <SubSubHeader>
- <TimeSince date={row.firstSeen} />{' '}
- </SubSubHeader>
- {firstSeenExample?.release && (
- <VersionHoverCard
- organization={organization}
- projectSlug="sentry"
- releaseVersion={firstSeenExample.release.version}
- showUnderline
- underlineColor="linkUnderline"
- >
- <Version version={String(firstSeenExample.release.version)} truncate />
- </VersionHoverCard>
- )}
- </span>
- )}
- </FlexRowItem>
- <FlexRowItem>
- <SubHeader>
- {t('Last Seen')}
- {row.retired === 1 && <Badge type="warning" text="old" />}
- </SubHeader>
- <SubSubHeader>
- <TimeSince date={row.lastSeen} />
- </SubSubHeader>
- {lastSeenExample?.release && (
- <VersionHoverCard
- organization={organization}
- projectSlug="sentry"
- releaseVersion={lastSeenExample.release.version}
- showUnderline
- underlineColor="linkUnderline"
- >
- <Version version={String(lastSeenExample.release.version)} truncate />
- </VersionHoverCard>
- )}
- </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 (Spans Per Minute)')}</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 P50 / P95')}</SubHeader>
- <SubSubHeader>
- {row.p50.toFixed(3)}ms / {row.p95.toFixed(3)}ms
- </SubSubHeader>
- <Chart
- statsPeriod="24h"
- height={140}
- data={[p50Series, p95Series]}
- start=""
- end=""
- loading={isDataLoading}
- utc={false}
- chartColors={theme.charts.getColorPalette(4).slice(3, 5)}
- 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: {action: string; domain: string}
- ) => {
- 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;
- });
- };
- const throughputQueryToChartData = (
- data: any,
- startTime: moment.Moment,
- endTime: moment.Moment
- ): Series[] => {
- const countSeries: Series = {seriesName: 'spm()', data: [] as any[]};
- const p50Series: Series = {seriesName: 'p50()', data: [] as any[]};
- const p95Series: Series = {seriesName: 'p95()', data: [] as any[]};
- data.forEach(({count, p50, p95, interval}) => {
- countSeries.data.push({value: count, name: interval});
- p50Series.data.push({value: p50, name: interval});
- p95Series.data.push({value: p95, name: interval});
- });
- return [
- zeroFillSeries(countSeries, moment.duration(INTERVAL, 'hours'), startTime, endTime),
- zeroFillSeries(p50Series, moment.duration(INTERVAL, 'hours'), startTime, endTime),
- zeroFillSeries(p95Series, 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;
- `;
- export const FlexRowContainer = styled('div')`
- display: flex;
- & > div:last-child {
- padding-right: ${space(1)};
- }
- padding-bottom: ${space(2)};
- `;
- export const FlexRowItem = styled('div')`
- padding-right: ${space(4)};
- flex: 1;
- `;
- 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};
- `;
|