databaseTableView.tsx 8.2 KB

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