123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- // eslint-disable-next-line no-restricted-imports
- import {browserHistory, withRouter, WithRouterProps} from 'react-router';
- import {useTheme} from '@emotion/react';
- import type {LegendComponentOption} from 'echarts';
- import ChartZoom from 'sentry/components/charts/chartZoom';
- import {
- LineChart,
- LineChartProps,
- LineChartSeries,
- } from 'sentry/components/charts/lineChart';
- import TransitionChart from 'sentry/components/charts/transitionChart';
- import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
- import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
- import {t} from 'sentry/locale';
- import {EventsStatsData, OrganizationSummary, Project} from 'sentry/types';
- import {Series} from 'sentry/types/echarts';
- import {getUtcToLocalDateObject} from 'sentry/utils/dates';
- import {
- axisLabelFormatter,
- getDurationUnit,
- tooltipFormatter,
- } from 'sentry/utils/discover/charts';
- import {aggregateOutputType} from 'sentry/utils/discover/fields';
- import getDynamicText from 'sentry/utils/getDynamicText';
- import {decodeList} from 'sentry/utils/queryString';
- import {Theme} from 'sentry/utils/theme';
- import {ViewProps} from '../types';
- import {
- NormalizedTrendsTransaction,
- TrendChangeType,
- TrendFunctionField,
- TrendsStats,
- } from './types';
- import {
- generateTrendFunctionAsString,
- getCurrentTrendFunction,
- getCurrentTrendParameter,
- getUnselectedSeries,
- transformEventStatsSmoothed,
- trendToColor,
- } from './utils';
- type Props = WithRouterProps &
- ViewProps & {
- isLoading: boolean;
- location: Location;
- organization: OrganizationSummary;
- projects: Project[];
- statsData: TrendsStats;
- trendChangeType: TrendChangeType;
- disableLegend?: boolean;
- disableXAxis?: boolean;
- grid?: LineChartProps['grid'];
- height?: number;
- transaction?: NormalizedTrendsTransaction;
- trendFunctionField?: TrendFunctionField;
- };
- function transformEventStats(data: EventsStatsData, seriesName?: string): Series[] {
- return [
- {
- seriesName: seriesName || 'Current',
- data: data.map(([timestamp, countsForTimestamp]) => ({
- name: timestamp * 1000,
- value: countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
- })),
- },
- ];
- }
- function getLegend(trendFunction: string): LegendComponentOption {
- return {
- right: 10,
- top: 0,
- itemGap: 12,
- align: 'left',
- data: [
- {
- name: 'Baseline',
- icon: 'path://M180 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z, M810 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40zm, M1440 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z',
- },
- {
- name: 'Releases',
- },
- {
- name: trendFunction,
- },
- ],
- };
- }
- function getIntervalLine(
- theme: Theme,
- series: Series[],
- intervalRatio: number,
- transaction?: NormalizedTrendsTransaction
- ): LineChartSeries[] {
- if (!transaction || !series.length || !series[0].data || !series[0].data.length) {
- return [];
- }
- const seriesStart = parseInt(series[0].data[0].name as string, 10);
- const seriesEnd = parseInt(series[0].data.slice(-1)[0].name as string, 10);
- if (seriesEnd < seriesStart) {
- return [];
- }
- const periodLine: LineChartSeries = {
- data: [],
- color: theme.textColor,
- markLine: {
- data: [],
- label: {},
- lineStyle: {
- color: theme.textColor,
- type: 'dashed',
- width: 1,
- },
- symbol: ['none', 'none'],
- tooltip: {
- show: false,
- },
- },
- seriesName: 'Baseline',
- };
- const periodLineLabel = {
- fontSize: 11,
- show: true,
- color: theme.textColor,
- silent: true,
- };
- const previousPeriod = {
- ...periodLine,
- markLine: {...periodLine.markLine},
- seriesName: 'Baseline',
- };
- const currentPeriod = {
- ...periodLine,
- markLine: {...periodLine.markLine},
- seriesName: 'Baseline',
- };
- const periodDividingLine = {
- ...periodLine,
- markLine: {...periodLine.markLine},
- seriesName: 'Period split',
- };
- const seriesDiff = seriesEnd - seriesStart;
- const seriesLine = seriesDiff * intervalRatio + seriesStart;
- previousPeriod.markLine.data = [
- [
- {value: 'Past', coord: [seriesStart, transaction.aggregate_range_1]},
- {coord: [seriesLine, transaction.aggregate_range_1]},
- ],
- ];
- previousPeriod.markLine.tooltip = {
- formatter: () => {
- return [
- '<div class="tooltip-series tooltip-series-solo">',
- '<div>',
- `<span class="tooltip-label"><strong>${t('Past Baseline')}</strong></span>`,
- // p50() coerces the axis to be time based
- tooltipFormatter(transaction.aggregate_range_1, 'duration'),
- '</div>',
- '</div>',
- '<div class="tooltip-arrow"></div>',
- ].join('');
- },
- };
- currentPeriod.markLine.data = [
- [
- {value: 'Present', coord: [seriesLine, transaction.aggregate_range_2]},
- {coord: [seriesEnd, transaction.aggregate_range_2]},
- ],
- ];
- currentPeriod.markLine.tooltip = {
- formatter: () => {
- return [
- '<div class="tooltip-series tooltip-series-solo">',
- '<div>',
- `<span class="tooltip-label"><strong>${t('Present Baseline')}</strong></span>`,
- // p50() coerces the axis to be time based
- tooltipFormatter(transaction.aggregate_range_2, 'duration'),
- '</div>',
- '</div>',
- '<div class="tooltip-arrow"></div>',
- ].join('');
- },
- };
- periodDividingLine.markLine = {
- data: [
- {
- xAxis: seriesLine,
- },
- ],
- label: {show: false},
- lineStyle: {
- color: theme.textColor,
- type: 'solid',
- width: 2,
- },
- symbol: ['none', 'none'],
- tooltip: {
- show: false,
- },
- silent: true,
- };
- previousPeriod.markLine.label = {
- ...periodLineLabel,
- formatter: 'Past',
- position: 'insideStartBottom',
- };
- currentPeriod.markLine.label = {
- ...periodLineLabel,
- formatter: 'Present',
- position: 'insideEndBottom',
- };
- const additionalLineSeries = [previousPeriod, currentPeriod, periodDividingLine];
- return additionalLineSeries;
- }
- export function Chart({
- trendChangeType,
- router,
- statsPeriod,
- transaction,
- statsData,
- isLoading,
- location,
- start: propsStart,
- end: propsEnd,
- trendFunctionField,
- disableXAxis,
- disableLegend,
- grid,
- height,
- projects,
- project,
- }: Props) {
- const theme = useTheme();
- const handleLegendSelectChanged = legendChange => {
- const {selected} = legendChange;
- const unselected = Object.keys(selected).filter(key => !selected[key]);
- const query = {
- ...location.query,
- };
- const queryKey = getUnselectedSeries(trendChangeType);
- query[queryKey] = unselected;
- const to = {
- ...location,
- query,
- };
- browserHistory.push(to);
- };
- const lineColor = trendToColor[trendChangeType || ''];
- const events =
- statsData && transaction?.project && transaction?.transaction
- ? statsData[[transaction.project, transaction.transaction].join(',')]
- : undefined;
- const data = events?.data ?? [];
- const trendFunction = getCurrentTrendFunction(location, trendFunctionField);
- const trendParameter = getCurrentTrendParameter(location, projects, project);
- const chartLabel = generateTrendFunctionAsString(
- trendFunction.field,
- trendParameter.column
- );
- const results = transformEventStats(data, chartLabel);
- const {smoothedResults, minValue, maxValue} = transformEventStatsSmoothed(
- results,
- chartLabel
- );
- const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
- const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
- const {utc} = normalizeDateTimeParams(location.query);
- const seriesSelection = decodeList(
- location.query[getUnselectedSeries(trendChangeType)]
- ).reduce((selection, metric) => {
- selection[metric] = false;
- return selection;
- }, {});
- const legend: LegendComponentOption = disableLegend
- ? {show: false}
- : {
- ...getLegend(chartLabel),
- selected: seriesSelection,
- };
- const loading = isLoading;
- const reloading = isLoading;
- const yMax = Math.max(
- maxValue,
- transaction?.aggregate_range_2 || 0,
- transaction?.aggregate_range_1 || 0
- );
- const yMin = Math.min(
- minValue,
- transaction?.aggregate_range_1 || Number.MAX_SAFE_INTEGER,
- transaction?.aggregate_range_2 || Number.MAX_SAFE_INTEGER
- );
- const smoothedSeries = smoothedResults
- ? smoothedResults.map(values => {
- return {
- ...values,
- color: lineColor.default,
- lineStyle: {
- opacity: 1,
- },
- };
- })
- : [];
- const intervalSeries = getIntervalLine(theme, smoothedResults || [], 0.5, transaction);
- const yDiff = yMax - yMin;
- const yMargin = yDiff * 0.1;
- const series = [...smoothedSeries, ...intervalSeries];
- const durationUnit = getDurationUnit(series);
- const chartOptions: Omit<LineChartProps, 'series'> = {
- tooltip: {
- valueFormatter: (value, seriesName) => {
- return tooltipFormatter(value, aggregateOutputType(seriesName));
- },
- },
- yAxis: {
- min: Math.max(0, yMin - yMargin),
- max: yMax + yMargin,
- minInterval: durationUnit,
- axisLabel: {
- color: theme.chartLabel,
- formatter: (value: number) =>
- axisLabelFormatter(value, 'duration', undefined, durationUnit),
- },
- },
- };
- return (
- <ChartZoom
- router={router}
- period={statsPeriod}
- start={start}
- end={end}
- utc={utc === 'true'}
- >
- {zoomRenderProps => {
- return (
- <TransitionChart loading={loading} reloading={reloading}>
- <TransparentLoadingMask visible={reloading} />
- {getDynamicText({
- value: (
- <LineChart
- height={height}
- {...zoomRenderProps}
- {...chartOptions}
- onLegendSelectChanged={handleLegendSelectChanged}
- series={series}
- seriesOptions={{
- showSymbol: false,
- }}
- legend={legend}
- toolBox={{
- show: false,
- }}
- grid={
- grid ?? {
- left: '10px',
- right: '10px',
- top: '40px',
- bottom: '0px',
- }
- }
- xAxis={disableXAxis ? {show: false} : undefined}
- />
- ),
- fixed: 'Duration Chart',
- })}
- </TransitionChart>
- );
- }}
- </ChartZoom>
- );
- }
- export default withRouter(Chart);
|