similarQueryView.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import GridEditable from 'sentry/components/gridEditable';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import {
  6. DataRow,
  7. similarity,
  8. TableColumnHeader,
  9. } from 'sentry/views/starfish/modules/databaseModule/databaseTableView';
  10. import {useQueryMainTable} from 'sentry/views/starfish/modules/databaseModule/queries';
  11. type Props = {
  12. mainTableRow: DataRow;
  13. };
  14. const COLUMN_ORDER: TableColumnHeader[] = [
  15. {
  16. key: 'description',
  17. name: 'Query',
  18. width: 300,
  19. },
  20. {
  21. key: 'epm',
  22. name: 'Tpm',
  23. },
  24. {
  25. key: 'p75',
  26. name: 'p75',
  27. },
  28. {
  29. key: 'total_time',
  30. name: 'Total Time',
  31. },
  32. ];
  33. function SimilarQueryView(props: Props) {
  34. const {mainTableRow} = props;
  35. const {isLoading, data} = useQueryMainTable({
  36. limit: 410,
  37. });
  38. const location = useLocation();
  39. const theme = useTheme();
  40. const similarQueries = data.filter(
  41. row => similarity(row.description, mainTableRow.description) > 0.8
  42. );
  43. const renderHeadCell = (col): React.ReactNode => {
  44. return <span>{col.name}</span>;
  45. };
  46. const renderBodyCell = (column: TableColumnHeader, row: DataRow): React.ReactNode => {
  47. const {key} = column;
  48. let renderedValue: React.ReactNode = row[key];
  49. if (key === 'description') {
  50. const mainTableQueryWords = new Set(mainTableRow.description.split(' '));
  51. const diffQuery = (
  52. <div>
  53. {row.description.split(' ').map(word => {
  54. if (mainTableQueryWords.has(word)) {
  55. return `${word} `;
  56. }
  57. return (
  58. <span style={{color: theme.green400}} key={word}>
  59. {`${word} `}
  60. </span>
  61. );
  62. })}
  63. </div>
  64. );
  65. renderedValue = diffQuery;
  66. }
  67. if (key === 'epm' || key === 'p75' || key === 'total_time') {
  68. const val = row[key];
  69. const sign = val > mainTableRow[key] ? '+' : '';
  70. const percentage = (val / mainTableRow[key] - 1) * 100;
  71. let unit = '';
  72. if (key === 'p75' || key === 'total_time') {
  73. unit = 'ms';
  74. }
  75. renderedValue = (
  76. <Fragment>
  77. {val.toFixed(3)}
  78. {unit} ({sign}
  79. <span style={{color: sign ? theme.red400 : theme.green400}}>
  80. {percentage.toFixed(2)}%
  81. </span>
  82. )
  83. </Fragment>
  84. );
  85. }
  86. return <span>{renderedValue}</span>;
  87. };
  88. if (!isLoading && similarQueries.length > 0) {
  89. return (
  90. <GridEditable
  91. isLoading={isLoading}
  92. data={similarQueries as any}
  93. columnOrder={COLUMN_ORDER}
  94. columnSortBy={[]}
  95. grid={{
  96. renderHeadCell,
  97. renderBodyCell,
  98. }}
  99. location={location}
  100. />
  101. );
  102. }
  103. return <Fragment />;
  104. }
  105. export default SimilarQueryView;