content.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Alert} from 'sentry/components/alert';
  5. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  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 PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  13. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  14. import TransactionNameSearchBar from 'sentry/components/performance/searchBar';
  15. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  16. import {t} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import type {PageFilters} from 'sentry/types/core';
  19. import type {Organization} from 'sentry/types/organization';
  20. import type {Project} from 'sentry/types/project';
  21. import {trackAnalytics} from 'sentry/utils/analytics';
  22. import {browserHistory} from 'sentry/utils/browserHistory';
  23. import type EventView from 'sentry/utils/discover/eventView';
  24. import {generateAggregateFields} from 'sentry/utils/discover/fields';
  25. import {decodeScalar} from 'sentry/utils/queryString';
  26. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  27. import withPageFilters from 'sentry/utils/withPageFilters';
  28. import getSelectedQueryKey from 'sentry/views/performance/trends/utils/getSelectedQueryKey';
  29. import {getPerformanceLandingUrl, getTransactionSearchQuery} from '../utils';
  30. import ChangedTransactions from './changedTransactions';
  31. import type {TrendFunctionField, TrendView} from './types';
  32. import {TrendChangeType} from './types';
  33. import {
  34. DEFAULT_MAX_DURATION,
  35. DEFAULT_TRENDS_STATS_PERIOD,
  36. getCurrentTrendFunction,
  37. getCurrentTrendParameter,
  38. modifyTransactionNameTrendsQuery,
  39. modifyTrendsViewDefaultPeriod,
  40. resetCursors,
  41. TRENDS_FUNCTIONS,
  42. TRENDS_PARAMETERS,
  43. } from './utils';
  44. type Props = {
  45. eventView: EventView;
  46. location: Location;
  47. organization: Organization;
  48. projects: Project[];
  49. selection: PageFilters;
  50. };
  51. type State = {
  52. error?: string;
  53. previousTrendFunction?: TrendFunctionField;
  54. };
  55. export const defaultTrendsSelectionDate = {
  56. start: null,
  57. end: null,
  58. utc: false,
  59. period: DEFAULT_TRENDS_STATS_PERIOD,
  60. };
  61. class TrendsContent extends Component<Props, State> {
  62. state: State = {};
  63. handleSearch = (searchQuery: string) => {
  64. const {location} = this.props;
  65. const cursors = resetCursors();
  66. browserHistory.push({
  67. pathname: location.pathname,
  68. query: {
  69. ...location.query,
  70. ...cursors,
  71. query: String(searchQuery).trim() || undefined,
  72. },
  73. });
  74. };
  75. setError = (error: string | undefined) => {
  76. this.setState({error});
  77. };
  78. handleTrendFunctionChange = (field: string) => {
  79. const {organization, location} = this.props;
  80. const offsets = {};
  81. Object.values(TrendChangeType).forEach(trendChangeType => {
  82. const queryKey = getSelectedQueryKey(trendChangeType);
  83. offsets[queryKey] = undefined;
  84. });
  85. trackAnalytics('performance_views.trends.change_function', {
  86. organization,
  87. function_name: field,
  88. });
  89. this.setState({
  90. previousTrendFunction: getCurrentTrendFunction(location).field,
  91. });
  92. const cursors = resetCursors();
  93. browserHistory.push({
  94. pathname: location.pathname,
  95. query: {
  96. ...location.query,
  97. ...offsets,
  98. ...cursors,
  99. trendFunction: field,
  100. },
  101. });
  102. };
  103. renderError() {
  104. const {error} = this.state;
  105. if (!error) {
  106. return null;
  107. }
  108. return (
  109. <Alert type="error" showIcon>
  110. {error}
  111. </Alert>
  112. );
  113. }
  114. handleParameterChange = (label: string) => {
  115. const {organization, location} = this.props;
  116. const cursors = resetCursors();
  117. trackAnalytics('performance_views.trends.change_parameter', {
  118. organization,
  119. parameter_name: label,
  120. });
  121. browserHistory.push({
  122. pathname: location.pathname,
  123. query: {
  124. ...location.query,
  125. ...cursors,
  126. trendParameter: label,
  127. },
  128. });
  129. };
  130. getFreeTextFromQuery(query: string) {
  131. const conditions = new MutableSearch(query);
  132. const transactionValues = conditions.getFilterValues('transaction');
  133. if (transactionValues.length) {
  134. return transactionValues[0];
  135. }
  136. if (conditions.freeText.length > 0) {
  137. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  138. // so no need to wrap it here
  139. return conditions.freeText.join(' ');
  140. }
  141. return '';
  142. }
  143. getPerformanceLink() {
  144. const {location} = this.props;
  145. const newQuery = {
  146. ...location.query,
  147. };
  148. const query = decodeScalar(location.query.query, '');
  149. const conditions = new MutableSearch(query);
  150. // This stops errors from occurring when navigating to other views since we are appending aggregates to the trends view
  151. conditions.removeFilter('tpm()');
  152. conditions.removeFilter('confidence()');
  153. conditions.removeFilter('transaction.duration');
  154. newQuery.query = conditions.formatString();
  155. return {
  156. pathname: getPerformanceLandingUrl(this.props.organization),
  157. query: newQuery,
  158. };
  159. }
  160. render() {
  161. const {organization, eventView, location, projects} = this.props;
  162. const {previousTrendFunction} = this.state;
  163. const trendView = eventView.clone() as TrendView;
  164. modifyTrendsViewDefaultPeriod(trendView, location);
  165. if (organization.features.includes('performance-new-trends')) {
  166. modifyTransactionNameTrendsQuery(trendView);
  167. }
  168. const fields = generateAggregateFields(
  169. organization,
  170. [
  171. {
  172. field: 'absolute_correlation()',
  173. },
  174. {
  175. field: 'trend_percentage()',
  176. },
  177. {
  178. field: 'trend_difference()',
  179. },
  180. {
  181. field: 'count_percentage()',
  182. },
  183. {
  184. field: 'tpm()',
  185. },
  186. {
  187. field: 'tps()',
  188. },
  189. ],
  190. ['epm()', 'eps()']
  191. );
  192. const currentTrendFunction = getCurrentTrendFunction(location);
  193. const currentTrendParameter = getCurrentTrendParameter(
  194. location,
  195. projects,
  196. eventView.project
  197. );
  198. const query = getTransactionSearchQuery(location);
  199. return (
  200. <PageFiltersContainer
  201. defaultSelection={{
  202. datetime: defaultTrendsSelectionDate,
  203. }}
  204. >
  205. <Layout.Header>
  206. <Layout.HeaderContent>
  207. <Breadcrumbs
  208. crumbs={[
  209. {
  210. label: 'Performance',
  211. to: this.getPerformanceLink(),
  212. },
  213. {
  214. label: 'Trends',
  215. },
  216. ]}
  217. />
  218. <Layout.Title>{t('Trends')}</Layout.Title>
  219. </Layout.HeaderContent>
  220. </Layout.Header>
  221. <Layout.Body>
  222. <Layout.Main fullWidth>
  223. <DefaultTrends location={location} eventView={eventView} projects={projects}>
  224. <FilterActions>
  225. <PageFilterBar condensed>
  226. <ProjectPageFilter />
  227. <EnvironmentPageFilter />
  228. <DatePageFilter />
  229. </PageFilterBar>
  230. {organization.features.includes('performance-new-trends') ? (
  231. <StyledTransactionNameSearchBar
  232. organization={organization}
  233. eventView={trendView}
  234. onSearch={this.handleSearch}
  235. query={this.getFreeTextFromQuery(query)}
  236. />
  237. ) : (
  238. <StyledSearchBar
  239. searchSource="trends"
  240. organization={organization}
  241. projectIds={trendView.project}
  242. query={query}
  243. fields={fields}
  244. onSearch={this.handleSearch}
  245. maxQueryLength={MAX_QUERY_LENGTH}
  246. />
  247. )}
  248. <CompactSelect
  249. triggerProps={{prefix: t('Percentile')}}
  250. value={currentTrendFunction.field}
  251. options={TRENDS_FUNCTIONS.map(({label, field}) => ({
  252. value: field,
  253. label,
  254. }))}
  255. onChange={opt => this.handleTrendFunctionChange(opt.value)}
  256. />
  257. <CompactSelect
  258. triggerProps={{prefix: t('Parameter')}}
  259. value={currentTrendParameter.label}
  260. options={TRENDS_PARAMETERS.map(({label}) => ({
  261. value: label,
  262. label,
  263. }))}
  264. onChange={opt => this.handleParameterChange(opt.value)}
  265. />
  266. </FilterActions>
  267. <ListContainer>
  268. <ChangedTransactions
  269. trendChangeType={TrendChangeType.IMPROVED}
  270. previousTrendFunction={previousTrendFunction}
  271. trendView={trendView}
  272. location={location}
  273. setError={this.setError}
  274. withBreakpoint={organization.features.includes(
  275. 'performance-new-trends'
  276. )}
  277. />
  278. <ChangedTransactions
  279. trendChangeType={TrendChangeType.REGRESSION}
  280. previousTrendFunction={previousTrendFunction}
  281. trendView={trendView}
  282. location={location}
  283. setError={this.setError}
  284. withBreakpoint={organization.features.includes(
  285. 'performance-new-trends'
  286. )}
  287. />
  288. </ListContainer>
  289. </DefaultTrends>
  290. </Layout.Main>
  291. </Layout.Body>
  292. </PageFiltersContainer>
  293. );
  294. }
  295. }
  296. type DefaultTrendsProps = {
  297. children: React.ReactNode[];
  298. eventView: EventView;
  299. location: Location;
  300. projects: Project[];
  301. };
  302. class DefaultTrends extends Component<DefaultTrendsProps> {
  303. hasPushedDefaults = false;
  304. render() {
  305. const {children, location, eventView, projects} = this.props;
  306. const queryString = decodeScalar(location.query.query);
  307. const trendParameter = getCurrentTrendParameter(
  308. location,
  309. projects,
  310. eventView.project
  311. );
  312. const conditions = new MutableSearch(queryString || '');
  313. if (queryString || this.hasPushedDefaults) {
  314. this.hasPushedDefaults = true;
  315. return <Fragment>{children}</Fragment>;
  316. }
  317. this.hasPushedDefaults = true;
  318. conditions.setFilterValues('tpm()', ['>0.01']);
  319. conditions.setFilterValues(trendParameter.column, ['>0', `<${DEFAULT_MAX_DURATION}`]);
  320. const query = conditions.formatString();
  321. eventView.query = query;
  322. browserHistory.push({
  323. pathname: location.pathname,
  324. query: {
  325. ...location.query,
  326. cursor: undefined,
  327. query: String(query).trim() || undefined,
  328. },
  329. });
  330. return null;
  331. }
  332. }
  333. const FilterActions = styled('div')`
  334. display: grid;
  335. gap: ${space(2)};
  336. margin-bottom: ${space(2)};
  337. @media (min-width: ${p => p.theme.breakpoints.small}) {
  338. grid-template-columns: repeat(3, min-content);
  339. }
  340. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  341. grid-template-columns: auto 1fr auto auto;
  342. }
  343. `;
  344. const StyledSearchBar = styled(SearchBar)`
  345. @media (min-width: ${p => p.theme.breakpoints.small}) {
  346. order: 1;
  347. grid-column: 1/5;
  348. }
  349. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  350. order: initial;
  351. grid-column: auto;
  352. }
  353. `;
  354. const StyledTransactionNameSearchBar = styled(TransactionNameSearchBar)`
  355. @media (min-width: ${p => p.theme.breakpoints.small}) {
  356. order: 1;
  357. grid-column: 1/5;
  358. }
  359. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  360. order: initial;
  361. grid-column: auto;
  362. }
  363. `;
  364. const ListContainer = styled('div')`
  365. display: grid;
  366. gap: ${space(2)};
  367. margin-bottom: ${space(2)};
  368. @media (min-width: ${p => p.theme.breakpoints.small}) {
  369. grid-template-columns: repeat(2, minmax(0, 1fr));
  370. }
  371. `;
  372. export default withPageFilters(TrendsContent);