123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- import {Component, Fragment} from 'react';
- import {InjectedRouter} from 'react-router';
- import styled from '@emotion/styled';
- import {LineSeriesOption} from 'echarts';
- import {Location} from 'history';
- import isEqual from 'lodash/isEqual';
- import {Client} from 'sentry/api';
- import {BarChart} from 'sentry/components/charts/barChart';
- import EventsChart from 'sentry/components/charts/eventsChart';
- import {getInterval, getPreviousSeriesName} from 'sentry/components/charts/utils';
- import {WorldMapChart} from 'sentry/components/charts/worldMapChart';
- import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
- import {Panel} from 'sentry/components/panels';
- import Placeholder from 'sentry/components/placeholder';
- import {t} from 'sentry/locale';
- import {Organization, SelectValue} from 'sentry/types';
- import {valueIsEqual} from 'sentry/utils';
- import {getUtcToLocalDateObject} from 'sentry/utils/dates';
- import EventView from 'sentry/utils/discover/eventView';
- import {isEquation, stripEquationPrefix} from 'sentry/utils/discover/fields';
- import {
- DisplayModes,
- MULTI_Y_AXIS_SUPPORTED_DISPLAY_MODES,
- TOP_EVENT_MODES,
- TOP_N,
- } from 'sentry/utils/discover/types';
- import getDynamicText from 'sentry/utils/getDynamicText';
- import {decodeScalar} from 'sentry/utils/queryString';
- import withApi from 'sentry/utils/withApi';
- import ChartFooter from './chartFooter';
- type ResultsChartProps = {
- api: Client;
- confirmedQuery: boolean;
- eventView: EventView;
- location: Location;
- organization: Organization;
- router: InjectedRouter;
- yAxisValue: string[];
- processedLineSeries?: LineSeriesOption[];
- };
- class ResultsChart extends Component<ResultsChartProps> {
- shouldComponentUpdate(nextProps: ResultsChartProps) {
- const {eventView, ...restProps} = this.props;
- const {eventView: nextEventView, ...restNextProps} = nextProps;
- if (!eventView.isEqualTo(nextEventView)) {
- return true;
- }
- return !isEqual(restProps, restNextProps);
- }
- render() {
- const {
- api,
- eventView,
- location,
- organization,
- router,
- confirmedQuery,
- yAxisValue,
- processedLineSeries,
- } = this.props;
- const hasPerformanceChartInterpolation = organization.features.includes(
- 'performance-chart-interpolation'
- );
- const globalSelection = eventView.getPageFilters();
- const start = globalSelection.datetime.start
- ? getUtcToLocalDateObject(globalSelection.datetime.start)
- : null;
- const end = globalSelection.datetime.end
- ? getUtcToLocalDateObject(globalSelection.datetime.end)
- : null;
- const {utc} = normalizeDateTimeParams(location.query);
- const apiPayload = eventView.getEventsAPIPayload(location);
- const display = eventView.getDisplayMode();
- const isTopEvents =
- display === DisplayModes.TOP5 || display === DisplayModes.DAILYTOP5;
- const isPeriod = display === DisplayModes.DEFAULT || display === DisplayModes.TOP5;
- const isDaily = display === DisplayModes.DAILYTOP5 || display === DisplayModes.DAILY;
- const isPrevious = display === DisplayModes.PREVIOUS;
- const referrer = `api.discover.${display}-chart`;
- const topEvents = eventView.topEvents ? parseInt(eventView.topEvents, 10) : TOP_N;
- const chartComponent =
- display === DisplayModes.WORLDMAP
- ? WorldMapChart
- : display === DisplayModes.BAR
- ? BarChart
- : undefined;
- const interval =
- display === DisplayModes.BAR
- ? getInterval(
- {
- start,
- end,
- period: globalSelection.datetime.period,
- utc: utc === 'true',
- },
- 'low'
- )
- : eventView.interval;
- const seriesLabels = yAxisValue.map(stripEquationPrefix);
- const disableableSeries = [
- ...seriesLabels,
- ...seriesLabels.map(getPreviousSeriesName),
- ];
- if (processedLineSeries?.length) {
- processedLineSeries.forEach(series =>
- disableableSeries.push((series.name as string) ?? '')
- );
- }
- return (
- <Fragment>
- {getDynamicText({
- value: (
- <EventsChart
- api={api}
- router={router}
- query={apiPayload.query}
- organization={organization}
- showLegend
- yAxis={yAxisValue}
- projects={globalSelection.projects}
- environments={globalSelection.environments}
- start={start}
- end={end}
- period={globalSelection.datetime.period}
- disablePrevious={!isPrevious}
- disableReleases={!isPeriod}
- field={isTopEvents ? apiPayload.field : undefined}
- interval={interval}
- showDaily={isDaily}
- topEvents={isTopEvents ? topEvents : undefined}
- orderby={isTopEvents ? decodeScalar(apiPayload.sort) : undefined}
- utc={utc === 'true'}
- confirmedQuery={confirmedQuery}
- withoutZerofill={hasPerformanceChartInterpolation}
- chartComponent={chartComponent}
- referrer={referrer}
- fromDiscover
- disableableSeries={disableableSeries}
- additionalSeries={processedLineSeries}
- />
- ),
- fixed: <Placeholder height="200px" testId="skeleton-ui" />,
- })}
- </Fragment>
- );
- }
- }
- type ContainerProps = {
- api: Client;
- confirmedQuery: boolean;
- disableProcessedBaselineToggle: boolean;
- eventView: EventView;
- location: Location;
- onAxisChange: (value: string[]) => void;
- onDisplayChange: (value: string) => void;
- onIntervalChange: (value: string | undefined) => void;
- onTopEventsChange: (value: string) => void;
- organization: Organization;
- router: InjectedRouter;
- setShowBaseline: (value: boolean) => void;
- showBaseline: boolean;
- // chart footer props
- total: number | null;
- yAxis: string[];
- loadingProcessedTotals?: boolean;
- processedLineSeries?: LineSeriesOption[];
- processedTotal?: number;
- };
- type ContainerState = {
- yAxisOptions: SelectValue<string>[];
- };
- class ResultsChartContainer extends Component<ContainerProps, ContainerState> {
- state: ContainerState = {
- yAxisOptions: this.getYAxisOptions(this.props.eventView),
- };
- componentWillReceiveProps(nextProps) {
- const yAxisOptions = this.getYAxisOptions(this.props.eventView);
- const nextYAxisOptions = this.getYAxisOptions(nextProps.eventView);
- if (!valueIsEqual(yAxisOptions, nextYAxisOptions, true)) {
- this.setState({yAxisOptions: nextYAxisOptions});
- }
- }
- shouldComponentUpdate(nextProps: ContainerProps) {
- const {eventView, ...restProps} = this.props;
- const {eventView: nextEventView, ...restNextProps} = nextProps;
- if (
- !eventView.isEqualTo(nextEventView) ||
- this.props.confirmedQuery !== nextProps.confirmedQuery
- ) {
- return true;
- }
- return !isEqual(restProps, restNextProps);
- }
- getYAxisOptions(eventView) {
- const yAxisOptions = eventView.getYAxisOptions();
- // Equations on World Map isn't supported on the events-geo endpoint
- // Disabling equations as an option to prevent erroring out
- if (eventView.getDisplayMode() === DisplayModes.WORLDMAP) {
- return yAxisOptions.filter(({value}) => !isEquation(value));
- }
- return yAxisOptions;
- }
- render() {
- const {
- api,
- eventView,
- location,
- router,
- total,
- onAxisChange,
- onDisplayChange,
- onIntervalChange,
- onTopEventsChange,
- organization,
- confirmedQuery,
- yAxis,
- disableProcessedBaselineToggle,
- processedLineSeries,
- showBaseline,
- setShowBaseline,
- processedTotal,
- loadingProcessedTotals,
- } = this.props;
- const {yAxisOptions} = this.state;
- const hasQueryFeature = organization.features.includes('discover-query');
- const displayOptions = eventView
- .getDisplayOptions()
- .filter(opt => {
- // top5 modes are only available with larger packages in saas.
- // We remove instead of disable here as showing tooltips in dropdown
- // menus is clunky.
- if (TOP_EVENT_MODES.includes(opt.value) && !hasQueryFeature) {
- return false;
- }
- return true;
- })
- .map(opt => {
- // Can only use default display or total daily with multi y axis
- if (TOP_EVENT_MODES.includes(opt.value)) {
- opt.label = DisplayModes.TOP5 === opt.value ? 'Top Period' : 'Top Daily';
- }
- if (
- yAxis.length > 1 &&
- !MULTI_Y_AXIS_SUPPORTED_DISPLAY_MODES.includes(opt.value as DisplayModes)
- ) {
- return {
- ...opt,
- disabled: true,
- tooltip: t(
- 'Change the Y-Axis dropdown to display only 1 function to use this view.'
- ),
- };
- }
- return opt;
- });
- return (
- <StyledPanel>
- {(yAxis.length > 0 && (
- <ResultsChart
- api={api}
- eventView={eventView}
- location={location}
- organization={organization}
- router={router}
- confirmedQuery={confirmedQuery}
- yAxisValue={yAxis}
- processedLineSeries={processedLineSeries}
- />
- )) || <NoChartContainer>{t('No Y-Axis selected.')}</NoChartContainer>}
- <ChartFooter
- organization={organization}
- total={total}
- disableProcessedBaselineToggle={disableProcessedBaselineToggle}
- yAxisValue={yAxis}
- yAxisOptions={yAxisOptions}
- eventView={eventView}
- onAxisChange={onAxisChange}
- displayOptions={displayOptions}
- displayMode={eventView.getDisplayMode()}
- onDisplayChange={onDisplayChange}
- onTopEventsChange={onTopEventsChange}
- onIntervalChange={onIntervalChange}
- topEvents={eventView.topEvents ?? TOP_N.toString()}
- showBaseline={showBaseline}
- setShowBaseline={setShowBaseline}
- processedTotal={processedTotal}
- loadingProcessedTotals={loadingProcessedTotals}
- />
- </StyledPanel>
- );
- }
- }
- export default withApi(ResultsChartContainer);
- const StyledPanel = styled(Panel)`
- @media (min-width: ${p => p.theme.breakpoints.large}) {
- margin: 0;
- }
- `;
- const NoChartContainer = styled('div')<{height?: string}>`
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- flex: 1;
- flex-shrink: 0;
- overflow: hidden;
- height: ${p => p.height || '200px'};
- position: relative;
- border-color: transparent;
- margin-bottom: 0;
- color: ${p => p.theme.gray300};
- font-size: ${p => p.theme.fontSizeExtraLarge};
- `;
|