content.tsx 11 KB

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