summaryTable.tsx 7.3 KB

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