databaseLandingPage.tsx 9.3 KB

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