index.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import type {Query} from 'history';
  4. import EventsRequest from 'sentry/components/charts/eventsRequest';
  5. import {HeaderTitleLegend} from 'sentry/components/charts/styles';
  6. import {getInterval, getSeriesSelection} from 'sentry/components/charts/utils';
  7. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import {t} from 'sentry/locale';
  10. import type {Series} from 'sentry/types/echarts';
  11. import type {
  12. EventsStats,
  13. EventsStatsData,
  14. OrganizationSummary,
  15. } from 'sentry/types/organization';
  16. import type {Project} from 'sentry/types/project';
  17. import {browserHistory} from 'sentry/utils/browserHistory';
  18. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  19. import type EventView from 'sentry/utils/discover/eventView';
  20. import {DURATION_UNITS, SIZE_UNITS} from 'sentry/utils/discover/fieldRenderers';
  21. import {getAggregateAlias} from 'sentry/utils/discover/fields';
  22. import {useMetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  23. import TrendsDiscoverQuery from 'sentry/utils/performance/trends/trendsDiscoverQuery';
  24. import useApi from 'sentry/utils/useApi';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import useRouter from 'sentry/utils/useRouter';
  27. import type {TrendFunctionField, TrendView} from 'sentry/views/performance/trends/types';
  28. import {TrendChangeType} from 'sentry/views/performance/trends/types';
  29. import {modifyTrendView, normalizeTrends} from 'sentry/views/performance/trends/utils';
  30. import generateTrendFunctionAsString from 'sentry/views/performance/trends/utils/generateTrendFunctionAsString';
  31. import type {ViewProps} from 'sentry/views/performance/types';
  32. import {getSelectedTransaction} from 'sentry/views/performance/utils/getSelectedTransaction';
  33. import Content from './content';
  34. type Props = ViewProps & {
  35. eventView: EventView;
  36. organization: OrganizationSummary;
  37. projects: Project[];
  38. queryExtra: Query;
  39. trendFunction: TrendFunctionField;
  40. trendParameter: string;
  41. withoutZerofill: boolean;
  42. withBreakpoint?: boolean;
  43. };
  44. function TrendChart({
  45. project,
  46. environment,
  47. organization,
  48. query,
  49. statsPeriod,
  50. trendFunction,
  51. trendParameter,
  52. queryExtra,
  53. withoutZerofill,
  54. withBreakpoint,
  55. eventView,
  56. start: propsStart,
  57. end: propsEnd,
  58. projects,
  59. }: Props) {
  60. const router = useRouter();
  61. const location = useLocation();
  62. const api = useApi();
  63. const theme = useTheme();
  64. const {isLoading: isCardinalityCheckLoading, outcome} = useMetricsCardinalityContext();
  65. const shouldGetBreakpoint =
  66. withBreakpoint && !isCardinalityCheckLoading && !outcome?.forceTransactionsOnly;
  67. function handleLegendSelectChanged(legendChange: {
  68. name: string;
  69. selected: Record<string, boolean>;
  70. type: string;
  71. }) {
  72. const {selected} = legendChange;
  73. const unselected = Object.keys(selected).filter(key => !selected[key]);
  74. const to = {
  75. ...location,
  76. query: {
  77. ...location.query,
  78. unselectedSeries: unselected,
  79. },
  80. };
  81. browserHistory.push(to);
  82. }
  83. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  84. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  85. const utc = normalizeDateTimeParams(location.query)?.utc === 'true';
  86. const period = statsPeriod;
  87. const legend = {
  88. right: 10,
  89. top: 0,
  90. selected: getSeriesSelection(location),
  91. };
  92. const datetimeSelection = {start, end, period};
  93. const contentCommonProps = {
  94. theme,
  95. router,
  96. start,
  97. end,
  98. utc,
  99. legend,
  100. queryExtra,
  101. period,
  102. projects: project,
  103. environments: environment,
  104. onLegendSelectChanged: handleLegendSelectChanged,
  105. };
  106. const requestCommonProps = {
  107. api,
  108. start,
  109. end,
  110. project,
  111. environment,
  112. query,
  113. period,
  114. interval: getInterval(datetimeSelection, 'high'),
  115. };
  116. const header = (
  117. <HeaderTitleLegend>
  118. {t('Trend')}
  119. <QuestionTooltip
  120. size="sm"
  121. position="top"
  122. title={t('Trends shows the smoothed value of an aggregate over time.')}
  123. />
  124. </HeaderTitleLegend>
  125. );
  126. const trendDisplay = generateTrendFunctionAsString(trendFunction, trendParameter);
  127. const trendView = eventView.clone() as TrendView;
  128. modifyTrendView(
  129. trendView,
  130. location,
  131. TrendChangeType.ANY,
  132. projects,
  133. shouldGetBreakpoint
  134. );
  135. function transformTimeseriesData(
  136. data: EventsStatsData,
  137. meta: EventsStats['meta'],
  138. seriesName: string
  139. ): Series[] {
  140. let scale = 1;
  141. if (seriesName) {
  142. const unit = meta?.units?.[getAggregateAlias(seriesName)];
  143. // Scale series values to milliseconds or bytes depending on units from meta
  144. scale = (unit && (DURATION_UNITS[unit] ?? SIZE_UNITS[unit])) ?? 1;
  145. }
  146. return [
  147. {
  148. seriesName,
  149. data: data.map(([timestamp, countsForTimestamp]) => ({
  150. name: timestamp * 1000,
  151. value: countsForTimestamp.reduce((acc, {count}) => acc + count, 0) * scale,
  152. })),
  153. },
  154. ];
  155. }
  156. return (
  157. <Fragment>
  158. {header}
  159. {shouldGetBreakpoint ? (
  160. // queries events-trends-statsv2 for breakpoint data (feature flag only)
  161. <TrendsDiscoverQuery
  162. eventView={trendView}
  163. orgSlug={organization.slug}
  164. location={location}
  165. limit={1}
  166. withBreakpoint
  167. >
  168. {({isLoading, trendsData}) => {
  169. const events = normalizeTrends(trendsData?.events?.data || []);
  170. // keep trend change type as regression until the backend can support passing the type
  171. const selectedTransaction = getSelectedTransaction(
  172. location,
  173. TrendChangeType.ANY,
  174. events
  175. );
  176. const statsData = trendsData?.stats || {};
  177. const transactionEvent = (
  178. statsData &&
  179. selectedTransaction?.project &&
  180. selectedTransaction?.transaction
  181. ? statsData[
  182. [selectedTransaction?.project, selectedTransaction?.transaction].join(
  183. ','
  184. )
  185. ]
  186. : undefined
  187. ) as EventsStats;
  188. const data = transactionEvent?.data ?? [];
  189. const meta = transactionEvent?.meta ?? ({} as EventsStats['meta']);
  190. const timeSeriesMetricsData = transformTimeseriesData(
  191. data,
  192. meta,
  193. trendDisplay
  194. );
  195. const metricsTimeFrame =
  196. transactionEvent?.start && transactionEvent.end
  197. ? {start: transactionEvent.start * 1000, end: transactionEvent.end * 1000}
  198. : undefined;
  199. return data.length !== 0 ? (
  200. <Content
  201. series={timeSeriesMetricsData}
  202. errored={!trendsData && !isLoading}
  203. loading={isLoading || isCardinalityCheckLoading}
  204. reloading={isLoading}
  205. timeFrame={metricsTimeFrame}
  206. withBreakpoint
  207. transaction={selectedTransaction}
  208. {...contentCommonProps}
  209. />
  210. ) : (
  211. // queries events-stats for trend data if metrics trend data not found
  212. <EventsRequest
  213. {...requestCommonProps}
  214. organization={organization}
  215. showLoading={false}
  216. includePrevious={false}
  217. yAxis={trendDisplay}
  218. currentSeriesNames={[trendDisplay]}
  219. partial
  220. withoutZerofill={withoutZerofill}
  221. referrer="api.performance.transaction-summary.trends-chart"
  222. >
  223. {({errored, loading, reloading, timeseriesData, timeframe}) => {
  224. return (
  225. <Content
  226. series={timeseriesData}
  227. errored={errored}
  228. loading={loading || isLoading}
  229. reloading={reloading}
  230. timeFrame={timeframe}
  231. withBreakpoint
  232. transaction={selectedTransaction}
  233. {...contentCommonProps}
  234. />
  235. );
  236. }}
  237. </EventsRequest>
  238. );
  239. }}
  240. </TrendsDiscoverQuery>
  241. ) : (
  242. <EventsRequest
  243. {...requestCommonProps}
  244. organization={organization}
  245. showLoading={false}
  246. includePrevious={false}
  247. yAxis={trendDisplay}
  248. currentSeriesNames={[trendDisplay]}
  249. partial
  250. withoutZerofill={withoutZerofill}
  251. referrer="api.performance.transaction-summary.trends-chart"
  252. >
  253. {({errored, loading, reloading, timeseriesData, timeframe: timeFrame}) => {
  254. return (
  255. <Content
  256. series={timeseriesData}
  257. errored={errored}
  258. loading={loading || isCardinalityCheckLoading}
  259. reloading={reloading}
  260. timeFrame={timeFrame}
  261. {...contentCommonProps}
  262. />
  263. );
  264. }}
  265. </EventsRequest>
  266. )}
  267. </Fragment>
  268. );
  269. }
  270. export default TrendChart;