databaseLandingPage.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import React, {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import pickBy from 'lodash/pickBy';
  5. import Alert from 'sentry/components/alert';
  6. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  7. import FloatingFeedbackWidget from 'sentry/components/feedback/widget/floatingFeedbackWidget';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  10. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  13. import SearchBar from 'sentry/components/searchBar';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import {fromSorts} from 'sentry/utils/discover/eventView';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  21. import {useOnboardingProject} from 'sentry/views/performance/browser/webVitals/utils/useOnboardingProject';
  22. import {DurationChart} from 'sentry/views/performance/database/durationChart';
  23. import {ModulePageProviders} from 'sentry/views/performance/database/modulePageProviders';
  24. import {NoDataMessage} from 'sentry/views/performance/database/noDataMessage';
  25. import {isAValidSort, QueriesTable} from 'sentry/views/performance/database/queriesTable';
  26. import {ThroughputChart} from 'sentry/views/performance/database/throughputChart';
  27. import {useSelectedDurationAggregate} from 'sentry/views/performance/database/useSelectedDurationAggregate';
  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: pickBy(tableFilters, value => value !== undefined),
  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 {isLoading: isThroughputDataLoading, data: throughputData} = useSpanMetricsSeries(
  89. {
  90. filters: chartFilters,
  91. yAxis: ['spm()'],
  92. referrer: 'api.starfish.span-landing-page-metrics-chart',
  93. }
  94. );
  95. const {isLoading: isDurationDataLoading, data: durationData} = useSpanMetricsSeries({
  96. filters: chartFilters,
  97. yAxis: [`${selectedAggregate}(${SpanMetricsField.SPAN_SELF_TIME})`],
  98. referrer: 'api.starfish.span-landing-page-metrics-chart',
  99. });
  100. const isCriticalDataLoading =
  101. isThroughputDataLoading || isDurationDataLoading || queryListResponse.isLoading;
  102. const isAnyCriticalDataAvailable =
  103. (queryListResponse.data ?? []).length > 0 ||
  104. durationData[`${selectedAggregate}(span.self_time)`].data?.some(
  105. ({value}) => value > 0
  106. ) ||
  107. throughputData['spm()'].data?.some(({value}) => value > 0);
  108. useSynchronizeCharts([!isThroughputDataLoading && !isDurationDataLoading]);
  109. return (
  110. <React.Fragment>
  111. <Layout.Header>
  112. <Layout.HeaderContent>
  113. <Breadcrumbs
  114. crumbs={[
  115. {
  116. label: 'Performance',
  117. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  118. preservePageFilters: true,
  119. },
  120. {
  121. label: 'Queries',
  122. },
  123. ]}
  124. />
  125. <Layout.Title>{t('Queries')}</Layout.Title>
  126. </Layout.HeaderContent>
  127. </Layout.Header>
  128. <Layout.Body>
  129. <Layout.Main fullWidth>
  130. {!onboardingProject && !isCriticalDataLoading && (
  131. <NoDataMessage
  132. Wrapper={AlertBanner}
  133. isDataAvailable={isAnyCriticalDataAvailable}
  134. />
  135. )}
  136. <FloatingFeedbackWidget />
  137. <PaddedContainer>
  138. <PageFilterBar condensed>
  139. <ProjectPageFilter />
  140. <EnvironmentPageFilter />
  141. <DatePageFilter />
  142. </PageFilterBar>
  143. </PaddedContainer>
  144. {onboardingProject && (
  145. <Onboarding organization={organization} project={onboardingProject} />
  146. )}
  147. {!onboardingProject && (
  148. <Fragment>
  149. <ChartContainer>
  150. <ThroughputChart
  151. series={throughputData['spm()']}
  152. isLoading={isThroughputDataLoading}
  153. />
  154. <DurationChart
  155. series={durationData[`${selectedAggregate}(span.self_time)`]}
  156. isLoading={isDurationDataLoading}
  157. />
  158. </ChartContainer>
  159. <FilterOptionsContainer>
  160. <SelectorContainer>
  161. <ActionSelector moduleName={moduleName} value={spanAction ?? ''} />
  162. </SelectorContainer>
  163. <SelectorContainer>
  164. <DomainSelector moduleName={moduleName} value={spanDomain ?? ''} />
  165. </SelectorContainer>
  166. </FilterOptionsContainer>
  167. <SearchBarContainer>
  168. <SearchBar
  169. query={spanDescription}
  170. placeholder={t('Search for more Queries')}
  171. onSearch={handleSearch}
  172. />
  173. </SearchBarContainer>
  174. <QueriesTable response={queryListResponse} sort={sort} />
  175. </Fragment>
  176. )}
  177. </Layout.Main>
  178. </Layout.Body>
  179. </React.Fragment>
  180. );
  181. }
  182. const DEFAULT_SORT = {
  183. field: 'time_spent_percentage()' as const,
  184. kind: 'desc' as const,
  185. };
  186. const PaddedContainer = styled('div')`
  187. margin-bottom: ${space(2)};
  188. `;
  189. const ChartContainer = styled('div')`
  190. display: grid;
  191. grid-template-columns: 1fr;
  192. @media (min-width: ${p => p.theme.breakpoints.small}) {
  193. grid-template-columns: 1fr 1fr;
  194. gap: ${space(2)};
  195. }
  196. `;
  197. function AlertBanner(props) {
  198. return <Alert {...props} type="info" showIcon />;
  199. }
  200. const FilterOptionsContainer = styled('div')`
  201. display: flex;
  202. flex-wrap: wrap;
  203. gap: ${space(2)};
  204. margin-bottom: ${space(2)};
  205. @media (min-width: ${p => p.theme.breakpoints.small}) {
  206. flex-wrap: nowrap;
  207. }
  208. `;
  209. const SelectorContainer = styled('div')`
  210. flex-basis: 100%;
  211. @media (min-width: ${p => p.theme.breakpoints.small}) {
  212. flex-basis: auto;
  213. }
  214. `;
  215. const SearchBarContainer = styled('div')`
  216. margin-bottom: ${space(2)};
  217. `;
  218. const LIMIT: number = 25;
  219. function LandingPageWithProviders() {
  220. return (
  221. <ModulePageProviders title={[t('Performance'), t('Database')].join(' — ')}>
  222. <DatabaseLandingPage />
  223. </ModulePageProviders>
  224. );
  225. }
  226. export default LandingPageWithProviders;