content.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 {trackAnalyticsEvent} from 'sentry/utils/analytics';
  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. trackAnalyticsEvent({
  81. eventKey: 'performance_views.trends.change_function',
  82. eventName: 'Performance Views: Change Function',
  83. organization_id: parseInt(organization.id, 10),
  84. function_name: field,
  85. });
  86. this.setState({
  87. previousTrendFunction: getCurrentTrendFunction(location).field,
  88. });
  89. const cursors = resetCursors();
  90. browserHistory.push({
  91. pathname: location.pathname,
  92. query: {
  93. ...location.query,
  94. ...offsets,
  95. ...cursors,
  96. trendFunction: field,
  97. },
  98. });
  99. };
  100. renderError() {
  101. const {error} = this.state;
  102. if (!error) {
  103. return null;
  104. }
  105. return (
  106. <Alert type="error" showIcon>
  107. {error}
  108. </Alert>
  109. );
  110. }
  111. handleParameterChange = (label: string) => {
  112. const {organization, location} = this.props;
  113. const cursors = resetCursors();
  114. trackAnalyticsEvent({
  115. eventKey: 'performance_views.trends.change_parameter',
  116. eventName: 'Performance Views: Change Parameter',
  117. organization_id: parseInt(organization.id, 10),
  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. getPerformanceLink() {
  130. const {location} = this.props;
  131. const newQuery = {
  132. ...location.query,
  133. };
  134. const query = decodeScalar(location.query.query, '');
  135. const conditions = new MutableSearch(query);
  136. // This stops errors from occurring when navigating to other views since we are appending aggregates to the trends view
  137. conditions.removeFilter('tpm()');
  138. conditions.removeFilter('confidence()');
  139. conditions.removeFilter('transaction.duration');
  140. newQuery.query = conditions.formatString();
  141. return {
  142. pathname: getPerformanceLandingUrl(this.props.organization),
  143. query: newQuery,
  144. };
  145. }
  146. render() {
  147. const {organization, eventView, location, projects} = this.props;
  148. const {previousTrendFunction} = this.state;
  149. const trendView = eventView.clone() as TrendView;
  150. modifyTrendsViewDefaultPeriod(trendView, location);
  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. <Layout.Header>
  189. <Layout.HeaderContent>
  190. <Breadcrumbs
  191. crumbs={[
  192. {
  193. label: 'Performance',
  194. to: this.getPerformanceLink(),
  195. },
  196. {
  197. label: 'Trends',
  198. },
  199. ]}
  200. />
  201. <Layout.Title>{t('Trends')}</Layout.Title>
  202. </Layout.HeaderContent>
  203. </Layout.Header>
  204. <Layout.Body>
  205. <Layout.Main fullWidth>
  206. <DefaultTrends location={location} eventView={eventView} projects={projects}>
  207. <FilterActions>
  208. <PageFilterBar condensed>
  209. <ProjectPageFilter />
  210. <EnvironmentPageFilter />
  211. <DatePageFilter alignDropdown="left" />
  212. </PageFilterBar>
  213. <StyledSearchBar
  214. searchSource="trends"
  215. organization={organization}
  216. projectIds={trendView.project}
  217. query={query}
  218. fields={fields}
  219. onSearch={this.handleSearch}
  220. maxQueryLength={MAX_QUERY_LENGTH}
  221. />
  222. <CompactSelect
  223. triggerProps={{prefix: t('Percentile')}}
  224. value={currentTrendFunction.field}
  225. options={TRENDS_FUNCTIONS.map(({label, field}) => ({
  226. value: field,
  227. label,
  228. }))}
  229. onChange={opt => this.handleTrendFunctionChange(opt.value)}
  230. />
  231. <CompactSelect
  232. triggerProps={{prefix: t('Parameter')}}
  233. value={currentTrendParameter.label}
  234. options={TRENDS_PARAMETERS.map(({label}) => ({
  235. value: label,
  236. label,
  237. }))}
  238. onChange={opt => this.handleParameterChange(opt.value)}
  239. />
  240. </FilterActions>
  241. <ListContainer>
  242. <ChangedTransactions
  243. trendChangeType={TrendChangeType.IMPROVED}
  244. previousTrendFunction={previousTrendFunction}
  245. trendView={trendView}
  246. location={location}
  247. setError={this.setError}
  248. />
  249. <ChangedTransactions
  250. trendChangeType={TrendChangeType.REGRESSION}
  251. previousTrendFunction={previousTrendFunction}
  252. trendView={trendView}
  253. location={location}
  254. setError={this.setError}
  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 ListContainer = styled('div')`
  323. display: grid;
  324. gap: ${space(2)};
  325. @media (min-width: ${p => p.theme.breakpoints.small}) {
  326. grid-template-columns: repeat(2, minmax(0, 1fr));
  327. }
  328. `;
  329. export default withPageFilters(TrendsContent);