content.tsx 10 KB

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