content.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import omit from 'lodash/omit';
  6. import {CompactSelect} from 'sentry/components/compactSelect';
  7. import DatePageFilter from 'sentry/components/datePageFilter';
  8. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  9. import SearchBar from 'sentry/components/events/searchBar';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  13. import Pagination from 'sentry/components/pagination';
  14. import {space} from 'sentry/styles/space';
  15. import {Organization} from 'sentry/types';
  16. import {defined} from 'sentry/utils';
  17. import {trackAnalytics} from 'sentry/utils/analytics';
  18. import DiscoverQuery from 'sentry/utils/discover/discoverQuery';
  19. import EventView from 'sentry/utils/discover/eventView';
  20. import SuspectSpansQuery from 'sentry/utils/performance/suspectSpans/suspectSpansQuery';
  21. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  22. import {decodeScalar} from 'sentry/utils/queryString';
  23. import useProjects from 'sentry/utils/useProjects';
  24. import {SetStateAction} from '../types';
  25. import OpsFilter from './opsFilter';
  26. import SuspectSpansTable from './suspectSpansTable';
  27. import {SpanSort, SpansTotalValues} from './types';
  28. import {
  29. getSuspectSpanSortFromEventView,
  30. getTotalsView,
  31. SPAN_RELATIVE_PERIODS,
  32. SPAN_RETENTION_DAYS,
  33. SPAN_SORT_OPTIONS,
  34. SPAN_SORT_TO_FIELDS,
  35. } from './utils';
  36. const ANALYTICS_VALUES = {
  37. spanOp: (organization: Organization, value: string | undefined) =>
  38. trackAnalytics('performance_views.spans.change_op', {
  39. organization,
  40. operation_name: value,
  41. }),
  42. sort: (organization: Organization, value: string | undefined) =>
  43. trackAnalytics('performance_views.spans.change_sort', {
  44. organization,
  45. sort_column: value,
  46. }),
  47. };
  48. type Props = {
  49. eventView: EventView;
  50. location: Location;
  51. organization: Organization;
  52. projectId: string;
  53. setError: SetStateAction<string | undefined>;
  54. transactionName: string;
  55. };
  56. function SpansContent(props: Props) {
  57. const {location, organization, eventView, projectId, transactionName} = props;
  58. const query = decodeScalar(location.query.query, '');
  59. function handleChange(key: string) {
  60. return function (value: string | undefined) {
  61. ANALYTICS_VALUES[key]?.(organization, value);
  62. const queryParams = normalizeDateTimeParams({
  63. ...(location.query || {}),
  64. [key]: value,
  65. });
  66. // do not propagate pagination when making a new search
  67. const toOmit = ['cursor'];
  68. if (!defined(value)) {
  69. toOmit.push(key);
  70. }
  71. const searchQueryParams = omit(queryParams, toOmit);
  72. browserHistory.push({
  73. ...location,
  74. query: searchQueryParams,
  75. });
  76. };
  77. }
  78. const spanOp = decodeScalar(location.query.spanOp);
  79. const spanGroup = decodeScalar(location.query.spanGroup);
  80. const sort = getSuspectSpanSortFromEventView(eventView);
  81. const spansView = getSpansEventView(eventView, sort.field);
  82. const totalsView = getTotalsView(eventView);
  83. const {projects} = useProjects();
  84. return (
  85. <Layout.Main fullWidth>
  86. <FilterActions>
  87. <OpsFilter
  88. location={location}
  89. eventView={eventView}
  90. organization={organization}
  91. handleOpChange={handleChange('spanOp')}
  92. transactionName={transactionName}
  93. />
  94. <PageFilterBar condensed>
  95. <EnvironmentPageFilter />
  96. <DatePageFilter
  97. alignDropdown="left"
  98. maxPickableDays={SPAN_RETENTION_DAYS}
  99. relativeOptions={SPAN_RELATIVE_PERIODS}
  100. />
  101. </PageFilterBar>
  102. <StyledSearchBar
  103. organization={organization}
  104. projectIds={eventView.project}
  105. query={query}
  106. fields={eventView.fields}
  107. onSearch={handleChange('query')}
  108. />
  109. <CompactSelect
  110. value={sort.field}
  111. options={SPAN_SORT_OPTIONS.map(opt => ({value: opt.field, label: opt.label}))}
  112. onChange={opt => handleChange('sort')(opt.value)}
  113. triggerProps={{prefix: sort.prefix}}
  114. triggerLabel={sort.label}
  115. />
  116. </FilterActions>
  117. <DiscoverQuery
  118. eventView={totalsView}
  119. orgSlug={organization.slug}
  120. location={location}
  121. referrer="api.performance.transaction-spans"
  122. cursor="0:0:1"
  123. noPagination
  124. >
  125. {({tableData}) => {
  126. const totals: SpansTotalValues | null =
  127. (tableData?.data?.[0] as SpansTotalValues | undefined) ?? null;
  128. return (
  129. <SuspectSpansQuery
  130. location={location}
  131. orgSlug={organization.slug}
  132. eventView={spansView}
  133. limit={10}
  134. perSuspect={0}
  135. spanOps={defined(spanOp) ? [spanOp] : []}
  136. spanGroups={defined(spanGroup) ? [spanGroup] : []}
  137. >
  138. {({suspectSpans, isLoading, pageLinks}) => (
  139. <Fragment>
  140. <VisuallyCompleteWithData
  141. id="TransactionSpans-SuspectSpansTable"
  142. hasData={!!suspectSpans?.length}
  143. isLoading={isLoading}
  144. >
  145. <SuspectSpansTable
  146. location={location}
  147. organization={organization}
  148. transactionName={transactionName}
  149. project={projects.find(p => p.id === projectId)}
  150. isLoading={isLoading}
  151. suspectSpans={suspectSpans ?? []}
  152. totals={totals}
  153. sort={sort.field}
  154. />
  155. </VisuallyCompleteWithData>
  156. <Pagination pageLinks={pageLinks ?? null} />
  157. </Fragment>
  158. )}
  159. </SuspectSpansQuery>
  160. );
  161. }}
  162. </DiscoverQuery>
  163. </Layout.Main>
  164. );
  165. }
  166. function getSpansEventView(eventView: EventView, sort: SpanSort): EventView {
  167. eventView = eventView.clone();
  168. const fields = SPAN_SORT_TO_FIELDS[sort];
  169. eventView.fields = fields ? fields.map(field => ({field})) : [];
  170. return eventView;
  171. }
  172. const FilterActions = styled('div')`
  173. display: grid;
  174. gap: ${space(2)};
  175. margin-bottom: ${space(2)};
  176. @media (min-width: ${p => p.theme.breakpoints.small}) {
  177. grid-template-columns: repeat(3, min-content);
  178. }
  179. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  180. grid-template-columns: auto auto 1fr auto;
  181. }
  182. `;
  183. const StyledSearchBar = styled(SearchBar)`
  184. @media (min-width: ${p => p.theme.breakpoints.small}) {
  185. order: 1;
  186. grid-column: 1/5;
  187. }
  188. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  189. order: initial;
  190. grid-column: auto;
  191. }
  192. `;
  193. export default SpansContent;