breakdownBar.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import {Fragment, useState} from 'react';
  2. import isPropValid from '@emotion/is-prop-valid';
  3. import styled from '@emotion/styled';
  4. import {motion} from 'framer-motion';
  5. import {Tooltip} from 'sentry/components/tooltip';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {percent} from 'sentry/utils';
  9. import {useQuery} from 'sentry/utils/queryClient';
  10. import {
  11. getOtherDomainsActionsAndOpTimeseries,
  12. getTopDomainsActionsAndOp,
  13. getTopDomainsActionsAndOpTimeseries,
  14. spanThroughput,
  15. totalCumulativeTime,
  16. } from 'sentry/views/starfish/views/webServiceView/queries';
  17. import {WebServiceBreakdownChart} from 'sentry/views/starfish/views/webServiceView/webServiceBreakdownChart';
  18. const COLORS = ['#402A65', '#694D99', '#9A81C4', '#BBA6DF', '#EAE2F8', '#F8F6FC'];
  19. const TOOLTIP_DELAY = 800;
  20. const HOST = 'http://localhost:8080';
  21. type ModuleSegment = {
  22. action: string;
  23. domain: string;
  24. span_operation: string;
  25. sum: number;
  26. };
  27. type Props = {
  28. title: string;
  29. transaction?: string;
  30. };
  31. export function getSegmentLabel(span_operation, action, domain) {
  32. if (span_operation === 'http.client') {
  33. return t('%s requests to %s', action, domain);
  34. }
  35. if (span_operation === 'db') {
  36. return t('%s queries on %s', action, domain);
  37. }
  38. return span_operation || domain || undefined;
  39. }
  40. function FacetBreakdownBar({title, transaction: maybeTransaction}: Props) {
  41. const [hoveredValue, setHoveredValue] = useState<ModuleSegment | null>(null);
  42. const transaction = maybeTransaction ?? '';
  43. const {data: segments} = useQuery({
  44. queryKey: ['webServiceSpanGrouping', transaction],
  45. queryFn: () =>
  46. fetch(`${HOST}/?query=${getTopDomainsActionsAndOp({transaction})}`).then(res =>
  47. res.json()
  48. ),
  49. retry: false,
  50. initialData: [],
  51. });
  52. const {data: cumulativeTime} = useQuery({
  53. queryKey: ['totalCumulativeTime', transaction],
  54. queryFn: () =>
  55. fetch(`${HOST}/?query=${totalCumulativeTime({transaction})}`).then(res =>
  56. res.json()
  57. ),
  58. retry: false,
  59. initialData: [],
  60. });
  61. const totalValues = cumulativeTime.reduce((acc, segment) => acc + segment.sum, 0);
  62. const totalSegments = segments.reduce((acc, segment) => acc + segment.sum, 0);
  63. const otherValue = totalValues - totalSegments;
  64. const otherSegment = {
  65. span_operation: 'other',
  66. sum: otherValue,
  67. action: '',
  68. domain: '',
  69. } as ModuleSegment;
  70. let topConditions =
  71. segments.length > 0
  72. ? ` (span_operation = '${segments[0].span_operation}' ${
  73. segments[0].action ? `AND action = '${segments[0].action}'` : ''
  74. } ${segments[0].domain ? `AND domain = '${segments[0].domain}'` : ''})`
  75. : '';
  76. for (let index = 1; index < segments.length; index++) {
  77. const element = segments[index];
  78. topConditions = topConditions.concat(
  79. ' OR ',
  80. `(span_operation = '${element.span_operation}' ${
  81. element.action ? `AND action = '${element.action}'` : ''
  82. } ${element.domain ? `AND domain = '${element.domain}'` : ''})`
  83. );
  84. }
  85. const {isLoading: isTopDataLoading, data: topData} = useQuery({
  86. queryKey: ['topSpanGroupTimeseries', transaction, topConditions],
  87. queryFn: () =>
  88. fetch(
  89. `${HOST}/?query=${getTopDomainsActionsAndOpTimeseries({
  90. transaction,
  91. topConditions,
  92. })}`
  93. ).then(res => res.json()),
  94. retry: false,
  95. initialData: [],
  96. });
  97. const {isLoading: isOtherDataLoading, data: otherData} = useQuery({
  98. queryKey: ['otherSpanGroupTimeseries', transaction, topConditions],
  99. queryFn: () =>
  100. fetch(
  101. `${HOST}/?query=${getOtherDomainsActionsAndOpTimeseries({
  102. transaction,
  103. topConditions,
  104. })}`
  105. ).then(res => res.json()),
  106. retry: false,
  107. initialData: [],
  108. });
  109. const {data: throughputData} = useQuery({
  110. queryKey: ['httpThroughputData', transaction],
  111. queryFn: () =>
  112. fetch(`${HOST}/?query=${spanThroughput({transaction})}`).then(res => res.json()),
  113. retry: false,
  114. initialData: [],
  115. });
  116. function renderTitle() {
  117. return (
  118. <Title>
  119. <TitleType>{title}</TitleType>
  120. </Title>
  121. );
  122. }
  123. function renderSegments() {
  124. if (totalValues === 0) {
  125. return (
  126. <SegmentBar>
  127. <p>{t('No recent data.')}</p>
  128. </SegmentBar>
  129. );
  130. }
  131. return (
  132. <SegmentBar>
  133. {segments.map((value, index) => {
  134. const pct = percent(value.sum, totalValues);
  135. const pctLabel = Math.floor(pct);
  136. const segmentProps = {
  137. index,
  138. onClick: () => {},
  139. };
  140. const segmentLabel = getSegmentLabel(
  141. value.span_operation,
  142. value.action,
  143. value.domain
  144. );
  145. return (
  146. <div
  147. key={`segment-${segmentLabel}`}
  148. style={{width: pct + '%'}}
  149. onMouseOver={() => {
  150. setHoveredValue(value);
  151. }}
  152. onMouseLeave={() => setHoveredValue(null)}
  153. >
  154. <Tooltip skipWrapper delay={TOOLTIP_DELAY} title={segmentLabel}>
  155. <Segment
  156. aria-label={`${segmentLabel} ${t('segment')}`}
  157. color={COLORS[index]}
  158. {...segmentProps}
  159. >
  160. {/* if the first segment is 6% or less, the label won't fit cleanly into the segment, so don't show the label */}
  161. {index === 0 && pctLabel > 6 ? `${pctLabel}%` : null}
  162. </Segment>
  163. </Tooltip>
  164. </div>
  165. );
  166. })}
  167. {otherValue > 0 && (
  168. <div
  169. key="segment-other"
  170. style={{width: percent(otherValue, totalValues) + '%'}}
  171. onMouseOver={() => {
  172. setHoveredValue(otherSegment);
  173. }}
  174. onMouseLeave={() => setHoveredValue(null)}
  175. >
  176. <Tooltip skipWrapper delay={TOOLTIP_DELAY} title="other">
  177. <Segment
  178. aria-label="other segment"
  179. color={COLORS[5]}
  180. {...{
  181. index: 5,
  182. onClick: () => {},
  183. }}
  184. />
  185. </Tooltip>
  186. </div>
  187. )}
  188. </SegmentBar>
  189. );
  190. }
  191. function renderLegend() {
  192. return (
  193. <LegendAnimateContainer expanded animate={{height: '100%', opacity: 1}}>
  194. <LegendContainer>
  195. {segments.map((segment, index) => {
  196. const pctLabel = Math.floor(percent(segment.sum, totalValues));
  197. const unfocus = !!hoveredValue && hoveredValue !== segment;
  198. const focus = hoveredValue === segment;
  199. const label = getSegmentLabel(
  200. segment.span_operation,
  201. segment.action,
  202. segment.domain
  203. );
  204. return (
  205. <li key={`segment-${label}-${index}`}>
  206. <LegendRow
  207. onMouseOver={() => setHoveredValue(segment)}
  208. onMouseLeave={() => setHoveredValue(null)}
  209. onClick={() => {}}
  210. >
  211. <LegendDot color={COLORS[index]} focus={focus} />
  212. <LegendText unfocus={unfocus}>
  213. {label ?? <NotApplicableLabel>{t('n/a')}</NotApplicableLabel>}
  214. </LegendText>
  215. {<LegendPercent>{`${pctLabel}%`}</LegendPercent>}
  216. </LegendRow>
  217. </li>
  218. );
  219. })}
  220. </LegendContainer>
  221. </LegendAnimateContainer>
  222. );
  223. }
  224. return (
  225. <Fragment>
  226. <TagSummary>
  227. <details open aria-expanded onClick={e => e.preventDefault()}>
  228. <StyledSummary>
  229. <TagHeader>
  230. {renderTitle()}
  231. {renderSegments()}
  232. </TagHeader>
  233. </StyledSummary>
  234. {renderLegend()}
  235. </details>
  236. </TagSummary>
  237. <WebServiceBreakdownChart
  238. isTopDataLoading={isTopDataLoading}
  239. topData={topData}
  240. isOtherDataLoading={isOtherDataLoading}
  241. otherData={otherData}
  242. throughputData={throughputData}
  243. />
  244. </Fragment>
  245. );
  246. }
  247. export default FacetBreakdownBar;
  248. const TagSummary = styled('div')`
  249. margin-bottom: ${space(2)};
  250. `;
  251. const TagHeader = styled('span')<{clickable?: boolean}>`
  252. ${p => (p.clickable ? 'cursor: pointer' : null)};
  253. `;
  254. const SegmentBar = styled('div')`
  255. display: flex;
  256. overflow: hidden;
  257. `;
  258. const Title = styled('div')`
  259. display: flex;
  260. font-size: ${p => p.theme.fontSizeMedium};
  261. justify-content: space-between;
  262. margin-bottom: ${space(1)};
  263. line-height: 1.1;
  264. `;
  265. const TitleType = styled('div')`
  266. flex: none;
  267. color: ${p => p.theme.textColor};
  268. font-weight: bold;
  269. font-size: ${p => p.theme.fontSizeMedium};
  270. margin-right: ${space(1)};
  271. align-self: center;
  272. `;
  273. const Segment = styled('span', {shouldForwardProp: isPropValid})<{color: string}>`
  274. &:hover {
  275. color: ${p => p.theme.white};
  276. }
  277. display: block;
  278. width: 100%;
  279. height: ${space(2)};
  280. color: ${p => p.theme.white};
  281. outline: none;
  282. background-color: ${p => p.color};
  283. text-align: right;
  284. font-size: ${p => p.theme.fontSizeExtraSmall};
  285. padding: 1px ${space(0.5)} 0 0;
  286. `;
  287. const LegendAnimateContainer = styled(motion.div, {
  288. shouldForwardProp: prop =>
  289. prop === 'animate' || (prop !== 'expanded' && isPropValid(prop)),
  290. })<{expanded: boolean}>`
  291. height: 0;
  292. opacity: 0;
  293. ${p => (!p.expanded ? 'overflow: hidden;' : '')}
  294. `;
  295. const LegendContainer = styled('ol')`
  296. list-style: none;
  297. padding: 0;
  298. margin: ${space(1)} 0;
  299. `;
  300. const LegendRow = styled('div')`
  301. display: flex;
  302. align-items: center;
  303. cursor: pointer;
  304. padding: ${space(0.5)} 0;
  305. `;
  306. const LegendDot = styled('span')<{color: string; focus: boolean}>`
  307. padding: 0;
  308. position: relative;
  309. width: 11px;
  310. height: 11px;
  311. text-indent: -9999em;
  312. display: inline-block;
  313. border-radius: 50%;
  314. flex-shrink: 0;
  315. background-color: ${p => p.color};
  316. &:after {
  317. content: '';
  318. border-radius: 50%;
  319. position: absolute;
  320. top: 0;
  321. left: 0;
  322. width: 100%;
  323. height: 100%;
  324. outline: ${p => p.theme.gray100} ${space(0.5)} solid;
  325. opacity: ${p => (p.focus ? '1' : '0')};
  326. transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  327. }
  328. `;
  329. const LegendText = styled('span')<{unfocus: boolean}>`
  330. font-size: ${p => p.theme.fontSizeSmall};
  331. margin-left: ${space(1)};
  332. overflow: hidden;
  333. white-space: nowrap;
  334. text-overflow: ellipsis;
  335. transition: color 0.3s;
  336. color: ${p => (p.unfocus ? p.theme.gray300 : p.theme.gray400)};
  337. `;
  338. const LegendPercent = styled('span')`
  339. font-size: ${p => p.theme.fontSizeSmall};
  340. margin-left: ${space(1)};
  341. color: ${p => p.theme.gray300};
  342. text-align: right;
  343. flex-grow: 1;
  344. `;
  345. const NotApplicableLabel = styled('span')`
  346. color: ${p => p.theme.gray300};
  347. `;
  348. const StyledSummary = styled('summary')`
  349. &::-webkit-details-marker {
  350. display: none;
  351. }
  352. `;