123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- import {useEffect, useRef} from 'react';
- import {Theme} from '@emotion/react';
- import styled from '@emotion/styled';
- import {AreaChart} from 'sentry/components/charts/areaChart';
- import {BarChart} from 'sentry/components/charts/barChart';
- import ChartZoom from 'sentry/components/charts/chartZoom';
- import Legend from 'sentry/components/charts/components/legend';
- import {LineChart} from 'sentry/components/charts/lineChart';
- import ReleaseSeries from 'sentry/components/charts/releaseSeries';
- import {RELEASE_LINES_THRESHOLD} from 'sentry/components/charts/utils';
- import {t} from 'sentry/locale';
- import {DateString, PageFilters} from 'sentry/types';
- import {ReactEchartsRef} from 'sentry/types/echarts';
- import {
- formatMetricsUsingUnitAndOp,
- getNameFromMRI,
- MetricDisplayType,
- } from 'sentry/utils/metrics';
- import theme from 'sentry/utils/theme';
- import useRouter from 'sentry/utils/useRouter';
- import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
- import {getFormatter} from '../../components/charts/components/tooltip';
- import {Series} from './widget';
- type ChartProps = {
- displayType: MetricDisplayType;
- environments: PageFilters['environments'];
- projects: PageFilters['projects'];
- series: Series[];
- end?: string;
- onZoom?: (start: DateString, end: DateString) => void;
- operation?: string;
- period?: string;
- start?: string;
- utc?: boolean;
- };
- export function MetricChart({
- series,
- displayType,
- start,
- end,
- period,
- utc,
- operation,
- projects,
- environments,
- onZoom,
- }: ChartProps) {
- const chartRef = useRef<ReactEchartsRef>(null);
- const router = useRouter();
- // TODO(ddm): Try to do this in a more elegant way
- useEffect(() => {
- const echartsInstance = chartRef?.current?.getEchartsInstance();
- if (echartsInstance && !echartsInstance.group) {
- echartsInstance.group = DDM_CHART_GROUP;
- }
- });
- const unit = series[0]?.unit;
- const seriesToShow = series.filter(s => !s.hidden);
- // TODO(ddm): This assumes that all series have the same bucket size
- const bucketSize = seriesToShow[0]?.data[1]?.name - seriesToShow[0]?.data[0]?.name;
- const seriesLength = seriesToShow[0]?.data.length;
- const formatters = {
- valueFormatter: (value: number) =>
- formatMetricsUsingUnitAndOp(value, unit, operation),
- nameFormatter: mri => getNameFromMRI(mri),
- isGroupedByDate: true,
- bucketSize,
- showTimeInTooltip: true,
- };
- const displayFogOfWar = operation && ['sum', 'count'].includes(operation);
- const chartProps = {
- forwardedRef: chartRef,
- isGroupedByDate: true,
- height: 300,
- colors: seriesToShow.map(s => s.color),
- grid: {top: 20, bottom: 20, left: 15, right: 25},
- tooltip: {
- formatter: (params, asyncTicket) => {
- const hoveredEchartElement = Array.from(document.querySelectorAll(':hover')).find(
- element => {
- return element.classList.contains('echarts-for-react');
- }
- );
- if (hoveredEchartElement === chartRef?.current?.ele) {
- return getFormatter(formatters)(params, asyncTicket);
- }
- return '';
- },
- },
- yAxis: {
- axisLabel: {
- formatter: (value: number) => {
- return formatMetricsUsingUnitAndOp(value, unit, operation);
- },
- },
- },
- };
- return (
- <ChartWrapper>
- <ChartZoom
- router={router}
- period={period}
- start={start}
- end={end}
- utc={utc}
- onZoom={zoomPeriod => {
- onZoom?.(zoomPeriod.start, zoomPeriod.end);
- }}
- >
- {zoomRenderProps => (
- <ReleaseSeries
- utc={utc}
- period={period}
- start={zoomRenderProps.start!}
- end={zoomRenderProps.end!}
- projects={projects}
- environments={environments}
- preserveQueryParams
- >
- {({releaseSeries}) => {
- const releaseSeriesData = releaseSeries?.[0]?.markLine?.data ?? [];
- const selected =
- releaseSeriesData?.length >= RELEASE_LINES_THRESHOLD
- ? {[t('Releases')]: false}
- : {};
- const legend = releaseSeriesData?.length
- ? Legend({
- itemGap: 20,
- top: 0,
- right: 20,
- data: releaseSeries.map(s => s.seriesName),
- theme: theme as Theme,
- selected,
- })
- : undefined;
- const allProps = {
- ...chartProps,
- ...zoomRenderProps,
- series: [...seriesToShow, ...releaseSeries],
- legend,
- };
- return displayType === MetricDisplayType.LINE ? (
- <LineChart {...allProps} />
- ) : displayType === MetricDisplayType.AREA ? (
- <AreaChart {...allProps} />
- ) : (
- <BarChart stacked animation={false} {...allProps} />
- );
- }}
- </ReleaseSeries>
- )}
- </ChartZoom>
- {displayFogOfWar && (
- <FogOfWar bucketSize={bucketSize} seriesLength={seriesLength} />
- )}
- </ChartWrapper>
- );
- }
- function FogOfWar({
- bucketSize,
- seriesLength,
- }: {
- bucketSize?: number;
- seriesLength?: number;
- }) {
- if (!bucketSize || !seriesLength) {
- return null;
- }
- // For smaller time frames we want to show a wider fog of war
- const widthFactor = bucketSize > 30 * 60_000 ? 1 : 2;
- const fogOfWarWidth = widthFactor * bucketSize + 30_000;
- const seriesWidth = bucketSize * seriesLength;
- // If either of these are undefiend, NaN or 0 the result will be invalid
- if (!fogOfWarWidth || !seriesWidth) {
- return null;
- }
- const width = (fogOfWarWidth / seriesWidth) * 100;
- return <FogOfWarOverlay width={width ?? 0} />;
- }
- const ChartWrapper = styled('div')`
- position: relative;
- height: 300px;
- `;
- const FogOfWarOverlay = styled('div')<{width?: number}>`
- height: 244px;
- width: ${p => p.width}%;
- position: absolute;
- right: 21px;
- top: 18px;
- pointer-events: none;
- background: linear-gradient(
- 90deg,
- ${p => p.theme.background}00 0%,
- ${p => p.theme.background}FF 70%,
- ${p => p.theme.background}FF 100%
- );
- `;
|