table.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {PanelTable, PanelTableHeader} from 'sentry/components/panels/panelTable';
  4. import TextOverflow from 'sentry/components/textOverflow';
  5. import {IconArrow} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {MetricsQueryApiResponse} from 'sentry/types';
  9. import {isNotQueryOnly, unescapeMetricsFormula} from 'sentry/utils/metrics';
  10. import {formatMetricUsingUnit} from 'sentry/utils/metrics/formatters';
  11. import {formatMRIField, MRIToField} from 'sentry/utils/metrics/mri';
  12. import {
  13. isMetricFormula,
  14. type MetricsQueryApiQueryParams,
  15. type MetricsQueryApiRequestQuery,
  16. } from 'sentry/utils/metrics/useMetricsQuery';
  17. import type {Order} from 'sentry/views/dashboards/metrics/types';
  18. import {LoadingScreen} from 'sentry/views/insights/common/components/chart';
  19. interface MetricTableContainerProps {
  20. isLoading: boolean;
  21. metricQueries: MetricsQueryApiQueryParams[];
  22. timeseriesData?: MetricsQueryApiResponse;
  23. }
  24. export function MetricTableContainer({
  25. timeseriesData,
  26. metricQueries,
  27. isLoading,
  28. }: MetricTableContainerProps) {
  29. const tableData = useMemo(() => {
  30. return timeseriesData
  31. ? getTableData(timeseriesData, metricQueries)
  32. : {headers: [], rows: []};
  33. }, [timeseriesData, metricQueries]);
  34. return <MetricTable isLoading={isLoading} data={tableData} borderless />;
  35. }
  36. interface MetricTableProps {
  37. data: TableData;
  38. isLoading: boolean;
  39. borderless?: boolean;
  40. onOrderChange?: ({id, order}: {id: number; order: Order}) => void;
  41. }
  42. export function MetricTable({
  43. isLoading,
  44. data,
  45. borderless,
  46. onOrderChange,
  47. }: MetricTableProps) {
  48. const handleCellClick = useCallback(
  49. column => {
  50. if (!onOrderChange) {
  51. return;
  52. }
  53. const {order} = column;
  54. const newOrder = order === 'desc' ? 'asc' : 'desc';
  55. onOrderChange({...column, order: newOrder});
  56. },
  57. [onOrderChange]
  58. );
  59. function renderRow(row: Row, index: number) {
  60. return data.headers.map((column, columnIndex) => {
  61. const key = `${index}-${columnIndex}:${column.name}`;
  62. const value = row[column.name].formattedValue ?? row[column.name].value;
  63. if (!value) {
  64. return (
  65. <TableCell type={column.type} key={key} noValue>
  66. {column.type === 'field' ? 'n/a' : '(none)'}
  67. </TableCell>
  68. );
  69. }
  70. return (
  71. <TableCell type={column.type} key={key}>
  72. {value}
  73. </TableCell>
  74. );
  75. });
  76. }
  77. if (isLoading) {
  78. return <LoadingScreen loading />;
  79. }
  80. return (
  81. <StyledPanelTable
  82. borderless={borderless}
  83. headers={data.headers.map((column, index) => {
  84. return (
  85. <HeaderCell
  86. key={index}
  87. type={column.type}
  88. onClick={() => handleCellClick(column)}
  89. disabled={column.type !== 'field' || !onOrderChange}
  90. >
  91. {column.order && (
  92. <IconArrow direction={column.order === 'asc' ? 'up' : 'down'} size="xs" />
  93. )}
  94. <TextOverflow>{column.label}</TextOverflow>
  95. </HeaderCell>
  96. );
  97. })}
  98. stickyHeaders
  99. emptyMessage={t('No results')}
  100. >
  101. {data.rows.map(renderRow)}
  102. </StyledPanelTable>
  103. );
  104. }
  105. const equalGroupBys = (a: Record<string, unknown>, b: Record<string, unknown>) => {
  106. return JSON.stringify(a) === JSON.stringify(b);
  107. };
  108. const getEmptyGroup = (tags: string[]) =>
  109. tags.reduce((acc, tag) => {
  110. acc[tag] = '';
  111. return acc;
  112. }, {});
  113. function getGroupByCombos(
  114. queries: MetricsQueryApiRequestQuery[],
  115. results: MetricsQueryApiResponse['data']
  116. ): Record<string, string>[] {
  117. const groupBys = Array.from(new Set(queries.flatMap(query => query.groupBy ?? [])));
  118. const emptyBy = getEmptyGroup(groupBys);
  119. const allCombos = results.flatMap(group => {
  120. return group.map(entry => ({...emptyBy, ...entry.by}));
  121. });
  122. const uniqueCombos = allCombos.filter(
  123. (combo, index, self) => index === self.findIndex(other => equalGroupBys(other, combo))
  124. );
  125. return uniqueCombos;
  126. }
  127. type Row = Record<string, {formattedValue?: string; value?: number}>;
  128. interface TableData {
  129. headers: {
  130. label: string;
  131. name: string;
  132. order: Order;
  133. type: string;
  134. }[];
  135. rows: Row[];
  136. }
  137. export function getTableData(
  138. data: MetricsQueryApiResponse,
  139. expressions: MetricsQueryApiQueryParams[]
  140. ): TableData {
  141. const queries = expressions.filter(isNotQueryOnly) as MetricsQueryApiRequestQuery[];
  142. // @ts-expect-error TODO(metrics): use DashboardMetricsExpression type
  143. const shownExpressions = expressions.filter(e => !e.isHidden);
  144. const tags = [...new Set(queries.flatMap(query => query.groupBy ?? []))];
  145. const normalizedResults = shownExpressions.map((expression, index) => {
  146. const expressionResults = data.data[index];
  147. const meta = data.meta[index];
  148. const lastMetaEntry = data.meta[index]?.[meta.length - 1];
  149. const metaUnit =
  150. (lastMetaEntry && 'unit' in lastMetaEntry && lastMetaEntry.unit) || 'none';
  151. const normalizedGroupResults = expressionResults.map(group => {
  152. return {
  153. by: {...getEmptyGroup(tags), ...group.by},
  154. totals: group.totals,
  155. formattedValue: formatMetricUsingUnit(group.totals, metaUnit),
  156. };
  157. });
  158. return {name: expression.name, results: normalizedGroupResults};
  159. }, {});
  160. const groupByCombos = getGroupByCombos(queries, data.data);
  161. const rows: Row[] = groupByCombos.map(combo => {
  162. const row = Object.entries(combo).reduce((acc, [key, value]) => {
  163. acc[key] = {value};
  164. return acc;
  165. }, {});
  166. normalizedResults.forEach(({name, results}) => {
  167. const entry = results.find(e => equalGroupBys(e.by, combo));
  168. row[name] = {value: entry?.totals, formattedValue: entry?.formattedValue};
  169. });
  170. return row;
  171. });
  172. const headers = [
  173. ...tags.map(tagName => ({
  174. name: tagName,
  175. label: tagName,
  176. type: 'tag',
  177. order: undefined,
  178. })),
  179. ...shownExpressions.map(query => ({
  180. name: query.name,
  181. // @ts-expect-error TODO(metrics): use DashboardMetricsExpression type
  182. id: query.id,
  183. label:
  184. // TODO(metrics): consider consolidating with getMetricQueryName (different types)
  185. query.alias ??
  186. (isMetricFormula(query)
  187. ? unescapeMetricsFormula(query.formula)
  188. : formatMRIField(MRIToField(query.mri, query.op))),
  189. type: 'field',
  190. order: query.orderBy,
  191. })),
  192. ];
  193. const tableData = {
  194. headers,
  195. rows: sortRows(rows, headers),
  196. };
  197. return tableData;
  198. }
  199. function sortRows(rows: Row[], headers: TableData['headers']) {
  200. const orderedByColumn = headers.find(header => !!header.order);
  201. if (!orderedByColumn) {
  202. return rows;
  203. }
  204. const sorted = rows.sort((a, b) => {
  205. const aValue = a[orderedByColumn.name]?.value ?? '';
  206. const bValue = b[orderedByColumn.name]?.value ?? '';
  207. if (orderedByColumn.order === 'asc') {
  208. return aValue > bValue ? 1 : -1;
  209. }
  210. return aValue < bValue ? 1 : -1;
  211. });
  212. return sorted;
  213. }
  214. const Cell = styled('div')<{type?: string}>`
  215. display: flex;
  216. flex-direction: row;
  217. justify-content: ${p => (p.type === 'field' ? ' flex-end' : ' flex-start')};
  218. `;
  219. const StyledPanelTable = styled(PanelTable)<{borderless?: boolean}>`
  220. position: relative;
  221. display: grid;
  222. overflow: auto;
  223. margin: 0;
  224. margin-top: ${space(1.5)};
  225. border-radius: ${p => p.theme.borderRadius};
  226. font-size: ${p => p.theme.fontSizeMedium};
  227. box-shadow: none;
  228. ${p =>
  229. p.borderless &&
  230. `border-radius: 0 0 ${p.theme.borderRadius} ${p.theme.borderRadius};
  231. border-left: 0;
  232. border-right: 0;
  233. border-bottom: 0;`}
  234. ${PanelTableHeader} {
  235. height: min-content;
  236. }
  237. `;
  238. const HeaderCell = styled('div')<{disabled: boolean; type?: string}>`
  239. padding: 0 ${space(0.5)};
  240. display: flex;
  241. flex-direction: row;
  242. align-items: stretch;
  243. gap: ${space(0.5)};
  244. cursor: ${p => (p.disabled ? 'default' : 'pointer')};
  245. justify-content: ${p => (p.type === 'field' ? ' flex-end' : ' flex-start')};
  246. `;
  247. export const TableCell = styled(Cell)<{noValue?: boolean}>`
  248. padding: ${space(1)} ${space(3)};
  249. ${p => p.noValue && `color: ${p.theme.gray300};`}
  250. `;