content.tsx 11 KB

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