cacheLandingPage.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import React from 'react';
  2. import keyBy from 'lodash/keyBy';
  3. import FeatureBadge from 'sentry/components/badge/featureBadge';
  4. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  9. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  10. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  11. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  12. import {t} from 'sentry/locale';
  13. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  14. import {decodeScalar, decodeSorts} from 'sentry/utils/queryString';
  15. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  16. import {useLocation} from 'sentry/utils/useLocation';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  19. import {CacheHitMissChart} from 'sentry/views/performance/cache/charts/hitMissChart';
  20. import {ThroughputChart} from 'sentry/views/performance/cache/charts/throughputChart';
  21. import {Referrer} from 'sentry/views/performance/cache/referrers';
  22. import {CacheSamplePanel} from 'sentry/views/performance/cache/samplePanel/samplePanel';
  23. import {
  24. BASE_FILTERS,
  25. MODULE_TITLE,
  26. ONBOARDING_CONTENT,
  27. RELEASE_LEVEL,
  28. } from 'sentry/views/performance/cache/settings';
  29. import {
  30. isAValidSort,
  31. TransactionsTable,
  32. } from 'sentry/views/performance/cache/tables/transactionsTable';
  33. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  34. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  35. import {ModulesOnboarding} from 'sentry/views/performance/onboarding/modulesOnboarding';
  36. import {OnboardingContent} from 'sentry/views/performance/onboarding/onboardingContent';
  37. import {useMetrics, useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
  38. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useDiscoverSeries';
  39. import {SpanFunction, SpanMetricsField} from 'sentry/views/starfish/types';
  40. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  41. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  42. const {CACHE_MISS_RATE} = SpanFunction;
  43. const {CACHE_ITEM_SIZE} = SpanMetricsField;
  44. export function CacheLandingPage() {
  45. const organization = useOrganization();
  46. const location = useLocation();
  47. const sortField = decodeScalar(location.query?.[QueryParameterNames.TRANSACTIONS_SORT]);
  48. const sort = decodeSorts(sortField).filter(isAValidSort).at(0) ?? DEFAULT_SORT;
  49. const cursor = decodeScalar(location.query?.[QueryParameterNames.TRANSACTIONS_CURSOR]);
  50. const {
  51. isLoading: isCacheHitRateLoading,
  52. data: cacheHitRateData,
  53. error: cacheHitRateError,
  54. } = useSpanMetricsSeries(
  55. {
  56. yAxis: [`${CACHE_MISS_RATE}()`],
  57. search: MutableSearch.fromQueryObject(BASE_FILTERS),
  58. },
  59. Referrer.LANDING_CACHE_HIT_MISS_CHART
  60. );
  61. const {
  62. isLoading: isThroughputDataLoading,
  63. data: throughputData,
  64. error: throughputError,
  65. } = useSpanMetricsSeries(
  66. {
  67. search: MutableSearch.fromQueryObject(BASE_FILTERS),
  68. yAxis: ['spm()'],
  69. },
  70. Referrer.LANDING_CACHE_THROUGHPUT_CHART
  71. );
  72. const {
  73. isFetching: isTransactionsListFetching,
  74. data: transactionsList,
  75. meta: transactionsListMeta,
  76. error: transactionsListError,
  77. pageLinks: transactionsListPageLinks,
  78. } = useSpanMetrics(
  79. {
  80. search: MutableSearch.fromQueryObject(BASE_FILTERS),
  81. fields: [
  82. 'project',
  83. 'project.id',
  84. 'transaction',
  85. 'spm()',
  86. `${CACHE_MISS_RATE}()`,
  87. 'sum(span.self_time)',
  88. 'time_spent_percentage()',
  89. `avg(${CACHE_ITEM_SIZE})`,
  90. ],
  91. sorts: [sort],
  92. cursor,
  93. limit: TRANSACTIONS_TABLE_ROW_COUNT,
  94. },
  95. Referrer.LANDING_CACHE_TRANSACTION_LIST
  96. );
  97. const {
  98. data: transactionDurationData,
  99. error: transactionDurationError,
  100. meta: transactionDurationMeta,
  101. isFetching: isTransactionDurationFetching,
  102. } = useMetrics(
  103. {
  104. search: `transaction:[${transactionsList.map(({transaction}) => `"${transaction}"`).join(',')}]`,
  105. fields: [`avg(transaction.duration)`, 'transaction'],
  106. enabled: !isTransactionsListFetching && transactionsList.length > 0,
  107. },
  108. Referrer.LANDING_CACHE_TRANSACTION_DURATION
  109. );
  110. const transactionDurationsMap = keyBy(transactionDurationData, 'transaction');
  111. const transactionsListWithDuration =
  112. transactionsList?.map(transaction => ({
  113. ...transaction,
  114. 'avg(transaction.duration)':
  115. transactionDurationsMap[transaction.transaction]?.['avg(transaction.duration)'],
  116. })) || [];
  117. const meta = combineMeta(transactionsListMeta, transactionDurationMeta);
  118. addCustomMeta(meta);
  119. return (
  120. <React.Fragment>
  121. <Layout.Header>
  122. <Layout.HeaderContent>
  123. <Breadcrumbs
  124. crumbs={[
  125. {
  126. label: t('Performance'),
  127. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  128. preservePageFilters: true,
  129. },
  130. {
  131. label: MODULE_TITLE,
  132. },
  133. ]}
  134. />
  135. <Layout.Title>
  136. {MODULE_TITLE}
  137. <FeatureBadge type={RELEASE_LEVEL} />
  138. </Layout.Title>
  139. </Layout.HeaderContent>
  140. <Layout.HeaderActions>
  141. <ButtonBar gap={1}>
  142. <FeedbackWidgetButton />
  143. </ButtonBar>
  144. </Layout.HeaderActions>
  145. </Layout.Header>
  146. <Layout.Body>
  147. <Layout.Main fullWidth>
  148. <ModuleLayout.Layout>
  149. <ModuleLayout.Full>
  150. <PageFilterBar condensed>
  151. <ProjectPageFilter />
  152. <EnvironmentPageFilter />
  153. <DatePageFilter />
  154. </PageFilterBar>
  155. </ModuleLayout.Full>
  156. <ModulesOnboarding
  157. moduleQueryFilter={MutableSearch.fromQueryObject(BASE_FILTERS)}
  158. onboardingContent={<OnboardingContent {...ONBOARDING_CONTENT} />}
  159. referrer={Referrer.LANDING_CACHE_ONBOARDING}
  160. >
  161. <ModuleLayout.Half>
  162. <CacheHitMissChart
  163. series={{
  164. seriesName: DataTitles.cacheMissRate,
  165. data: cacheHitRateData[`${CACHE_MISS_RATE}()`]?.data,
  166. }}
  167. isLoading={isCacheHitRateLoading}
  168. error={cacheHitRateError}
  169. />
  170. </ModuleLayout.Half>
  171. <ModuleLayout.Half>
  172. <ThroughputChart
  173. series={throughputData['spm()']}
  174. isLoading={isThroughputDataLoading}
  175. error={throughputError}
  176. />
  177. </ModuleLayout.Half>
  178. <ModuleLayout.Full>
  179. <TransactionsTable
  180. data={transactionsListWithDuration}
  181. isLoading={isTransactionsListFetching || isTransactionDurationFetching}
  182. sort={sort}
  183. error={transactionsListError || transactionDurationError}
  184. meta={meta}
  185. pageLinks={transactionsListPageLinks}
  186. />
  187. </ModuleLayout.Full>
  188. </ModulesOnboarding>
  189. </ModuleLayout.Layout>
  190. </Layout.Main>
  191. </Layout.Body>
  192. <CacheSamplePanel />
  193. </React.Fragment>
  194. );
  195. }
  196. function PageWithProviders() {
  197. return (
  198. <ModulePageProviders
  199. title={[t('Performance'), MODULE_TITLE].join(' — ')}
  200. features="performance-cache-view"
  201. >
  202. <CacheLandingPage />
  203. </ModulePageProviders>
  204. );
  205. }
  206. export default PageWithProviders;
  207. const combineMeta = (
  208. meta1?: EventsMetaType,
  209. meta2?: EventsMetaType
  210. ): EventsMetaType | undefined => {
  211. if (!meta1 && !meta2) {
  212. return undefined;
  213. }
  214. if (!meta1) {
  215. return meta2;
  216. }
  217. if (!meta2) {
  218. return meta1;
  219. }
  220. return {
  221. fields: {...meta1.fields, ...meta2.fields},
  222. units: {...meta1.units, ...meta2.units},
  223. };
  224. };
  225. // TODO - this should come from the backend
  226. const addCustomMeta = (meta?: EventsMetaType) => {
  227. if (meta) {
  228. meta.fields[`avg(${CACHE_ITEM_SIZE})`] = 'size';
  229. meta.units[`avg(${CACHE_ITEM_SIZE})`] = 'byte';
  230. }
  231. };
  232. const DEFAULT_SORT = {
  233. field: 'time_spent_percentage()' as const,
  234. kind: 'desc' as const,
  235. };
  236. const TRANSACTIONS_TABLE_ROW_COUNT = 20;