databaseTableView.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import {CSSProperties, useState} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import Badge from 'sentry/components/badge';
  6. import GridEditable, {GridColumnHeader} from 'sentry/components/gridEditable';
  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 {Series} from 'sentry/types/echarts';
  11. import {getDuration} from 'sentry/utils/formatters';
  12. import Sparkline, {
  13. generateHorizontalLine,
  14. } from 'sentry/views/starfish/components/sparkline';
  15. import {Sort} from 'sentry/views/starfish/modules/databaseModule';
  16. import {SortableHeader} from 'sentry/views/starfish/modules/databaseModule/panel/queryTransactionTable';
  17. type Props = {
  18. isDataLoading: boolean;
  19. location: Location;
  20. onSelect: (row: DataRow, rowIndex: number) => void;
  21. columns?: any;
  22. data?: DataRow[];
  23. noP95?: boolean;
  24. onSortChange?: ({direction, sortHeader}: MainTableSort) => void;
  25. p95asNumber?: boolean;
  26. selectedRow?: DataRow;
  27. };
  28. export type DataRow = {
  29. action: string;
  30. count: number;
  31. data_keys: Array<string>;
  32. data_values: Array<string>;
  33. description: string;
  34. domain: string;
  35. epm: number;
  36. firstSeen: string;
  37. formatted_desc: string;
  38. group_id: string;
  39. lastSeen: string;
  40. newish: number;
  41. p50: number;
  42. p50_trend: Series;
  43. p75: number;
  44. p95: number;
  45. p95_trend: Series;
  46. retired: number;
  47. throughput: Series;
  48. total_time: number;
  49. transactions: number;
  50. };
  51. export type Keys =
  52. | 'description'
  53. | 'domain'
  54. | 'p50'
  55. | 'p95'
  56. | 'epm'
  57. | 'p95'
  58. | 'transactions'
  59. | 'total_time';
  60. export type TableColumnHeader = GridColumnHeader<Keys>;
  61. export type MainTableSort = Sort<TableColumnHeader>;
  62. export function similarity(value: string, other: string): number {
  63. // If they're identical we don't care
  64. if (value === other || other === undefined || value === undefined) {
  65. return -1;
  66. }
  67. const short_words = value.length < other.length ? value.split(' ') : other.split(' ');
  68. const long_words = value.length > other.length ? value.split(' ') : other.split(' ');
  69. const total = long_words.length;
  70. let count = 0;
  71. while (long_words.length > 0) {
  72. const word = long_words.pop();
  73. if (word && short_words.includes(word)) {
  74. count += 1;
  75. short_words.splice(short_words.indexOf(word), 1);
  76. }
  77. }
  78. return count / total;
  79. }
  80. function renderBadge(row, selectedRow) {
  81. const similar = similarity(selectedRow?.description, row.description) > 0.8;
  82. const newish = row?.newish === 1;
  83. const retired = row?.retired === 1;
  84. let response: React.ReactNode | null = null;
  85. if (similar) {
  86. if (newish && selectedRow.newish !== 1) {
  87. response = (
  88. <span>
  89. <StyledBadge type="new" text="new" />
  90. <StyledBadge type="alpha" text="similar" />
  91. </span>
  92. );
  93. } else if (retired && selectedRow.retired !== 1) {
  94. response = (
  95. <span>
  96. <StyledBadge type="warning" text="old" />
  97. <StyledBadge type="alpha" text="similar" />
  98. </span>
  99. );
  100. } else {
  101. response = <StyledBadge type="alpha" text="similar" />;
  102. }
  103. } else if (newish) {
  104. response = <StyledBadge type="new" text="new" />;
  105. } else if (retired) {
  106. response = <StyledBadge type="warning" text="old" />;
  107. }
  108. return response;
  109. }
  110. export default function DatabaseTableView({
  111. location,
  112. data,
  113. onSelect,
  114. p95asNumber,
  115. noP95,
  116. onSortChange,
  117. selectedRow,
  118. isDataLoading,
  119. columns,
  120. }: Props) {
  121. const [sort, setSort] = useState<{
  122. direction: 'desc' | 'asc' | undefined;
  123. sortHeader: TableColumnHeader | undefined;
  124. }>({direction: undefined, sortHeader: undefined});
  125. const theme = useTheme();
  126. let COLUMN_ORDER: TableColumnHeader[] = [
  127. {
  128. key: 'description',
  129. name: 'Query',
  130. width: 550,
  131. },
  132. {
  133. key: 'domain',
  134. name: 'Table',
  135. width: 175,
  136. },
  137. {
  138. key: 'epm',
  139. name: 'Throughput (SPM)',
  140. width: 175,
  141. },
  142. {
  143. key: 'p50',
  144. name: 'P50',
  145. width: 175,
  146. },
  147. {
  148. key: 'p95',
  149. name: 'P95',
  150. width: !p95asNumber ? 175 : undefined,
  151. },
  152. {
  153. key: 'transactions',
  154. name: 'transactions',
  155. },
  156. {
  157. key: 'total_time',
  158. name: 'Total Time',
  159. },
  160. ];
  161. if (noP95) {
  162. COLUMN_ORDER = COLUMN_ORDER.filter(col => col.key !== 'p95');
  163. }
  164. function onSortClick(col: TableColumnHeader) {
  165. let direction: 'desc' | 'asc' | undefined = undefined;
  166. if (!sort.direction || col.key !== sort.sortHeader?.key) {
  167. direction = 'desc';
  168. } else if (sort.direction === 'desc') {
  169. direction = 'asc';
  170. }
  171. if (onSortChange) {
  172. setSort({direction, sortHeader: col});
  173. onSortChange({direction, sortHeader: col});
  174. }
  175. }
  176. function renderHeadCell(col: TableColumnHeader): React.ReactNode {
  177. const sortableKeys: Keys[] = [
  178. 'p50',
  179. 'p95',
  180. 'epm',
  181. 'total_time',
  182. 'domain',
  183. 'transactions',
  184. ];
  185. if (sortableKeys.includes(col.key)) {
  186. const isBeingSorted = col.key === sort.sortHeader?.key;
  187. const direction = isBeingSorted ? sort.direction : undefined;
  188. return (
  189. <SortableHeader
  190. onClick={() => onSortClick(col)}
  191. direction={direction}
  192. title={col.name}
  193. />
  194. );
  195. }
  196. return <span>{col.name}</span>;
  197. }
  198. function renderBodyCell(
  199. column: TableColumnHeader,
  200. row: DataRow,
  201. rowIndex: number
  202. ): React.ReactNode {
  203. const {key} = column;
  204. const isSelectedRow = selectedRow?.group_id === row.group_id;
  205. const rowStyle: CSSProperties | undefined = isSelectedRow
  206. ? {fontWeight: 'bold'}
  207. : undefined;
  208. if (key === 'description') {
  209. const value = row.description;
  210. return (
  211. <Link onClick={() => onSelect(row, rowIndex)} to="" style={rowStyle}>
  212. {value.substring(0, 30)}
  213. {value.length > 30 ? '...' : ''}
  214. {value.length > 30 ? value.substring(value.length - 30) : ''}
  215. {renderBadge(row, selectedRow)}
  216. </Link>
  217. );
  218. }
  219. if (key === 'epm') {
  220. const horizontalLine = generateHorizontalLine('', row.epm, theme);
  221. return (
  222. <GraphRow>
  223. <span style={rowStyle}>{row.epm.toFixed(2)}</span>
  224. <Graphline>
  225. <Sparkline
  226. color={CHART_PALETTE[3][0]}
  227. series={row.throughput}
  228. markLine={horizontalLine}
  229. width={column.width ? column.width - column.width / 5 - 50 : undefined}
  230. />
  231. </Graphline>
  232. </GraphRow>
  233. );
  234. }
  235. if (key === 'p50') {
  236. const horizontalLine = generateHorizontalLine('', row.p50, theme);
  237. return (
  238. <GraphRow>
  239. <span style={rowStyle}>{getDuration(row.p50 / 1000, 2, true)}</span>
  240. <Graphline>
  241. <Sparkline
  242. color={CHART_PALETTE[3][1]}
  243. series={row.p50_trend}
  244. markLine={horizontalLine}
  245. width={column.width ? column.width - column.width / 5 - 50 : undefined}
  246. />
  247. </Graphline>
  248. </GraphRow>
  249. );
  250. }
  251. if (key === 'p95') {
  252. const horizontalLine = generateHorizontalLine('', row.p95, theme);
  253. return (
  254. <GraphRow>
  255. <span style={rowStyle}>{getDuration(row.p95 / 1000, 2, true)}</span>
  256. <Graphline>
  257. {!p95asNumber && (
  258. <Sparkline
  259. color={CHART_PALETTE[3][2]}
  260. series={row.p95_trend}
  261. markLine={horizontalLine}
  262. width={column.width ? column.width - column.width / 5 - 50 : undefined}
  263. />
  264. )}
  265. </Graphline>
  266. </GraphRow>
  267. );
  268. }
  269. if (key === 'total_time') {
  270. return <span style={rowStyle}>{getDuration(row.total_time / 1000, 2, true)}</span>;
  271. }
  272. return <span style={rowStyle}>{row[key]}</span>;
  273. }
  274. return (
  275. <GridEditable
  276. isLoading={isDataLoading}
  277. data={data as any}
  278. columnOrder={columns ?? COLUMN_ORDER}
  279. columnSortBy={[]}
  280. grid={{
  281. renderHeadCell,
  282. renderBodyCell,
  283. }}
  284. location={location}
  285. />
  286. );
  287. }
  288. const StyledBadge = styled(Badge)`
  289. margin-left: ${space(0.75)};
  290. `;
  291. const Graphline = styled('div')`
  292. margin-left: auto;
  293. `;
  294. const GraphRow = styled('div')`
  295. display: inline-flex;
  296. `;