123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- import {useCallback, useLayoutEffect, useMemo} from 'react';
- import styled from '@emotion/styled';
- import * as echarts from 'echarts/core';
- import type {Field} from 'sentry/components/ddm/metricSamplesTable';
- import {space} from 'sentry/styles/space';
- import {getMetricsCorrelationSpanUrl, unescapeMetricsFormula} from 'sentry/utils/metrics';
- import {MetricQueryType, type MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
- import type {MetricsQueryApiQueryParams} from 'sentry/utils/metrics/useMetricsQuery';
- import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
- import useOrganization from 'sentry/utils/useOrganization';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import useProjects from 'sentry/utils/useProjects';
- import useRouter from 'sentry/utils/useRouter';
- import {DDM_CHART_GROUP, MIN_WIDGET_WIDTH} from 'sentry/views/ddm/constants';
- import {useDDMContext} from 'sentry/views/ddm/context';
- import {parseFormula} from 'sentry/views/ddm/formulaParser/parser';
- import {type TokenList, TokenType} from 'sentry/views/ddm/formulaParser/types';
- import {getQuerySymbol} from 'sentry/views/ddm/querySymbol';
- import {useGetCachedChartPalette} from 'sentry/views/ddm/utils/metricsChartPalette';
- import type {Sample} from './widget';
- import {MetricWidget} from './widget';
- interface WidgetDependencies {
- dependencies: MetricsQueryApiQueryParams[];
- isError: boolean;
- }
- function widgetToQuery(
- widget: MetricWidgetQueryParams,
- isQueryOnly = false
- ): MetricsQueryApiQueryParams {
- return widget.type === MetricQueryType.FORMULA
- ? {
- name: getQuerySymbol(widget.id),
- formula: widget.formula,
- }
- : {
- name: getQuerySymbol(widget.id),
- mri: widget.mri,
- op: widget.op,
- groupBy: widget.groupBy,
- query: widget.query,
- isQueryOnly: isQueryOnly || widget.isHidden,
- };
- }
- export function MetricScratchpad() {
- const {
- setSelectedWidgetIndex,
- selectedWidgetIndex,
- widgets,
- updateWidget,
- showQuerySymbols,
- highlightedSampleId,
- focusArea,
- isMultiChartMode,
- metricsSamples,
- } = useDDMContext();
- const {selection} = usePageFilters();
- const router = useRouter();
- const organization = useOrganization();
- const {projects} = useProjects();
- const getChartPalette = useGetCachedChartPalette();
- // Make sure all charts are connected to the same group whenever the widgets definition changes
- useLayoutEffect(() => {
- echarts.connect(DDM_CHART_GROUP);
- }, [widgets]);
- const handleChange = useCallback(
- (index: number, widget: Partial<MetricWidgetQueryParams>) => {
- updateWidget(index, widget);
- },
- [updateWidget]
- );
- const handleSampleClick = useCallback(
- (sample: Sample) => {
- const project = projects.find(p => parseInt(p.id, 10) === sample.projectId);
- router.push(
- getMetricsCorrelationSpanUrl(
- organization,
- project?.slug,
- sample.spanId,
- sample.transactionId,
- sample.transactionSpanId
- )
- );
- },
- [projects, router, organization]
- );
- const handleSampleClickV2 = useCallback(
- (sample: MetricsSamplesResults<Field>['data'][number]) => {
- router.push(
- getMetricsCorrelationSpanUrl(
- organization,
- sample.project,
- sample.id,
- sample['transaction.id'],
- sample['segment.id']
- )
- );
- },
- [router, organization]
- );
- const firstWidget = widgets[0];
- const Wrapper =
- !isMultiChartMode || widgets.length === 1
- ? StyledSingleWidgetWrapper
- : StyledMetricDashboard;
- const queriesLookup = useMemo(() => {
- const lookup = new Map<string, MetricWidgetQueryParams>();
- widgets.forEach(widget => {
- lookup.set(getQuerySymbol(widget.id), widget);
- });
- return lookup;
- }, [widgets]);
- const getFormulaQueryDependencies = useCallback(
- (formula: string): WidgetDependencies => {
- let tokens: TokenList = [];
- try {
- tokens = parseFormula(unescapeMetricsFormula(formula));
- } catch {
- // We should not end up here, but if we do, we should not crash the UI
- return {dependencies: [], isError: true};
- }
- const dependencies: MetricsQueryApiQueryParams[] = [];
- let isError: boolean = false;
- tokens.forEach(token => {
- if (token.type === TokenType.VARIABLE) {
- const widget = queriesLookup.get(token.content);
- if (widget && widget.type === MetricQueryType.QUERY) {
- dependencies.push(widgetToQuery(widget, true));
- } else {
- isError = true;
- }
- }
- });
- return {dependencies, isError};
- },
- [queriesLookup]
- );
- const formulaDependencies = useMemo(() => {
- return widgets.reduce((acc: Record<number, WidgetDependencies>, widget) => {
- if (widget.type === MetricQueryType.FORMULA) {
- acc[widget.id] = getFormulaQueryDependencies(widget.formula);
- }
- return acc;
- }, {});
- }, [getFormulaQueryDependencies, widgets]);
- const filteredWidgets = useMemo(() => {
- return widgets.filter(
- w =>
- w.type !== MetricQueryType.FORMULA || formulaDependencies[w.id]?.isError === false
- );
- }, [formulaDependencies, widgets]);
- return (
- <Wrapper>
- {isMultiChartMode ? (
- filteredWidgets.map((widget, index) =>
- widget.isHidden ? null : (
- <MultiChartWidgetQueries
- formulaDependencies={formulaDependencies}
- widget={widget}
- key={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}
- onSampleClickV2={handleSampleClickV2}
- chartHeight={200}
- highlightedSampleId={
- selectedWidgetIndex === index ? highlightedSampleId : undefined
- }
- metricsSamples={metricsSamples}
- context="ddm"
- />
- )}
- </MultiChartWidgetQueries>
- )
- )
- ) : (
- <MetricWidget
- index={0}
- getChartPalette={getChartPalette}
- onSelect={setSelectedWidgetIndex}
- displayType={firstWidget.displayType}
- focusedSeries={firstWidget.focusedSeries}
- tableSort={firstWidget.sort}
- queries={filteredWidgets
- .filter(w => !(w.type === MetricQueryType.FORMULA && w.isHidden))
- .map(w => widgetToQuery(w))}
- isSelected
- hasSiblings={false}
- onChange={handleChange}
- filters={selection}
- focusAreaProps={focusArea}
- showQuerySymbols={false}
- onSampleClick={handleSampleClick}
- onSampleClickV2={handleSampleClickV2}
- chartHeight={200}
- highlightedSampleId={highlightedSampleId}
- metricsSamples={metricsSamples}
- context="ddm"
- />
- )}
- </Wrapper>
- );
- }
- function MultiChartWidgetQueries({
- widget,
- formulaDependencies,
- children,
- }: {
- children: (queries: MetricsQueryApiQueryParams[]) => JSX.Element;
- formulaDependencies: Record<number, WidgetDependencies>;
- widget: MetricWidgetQueryParams;
- }) {
- const queries = useMemo(() => {
- return [
- widgetToQuery(widget),
- ...(widget.type === MetricQueryType.FORMULA
- ? formulaDependencies[widget.id]?.dependencies
- : []),
- ];
- }, [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));
- `;
|