databaseLandingPage.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 {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/useSpanMetrics';
  32. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  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 = fromSorts(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. search: MutableSearch.fromQueryObject(tableFilters),
  75. fields: [
  76. 'project.id',
  77. 'span.group',
  78. 'span.description',
  79. 'spm()',
  80. 'avg(span.self_time)',
  81. 'sum(span.self_time)',
  82. 'time_spent_percentage()',
  83. ],
  84. sorts: [sort],
  85. limit: LIMIT,
  86. cursor,
  87. referrer: 'api.starfish.use-span-list',
  88. });
  89. const {
  90. isLoading: isThroughputDataLoading,
  91. data: throughputData,
  92. error: throughputError,
  93. } = useSpanMetricsSeries({
  94. search: MutableSearch.fromQueryObject(chartFilters),
  95. yAxis: ['spm()'],
  96. referrer: 'api.starfish.span-landing-page-metrics-chart',
  97. });
  98. const {
  99. isLoading: isDurationDataLoading,
  100. data: durationData,
  101. error: durationError,
  102. } = useSpanMetricsSeries({
  103. search: MutableSearch.fromQueryObject(chartFilters),
  104. yAxis: [`${selectedAggregate}(${SpanMetricsField.SPAN_SELF_TIME})`],
  105. referrer: 'api.starfish.span-landing-page-metrics-chart',
  106. });
  107. const isCriticalDataLoading =
  108. isThroughputDataLoading || isDurationDataLoading || queryListResponse.isLoading;
  109. const isAnyCriticalDataAvailable =
  110. (queryListResponse.data ?? []).length > 0 ||
  111. durationData[`${selectedAggregate}(span.self_time)`].data?.some(
  112. ({value}) => value > 0
  113. ) ||
  114. throughputData['spm()'].data?.some(({value}) => value > 0);
  115. useSynchronizeCharts([!isThroughputDataLoading && !isDurationDataLoading]);
  116. return (
  117. <React.Fragment>
  118. <Layout.Header>
  119. <Layout.HeaderContent>
  120. <Breadcrumbs
  121. crumbs={[
  122. {
  123. label: 'Performance',
  124. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  125. preservePageFilters: true,
  126. },
  127. {
  128. label: 'Queries',
  129. },
  130. ]}
  131. />
  132. <Layout.Title>{t('Queries')}</Layout.Title>
  133. </Layout.HeaderContent>
  134. </Layout.Header>
  135. <Layout.Body>
  136. <Layout.Main fullWidth>
  137. <ModuleLayout.Layout>
  138. {!onboardingProject && !isCriticalDataLoading && (
  139. <ModuleLayout.Full>
  140. <NoDataMessage
  141. Wrapper={AlertBanner}
  142. isDataAvailable={isAnyCriticalDataAvailable}
  143. />
  144. </ModuleLayout.Full>
  145. )}
  146. <FloatingFeedbackWidget />
  147. <ModuleLayout.Full>
  148. <PageFilterBar condensed>
  149. <ProjectPageFilter />
  150. <EnvironmentPageFilter />
  151. <DatePageFilter />
  152. </PageFilterBar>
  153. </ModuleLayout.Full>
  154. {onboardingProject && (
  155. <ModuleLayout.Full>
  156. <Onboarding organization={organization} project={onboardingProject} />
  157. </ModuleLayout.Full>
  158. )}
  159. {!onboardingProject && (
  160. <Fragment>
  161. <ModuleLayout.Half>
  162. <ThroughputChart
  163. series={throughputData['spm()']}
  164. isLoading={isThroughputDataLoading}
  165. error={throughputError}
  166. />
  167. </ModuleLayout.Half>
  168. <ModuleLayout.Half>
  169. <DurationChart
  170. series={durationData[`${selectedAggregate}(span.self_time)`]}
  171. isLoading={isDurationDataLoading}
  172. error={durationError}
  173. />
  174. </ModuleLayout.Half>
  175. <ModuleLayout.Full>
  176. <FilterOptionsContainer>
  177. <SelectorContainer>
  178. <ActionSelector moduleName={moduleName} value={spanAction ?? ''} />
  179. </SelectorContainer>
  180. <SelectorContainer>
  181. <DomainSelector moduleName={moduleName} value={spanDomain ?? ''} />
  182. </SelectorContainer>
  183. </FilterOptionsContainer>
  184. </ModuleLayout.Full>
  185. <ModuleLayout.Full>
  186. <SearchBar
  187. query={spanDescription}
  188. placeholder={t('Search for more Queries')}
  189. onSearch={handleSearch}
  190. />
  191. </ModuleLayout.Full>
  192. <ModuleLayout.Full>
  193. <QueriesTable response={queryListResponse} sort={sort} />
  194. </ModuleLayout.Full>
  195. </Fragment>
  196. )}
  197. </ModuleLayout.Layout>
  198. </Layout.Main>
  199. </Layout.Body>
  200. </React.Fragment>
  201. );
  202. }
  203. const DEFAULT_SORT = {
  204. field: 'time_spent_percentage()' as const,
  205. kind: 'desc' as const,
  206. };
  207. function AlertBanner(props) {
  208. return <Alert {...props} type="info" showIcon />;
  209. }
  210. const FilterOptionsContainer = styled('div')`
  211. display: flex;
  212. flex-wrap: wrap;
  213. gap: ${space(2)};
  214. @media (min-width: ${p => p.theme.breakpoints.small}) {
  215. flex-wrap: nowrap;
  216. }
  217. `;
  218. const SelectorContainer = styled('div')`
  219. flex-basis: 100%;
  220. @media (min-width: ${p => p.theme.breakpoints.small}) {
  221. flex-basis: auto;
  222. }
  223. `;
  224. const LIMIT: number = 25;
  225. function LandingPageWithProviders() {
  226. return (
  227. <ModulePageProviders
  228. title={[t('Performance'), t('Database')].join(' — ')}
  229. baseURL="/performance/database"
  230. features="performance-database-view"
  231. >
  232. <DatabaseLandingPage />
  233. </ModulePageProviders>
  234. );
  235. }
  236. export default LandingPageWithProviders;