index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import debounce from 'lodash/debounce';
  4. import {t} from 'sentry/locale';
  5. import {space} from 'sentry/styles/space';
  6. import {browserHistory} from 'sentry/utils/browserHistory';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import {RESOURCE_THROUGHPUT_UNIT} from 'sentry/views/performance/browser/resources';
  10. import ResourceTable from 'sentry/views/performance/browser/resources/resourceView/resourceTable';
  11. import {
  12. FONT_FILE_EXTENSIONS,
  13. IMAGE_FILE_EXTENSIONS,
  14. } from 'sentry/views/performance/browser/resources/shared/constants';
  15. import RenderBlockingSelector from 'sentry/views/performance/browser/resources/shared/renderBlockingSelector';
  16. import SelectControlWithProps from 'sentry/views/performance/browser/resources/shared/selectControlWithProps';
  17. import {ResourceSpanOps} from 'sentry/views/performance/browser/resources/shared/types';
  18. import {
  19. BrowserStarfishFields,
  20. useResourceModuleFilters,
  21. } from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  22. import {useResourcePagesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcePagesQuery';
  23. import {useResourceSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  24. import {getResourceTypeFilter} from 'sentry/views/performance/browser/resources/utils/useResourcesQuery';
  25. import {ModuleName} from 'sentry/views/starfish/types';
  26. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  27. import {SpanTimeCharts} from 'sentry/views/starfish/views/spans/spanTimeCharts';
  28. import type {ModuleFilters} from 'sentry/views/starfish/views/spans/useModuleFilters';
  29. const {
  30. SPAN_OP: RESOURCE_TYPE,
  31. SPAN_DOMAIN,
  32. TRANSACTION,
  33. RESOURCE_RENDER_BLOCKING_STATUS,
  34. } = BrowserStarfishFields;
  35. export const DEFAULT_RESOURCE_TYPES = [
  36. ResourceSpanOps.SCRIPT,
  37. ResourceSpanOps.CSS,
  38. ResourceSpanOps.FONT,
  39. ResourceSpanOps.IMAGE,
  40. ];
  41. type Option = {
  42. label: string | React.ReactElement;
  43. value: string;
  44. };
  45. function ResourceView() {
  46. const filters = useResourceModuleFilters();
  47. const sort = useResourceSort();
  48. const spanTimeChartsFilters: ModuleFilters = {
  49. 'span.op': `[${DEFAULT_RESOURCE_TYPES.join(',')}]`,
  50. ...(filters[SPAN_DOMAIN] ? {[SPAN_DOMAIN]: filters[SPAN_DOMAIN]} : {}),
  51. };
  52. const extraQuery = getResourceTypeFilter(undefined, DEFAULT_RESOURCE_TYPES);
  53. return (
  54. <Fragment>
  55. <SpanTimeChartsContainer>
  56. <SpanTimeCharts
  57. moduleName={ModuleName.RESOURCE}
  58. appliedFilters={spanTimeChartsFilters}
  59. throughputUnit={RESOURCE_THROUGHPUT_UNIT}
  60. extraQuery={extraQuery}
  61. />
  62. </SpanTimeChartsContainer>
  63. <FilterOptionsContainer columnCount={3}>
  64. <ResourceTypeSelector value={filters[RESOURCE_TYPE] || ''} />
  65. <TransactionSelector
  66. value={filters[TRANSACTION] || ''}
  67. defaultResourceTypes={DEFAULT_RESOURCE_TYPES}
  68. />
  69. <RenderBlockingSelector value={filters[RESOURCE_RENDER_BLOCKING_STATUS] || ''} />
  70. </FilterOptionsContainer>
  71. <ResourceTable sort={sort} defaultResourceTypes={DEFAULT_RESOURCE_TYPES} />
  72. </Fragment>
  73. );
  74. }
  75. function ResourceTypeSelector({value}: {value?: string}) {
  76. const location = useLocation();
  77. const {features} = useOrganization();
  78. const hasImageView = features.includes('insights-initial-modules');
  79. const options: Option[] = [
  80. {value: '', label: 'All'},
  81. {value: 'resource.script', label: `${t('JavaScript')} (.js)`},
  82. {value: 'resource.css', label: `${t('Stylesheet')} (.css)`},
  83. {
  84. value: 'resource.font',
  85. label: `${t('Font')} (${FONT_FILE_EXTENSIONS.map(e => `.${e}`).join(', ')})`,
  86. },
  87. ...(hasImageView
  88. ? [
  89. {
  90. value: ResourceSpanOps.IMAGE,
  91. label: `${t('Image')} (${IMAGE_FILE_EXTENSIONS.map(e => `.${e}`).join(', ')})`,
  92. },
  93. ]
  94. : []),
  95. ];
  96. return (
  97. <SelectControlWithProps
  98. inFieldLabel={`${t('Type')}:`}
  99. options={options}
  100. value={value}
  101. onChange={newValue => {
  102. browserHistory.push({
  103. ...location,
  104. query: {
  105. ...location.query,
  106. [RESOURCE_TYPE]: newValue?.value,
  107. [QueryParameterNames.SPANS_CURSOR]: undefined,
  108. },
  109. });
  110. }}
  111. />
  112. );
  113. }
  114. export function TransactionSelector({
  115. value,
  116. defaultResourceTypes,
  117. }: {
  118. defaultResourceTypes?: string[];
  119. value?: string;
  120. }) {
  121. const [state, setState] = useState({
  122. search: '',
  123. inputChanged: false,
  124. shouldRequeryOnInputChange: false,
  125. });
  126. const location = useLocation();
  127. const {data: pages, isLoading} = useResourcePagesQuery(
  128. defaultResourceTypes,
  129. state.search
  130. );
  131. // If the maximum number of pages is returned, we need to requery on input change to get full results
  132. if (!state.shouldRequeryOnInputChange && pages && pages.length >= 100) {
  133. setState({...state, shouldRequeryOnInputChange: true});
  134. }
  135. // Everytime loading is complete, reset the inputChanged state
  136. useEffect(() => {
  137. if (!isLoading && state.inputChanged) {
  138. setState({...state, inputChanged: false});
  139. }
  140. // eslint-disable-next-line react-hooks/exhaustive-deps
  141. }, [isLoading]);
  142. const optionsReady = !isLoading && !state.inputChanged;
  143. const options: Option[] = optionsReady
  144. ? [{value: '', label: 'All'}, ...pages.map(page => ({value: page, label: page}))]
  145. : [];
  146. // eslint-disable-next-line react-hooks/exhaustive-deps
  147. const debounceUpdateSearch = useCallback(
  148. debounce((search, currentState) => {
  149. setState({...currentState, search});
  150. }, 500),
  151. []
  152. );
  153. return (
  154. <SelectControlWithProps
  155. inFieldLabel={`${t('Page')}:`}
  156. options={options}
  157. value={value}
  158. onInputChange={input => {
  159. if (state.shouldRequeryOnInputChange) {
  160. setState({...state, inputChanged: true});
  161. debounceUpdateSearch(input, state);
  162. }
  163. }}
  164. noOptionsMessage={() => (optionsReady ? undefined : t('Loading...'))}
  165. onChange={newValue => {
  166. browserHistory.push({
  167. ...location,
  168. query: {
  169. ...location.query,
  170. [TRANSACTION]: newValue?.value,
  171. [QueryParameterNames.SPANS_CURSOR]: undefined,
  172. },
  173. });
  174. }}
  175. />
  176. );
  177. }
  178. export const SpanTimeChartsContainer = styled('div')`
  179. margin-bottom: ${space(2)};
  180. `;
  181. export const FilterOptionsContainer = styled('div')<{columnCount: number}>`
  182. display: grid;
  183. grid-template-columns: repeat(${props => props.columnCount}, 1fr);
  184. gap: ${space(2)};
  185. margin-bottom: ${space(2)};
  186. max-width: 800px;
  187. `;
  188. export default ResourceView;