content.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 SearchBar from 'sentry/components/events/searchBar';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  10. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  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. maxPickableDays={SPAN_RETENTION_DAYS}
  98. relativeOptions={SPAN_RELATIVE_PERIODS}
  99. />
  100. </PageFilterBar>
  101. <StyledSearchBar
  102. organization={organization}
  103. projectIds={eventView.project}
  104. query={query}
  105. fields={eventView.fields}
  106. onSearch={handleChange('query')}
  107. />
  108. <CompactSelect
  109. value={sort.field}
  110. options={SPAN_SORT_OPTIONS.map(opt => ({value: opt.field, label: opt.label}))}
  111. onChange={opt => handleChange('sort')(opt.value)}
  112. triggerProps={{prefix: sort.prefix}}
  113. triggerLabel={sort.label}
  114. />
  115. </FilterActions>
  116. <DiscoverQuery
  117. eventView={totalsView}
  118. orgSlug={organization.slug}
  119. location={location}
  120. referrer="api.performance.transaction-spans"
  121. cursor="0:0:1"
  122. noPagination
  123. >
  124. {({tableData}) => {
  125. const totals: SpansTotalValues | null =
  126. (tableData?.data?.[0] as SpansTotalValues | undefined) ?? null;
  127. return (
  128. <SuspectSpansQuery
  129. location={location}
  130. orgSlug={organization.slug}
  131. eventView={spansView}
  132. limit={10}
  133. perSuspect={0}
  134. spanOps={defined(spanOp) ? [spanOp] : []}
  135. spanGroups={defined(spanGroup) ? [spanGroup] : []}
  136. >
  137. {({suspectSpans, isLoading, pageLinks}) => (
  138. <Fragment>
  139. <VisuallyCompleteWithData
  140. id="TransactionSpans-SuspectSpansTable"
  141. hasData={!!suspectSpans?.length}
  142. isLoading={isLoading}
  143. >
  144. <SuspectSpansTable
  145. location={location}
  146. organization={organization}
  147. transactionName={transactionName}
  148. project={projects.find(p => p.id === projectId)}
  149. isLoading={isLoading}
  150. suspectSpans={suspectSpans ?? []}
  151. totals={totals}
  152. sort={sort.field}
  153. />
  154. </VisuallyCompleteWithData>
  155. <Pagination pageLinks={pageLinks ?? null} />
  156. </Fragment>
  157. )}
  158. </SuspectSpansQuery>
  159. );
  160. }}
  161. </DiscoverQuery>
  162. </Layout.Main>
  163. );
  164. }
  165. function getSpansEventView(eventView: EventView, sort: SpanSort): EventView {
  166. eventView = eventView.clone();
  167. const fields = SPAN_SORT_TO_FIELDS[sort];
  168. eventView.fields = fields ? fields.map(field => ({field})) : [];
  169. return eventView;
  170. }
  171. const FilterActions = styled('div')`
  172. display: grid;
  173. gap: ${space(2)};
  174. margin-bottom: ${space(2)};
  175. @media (min-width: ${p => p.theme.breakpoints.small}) {
  176. grid-template-columns: repeat(3, min-content);
  177. }
  178. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  179. grid-template-columns: auto auto 1fr auto;
  180. }
  181. `;
  182. const StyledSearchBar = styled(SearchBar)`
  183. @media (min-width: ${p => p.theme.breakpoints.small}) {
  184. order: 1;
  185. grid-column: 1/5;
  186. }
  187. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  188. order: initial;
  189. grid-column: auto;
  190. }
  191. `;
  192. export default SpansContent;