databaseLandingPage.tsx 9.4 KB

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