import {Fragment} from 'react'; import {useTheme} from '@emotion/react'; import GridEditable from 'sentry/components/gridEditable'; import {useLocation} from 'sentry/utils/useLocation'; import { DataRow, Keys, similarity, TableColumnHeader, } from 'sentry/views/starfish/modules/databaseModule/databaseTableView'; import {useQueryMainTable} from 'sentry/views/starfish/modules/databaseModule/queries'; type Props = { mainTableRow: DataRow; }; const COLUMN_ORDER: TableColumnHeader[] = [ { key: 'description', name: 'Query', width: 300, }, { key: 'epm', name: 'Tpm', }, { key: 'p50', name: 'p50', }, { key: 'p95', name: 'p95', }, { key: 'total_time', name: 'Total Time', }, ]; function SimilarQueryView(props: Props) { const {mainTableRow} = props; const {isLoading, data} = useQueryMainTable({ limit: 410, }); const location = useLocation(); const theme = useTheme(); const similarQueries = data.filter( row => similarity(row.description, mainTableRow.description) > 0.8 ); const renderHeadCell = (col): React.ReactNode => { return {col.name}; }; const renderBodyCell = (column: TableColumnHeader, row: DataRow): React.ReactNode => { const {key} = column; let renderedValue: React.ReactNode = row[key]; if (key === 'description') { const mainTableQueryWords = new Set(mainTableRow.description.split(' ')); const diffQuery = (
{row.description.split(' ').map(word => { if (mainTableQueryWords.has(word)) { return `${word} `; } return ( {`${word} `} ); })}
); renderedValue = diffQuery; } const timeBasedKeys: Keys[] = ['p50', 'p95', 'total_time']; if ((['epm', ...timeBasedKeys] as Keys[]).includes(key)) { const val = row[key] as number; const mainTableVal = mainTableRow[key] as number; const sign = val > mainTableVal ? '+' : ''; const percentage = (val / mainTableVal - 1) * 100; let unit = ''; if (timeBasedKeys.includes(key)) { unit = 'ms'; } renderedValue = ( {val.toFixed(3)} {unit} ({sign} {percentage.toFixed(2)}% ) ); } return {renderedValue}; }; if (!isLoading && similarQueries.length > 0) { return ( ); } return ; } export default SimilarQueryView;