summaryTable.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import colorFn from 'color';
  4. import {LinkButton} from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconLightning, IconReleases} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {getUtcDateString} from 'sentry/utils/dates';
  11. import {formatMetricsUsingUnitAndOp, getNameFromMRI} from 'sentry/utils/metrics';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import usePageFilters from 'sentry/utils/usePageFilters';
  14. import useRouter from 'sentry/utils/useRouter';
  15. import {Series} from 'sentry/views/ddm/metricsExplorer';
  16. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  17. export function SummaryTable({
  18. series,
  19. operation,
  20. onClick,
  21. setHoveredLegend,
  22. }: {
  23. onClick: (seriesName: string) => void;
  24. series: Series[];
  25. setHoveredLegend: React.Dispatch<React.SetStateAction<string>> | undefined;
  26. operation?: string;
  27. }) {
  28. const {selection} = usePageFilters();
  29. const router = useRouter();
  30. const {slug} = useOrganization();
  31. const hasActions = series.some(s => s.release || s.transaction);
  32. const {start, end, statsPeriod, project, environment} = router.location.query;
  33. const releaseTo = (release: string) => {
  34. return {
  35. pathname: `/organizations/${slug}/releases/${encodeURIComponent(release)}/`,
  36. query: {
  37. start,
  38. end,
  39. pageStatsPeriod: statsPeriod,
  40. project,
  41. environment,
  42. },
  43. };
  44. };
  45. const transactionTo = (transaction: string) =>
  46. transactionSummaryRouteWithQuery({
  47. orgSlug: slug,
  48. transaction,
  49. projectID: selection.projects.map(p => String(p)),
  50. query: {
  51. query: '',
  52. environment: selection.environments,
  53. start: selection.datetime.start
  54. ? getUtcDateString(selection.datetime.start)
  55. : undefined,
  56. end: selection.datetime.end
  57. ? getUtcDateString(selection.datetime.end)
  58. : undefined,
  59. statsPeriod: selection.datetime.period,
  60. },
  61. });
  62. return (
  63. <SummaryTableWrapper hasActions={hasActions}>
  64. <HeaderCell />
  65. <HeaderCell>{t('Name')}</HeaderCell>
  66. <HeaderCell right>{t('Avg')}</HeaderCell>
  67. <HeaderCell right>{t('Min')}</HeaderCell>
  68. <HeaderCell right>{t('Max')}</HeaderCell>
  69. <HeaderCell right>{t('Sum')}</HeaderCell>
  70. {hasActions && <HeaderCell right>{t('Actions')}</HeaderCell>}
  71. {series
  72. .sort((a, b) => a.seriesName.localeCompare(b.seriesName))
  73. .map(({seriesName, color, hidden, unit, data, transaction, release}) => {
  74. const {avg, min, max, sum} = getValues(data);
  75. return (
  76. <Fragment key={seriesName}>
  77. <CellWrapper
  78. onClick={() => onClick(seriesName)}
  79. onMouseEnter={() => setHoveredLegend?.(seriesName)}
  80. onMouseLeave={() => setHoveredLegend?.('')}
  81. >
  82. <Cell>
  83. <ColorDot color={color} isHidden={!!hidden} />
  84. </Cell>
  85. <Cell>{getNameFromMRI(seriesName)}</Cell>
  86. {/* TODO(ddm): Add a tooltip with the full value, don't add on click in case users want to copy the value */}
  87. <Cell right>{formatMetricsUsingUnitAndOp(avg, unit, operation)}</Cell>
  88. <Cell right>{formatMetricsUsingUnitAndOp(min, unit, operation)}</Cell>
  89. <Cell right>{formatMetricsUsingUnitAndOp(max, unit, operation)}</Cell>
  90. <Cell right>{formatMetricsUsingUnitAndOp(sum, unit, operation)}</Cell>
  91. </CellWrapper>
  92. {hasActions && (
  93. <Cell right>
  94. <ButtonBar gap={0.5}>
  95. {transaction && (
  96. <div>
  97. <Tooltip title={t('Open Transaction Summary')}>
  98. <LinkButton to={transactionTo(transaction)} size="xs">
  99. <IconLightning size="xs" />
  100. </LinkButton>
  101. </Tooltip>
  102. </div>
  103. )}
  104. {release && (
  105. <div>
  106. <Tooltip title={t('Open Release Details')}>
  107. <LinkButton to={releaseTo(release)} size="xs">
  108. <IconReleases size="xs" />
  109. </LinkButton>
  110. </Tooltip>
  111. </div>
  112. )}
  113. </ButtonBar>
  114. </Cell>
  115. )}
  116. </Fragment>
  117. );
  118. })}
  119. </SummaryTableWrapper>
  120. );
  121. }
  122. function getValues(seriesData: Series['data']) {
  123. if (!seriesData) {
  124. return {min: null, max: null, avg: null, sum: null};
  125. }
  126. const res = seriesData.reduce(
  127. (acc, {value}) => {
  128. if (value === null) {
  129. return acc;
  130. }
  131. acc.min = Math.min(acc.min, value);
  132. acc.max = Math.max(acc.max, value);
  133. acc.sum += value;
  134. return acc;
  135. },
  136. {min: Infinity, max: -Infinity, sum: 0}
  137. );
  138. return {...res, avg: res.sum / seriesData.length};
  139. }
  140. // TODO(ddm): PanelTable component proved to be a bit too opinionated for this use case,
  141. // so we're using a custom styled component instead. Figure out what we want to do here
  142. const SummaryTableWrapper = styled(`div`)<{hasActions: boolean}>`
  143. display: grid;
  144. grid-template-columns: ${p =>
  145. p.hasActions ? '24px 8fr 1fr 1fr 1fr 1fr 1fr' : '24px 8fr 1fr 1fr 1fr 1fr'};
  146. `;
  147. // TODO(ddm): This is a copy of PanelTableHeader, try to figure out how to reuse it
  148. const HeaderCell = styled('div')<{right?: boolean}>`
  149. color: ${p => p.theme.subText};
  150. font-size: ${p => p.theme.fontSizeSmall};
  151. font-weight: 600;
  152. text-transform: uppercase;
  153. border-radius: ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0 0;
  154. line-height: 1;
  155. display: flex;
  156. flex-direction: column;
  157. justify-content: center;
  158. text-align: ${p => (p.right ? 'right' : 'left')};
  159. padding: ${space(0.5)} ${space(1)};
  160. `;
  161. const Cell = styled('div')<{right?: boolean}>`
  162. display: flex;
  163. padding: ${space(0.25)} ${space(1)};
  164. align-items: center;
  165. justify-content: ${p => (p.right ? 'flex-end' : 'flex-start')};
  166. `;
  167. const ColorDot = styled(`div`)<{color: string; isHidden: boolean}>`
  168. background-color: ${p =>
  169. p.isHidden ? 'transparent' : colorFn(p.color).alpha(1).string()};
  170. border: 1px solid ${p => p.color};
  171. border-radius: 50%;
  172. width: ${space(1)};
  173. height: ${space(1)};
  174. `;
  175. const CellWrapper = styled('div')`
  176. display: contents;
  177. &:hover {
  178. cursor: pointer;
  179. ${Cell} {
  180. background-color: ${p => p.theme.bodyBackground};
  181. }
  182. }
  183. `;