123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- import {Fragment, useCallback, useMemo} from 'react';
- import styled from '@emotion/styled';
- import Feature from 'sentry/components/acl/feature';
- import {LinkButton} from 'sentry/components/button';
- import HookOrDefault from 'sentry/components/hookOrDefault';
- import {
- type Field,
- MetricSamplesTable,
- SearchableMetricSamplesTable,
- } from 'sentry/components/metrics/metricSamplesTable';
- import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import type {PageFilters} from 'sentry/types/core';
- import type {MetricAggregation, MRI} from 'sentry/types/metrics';
- import {defined} from 'sentry/utils';
- import {isVirtualMetric} from 'sentry/utils/metrics';
- import type {FocusedMetricsSeries, MetricsWidget} from 'sentry/utils/metrics/types';
- import {isMetricsEquationWidget} from 'sentry/utils/metrics/types';
- import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
- import {useVirtualMetricsContext} from 'sentry/utils/metrics/virtualMetricsContext';
- import useOrganization from 'sentry/utils/useOrganization';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import type {FocusAreaProps} from 'sentry/views/metrics/context';
- import {useMetricsContext} from 'sentry/views/metrics/context';
- import {extendQueryWithGroupBys} from 'sentry/views/metrics/utils';
- import {generateTracesRouteWithQuery} from 'sentry/views/traces/utils';
- export function WidgetDetails() {
- const {
- selectedWidgetIndex,
- widgets,
- focusArea,
- setHighlightedSampleId,
- setMetricsSamples,
- hasPerformanceMetrics,
- } = useMetricsContext();
- const selectedWidget = widgets[selectedWidgetIndex] as MetricsWidget | undefined;
- const handleSampleRowHover = useCallback(
- (sampleId?: string) => {
- setHighlightedSampleId(sampleId);
- },
- [setHighlightedSampleId]
- );
- if (!selectedWidget || isMetricsEquationWidget(selectedWidget)) {
- return <MetricDetails onRowHover={handleSampleRowHover} focusArea={focusArea} />;
- }
- const {mri, aggregation, query, condition, focusedSeries} = selectedWidget;
- return (
- <MetricDetails
- mri={mri}
- aggregation={aggregation}
- condition={condition}
- query={query}
- focusedSeries={focusedSeries}
- onRowHover={handleSampleRowHover}
- setMetricsSamples={setMetricsSamples}
- focusArea={focusArea}
- hasPerformanceMetrics={hasPerformanceMetrics}
- />
- );
- }
- interface MetricDetailsProps {
- aggregation?: MetricAggregation;
- condition?: number;
- focusArea?: FocusAreaProps;
- focusedSeries?: FocusedMetricsSeries[];
- hasPerformanceMetrics?: boolean;
- mri?: MRI;
- onRowHover?: (sampleId?: string) => void;
- query?: string;
- setMetricsSamples?: React.Dispatch<
- React.SetStateAction<MetricsSamplesResults<Field>['data'] | undefined>
- >;
- }
- export function MetricDetails({
- mri,
- aggregation,
- condition,
- query,
- focusedSeries,
- onRowHover,
- focusArea,
- setMetricsSamples,
- hasPerformanceMetrics,
- }: MetricDetailsProps) {
- const {selection} = usePageFilters();
- const organization = useOrganization();
- const {getCondition} = useVirtualMetricsContext();
- const queryWithFocusedSeries = useMemo(
- () =>
- focusedSeries &&
- extendQueryWithGroupBys(
- query || '',
- focusedSeries.map(s => s.groupBy)
- ),
- [focusedSeries, query]
- );
- const selectionRange = focusArea?.selection?.range;
- const tracesTarget = useMemo(() => {
- const selectionDatetime =
- defined(selectionRange) && defined(selectionRange) && defined(selectionRange)
- ? ({
- start: selectionRange.start,
- end: selectionRange.end,
- } as PageFilters['datetime'])
- : undefined;
- if (mri && isVirtualMetric({mri})) {
- const conditionQuery = getCondition(mri, condition || -1)?.value || '';
- return generateTracesRouteWithQuery({
- orgSlug: organization.slug,
- query: {
- project: selection.projects as unknown as string[],
- environment: selection.environments,
- ...normalizeDateTimeParams(selectionDatetime ?? selection.datetime),
- query: `${conditionQuery.trim()} ${queryWithFocusedSeries?.trim()}`,
- },
- });
- }
- if (aggregation && mri) {
- return generateTracesRouteWithQuery({
- orgSlug: organization.slug,
- metric: {
- max: selectionRange?.max,
- min: selectionRange?.min,
- op: aggregation,
- query: queryWithFocusedSeries,
- mri: mri,
- },
- query: {
- project: selection.projects as unknown as string[],
- environment: selection.environments,
- ...normalizeDateTimeParams(selectionDatetime ?? selection.datetime),
- },
- });
- }
- return '';
- }, [
- aggregation,
- mri,
- organization.slug,
- queryWithFocusedSeries,
- selection,
- selectionRange,
- condition,
- getCondition,
- ]);
- return (
- <TrayWrapper>
- <TabsAndAction>
- <Heading>{t('Span Samples')}</Heading>
- <Feature
- features={[
- 'performance-trace-explorer-with-metrics',
- 'performance-trace-explorer',
- ]}
- requireAll
- >
- <LinkButton to={tracesTarget} size="sm">
- {t('Open in Traces')}
- </LinkButton>
- </Feature>
- </TabsAndAction>
- <ContentWrapper>
- <MetricSampleTableWrapper organization={organization}>
- {organization.features.includes('metrics-samples-list-search') ? (
- <SearchableMetricSamplesTable
- focusArea={selectionRange}
- mri={mri}
- onRowHover={onRowHover}
- aggregation={aggregation}
- condition={condition}
- query={queryWithFocusedSeries}
- setMetricsSamples={setMetricsSamples}
- hasPerformance={hasPerformanceMetrics}
- />
- ) : (
- <MetricSamplesTable
- focusArea={selectionRange}
- mri={mri}
- onRowHover={onRowHover}
- aggregation={aggregation}
- condition={condition}
- query={queryWithFocusedSeries}
- setMetricsSamples={setMetricsSamples}
- hasPerformance={hasPerformanceMetrics}
- />
- )}
- </MetricSampleTableWrapper>
- </ContentWrapper>
- </TrayWrapper>
- );
- }
- const MetricSampleTableWrapper = HookOrDefault({
- hookName: 'component:ddm-metrics-samples-list',
- defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
- });
- const Heading = styled('h6')`
- margin-bottom: ${space(0.5)};
- `;
- const TrayWrapper = styled('div')`
- padding-top: ${space(4)};
- display: grid;
- grid-template-rows: auto auto 1fr;
- `;
- const ContentWrapper = styled('div')`
- position: relative;
- padding-top: ${space(1)};
- `;
- const TabsAndAction = styled('div')`
- display: grid;
- grid-template-columns: 1fr auto;
- gap: ${space(4)};
- align-items: end;
- `;
|