123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- import {RouterFixture} from 'sentry-fixture/routerFixture';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {
- render,
- screen,
- userEvent,
- waitFor,
- within,
- } from 'sentry-test/reactTestingLibrary';
- import PageFiltersStore from 'sentry/stores/pageFiltersStore';
- import {DiscoverDatasets} from 'sentry/utils/discover/types';
- import {PageParamsProvider} from 'sentry/views/explore/contexts/pageParamsContext';
- import {SpanTagsProvider} from 'sentry/views/explore/contexts/spanTagsContext';
- import {MultiQueryModeContent} from 'sentry/views/explore/multiQueryMode/content';
- import {useReadQueriesFromLocation} from 'sentry/views/explore/multiQueryMode/locationUtils';
- jest.mock('sentry/components/lazyRender', () => ({
- LazyRender: ({children}: {children: React.ReactNode}) => children,
- }));
- describe('MultiQueryModeContent', function () {
- const {organization, project} = initializeOrg({
- organization: {
- features: ['visibility-explore-rpc'],
- },
- });
- let eventsRequest: any;
- let eventsStatsRequest: any;
- beforeEach(function () {
- // without this the `CompactSelect` component errors with a bunch of async updates
- jest.spyOn(console, 'error').mockImplementation();
- MockApiClient.clearMockResponses();
- PageFiltersStore.init();
- PageFiltersStore.onInitializeUrlState(
- {
- projects: [project].map(p => parseInt(p.id, 10)),
- environments: [],
- datetime: {
- period: '7d',
- start: null,
- end: null,
- utc: null,
- },
- },
- new Set()
- );
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/spans/fields/`,
- method: 'GET',
- body: [{key: 'span.op', name: 'span.op'}],
- });
- eventsRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/events/`,
- method: 'GET',
- body: {},
- });
- eventsStatsRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/events-stats/`,
- method: 'GET',
- body: {},
- });
- });
- it('updates visualization and outdated sorts', async function () {
- let queries: any;
- function Component() {
- queries = useReadQueriesFromLocation();
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- const section = screen.getByTestId('section-visualize-0');
- await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
- await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.self_time)'],
- sortBys: [
- {
- field: 'id',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.self_time'],
- groupBys: [],
- query: '',
- },
- ]);
- });
- it('updates sorts', async function () {
- let queries: any;
- function Component() {
- queries = useReadQueriesFromLocation();
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- const section = screen.getByTestId('section-sort-by-0');
- await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
- await userEvent.click(within(section).getByRole('option', {name: 'id'}));
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'id',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- });
- it('updates group bys and outdated sorts', async function () {
- let queries: any;
- function Component() {
- queries = useReadQueriesFromLocation();
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- const section = screen.getByTestId('section-group-by-0');
- await userEvent.click(within(section).getByRole('button', {name: 'None'}));
- await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
- expect(queries).toEqual([
- {
- yAxes: ['avg(span.duration)'],
- chartType: 1,
- sortBys: [
- {
- field: 'avg(span.duration)',
- kind: 'desc',
- },
- ],
- query: '',
- groupBys: ['span.op'],
- fields: ['id', 'span.duration'],
- },
- ]);
- });
- it('updates query at the correct index', async function () {
- let queries: any;
- function Component() {
- queries = useReadQueriesFromLocation();
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- // Add chart
- await userEvent.click(screen.getByRole('button', {name: 'Add Query'}));
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- const section = screen.getByTestId('section-visualize-0');
- await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
- await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.self_time)'],
- sortBys: [
- {
- field: 'id',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.self_time'],
- groupBys: [],
- query: '',
- },
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- await userEvent.click(screen.getAllByLabelText('Delete Query')[0]!);
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- });
- it('calls events and stats APIs', async function () {
- let queries: any;
- function Component() {
- queries = useReadQueriesFromLocation();
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- const section = screen.getByTestId('section-group-by-0');
- await userEvent.click(within(section).getByRole('button', {name: 'None'}));
- await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
- await waitFor(() =>
- expect(eventsStatsRequest).toHaveBeenNthCalledWith(
- 1,
- `/organizations/${organization.slug}/events-stats/`,
- expect.objectContaining({
- query: expect.objectContaining({
- dataset: 'spans',
- field: [],
- interval: '1h',
- orderby: undefined,
- project: ['2'],
- query: '!transaction.span_id:00',
- referrer: 'api.explorer.stats',
- statsPeriod: '7d',
- topEvents: undefined,
- useRpc: '1',
- yAxis: 'avg(span.duration)',
- }),
- })
- )
- );
- await waitFor(() =>
- expect(eventsRequest).toHaveBeenNthCalledWith(
- 1,
- `/organizations/${organization.slug}/events/`,
- expect.objectContaining({
- query: expect.objectContaining({
- dataset: 'spans',
- environment: [],
- field: [
- 'id',
- 'span.duration',
- 'transaction.span_id',
- 'trace',
- 'project',
- 'timestamp',
- ],
- per_page: 10,
- project: ['2'],
- query: '!transaction.span_id:00',
- referrer: 'api.explore.multi-query-spans-table',
- sort: '-span.duration',
- statsPeriod: '7d',
- useRpc: '1',
- }),
- })
- )
- );
- // group by requests
- await waitFor(() =>
- expect(eventsStatsRequest).toHaveBeenNthCalledWith(
- 2,
- `/organizations/${organization.slug}/events-stats/`,
- expect.objectContaining({
- query: expect.objectContaining({
- dataset: 'spans',
- excludeOther: 0,
- field: ['span.op', 'avg(span.duration)'],
- interval: '1h',
- orderby: '-avg_span_duration',
- project: ['2'],
- query: '!transaction.span_id:00',
- referrer: 'api.explorer.stats',
- sort: '-avg_span_duration',
- statsPeriod: '7d',
- topEvents: '5',
- useRpc: '1',
- yAxis: 'avg(span.duration)',
- }),
- })
- )
- );
- await waitFor(() =>
- expect(eventsRequest).toHaveBeenNthCalledWith(
- 2,
- `/organizations/${organization.slug}/events/`,
- expect.objectContaining({
- query: expect.objectContaining({
- dataset: 'spans',
- environment: [],
- field: ['span.op', 'avg(span.duration)'],
- per_page: 10,
- project: ['2'],
- query: '!transaction.span_id:00',
- referrer: 'api.explore.multi-query-spans-table',
- sort: '-avg_span_duration',
- statsPeriod: '7d',
- useRpc: '1',
- }),
- })
- )
- );
- });
- it('unstacking group by puts you in sample mode', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/events/`,
- method: 'GET',
- body: {
- data: [
- {
- 'span.op': 'POST',
- 'avg(span.duration)': 147.02002059925093,
- },
- {
- 'span.op': 'GET',
- 'avg(span.duration)': 1.9993342331511974,
- },
- ],
- },
- match: [
- function (_url: string, options: Record<string, any>) {
- return options.query.field.includes('span.op');
- },
- ],
- });
- let queries: any;
- function Component() {
- queries = useReadQueriesFromLocation();
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'span.duration',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: '',
- },
- ]);
- const section = screen.getByTestId('section-group-by-0');
- await userEvent.click(within(section).getByRole('button', {name: 'None'}));
- await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
- await userEvent.click(screen.getAllByTestId('unstack-link')[0]!);
- expect(queries).toEqual([
- {
- chartType: 1,
- yAxes: ['avg(span.duration)'],
- sortBys: [
- {
- field: 'id',
- kind: 'desc',
- },
- ],
- fields: ['id', 'span.duration'],
- groupBys: [],
- query: 'span.op:POST',
- },
- ]);
- });
- it('sets interval correctly', async function () {
- const router = RouterFixture({
- location: {
- pathname: '/traces/compare',
- query: {
- queries: [
- '{"groupBys":[],"query":"","sortBys":["-timestamp"],"yAxes":["avg(span.duration)"]}',
- ],
- },
- },
- });
- function Component() {
- return <MultiQueryModeContent />;
- }
- render(
- <PageParamsProvider>
- <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
- <Component />
- </SpanTagsProvider>
- </PageParamsProvider>,
- {router, organization}
- );
- const section = screen.getByTestId('section-visualization-0');
- expect(
- await within(section).findByRole('button', {name: '1 hour'})
- ).toBeInTheDocument();
- await userEvent.click(within(section).getByRole('button', {name: '1 hour'}));
- await userEvent.click(within(section).getByRole('option', {name: '30 minutes'}));
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/traces/compare',
- query: expect.objectContaining({
- interval: '30m',
- queries: [
- '{"groupBys":[],"query":"","sortBys":["-timestamp"],"yAxes":["avg(span.duration)"]}',
- ],
- }),
- });
- });
- });
|