123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import {useCallback, useLayoutEffect, useMemo} from 'react';
- import styled from '@emotion/styled';
- import * as echarts from 'echarts/core';
- import {addErrorMessage} from 'sentry/actionCreators/indicator';
- import type {Field} from 'sentry/components/metrics/metricSamplesTable';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
- import {
- isMetricsEquationWidget,
- MetricExpressionType,
- type MetricsWidget,
- } from 'sentry/utils/metrics/types';
- import type {MetricsQueryApiQueryParams} from 'sentry/utils/metrics/useMetricsQuery';
- import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
- import {useLocation} from 'sentry/utils/useLocation';
- import useOrganization from 'sentry/utils/useOrganization';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import useRouter from 'sentry/utils/useRouter';
- import {METRIC_CHART_GROUP, MIN_WIDGET_WIDTH} from 'sentry/views/metrics/constants';
- import {useMetricsContext} from 'sentry/views/metrics/context';
- import {useGetCachedChartPalette} from 'sentry/views/metrics/utils/metricsChartPalette';
- import {useFormulaDependencies} from 'sentry/views/metrics/utils/useFormulaDependencies';
- import {widgetToQuery} from 'sentry/views/metrics/utils/widgetToQuery';
- import {TraceViewSources} from '../performance/newTraceDetails/traceMetadataHeader';
- import {MetricWidget} from './widget';
- export function MetricScratchpad() {
- const {
- setSelectedWidgetIndex,
- selectedWidgetIndex,
- widgets,
- updateWidget,
- showQuerySymbols,
- highlightedSampleId,
- focusArea,
- isMultiChartMode,
- metricsSamples,
- } = useMetricsContext();
- const {selection} = usePageFilters();
- const location = useLocation();
- const router = useRouter();
- const organization = useOrganization();
- const getChartPalette = useGetCachedChartPalette();
- // Make sure all charts are connected to the same group whenever the widgets definition changes
- useLayoutEffect(() => {
- echarts.connect(METRIC_CHART_GROUP);
- }, [widgets]);
- const handleChange = useCallback(
- (index: number, widget: Partial<MetricsWidget>) => {
- updateWidget(index, widget);
- },
- [updateWidget]
- );
- const handleSampleClick = useCallback(
- (sample: MetricsSamplesResults<Field>['data'][number]) => {
- if (!sample['transaction.id']) {
- addErrorMessage(t('No matching transaction found'));
- return;
- }
- const isTransaction = sample.id === sample['segment.id'];
- const dataRow: {
- id: string;
- project: string;
- trace: string;
- timestamp?: number;
- } = {
- id: sample['transaction.id'],
- project: sample.project,
- trace: sample.trace,
- };
- if (sample.timestamp) {
- const timestamp = new Date(sample.timestamp).getTime();
- if (!isNaN(timestamp)) {
- dataRow.timestamp = timestamp / 1000;
- }
- }
- router.push(
- generateLinkToEventInTraceView({
- traceSlug: dataRow.trace,
- projectSlug: dataRow.project,
- eventId: dataRow.id,
- timestamp: dataRow.timestamp ?? '',
- location: {
- ...location,
- query: {...location.query, referrer: 'metrics', openPanel: 'open'},
- },
- organization,
- transactionName: isTransaction ? sample.transaction : undefined,
- spanId: isTransaction ? sample.id : undefined,
- source: TraceViewSources.METRICS,
- })
- );
- },
- [router, organization, location]
- );
- const firstWidget = widgets[0];
- const Wrapper =
- !isMultiChartMode || widgets.length === 1
- ? StyledSingleWidgetWrapper
- : StyledMetricDashboard;
- const formulaDependencies = useFormulaDependencies();
- const filteredWidgets = useMemo(() => {
- return widgets.filter(
- w =>
- w.type !== MetricExpressionType.EQUATION ||
- formulaDependencies[w.id]?.isError === false
- );
- }, [formulaDependencies, widgets]);
- return (
- <Wrapper>
- {isMultiChartMode ? (
- filteredWidgets.map((widget, index) =>
- widget.isHidden ? null : (
- <MultiChartWidgetQueries
- formulaDependencies={formulaDependencies}
- widget={widget}
- key={`${widget.type}_${widget.id}`}
- >
- {queries => (
- <MetricWidget
- queryId={widget.id}
- index={index}
- getChartPalette={getChartPalette}
- onSelect={setSelectedWidgetIndex}
- displayType={widget.displayType}
- focusedSeries={widget.focusedSeries}
- tableSort={widget.sort}
- queries={queries}
- isSelected={selectedWidgetIndex === index}
- hasSiblings={widgets.length > 1}
- onChange={handleChange}
- filters={selection}
- focusAreaProps={focusArea}
- showQuerySymbols={showQuerySymbols}
- onSampleClick={handleSampleClick}
- chartHeight={200}
- highlightedSampleId={
- selectedWidgetIndex === index ? highlightedSampleId : undefined
- }
- metricsSamples={metricsSamples}
- overlays={widget.overlays}
- />
- )}
- </MultiChartWidgetQueries>
- )
- )
- ) : (
- <MetricWidget
- index={0}
- getChartPalette={getChartPalette}
- onSelect={setSelectedWidgetIndex}
- displayType={firstWidget.displayType}
- focusedSeries={firstWidget.focusedSeries}
- tableSort={firstWidget.sort}
- queries={filteredWidgets
- .filter(w => !(w.type === MetricExpressionType.EQUATION && w.isHidden))
- .map(w => widgetToQuery(w))}
- isSelected
- hasSiblings={false}
- onChange={handleChange}
- filters={selection}
- focusAreaProps={focusArea}
- showQuerySymbols={false}
- onSampleClick={handleSampleClick}
- chartHeight={200}
- highlightedSampleId={highlightedSampleId}
- metricsSamples={metricsSamples}
- overlays={firstWidget.overlays}
- />
- )}
- </Wrapper>
- );
- }
- function MultiChartWidgetQueries({
- widget,
- formulaDependencies,
- children,
- }: {
- children: (queries: MetricsQueryApiQueryParams[]) => JSX.Element;
- formulaDependencies: ReturnType<typeof useFormulaDependencies>;
- widget: MetricsWidget;
- }) {
- const queries = useMemo(() => {
- return [
- widgetToQuery(widget),
- ...(isMetricsEquationWidget(widget)
- ? formulaDependencies[widget.id]?.dependencies?.map(dependency =>
- widgetToQuery(dependency, true)
- )
- : []),
- ];
- }, [widget, formulaDependencies]);
- return children(queries);
- }
- const StyledMetricDashboard = styled('div')`
- display: grid;
- grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
- gap: ${space(2)};
- @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
- grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
- }
- @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
- grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
- }
- grid-auto-rows: auto;
- `;
- const StyledSingleWidgetWrapper = styled('div')`
- display: grid;
- grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
- `;
|