content.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 {CompactSelect} from 'sentry/components/compactSelect';
  6. import SearchBar from 'sentry/components/events/searchBar';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  9. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  10. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  13. import TransactionNameSearchBar from 'sentry/components/performance/searchBar';
  14. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {PageFilters} from 'sentry/types/core';
  18. import type {Organization} from 'sentry/types/organization';
  19. import type {Project} from 'sentry/types/project';
  20. import {trackAnalytics} from 'sentry/utils/analytics';
  21. import {browserHistory} from 'sentry/utils/browserHistory';
  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 {TrendsHeader} from 'sentry/views/performance/trends/trendsHeader';
  28. import getSelectedQueryKey from 'sentry/views/performance/trends/utils/getSelectedQueryKey';
  29. import {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: Record<string, undefined> = {};
  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. render() {
  144. const {organization, eventView, location, projects} = this.props;
  145. const {previousTrendFunction} = this.state;
  146. const trendView = eventView.clone() as TrendView;
  147. modifyTrendsViewDefaultPeriod(trendView, location);
  148. if (organization.features.includes('performance-new-trends')) {
  149. modifyTransactionNameTrendsQuery(trendView);
  150. }
  151. const fields = generateAggregateFields(
  152. organization,
  153. [
  154. {
  155. field: 'absolute_correlation()',
  156. },
  157. {
  158. field: 'trend_percentage()',
  159. },
  160. {
  161. field: 'trend_difference()',
  162. },
  163. {
  164. field: 'count_percentage()',
  165. },
  166. {
  167. field: 'tpm()',
  168. },
  169. {
  170. field: 'tps()',
  171. },
  172. ],
  173. ['epm()', 'eps()']
  174. );
  175. const currentTrendFunction = getCurrentTrendFunction(location);
  176. const currentTrendParameter = getCurrentTrendParameter(
  177. location,
  178. projects,
  179. eventView.project
  180. );
  181. const query = getTransactionSearchQuery(location);
  182. return (
  183. <PageFiltersContainer
  184. defaultSelection={{
  185. datetime: defaultTrendsSelectionDate,
  186. }}
  187. >
  188. <TrendsHeader />
  189. <Layout.Body>
  190. <Layout.Main fullWidth>
  191. <DefaultTrends location={location} eventView={eventView} projects={projects}>
  192. <FilterActions>
  193. <PageFilterBar condensed>
  194. <ProjectPageFilter />
  195. <EnvironmentPageFilter />
  196. <DatePageFilter />
  197. </PageFilterBar>
  198. {organization.features.includes('performance-new-trends') ? (
  199. <StyledTransactionNameSearchBar
  200. organization={organization}
  201. eventView={trendView}
  202. onSearch={this.handleSearch}
  203. query={this.getFreeTextFromQuery(query)}
  204. />
  205. ) : (
  206. <StyledSearchBar
  207. searchSource="trends"
  208. organization={organization}
  209. projectIds={trendView.project}
  210. query={query}
  211. fields={fields}
  212. onSearch={this.handleSearch}
  213. maxQueryLength={MAX_QUERY_LENGTH}
  214. />
  215. )}
  216. <CompactSelect
  217. triggerProps={{prefix: t('Percentile')}}
  218. value={currentTrendFunction.field}
  219. options={TRENDS_FUNCTIONS.map(({label, field}) => ({
  220. value: field,
  221. label,
  222. }))}
  223. onChange={opt => this.handleTrendFunctionChange(opt.value)}
  224. />
  225. <CompactSelect
  226. triggerProps={{prefix: t('Parameter')}}
  227. value={currentTrendParameter.label}
  228. options={TRENDS_PARAMETERS.map(({label}) => ({
  229. value: label,
  230. label,
  231. }))}
  232. onChange={opt => this.handleParameterChange(opt.value)}
  233. />
  234. </FilterActions>
  235. <ListContainer>
  236. <ChangedTransactions
  237. trendChangeType={TrendChangeType.IMPROVED}
  238. previousTrendFunction={previousTrendFunction}
  239. trendView={trendView}
  240. location={location}
  241. setError={this.setError}
  242. withBreakpoint={organization.features.includes(
  243. 'performance-new-trends'
  244. )}
  245. />
  246. <ChangedTransactions
  247. trendChangeType={TrendChangeType.REGRESSION}
  248. previousTrendFunction={previousTrendFunction}
  249. trendView={trendView}
  250. location={location}
  251. setError={this.setError}
  252. withBreakpoint={organization.features.includes(
  253. 'performance-new-trends'
  254. )}
  255. />
  256. </ListContainer>
  257. </DefaultTrends>
  258. </Layout.Main>
  259. </Layout.Body>
  260. </PageFiltersContainer>
  261. );
  262. }
  263. }
  264. type DefaultTrendsProps = {
  265. children: React.ReactNode[];
  266. eventView: EventView;
  267. location: Location;
  268. projects: Project[];
  269. };
  270. class DefaultTrends extends Component<DefaultTrendsProps> {
  271. hasPushedDefaults = false;
  272. render() {
  273. const {children, location, eventView, projects} = this.props;
  274. const queryString = decodeScalar(location.query.query);
  275. const trendParameter = getCurrentTrendParameter(
  276. location,
  277. projects,
  278. eventView.project
  279. );
  280. const conditions = new MutableSearch(queryString || '');
  281. if (queryString || this.hasPushedDefaults) {
  282. this.hasPushedDefaults = true;
  283. return <Fragment>{children}</Fragment>;
  284. }
  285. this.hasPushedDefaults = true;
  286. conditions.setFilterValues('tpm()', ['>0.01']);
  287. conditions.setFilterValues(trendParameter.column, ['>0', `<${DEFAULT_MAX_DURATION}`]);
  288. const query = conditions.formatString();
  289. eventView.query = query;
  290. browserHistory.push({
  291. pathname: location.pathname,
  292. query: {
  293. ...location.query,
  294. cursor: undefined,
  295. query: String(query).trim() || undefined,
  296. },
  297. });
  298. return null;
  299. }
  300. }
  301. const FilterActions = styled('div')`
  302. display: grid;
  303. gap: ${space(2)};
  304. margin-bottom: ${space(2)};
  305. @media (min-width: ${p => p.theme.breakpoints.small}) {
  306. grid-template-columns: repeat(3, min-content);
  307. }
  308. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  309. grid-template-columns: auto 1fr auto auto;
  310. }
  311. `;
  312. const StyledSearchBar = styled(SearchBar)`
  313. @media (min-width: ${p => p.theme.breakpoints.small}) {
  314. order: 1;
  315. grid-column: 1/5;
  316. }
  317. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  318. order: initial;
  319. grid-column: auto;
  320. }
  321. `;
  322. const StyledTransactionNameSearchBar = styled(TransactionNameSearchBar)`
  323. @media (min-width: ${p => p.theme.breakpoints.small}) {
  324. order: 1;
  325. grid-column: 1/5;
  326. }
  327. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  328. order: initial;
  329. grid-column: auto;
  330. }
  331. `;
  332. const ListContainer = styled('div')`
  333. display: grid;
  334. gap: ${space(2)};
  335. margin-bottom: ${space(2)};
  336. @media (min-width: ${p => p.theme.breakpoints.small}) {
  337. grid-template-columns: repeat(2, minmax(0, 1fr));
  338. }
  339. `;
  340. export default withPageFilters(TrendsContent);