content.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {CompactSelect} from 'sentry/components/compactSelect';
  5. import {Alert} from 'sentry/components/core/alert';
  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.Container>
  110. <Alert type="error" showIcon>
  111. {error}
  112. </Alert>
  113. </Alert.Container>
  114. );
  115. }
  116. handleParameterChange = (label: string) => {
  117. const {organization, location} = this.props;
  118. const cursors = resetCursors();
  119. trackAnalytics('performance_views.trends.change_parameter', {
  120. organization,
  121. parameter_name: label,
  122. });
  123. browserHistory.push({
  124. pathname: location.pathname,
  125. query: {
  126. ...location.query,
  127. ...cursors,
  128. trendParameter: label,
  129. },
  130. });
  131. };
  132. getFreeTextFromQuery(query: string) {
  133. const conditions = new MutableSearch(query);
  134. const transactionValues = conditions.getFilterValues('transaction');
  135. if (transactionValues.length) {
  136. return transactionValues[0];
  137. }
  138. if (conditions.freeText.length > 0) {
  139. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  140. // so no need to wrap it here
  141. return conditions.freeText.join(' ');
  142. }
  143. return '';
  144. }
  145. render() {
  146. const {organization, eventView, location, projects} = this.props;
  147. const {previousTrendFunction} = this.state;
  148. const trendView = eventView.clone() as TrendView;
  149. modifyTrendsViewDefaultPeriod(trendView, location);
  150. if (organization.features.includes('performance-new-trends')) {
  151. modifyTransactionNameTrendsQuery(trendView);
  152. }
  153. const fields = generateAggregateFields(
  154. organization,
  155. [
  156. {
  157. field: 'absolute_correlation()',
  158. },
  159. {
  160. field: 'trend_percentage()',
  161. },
  162. {
  163. field: 'trend_difference()',
  164. },
  165. {
  166. field: 'count_percentage()',
  167. },
  168. {
  169. field: 'tpm()',
  170. },
  171. {
  172. field: 'tps()',
  173. },
  174. ],
  175. ['epm()', 'eps()']
  176. );
  177. const currentTrendFunction = getCurrentTrendFunction(location);
  178. const currentTrendParameter = getCurrentTrendParameter(
  179. location,
  180. projects,
  181. eventView.project
  182. );
  183. const query = getTransactionSearchQuery(location);
  184. return (
  185. <PageFiltersContainer
  186. defaultSelection={{
  187. datetime: defaultTrendsSelectionDate,
  188. }}
  189. >
  190. <TrendsHeader />
  191. <Layout.Body>
  192. <Layout.Main fullWidth>
  193. <DefaultTrends location={location} eventView={eventView} projects={projects}>
  194. <FilterActions>
  195. <PageFilterBar condensed>
  196. <ProjectPageFilter />
  197. <EnvironmentPageFilter />
  198. <DatePageFilter />
  199. </PageFilterBar>
  200. {organization.features.includes('performance-new-trends') ? (
  201. <StyledTransactionNameSearchBar
  202. organization={organization}
  203. eventView={trendView}
  204. onSearch={this.handleSearch}
  205. query={this.getFreeTextFromQuery(query)!}
  206. />
  207. ) : (
  208. <StyledSearchBar
  209. searchSource="trends"
  210. organization={organization}
  211. projectIds={trendView.project}
  212. query={query}
  213. fields={fields}
  214. onSearch={this.handleSearch}
  215. maxQueryLength={MAX_QUERY_LENGTH}
  216. />
  217. )}
  218. <CompactSelect
  219. triggerProps={{prefix: t('Percentile')}}
  220. value={currentTrendFunction.field}
  221. options={TRENDS_FUNCTIONS.map(({label, field}) => ({
  222. value: field,
  223. label,
  224. }))}
  225. onChange={opt => this.handleTrendFunctionChange(opt.value)}
  226. />
  227. <CompactSelect
  228. triggerProps={{prefix: t('Parameter')}}
  229. value={currentTrendParameter.label}
  230. options={TRENDS_PARAMETERS.map(({label}) => ({
  231. value: label,
  232. label,
  233. }))}
  234. onChange={opt => this.handleParameterChange(opt.value)}
  235. />
  236. </FilterActions>
  237. <ListContainer>
  238. <ChangedTransactions
  239. trendChangeType={TrendChangeType.IMPROVED}
  240. previousTrendFunction={previousTrendFunction}
  241. trendView={trendView}
  242. location={location}
  243. setError={this.setError}
  244. withBreakpoint={organization.features.includes(
  245. 'performance-new-trends'
  246. )}
  247. />
  248. <ChangedTransactions
  249. trendChangeType={TrendChangeType.REGRESSION}
  250. previousTrendFunction={previousTrendFunction}
  251. trendView={trendView}
  252. location={location}
  253. setError={this.setError}
  254. withBreakpoint={organization.features.includes(
  255. 'performance-new-trends'
  256. )}
  257. />
  258. </ListContainer>
  259. </DefaultTrends>
  260. </Layout.Main>
  261. </Layout.Body>
  262. </PageFiltersContainer>
  263. );
  264. }
  265. }
  266. type DefaultTrendsProps = {
  267. children: React.ReactNode[];
  268. eventView: EventView;
  269. location: Location;
  270. projects: Project[];
  271. };
  272. class DefaultTrends extends Component<DefaultTrendsProps> {
  273. hasPushedDefaults = false;
  274. render() {
  275. const {children, location, eventView, projects} = this.props;
  276. const queryString = decodeScalar(location.query.query);
  277. const trendParameter = getCurrentTrendParameter(
  278. location,
  279. projects,
  280. eventView.project
  281. );
  282. const conditions = new MutableSearch(queryString || '');
  283. if (queryString || this.hasPushedDefaults) {
  284. this.hasPushedDefaults = true;
  285. return <Fragment>{children}</Fragment>;
  286. }
  287. this.hasPushedDefaults = true;
  288. conditions.setFilterValues('tpm()', ['>0.01']);
  289. conditions.setFilterValues(trendParameter.column, ['>0', `<${DEFAULT_MAX_DURATION}`]);
  290. const query = conditions.formatString();
  291. eventView.query = query;
  292. browserHistory.push({
  293. pathname: location.pathname,
  294. query: {
  295. ...location.query,
  296. cursor: undefined,
  297. query: String(query).trim() || undefined,
  298. },
  299. });
  300. return null;
  301. }
  302. }
  303. const FilterActions = styled('div')`
  304. display: grid;
  305. gap: ${space(2)};
  306. margin-bottom: ${space(2)};
  307. @media (min-width: ${p => p.theme.breakpoints.small}) {
  308. grid-template-columns: repeat(3, min-content);
  309. }
  310. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  311. grid-template-columns: auto 1fr auto auto;
  312. }
  313. `;
  314. const StyledSearchBar = styled(SearchBar)`
  315. @media (min-width: ${p => p.theme.breakpoints.small}) {
  316. order: 1;
  317. grid-column: 1/5;
  318. }
  319. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  320. order: initial;
  321. grid-column: auto;
  322. }
  323. `;
  324. const StyledTransactionNameSearchBar = styled(TransactionNameSearchBar)`
  325. @media (min-width: ${p => p.theme.breakpoints.small}) {
  326. order: 1;
  327. grid-column: 1/5;
  328. }
  329. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  330. order: initial;
  331. grid-column: auto;
  332. }
  333. `;
  334. const ListContainer = styled('div')`
  335. display: grid;
  336. gap: ${space(2)};
  337. margin-bottom: ${space(2)};
  338. @media (min-width: ${p => p.theme.breakpoints.small}) {
  339. grid-template-columns: repeat(2, minmax(0, 1fr));
  340. }
  341. `;
  342. export default withPageFilters(TrendsContent);