databaseTableView.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {CSSProperties, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import Badge from 'sentry/components/badge';
  5. import GridEditable, {GridColumnHeader} from 'sentry/components/gridEditable';
  6. import {Hovercard} from 'sentry/components/hovercard';
  7. import Link from 'sentry/components/links/link';
  8. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  9. import {space} from 'sentry/styles/space';
  10. import Sparkline from 'sentry/views/starfish/components/sparkline';
  11. import {Sort} from 'sentry/views/starfish/modules/databaseModule';
  12. import {SortableHeader} from 'sentry/views/starfish/modules/databaseModule/panel/queryTransactionTable';
  13. import {highlightSql} from './panel';
  14. type Props = {
  15. isDataLoading: boolean;
  16. location: Location;
  17. onSelect: (row: DataRow, rowIndex: number) => void;
  18. columns?: any;
  19. data?: DataRow[];
  20. onSortChange?: ({direction, sortHeader}: MainTableSort) => void;
  21. selectedRow?: DataRow;
  22. };
  23. export type DataRow = {
  24. action: string;
  25. count: number;
  26. data_keys: Array<string>;
  27. data_values: Array<string>;
  28. description: string;
  29. domain: string;
  30. epm: number;
  31. firstSeen: string;
  32. formatted_desc: string;
  33. group_id: string;
  34. lastSeen: string;
  35. newish: number;
  36. p75: number;
  37. retired: number;
  38. total_time: number;
  39. transactions: number;
  40. };
  41. type Keys =
  42. | 'description'
  43. | 'domain'
  44. | 'throughput'
  45. | 'p75_trend'
  46. | 'epm'
  47. | 'p75'
  48. | 'transactions'
  49. | 'total_time';
  50. export type TableColumnHeader = GridColumnHeader<Keys>;
  51. export type MainTableSort = Sort<TableColumnHeader>;
  52. const COLUMN_ORDER: TableColumnHeader[] = [
  53. {
  54. key: 'description',
  55. name: 'Query',
  56. width: 600,
  57. },
  58. {
  59. key: 'domain',
  60. name: 'Table',
  61. width: 200,
  62. },
  63. {
  64. key: 'throughput',
  65. name: 'Throughput',
  66. width: 200,
  67. },
  68. {
  69. key: 'p75_trend',
  70. name: 'P75 Trend',
  71. width: 200,
  72. },
  73. {
  74. key: 'epm',
  75. name: 'Tpm',
  76. },
  77. {
  78. key: 'p75',
  79. name: 'p75',
  80. },
  81. {
  82. key: 'transactions',
  83. name: 'transactions',
  84. },
  85. {
  86. key: 'total_time',
  87. name: 'Total Time',
  88. },
  89. ];
  90. export function similarity(value: string, other: string): number {
  91. // If they're identical we don't care
  92. if (value === other || other === undefined || value === undefined) {
  93. return -1;
  94. }
  95. const short_words = value.length < other.length ? value.split(' ') : other.split(' ');
  96. const long_words = value.length > other.length ? value.split(' ') : other.split(' ');
  97. const total = long_words.length;
  98. let count = 0;
  99. while (long_words.length > 0) {
  100. const word = long_words.pop();
  101. if (word && short_words.includes(word)) {
  102. count += 1;
  103. short_words.splice(short_words.indexOf(word), 1);
  104. }
  105. }
  106. return count / total;
  107. }
  108. function renderBadge(row, selectedRow) {
  109. const similar = similarity(selectedRow?.description, row.description) > 0.8;
  110. const newish = row?.newish === 1;
  111. const retired = row?.retired === 1;
  112. let response: React.ReactNode | null = null;
  113. if (similar) {
  114. if (newish && selectedRow.newish !== 1) {
  115. response = (
  116. <span>
  117. <StyledBadge type="new" text="new" />
  118. <StyledBadge type="alpha" text="similar" />
  119. </span>
  120. );
  121. } else if (retired && selectedRow.retired !== 1) {
  122. response = (
  123. <span>
  124. <StyledBadge type="warning" text="old" />
  125. <StyledBadge type="alpha" text="similar" />
  126. </span>
  127. );
  128. } else {
  129. response = <StyledBadge type="alpha" text="similar" />;
  130. }
  131. } else if (newish) {
  132. response = <StyledBadge type="new" text="new" />;
  133. } else if (retired) {
  134. response = <StyledBadge type="warning" text="old" />;
  135. }
  136. return response;
  137. }
  138. export default function DatabaseTableView({
  139. location,
  140. data,
  141. onSelect,
  142. onSortChange,
  143. selectedRow,
  144. isDataLoading,
  145. columns,
  146. }: Props) {
  147. const [sort, setSort] = useState<{
  148. direction: 'desc' | 'asc' | undefined;
  149. sortHeader: TableColumnHeader | undefined;
  150. }>({direction: undefined, sortHeader: undefined});
  151. function onSortClick(col: TableColumnHeader) {
  152. let direction: 'desc' | 'asc' | undefined = undefined;
  153. if (!sort.direction || col.key !== sort.sortHeader?.key) {
  154. direction = 'desc';
  155. } else if (sort.direction === 'desc') {
  156. direction = 'asc';
  157. }
  158. if (onSortChange) {
  159. setSort({direction, sortHeader: col});
  160. onSortChange({direction, sortHeader: col});
  161. }
  162. }
  163. function renderHeadCell(col: TableColumnHeader): React.ReactNode {
  164. const sortableKeys: Keys[] = ['p75', 'epm', 'total_time', 'domain', 'transactions'];
  165. if (sortableKeys.includes(col.key)) {
  166. const isBeingSorted = col.key === sort.sortHeader?.key;
  167. const direction = isBeingSorted ? sort.direction : undefined;
  168. return (
  169. <SortableHeader
  170. onClick={() => onSortClick(col)}
  171. direction={direction}
  172. title={col.name}
  173. />
  174. );
  175. }
  176. return <span>{col.name}</span>;
  177. }
  178. function renderBodyCell(
  179. column: TableColumnHeader,
  180. row: DataRow,
  181. rowIndex: number
  182. ): React.ReactNode {
  183. const {key} = column;
  184. const isSelectedRow = selectedRow?.group_id === row.group_id;
  185. const rowStyle: CSSProperties | undefined = isSelectedRow
  186. ? {fontWeight: 'bold'}
  187. : undefined;
  188. const value = row[key];
  189. if (key === 'description') {
  190. let headerExtra = '';
  191. if (row.newish === 1) {
  192. headerExtra = `Query (First seen ${row.firstSeen})`;
  193. } else if (row.retired === 1) {
  194. headerExtra = `Query (Last seen ${row.lastSeen})`;
  195. }
  196. return (
  197. <Hovercard header={headerExtra} body={highlightSql(row.formatted_desc, row)}>
  198. <Link onClick={() => onSelect(row, rowIndex)} to="" style={rowStyle}>
  199. {value.substring(0, 30)}
  200. {value.length > 30 ? '...' : ''}
  201. {value.length > 30 ? value.substring(value.length - 30) : ''}
  202. </Link>
  203. {renderBadge(row, selectedRow)}
  204. </Hovercard>
  205. );
  206. }
  207. if (key === 'p75' || key === 'total_time') {
  208. return <span style={rowStyle}>{value.toFixed(2)}ms</span>;
  209. }
  210. if (key === 'throughput') {
  211. return (
  212. <Sparkline
  213. color={CHART_PALETTE[3][0]}
  214. series={value}
  215. width={column.width ? column.width - column.width / 5 : undefined}
  216. />
  217. );
  218. }
  219. if (key === 'p75_trend') {
  220. return (
  221. <Sparkline
  222. color={CHART_PALETTE[3][3]}
  223. series={value}
  224. width={column.width ? column.width - column.width / 5 : undefined}
  225. />
  226. );
  227. }
  228. return <span style={rowStyle}>{value}</span>;
  229. }
  230. return (
  231. <GridEditable
  232. isLoading={isDataLoading}
  233. data={data as any}
  234. columnOrder={columns ?? COLUMN_ORDER}
  235. columnSortBy={[]}
  236. grid={{
  237. renderHeadCell,
  238. renderBodyCell,
  239. }}
  240. location={location}
  241. />
  242. );
  243. }
  244. const StyledBadge = styled(Badge)`
  245. margin-left: ${space(0.75)};
  246. `;