widgetDetails.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {Fragment, useCallback, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import Feature from 'sentry/components/acl/feature';
  4. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  5. import {Button} from 'sentry/components/button';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import {
  8. type Field,
  9. MetricSamplesTable,
  10. SearchableMetricSamplesTable,
  11. } from 'sentry/components/metrics/metricSamplesTable';
  12. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  13. import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {PageFilters} from 'sentry/types/core';
  18. import type {MRI} from 'sentry/types/metrics';
  19. import {defined} from 'sentry/utils';
  20. import {trackAnalytics} from 'sentry/utils/analytics';
  21. import {isCustomMetric} from 'sentry/utils/metrics';
  22. import type {FocusedMetricsSeries, MetricsWidget} from 'sentry/utils/metrics/types';
  23. import {isMetricsEquationWidget} from 'sentry/utils/metrics/types';
  24. import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import usePageFilters from 'sentry/utils/usePageFilters';
  27. import {CodeLocations} from 'sentry/views/metrics/codeLocations';
  28. import type {FocusAreaProps} from 'sentry/views/metrics/context';
  29. import {useMetricsContext} from 'sentry/views/metrics/context';
  30. import {extendQueryWithGroupBys} from 'sentry/views/metrics/utils';
  31. import {generateTracesRouteWithQuery} from 'sentry/views/performance/traces/utils';
  32. enum Tab {
  33. SAMPLES = 'samples',
  34. CODE_LOCATIONS = 'codeLocations',
  35. }
  36. export function WidgetDetails() {
  37. const {
  38. selectedWidgetIndex,
  39. widgets,
  40. focusArea,
  41. setHighlightedSampleId,
  42. setMetricsSamples,
  43. } = useMetricsContext();
  44. const selectedWidget = widgets[selectedWidgetIndex] as MetricsWidget | undefined;
  45. const handleSampleRowHover = useCallback(
  46. (sampleId?: string) => {
  47. setHighlightedSampleId(sampleId);
  48. },
  49. [setHighlightedSampleId]
  50. );
  51. if (!selectedWidget || isMetricsEquationWidget(selectedWidget)) {
  52. return <MetricDetails onRowHover={handleSampleRowHover} focusArea={focusArea} />;
  53. }
  54. const {mri, op, query, focusedSeries} = selectedWidget;
  55. return (
  56. <MetricDetails
  57. mri={mri}
  58. op={op}
  59. query={query}
  60. focusedSeries={focusedSeries}
  61. onRowHover={handleSampleRowHover}
  62. setMetricsSamples={setMetricsSamples}
  63. focusArea={focusArea}
  64. />
  65. );
  66. }
  67. interface MetricDetailsProps {
  68. focusArea?: FocusAreaProps;
  69. focusedSeries?: FocusedMetricsSeries[];
  70. mri?: MRI;
  71. onRowHover?: (sampleId?: string) => void;
  72. op?: string;
  73. query?: string;
  74. setMetricsSamples?: React.Dispatch<
  75. React.SetStateAction<MetricsSamplesResults<Field>['data'] | undefined>
  76. >;
  77. }
  78. export function MetricDetails({
  79. mri,
  80. op,
  81. query,
  82. focusedSeries,
  83. onRowHover,
  84. focusArea,
  85. setMetricsSamples,
  86. }: MetricDetailsProps) {
  87. const {selection} = usePageFilters();
  88. const organization = useOrganization();
  89. const [selectedTab, setSelectedTab] = useState(Tab.SAMPLES);
  90. const isCodeLocationsDisabled = mri && !isCustomMetric({mri});
  91. if (isCodeLocationsDisabled && selectedTab === Tab.CODE_LOCATIONS) {
  92. setSelectedTab(Tab.SAMPLES);
  93. }
  94. const queryWithFocusedSeries = useMemo(
  95. () =>
  96. focusedSeries &&
  97. extendQueryWithGroupBys(
  98. query || '',
  99. focusedSeries.map(s => s.groupBy)
  100. ),
  101. [focusedSeries, query]
  102. );
  103. const handleTabChange = useCallback(
  104. (tab: Tab) => {
  105. if (tab === Tab.CODE_LOCATIONS) {
  106. trackAnalytics('ddm.code-locations', {
  107. organization,
  108. });
  109. }
  110. setSelectedTab(tab);
  111. },
  112. [organization]
  113. );
  114. const selectionRange = focusArea?.selection?.range;
  115. const selectionDatetime =
  116. defined(selectionRange) && defined(selectionRange) && defined(selectionRange)
  117. ? ({
  118. start: selectionRange.start,
  119. end: selectionRange.end,
  120. } as PageFilters['datetime'])
  121. : undefined;
  122. const tracesTarget = generateTracesRouteWithQuery({
  123. orgSlug: organization.slug,
  124. metric:
  125. op && mri
  126. ? {
  127. max: selectionRange?.max,
  128. min: selectionRange?.min,
  129. op: op,
  130. query: queryWithFocusedSeries,
  131. mri,
  132. }
  133. : undefined,
  134. query: {
  135. project: selection.projects as unknown as string[],
  136. environment: selection.environments,
  137. ...normalizeDateTimeParams(selectionDatetime ?? selection.datetime),
  138. },
  139. });
  140. return (
  141. <TrayWrapper>
  142. <Tabs value={selectedTab} onChange={handleTabChange}>
  143. <TabsAndAction>
  144. <TabList>
  145. <TabList.Item key={Tab.SAMPLES}>
  146. <GuideAnchor target="metrics_table" position="top">
  147. {t('Span Samples')}
  148. </GuideAnchor>
  149. </TabList.Item>
  150. <TabList.Item
  151. textValue={t('Code Location')}
  152. key={Tab.CODE_LOCATIONS}
  153. disabled={isCodeLocationsDisabled}
  154. >
  155. <Tooltip
  156. title={t(
  157. 'This metric is automatically collected by Sentry. It is not bound to a specific line of your code.'
  158. )}
  159. disabled={!isCodeLocationsDisabled}
  160. >
  161. <span style={{pointerEvents: 'all'}}>{t('Code Location')}</span>
  162. </Tooltip>
  163. </TabList.Item>
  164. </TabList>
  165. <Feature
  166. features={[
  167. 'performance-trace-explorer-with-metrics',
  168. 'performance-trace-explorer',
  169. ]}
  170. requireAll
  171. >
  172. <OpenInTracesButton to={tracesTarget} size="sm">
  173. {t('Open in Traces')}
  174. </OpenInTracesButton>
  175. </Feature>
  176. </TabsAndAction>
  177. <ContentWrapper>
  178. <TabPanels>
  179. <TabPanels.Item key={Tab.SAMPLES}>
  180. <MetricSampleTableWrapper organization={organization}>
  181. {organization.features.includes('metrics-samples-list-search') ? (
  182. <SearchableMetricSamplesTable
  183. focusArea={selectionRange}
  184. mri={mri}
  185. onRowHover={onRowHover}
  186. op={op}
  187. query={queryWithFocusedSeries}
  188. setMetricsSamples={setMetricsSamples}
  189. />
  190. ) : (
  191. <MetricSamplesTable
  192. focusArea={selectionRange}
  193. mri={mri}
  194. onRowHover={onRowHover}
  195. op={op}
  196. query={queryWithFocusedSeries}
  197. setMetricsSamples={setMetricsSamples}
  198. />
  199. )}
  200. </MetricSampleTableWrapper>
  201. </TabPanels.Item>
  202. <TabPanels.Item key={Tab.CODE_LOCATIONS}>
  203. <CodeLocations mri={mri} {...focusArea?.selection?.range} />
  204. </TabPanels.Item>
  205. </TabPanels>
  206. </ContentWrapper>
  207. </Tabs>
  208. </TrayWrapper>
  209. );
  210. }
  211. const MetricSampleTableWrapper = HookOrDefault({
  212. hookName: 'component:ddm-metrics-samples-list',
  213. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  214. });
  215. const TrayWrapper = styled('div')`
  216. padding-top: ${space(4)};
  217. display: grid;
  218. grid-template-rows: auto auto 1fr;
  219. `;
  220. const ContentWrapper = styled('div')`
  221. position: relative;
  222. padding-top: ${space(2)};
  223. `;
  224. const OpenInTracesButton = styled(Button)`
  225. margin-top: ${space(0.75)};
  226. `;
  227. const TabsAndAction = styled('div')`
  228. display: grid;
  229. grid-template-columns: 1fr auto;
  230. gap: ${space(4)};
  231. align-items: center;
  232. `;