content.tsx 13 KB

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