content.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import React from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import DropdownControl, {DropdownItem} from 'app/components/dropdownControl';
  6. import SearchBar from 'app/components/events/searchBar';
  7. import {MAX_QUERY_LENGTH} from 'app/constants';
  8. import {t} from 'app/locale';
  9. import space from 'app/styles/space';
  10. import {GlobalSelection, Organization} from 'app/types';
  11. import {trackAnalyticsEvent} from 'app/utils/analytics';
  12. import EventView from 'app/utils/discover/eventView';
  13. import {generateAggregateFields} from 'app/utils/discover/fields';
  14. import {decodeScalar} from 'app/utils/queryString';
  15. import {stringifyQueryObject, tokenizeSearch} from 'app/utils/tokenizeSearch';
  16. import withGlobalSelection from 'app/utils/withGlobalSelection';
  17. import {FilterViews} from '../landing';
  18. import {getTransactionSearchQuery} from '../utils';
  19. import ChangedTransactions from './changedTransactions';
  20. import {TrendChangeType, TrendFunctionField, TrendView} from './types';
  21. import {
  22. DEFAULT_MAX_DURATION,
  23. getCurrentTrendFunction,
  24. getCurrentTrendParameter,
  25. getSelectedQueryKey,
  26. resetCursors,
  27. TRENDS_FUNCTIONS,
  28. TRENDS_PARAMETERS,
  29. } from './utils';
  30. type Props = {
  31. organization: Organization;
  32. location: Location;
  33. eventView: EventView;
  34. selection: GlobalSelection;
  35. setError: (msg: string | undefined) => void;
  36. };
  37. type State = {
  38. previousTrendFunction?: TrendFunctionField;
  39. };
  40. class TrendsContent extends React.Component<Props, State> {
  41. state: State = {};
  42. handleSearch = (searchQuery: string) => {
  43. const {location} = this.props;
  44. const cursors = resetCursors();
  45. browserHistory.push({
  46. pathname: location.pathname,
  47. query: {
  48. ...location.query,
  49. ...cursors,
  50. query: String(searchQuery).trim() || undefined,
  51. },
  52. });
  53. };
  54. handleTrendFunctionChange = (field: string) => {
  55. const {organization, location} = this.props;
  56. const offsets = {};
  57. Object.values(TrendChangeType).forEach(trendChangeType => {
  58. const queryKey = getSelectedQueryKey(trendChangeType);
  59. offsets[queryKey] = undefined;
  60. });
  61. trackAnalyticsEvent({
  62. eventKey: 'performance_views.trends.change_function',
  63. eventName: 'Performance Views: Change Function',
  64. organization_id: parseInt(organization.id, 10),
  65. function_name: field,
  66. });
  67. this.setState({
  68. previousTrendFunction: getCurrentTrendFunction(location).field,
  69. });
  70. const cursors = resetCursors();
  71. browserHistory.push({
  72. pathname: location.pathname,
  73. query: {
  74. ...location.query,
  75. ...offsets,
  76. ...cursors,
  77. trendFunction: field,
  78. },
  79. });
  80. };
  81. handleParameterChange = (label: string) => {
  82. const {organization, location} = this.props;
  83. const cursors = resetCursors();
  84. trackAnalyticsEvent({
  85. eventKey: 'performance_views.trends.change_parameter',
  86. eventName: 'Performance Views: Change Parameter',
  87. organization_id: parseInt(organization.id, 10),
  88. parameter_name: label,
  89. });
  90. browserHistory.push({
  91. pathname: location.pathname,
  92. query: {
  93. ...location.query,
  94. ...cursors,
  95. trendParameter: label,
  96. },
  97. });
  98. };
  99. render() {
  100. const {organization, eventView, location, setError} = this.props;
  101. const {previousTrendFunction} = this.state;
  102. const trendView = eventView.clone() as TrendView;
  103. const fields = generateAggregateFields(
  104. organization,
  105. [
  106. {
  107. field: 'absolute_correlation()',
  108. },
  109. {
  110. field: 'trend_percentage()',
  111. },
  112. {
  113. field: 'trend_difference()',
  114. },
  115. {
  116. field: 'count_percentage()',
  117. },
  118. {
  119. field: 'tpm()',
  120. },
  121. {
  122. field: 'tps()',
  123. },
  124. ],
  125. ['epm()', 'eps()']
  126. );
  127. const currentTrendFunction = getCurrentTrendFunction(location);
  128. const currentTrendParameter = getCurrentTrendParameter(location);
  129. const query = getTransactionSearchQuery(location);
  130. return (
  131. <DefaultTrends location={location} eventView={eventView}>
  132. <StyledSearchContainer>
  133. <StyledSearchBar
  134. organization={organization}
  135. projectIds={trendView.project}
  136. query={query}
  137. fields={fields}
  138. onSearch={this.handleSearch}
  139. maxQueryLength={MAX_QUERY_LENGTH}
  140. />
  141. <TrendsDropdown>
  142. <DropdownControl
  143. buttonProps={{prefix: t('Display')}}
  144. label={currentTrendFunction.label}
  145. >
  146. {TRENDS_FUNCTIONS.map(({label, field}) => (
  147. <DropdownItem
  148. key={field}
  149. onSelect={this.handleTrendFunctionChange}
  150. eventKey={field}
  151. data-test-id={field}
  152. isActive={field === currentTrendFunction.field}
  153. >
  154. {label}
  155. </DropdownItem>
  156. ))}
  157. </DropdownControl>
  158. </TrendsDropdown>
  159. <TrendsDropdown>
  160. <DropdownControl
  161. buttonProps={{prefix: t('Parameter')}}
  162. label={currentTrendParameter.label}
  163. >
  164. {TRENDS_PARAMETERS.map(({label}) => (
  165. <DropdownItem
  166. key={label}
  167. onSelect={this.handleParameterChange}
  168. eventKey={label}
  169. data-test-id={label}
  170. isActive={label === currentTrendParameter.label}
  171. >
  172. {label}
  173. </DropdownItem>
  174. ))}
  175. </DropdownControl>
  176. </TrendsDropdown>
  177. </StyledSearchContainer>
  178. <TrendsLayoutContainer>
  179. <ChangedTransactions
  180. trendChangeType={TrendChangeType.IMPROVED}
  181. previousTrendFunction={previousTrendFunction}
  182. trendView={trendView}
  183. location={location}
  184. setError={setError}
  185. />
  186. <ChangedTransactions
  187. trendChangeType={TrendChangeType.REGRESSION}
  188. previousTrendFunction={previousTrendFunction}
  189. trendView={trendView}
  190. location={location}
  191. setError={setError}
  192. />
  193. </TrendsLayoutContainer>
  194. </DefaultTrends>
  195. );
  196. }
  197. }
  198. type DefaultTrendsProps = {
  199. children: React.ReactNode[];
  200. location: Location;
  201. eventView: EventView;
  202. };
  203. class DefaultTrends extends React.Component<DefaultTrendsProps> {
  204. hasPushedDefaults = false;
  205. render() {
  206. const {children, location, eventView} = this.props;
  207. const queryString = decodeScalar(location.query.query);
  208. const trendParameter = getCurrentTrendParameter(location);
  209. const conditions = tokenizeSearch(queryString || '');
  210. if (queryString || this.hasPushedDefaults) {
  211. this.hasPushedDefaults = true;
  212. return <React.Fragment>{children}</React.Fragment>;
  213. } else {
  214. this.hasPushedDefaults = true;
  215. conditions.setTagValues('tpm()', ['>0.01']);
  216. conditions.setTagValues(trendParameter.column, ['>0', `<${DEFAULT_MAX_DURATION}`]);
  217. }
  218. const query = stringifyQueryObject(conditions);
  219. eventView.query = query;
  220. browserHistory.push({
  221. pathname: location.pathname,
  222. query: {
  223. ...location.query,
  224. cursor: undefined,
  225. query: String(query).trim() || undefined,
  226. view: FilterViews.TRENDS,
  227. },
  228. });
  229. return null;
  230. }
  231. }
  232. const StyledSearchBar = styled(SearchBar)`
  233. flex-grow: 1;
  234. margin-bottom: ${space(2)};
  235. `;
  236. const TrendsDropdown = styled('div')`
  237. margin-left: ${space(1)};
  238. flex-grow: 0;
  239. `;
  240. const StyledSearchContainer = styled('div')`
  241. display: flex;
  242. `;
  243. const TrendsLayoutContainer = styled('div')`
  244. display: grid;
  245. grid-gap: ${space(2)};
  246. @media (min-width: ${p => p.theme.breakpoints[1]}) {
  247. grid-template-columns: repeat(2, minmax(0, 1fr));
  248. align-items: stretch;
  249. }
  250. `;
  251. export default withGlobalSelection(TrendsContent);