|
1 | 1 | import {useTheme} from '@emotion/react';
|
2 | 2 | import styled from '@emotion/styled';
|
3 | 3 |
|
4 |
| -import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants'; |
5 | 4 | import {t} from 'sentry/locale';
|
6 |
| -import {space} from 'sentry/styles/space'; |
7 | 5 | import type {Series} from 'sentry/types/echarts';
|
8 |
| -import usePageFilters from 'sentry/utils/usePageFilters'; |
| 6 | +import {MutableSearch} from 'sentry/utils/tokenizeSearch'; |
9 | 7 | import {ORDER} from 'sentry/views/insights/browser/webVitals/components/charts/performanceScoreChart';
|
10 |
| -import { |
11 |
| - useProjectWebVitalsScoresTimeseriesQuery, |
12 |
| - type WebVitalsScoreBreakdown, |
13 |
| -} from 'sentry/views/insights/browser/webVitals/queries/storedScoreQueries/useProjectWebVitalsScoresTimeseriesQuery'; |
| 8 | +import type {WebVitalsScoreBreakdown} from 'sentry/views/insights/browser/webVitals/queries/storedScoreQueries/useProjectWebVitalsScoresTimeseriesQuery'; |
14 | 9 | import type {WebVitals} from 'sentry/views/insights/browser/webVitals/types';
|
15 |
| -import {applyStaticWeightsToTimeseries} from 'sentry/views/insights/browser/webVitals/utils/applyStaticWeightsToTimeseries'; |
16 | 10 | import {getWeights} from 'sentry/views/insights/browser/webVitals/utils/getWeights';
|
17 | 11 | import type {BrowserType} from 'sentry/views/insights/browser/webVitals/utils/queryParameterDecoders/browserType';
|
18 |
| -import Chart, {ChartType} from 'sentry/views/insights/common/components/chart'; |
19 |
| -import ChartPanel from 'sentry/views/insights/common/components/chartPanel'; |
20 |
| -import type {SubregionCode} from 'sentry/views/insights/types'; |
| 12 | +import {InsightsTimeSeriesWidget} from 'sentry/views/insights/common/components/insightsTimeSeriesWidget'; |
| 13 | +import { |
| 14 | + type DiscoverSeries, |
| 15 | + useMetricsSeries, |
| 16 | +} from 'sentry/views/insights/common/queries/useDiscoverSeries'; |
| 17 | +import {SpanMetricsField, type SubregionCode} from 'sentry/views/insights/types'; |
| 18 | + |
| 19 | +import {DEFAULT_QUERY_FILTER} from '../../settings'; |
| 20 | + |
| 21 | +import {WebVitalsWeightList} from './webVitalWeightList'; |
21 | 22 |
|
22 | 23 | type Props = {
|
23 | 24 | browserTypes?: BrowserType[];
|
@@ -52,93 +53,96 @@ export function PerformanceScoreBreakdownChart({
|
52 | 53 | const theme = useTheme();
|
53 | 54 | const segmentColors = theme.chart.getColorPalette(3).slice(0, 5);
|
54 | 55 |
|
55 |
| - const pageFilters = usePageFilters(); |
56 |
| - |
57 |
| - const {data: timeseriesData, isLoading: isTimeseriesLoading} = |
58 |
| - useProjectWebVitalsScoresTimeseriesQuery({transaction, browserTypes, subregions}); |
| 56 | + const search = new MutableSearch( |
| 57 | + `${DEFAULT_QUERY_FILTER} has:measurements.score.total` |
| 58 | + ); |
59 | 59 |
|
60 |
| - const period = pageFilters.selection.datetime.period; |
61 |
| - // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message |
62 |
| - const performanceScoreSubtext = (period && DEFAULT_RELATIVE_PERIODS[period]) ?? ''; |
63 |
| - const chartSeriesOrder = ORDER; |
| 60 | + if (transaction) { |
| 61 | + search.addFilterValue('transaction', transaction); |
| 62 | + } |
64 | 63 |
|
65 |
| - const weightedTimeseriesData = applyStaticWeightsToTimeseries(timeseriesData); |
| 64 | + if (subregions) { |
| 65 | + search.addDisjunctionFilterValues(SpanMetricsField.USER_GEO_SUBREGION, subregions); |
| 66 | + } |
66 | 67 |
|
67 |
| - const weightedTimeseries = formatTimeSeriesResultsToChartData( |
68 |
| - weightedTimeseriesData, |
69 |
| - segmentColors, |
70 |
| - chartSeriesOrder |
71 |
| - ); |
| 68 | + if (browserTypes) { |
| 69 | + search.addDisjunctionFilterValues(SpanMetricsField.BROWSER_NAME, browserTypes); |
| 70 | + } |
72 | 71 |
|
73 |
| - const timeseries = formatTimeSeriesResultsToChartData( |
| 72 | + const { |
| 73 | + data: vitalScoresData, |
| 74 | + isLoading: areVitalScoresLoading, |
| 75 | + error: vitalScoresError, |
| 76 | + } = useMetricsSeries( |
74 | 77 | {
|
75 |
| - lcp: timeseriesData.lcp, |
76 |
| - fcp: timeseriesData.fcp, |
77 |
| - cls: timeseriesData.cls, |
78 |
| - ttfb: timeseriesData.ttfb, |
79 |
| - inp: timeseriesData.inp, |
80 |
| - total: timeseriesData.total, |
| 78 | + search, |
| 79 | + yAxis: [ |
| 80 | + 'performance_score(measurements.score.lcp)', |
| 81 | + 'performance_score(measurements.score.fcp)', |
| 82 | + 'performance_score(measurements.score.cls)', |
| 83 | + 'performance_score(measurements.score.inp)', |
| 84 | + 'performance_score(measurements.score.ttfb)', |
| 85 | + 'count()', |
| 86 | + ], |
| 87 | + transformAliasToInputFormat: true, |
81 | 88 | },
|
82 |
| - segmentColors, |
83 |
| - chartSeriesOrder |
| 89 | + 'api.performance.browser.web-vitals.timeseries-scores2' |
84 | 90 | );
|
85 | 91 |
|
86 |
| - const weights = getWeights( |
87 |
| - ORDER.filter(webVital => timeseriesData[webVital].some(series => series.value > 0)) |
88 |
| - ); |
| 92 | + const webVitalsThatHaveData: WebVitals[] = vitalScoresData |
| 93 | + ? ORDER.filter(webVital => { |
| 94 | + const key = `performance_score(measurements.score.${webVital})` as const; |
| 95 | + const series = vitalScoresData[key]!; |
89 | 96 |
|
90 |
| - return ( |
91 |
| - <StyledChartPanel title={t('Score Breakdown')}> |
92 |
| - <PerformanceScoreSubtext>{performanceScoreSubtext}</PerformanceScoreSubtext> |
93 |
| - <Chart |
94 |
| - stacked |
95 |
| - hideYAxisSplitLine |
96 |
| - height={180} |
97 |
| - data={isTimeseriesLoading ? [] : weightedTimeseries} |
98 |
| - disableXAxis |
99 |
| - loading={isTimeseriesLoading} |
100 |
| - type={ChartType.AREA} |
101 |
| - grid={{ |
102 |
| - left: 5, |
103 |
| - right: 5, |
104 |
| - top: 5, |
105 |
| - bottom: 0, |
106 |
| - }} |
107 |
| - dataMax={100} |
108 |
| - chartColors={segmentColors} |
109 |
| - tooltipFormatterOptions={{ |
110 |
| - nameFormatter: name => { |
111 |
| - // nameFormatter expects a string an will wrap the output in an html string. |
112 |
| - // Kind of a hack, but we can inject some html to escape styling for the subLabel. |
113 |
| - const subLabel = |
114 |
| - weights === undefined |
115 |
| - ? '' |
116 |
| - : // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message |
117 |
| - ` </strong>(${weights[name.toLocaleLowerCase()].toFixed( |
118 |
| - 0 |
119 |
| - )}% of Perf Score)<strong>`; |
120 |
| - return `${name} Score${subLabel}`; |
121 |
| - }, |
122 |
| - valueFormatter: (_value, _label, seriesParams: any) => { |
123 |
| - const timestamp = seriesParams?.data[0]; |
124 |
| - const value = timeseries |
125 |
| - .find(series => series.seriesName === seriesParams?.seriesName) |
126 |
| - ?.data.find(dataPoint => dataPoint.name === timestamp)?.value; |
127 |
| - return `<span class="tooltip-label-value">${value}</span>`; |
| 97 | + return series.data.some(datum => datum.value > 0); |
| 98 | + }) |
| 99 | + : []; |
| 100 | + |
| 101 | + const weights = getWeights(webVitalsThatHaveData); |
| 102 | + |
| 103 | + const allSeries: DiscoverSeries[] = vitalScoresData |
| 104 | + ? ORDER.map((webVital, index) => { |
| 105 | + const key = `performance_score(measurements.score.${webVital})` as const; |
| 106 | + const series = vitalScoresData[key]!; |
| 107 | + |
| 108 | + const scaledSeries: DiscoverSeries = { |
| 109 | + ...series, |
| 110 | + data: series.data.map(datum => { |
| 111 | + return { |
| 112 | + ...datum, |
| 113 | + value: datum.value * weights[webVital], |
| 114 | + }; |
| 115 | + }), |
| 116 | + color: segmentColors[index], |
| 117 | + meta: { |
| 118 | + // TODO: The backend doesn't return these score fields with the "score" type yet. Fill this in manually for now. |
| 119 | + fields: { |
| 120 | + ...series.meta?.fields, |
| 121 | + [key]: 'score', |
| 122 | + }, |
| 123 | + units: series.meta?.units, |
128 | 124 | },
|
129 |
| - }} |
| 125 | + }; |
| 126 | + |
| 127 | + return scaledSeries; |
| 128 | + }) |
| 129 | + : []; |
| 130 | + |
| 131 | + return ( |
| 132 | + <ChartContainer> |
| 133 | + <InsightsTimeSeriesWidget |
| 134 | + title={t('Score Breakdown')} |
| 135 | + height="100%" |
| 136 | + visualizationType="area" |
| 137 | + isLoading={areVitalScoresLoading} |
| 138 | + error={vitalScoresError} |
| 139 | + series={allSeries} |
| 140 | + description={<WebVitalsWeightList weights={weights} />} |
130 | 141 | />
|
131 |
| - </StyledChartPanel> |
| 142 | + </ChartContainer> |
132 | 143 | );
|
133 | 144 | }
|
134 | 145 |
|
135 |
| -const StyledChartPanel = styled(ChartPanel)` |
136 |
| - flex: 1; |
137 |
| -`; |
138 |
| - |
139 |
| -const PerformanceScoreSubtext = styled('div')` |
140 |
| - width: 100%; |
141 |
| - font-size: ${p => p.theme.fontSizeSmall}; |
142 |
| - color: ${p => p.theme.subText}; |
143 |
| - margin-bottom: ${space(1)}; |
| 146 | +const ChartContainer = styled('div')` |
| 147 | + flex: 1 1 0%; |
144 | 148 | `;
|
0 commit comments