databaseLandingPage.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import React, {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Alert from 'sentry/components/alert';
  5. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  6. import FloatingFeedbackWidget from 'sentry/components/feedback/widget/floatingFeedbackWidget';
  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 SearchBar from 'sentry/components/searchBar';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {fromSorts} from 'sentry/utils/discover/eventView';
  16. import {decodeScalar} from 'sentry/utils/queryString';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  20. import {useOnboardingProject} from 'sentry/views/performance/browser/webVitals/utils/useOnboardingProject';
  21. import {DurationChart} from 'sentry/views/performance/database/durationChart';
  22. import {NoDataMessage} from 'sentry/views/performance/database/noDataMessage';
  23. import {isAValidSort, QueriesTable} from 'sentry/views/performance/database/queriesTable';
  24. import {ThroughputChart} from 'sentry/views/performance/database/throughputChart';
  25. import {useSelectedDurationAggregate} from 'sentry/views/performance/database/useSelectedDurationAggregate';
  26. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  27. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  28. import Onboarding from 'sentry/views/performance/onboarding';
  29. import {useSynchronizeCharts} from 'sentry/views/starfish/components/chart';
  30. import {useSpanMetrics} from 'sentry/views/starfish/queries/useSpanMetrics';
  31. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  32. import {ModuleName, SpanMetricsField} from 'sentry/views/starfish/types';
  33. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  34. import {ActionSelector} from 'sentry/views/starfish/views/spans/selectors/actionSelector';
  35. import {DomainSelector} from 'sentry/views/starfish/views/spans/selectors/domainSelector';
  36. export function DatabaseLandingPage() {
  37. const organization = useOrganization();
  38. const moduleName = ModuleName.DB;
  39. const location = useLocation();
  40. const onboardingProject = useOnboardingProject();
  41. const [selectedAggregate] = useSelectedDurationAggregate();
  42. const spanDescription = decodeScalar(location.query?.['span.description'], '');
  43. const spanAction = decodeScalar(location.query?.['span.action']);
  44. const spanDomain = decodeScalar(location.query?.['span.domain']);
  45. const sortField = decodeScalar(location.query?.[QueryParameterNames.SPANS_SORT]);
  46. let sort = fromSorts(sortField).filter(isAValidSort)[0];
  47. if (!sort) {
  48. sort = DEFAULT_SORT;
  49. }
  50. const handleSearch = (newQuery: string) => {
  51. browserHistory.push({
  52. ...location,
  53. query: {
  54. ...location.query,
  55. 'span.description': newQuery === '' ? undefined : newQuery,
  56. [QueryParameterNames.SPANS_CURSOR]: undefined,
  57. },
  58. });
  59. };
  60. const chartFilters = {
  61. 'span.module': ModuleName.DB,
  62. has: 'span.description',
  63. };
  64. const tableFilters = {
  65. 'span.module': ModuleName.DB,
  66. 'span.action': spanAction,
  67. 'span.domain': spanDomain,
  68. 'span.description': spanDescription ? `*${spanDescription}*` : undefined,
  69. has: 'span.description',
  70. };
  71. const cursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  72. const queryListResponse = useSpanMetrics({
  73. filters: tableFilters,
  74. fields: [
  75. 'project.id',
  76. 'span.group',
  77. 'span.description',
  78. 'spm()',
  79. 'avg(span.self_time)',
  80. 'sum(span.self_time)',
  81. 'time_spent_percentage()',
  82. ],
  83. sorts: [sort],
  84. limit: LIMIT,
  85. cursor,
  86. referrer: 'api.starfish.use-span-list',
  87. });
  88. const {
  89. isLoading: isThroughputDataLoading,
  90. data: throughputData,
  91. error: throughputError,
  92. } = useSpanMetricsSeries({
  93. filters: chartFilters,
  94. yAxis: ['spm()'],
  95. referrer: 'api.starfish.span-landing-page-metrics-chart',
  96. });
  97. const {
  98. isLoading: isDurationDataLoading,
  99. data: durationData,
  100. error: durationError,
  101. } = useSpanMetricsSeries({
  102. filters: chartFilters,
  103. yAxis: [`${selectedAggregate}(${SpanMetricsField.SPAN_SELF_TIME})`],
  104. referrer: 'api.starfish.span-landing-page-metrics-chart',
  105. });
  106. const isCriticalDataLoading =
  107. isThroughputDataLoading || isDurationDataLoading || queryListResponse.isLoading;
  108. const isAnyCriticalDataAvailable =
  109. (queryListResponse.data ?? []).length > 0 ||
  110. durationData[`${selectedAggregate}(span.self_time)`].data?.some(
  111. ({value}) => value > 0
  112. ) ||
  113. throughputData['spm()'].data?.some(({value}) => value > 0);
  114. useSynchronizeCharts([!isThroughputDataLoading && !isDurationDataLoading]);
  115. return (
  116. <React.Fragment>
  117. <Layout.Header>
  118. <Layout.HeaderContent>
  119. <Breadcrumbs
  120. crumbs={[
  121. {
  122. label: 'Performance',
  123. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  124. preservePageFilters: true,
  125. },
  126. {
  127. label: 'Queries',
  128. },
  129. ]}
  130. />
  131. <Layout.Title>{t('Queries')}</Layout.Title>
  132. </Layout.HeaderContent>
  133. </Layout.Header>
  134. <Layout.Body>
  135. <Layout.Main fullWidth>
  136. <ModuleLayout.Layout>
  137. {!onboardingProject && !isCriticalDataLoading && (
  138. <ModuleLayout.Full>
  139. <NoDataMessage
  140. Wrapper={AlertBanner}
  141. isDataAvailable={isAnyCriticalDataAvailable}
  142. />
  143. </ModuleLayout.Full>
  144. )}
  145. <FloatingFeedbackWidget />
  146. <ModuleLayout.Full>
  147. <PageFilterBar condensed>
  148. <ProjectPageFilter />
  149. <EnvironmentPageFilter />
  150. <DatePageFilter />
  151. </PageFilterBar>
  152. </ModuleLayout.Full>
  153. {onboardingProject && (
  154. <ModuleLayout.Full>
  155. <Onboarding organization={organization} project={onboardingProject} />
  156. </ModuleLayout.Full>
  157. )}
  158. {!onboardingProject && (
  159. <Fragment>
  160. <ModuleLayout.Half>
  161. <ThroughputChart
  162. series={throughputData['spm()']}
  163. isLoading={isThroughputDataLoading}
  164. error={throughputError}
  165. />
  166. </ModuleLayout.Half>
  167. <ModuleLayout.Half>
  168. <DurationChart
  169. series={durationData[`${selectedAggregate}(span.self_time)`]}
  170. isLoading={isDurationDataLoading}
  171. error={durationError}
  172. />
  173. </ModuleLayout.Half>
  174. <ModuleLayout.Full>
  175. <FilterOptionsContainer>
  176. <SelectorContainer>
  177. <ActionSelector moduleName={moduleName} value={spanAction ?? ''} />
  178. </SelectorContainer>
  179. <SelectorContainer>
  180. <DomainSelector moduleName={moduleName} value={spanDomain ?? ''} />
  181. </SelectorContainer>
  182. </FilterOptionsContainer>
  183. </ModuleLayout.Full>
  184. <ModuleLayout.Full>
  185. <SearchBar
  186. query={spanDescription}
  187. placeholder={t('Search for more Queries')}
  188. onSearch={handleSearch}
  189. />
  190. </ModuleLayout.Full>
  191. <ModuleLayout.Full>
  192. <QueriesTable response={queryListResponse} sort={sort} />
  193. </ModuleLayout.Full>
  194. </Fragment>
  195. )}
  196. </ModuleLayout.Layout>
  197. </Layout.Main>
  198. </Layout.Body>
  199. </React.Fragment>
  200. );
  201. }
  202. const DEFAULT_SORT = {
  203. field: 'time_spent_percentage()' as const,
  204. kind: 'desc' as const,
  205. };
  206. function AlertBanner(props) {
  207. return <Alert {...props} type="info" showIcon />;
  208. }
  209. const FilterOptionsContainer = styled('div')`
  210. display: flex;
  211. flex-wrap: wrap;
  212. gap: ${space(2)};
  213. @media (min-width: ${p => p.theme.breakpoints.small}) {
  214. flex-wrap: nowrap;
  215. }
  216. `;
  217. const SelectorContainer = styled('div')`
  218. flex-basis: 100%;
  219. @media (min-width: ${p => p.theme.breakpoints.small}) {
  220. flex-basis: auto;
  221. }
  222. `;
  223. const LIMIT: number = 25;
  224. function LandingPageWithProviders() {
  225. return (
  226. <ModulePageProviders
  227. title={[t('Performance'), t('Database')].join(' — ')}
  228. baseURL="/performance/database"
  229. features="performance-database-view"
  230. >
  231. <DatabaseLandingPage />
  232. </ModulePageProviders>
  233. );
  234. }
  235. export default LandingPageWithProviders;