diff --git a/src/Exceptionless.Web/ClientApp/e2e/tests/bulk-selection-pending.e2e.ts b/src/Exceptionless.Web/ClientApp/e2e/tests/bulk-selection-pending.e2e.ts new file mode 100644 index 0000000000..64888b9b5d --- /dev/null +++ b/src/Exceptionless.Web/ClientApp/e2e/tests/bulk-selection-pending.e2e.ts @@ -0,0 +1,73 @@ +import { expect, test } from '../fixtures/e2e-test'; +import { createRepresentativeEvent } from '../support/synthetic-event'; + +test.use({ e2eUseGeneratedUser: true }); + +for (const kind of ['event', 'stack'] as const) { + test(`selecting all matching ${kind}s takes effect while the count refreshes`, async ({ e2eApi, e2eScenario, page }) => { + // Arrange + for (let index = 0; index < 5; index++) { + const referenceId = `${e2eScenario.referenceId}-pending-${index}`; + await e2eApi.submitEvent( + e2eScenario.projectId, + e2eScenario.projectToken, + createRepresentativeEvent({ + appUrl: e2eApi.environment.appUrl, + message: `${e2eScenario.message} ${referenceId}`, + referenceId, + runId: e2eApi.environment.runId + }) + ); + await e2eApi.pollForEventByReference(e2eScenario.userToken, e2eScenario.projectId, referenceId); + } + await page.goto(`/${kind}?project=${e2eScenario.projectId}&time=all&limit=2`); + const table = page.locator('table:has(thead [role="checkbox"])').first(); + await table.getByRole('checkbox', { name: 'Select row' }).nth(1).waitFor({ state: 'visible', timeout: 30_000 }); + const isCountRequest = (url: URL) => + url.pathname === `/api/v2/organizations/${e2eScenario.organizationId}/events` && url.searchParams.get('limit') === '1'; + const initialCount = page.waitForResponse((response) => isCountRequest(new URL(response.url())) && response.status() === 200); + await table.getByRole('checkbox', { name: 'Select all rows' }).click(); + await (await initialCount).finished(); + const selectAll = page.getByRole('button', { name: `Select all 5 ${kind}s matching this filter` }); + await selectAll.waitFor({ state: 'visible' }); + const countStarted = Promise.withResolvers(); + const countReleased = Promise.withResolvers(); + await page.route( + isCountRequest, + async (route) => { + countStarted.resolve(); + await countReleased.promise; + await route.continue(); + }, + { times: 1 } + ); + const countResponse = page.waitForResponse((response) => isCountRequest(new URL(response.url())) && response.status() === 200); + + // Act + let selectionText: null | string; + let dialogText: null | string; + try { + await selectAll.click(); + await countStarted.promise; + await page.getByRole('button', { name: 'Actions' }).click(); + await page.getByRole('menuitem', { name: kind === 'event' ? 'Delete' : 'Mark Ignored' }).click(); + const dialog = page.getByRole('alertdialog'); + await dialog.waitFor({ state: 'visible' }); + dialogText = await dialog.textContent(); + await dialog.getByRole('button', { name: 'Cancel' }).click(); + const selectionStatus = page.getByRole('status').filter({ hasText: `All 5 ${kind}s are selected` }); + await selectionStatus.waitFor({ state: 'visible', timeout: 3_000 }); + selectionText = await selectionStatus.textContent(); + await page.getByRole('button', { name: 'Clear selection' }).click(); + } finally { + countReleased.resolve(); + } + await (await countResponse).finished(); + + // Assert + expect(selectionText).toContain(`All 5 ${kind}s are selected`); + expect(dialogText).toContain(`About 5 ${kind}s match this filter`); + await expect(page.getByRole('button', { name: 'Clear selection' })).toBeHidden(); + await expect(table.getByRole('checkbox', { name: 'Select all rows' })).not.toBeChecked(); + }); +} diff --git a/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.test.ts b/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.test.ts index 3e45b88982..dc9b517709 100644 --- a/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.test.ts +++ b/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { createAllMatchingSelection, getAllMatchingIdentity } from './all-matching-selection.svelte'; @@ -6,7 +6,10 @@ describe('createAllMatchingSelection', () => { it('is active only for the query it was made for', () => { // Arrange const identity = getAllMatchingIdentity({ filter: 'type:error', organizationId: 'organization-id', time: '[now-7d TO now]' }); - const selection = createAllMatchingSelection(() => identity); + const selection = createAllMatchingSelection( + () => identity, + async () => undefined + ); // Act selection.select(1234); @@ -24,7 +27,10 @@ describe('createAllMatchingSelection', () => { ])('is dropped when the %s changes', (_, changed) => { // Arrange let identity = getAllMatchingIdentity({ filter: 'type:error', organizationId: 'organization-id', time: '[now-7d TO now]' }); - const selection = createAllMatchingSelection(() => identity); + const selection = createAllMatchingSelection( + () => identity, + async () => undefined + ); selection.select(1234); // Act @@ -37,7 +43,8 @@ describe('createAllMatchingSelection', () => { it('ignores a count that was fetched for a filter that is no longer showing', () => { // Arrange let identity = getAllMatchingIdentity({ filter: 'type:error', organizationId: 'organization-id' }); - const selection = createAllMatchingSelection(() => identity); + const refreshCount = vi.fn(async () => 1234); + const selection = createAllMatchingSelection(() => identity, refreshCount); const identityWhenCountingStarted = selection.identity; // Act @@ -46,6 +53,7 @@ describe('createAllMatchingSelection', () => { // Assert expect(selected).toBe(false); + expect(refreshCount).not.toHaveBeenCalled(); expect(selection.active).toBeUndefined(); }); @@ -53,7 +61,10 @@ describe('createAllMatchingSelection', () => { // Arrange const original = getAllMatchingIdentity({ filter: 'type:error', organizationId: 'organization-id' }); let identity = original; - const selection = createAllMatchingSelection(() => identity); + const selection = createAllMatchingSelection( + () => identity, + async () => undefined + ); selection.select(10); // Act @@ -64,4 +75,100 @@ describe('createAllMatchingSelection', () => { // Assert expect(selection.active).toBeUndefined(); }); + + it('selects the displayed estimate immediately and updates it when the count finishes', async () => { + // Arrange + const count = Promise.withResolvers(); + const selection = createAllMatchingSelection( + () => 'query', + () => count.promise + ); + + // Act + const selected = selection.select(5); + const initialCount = selection.active?.count; + count.resolve(7); + await count.promise; + + // Assert + expect(selected).toBe(true); + expect(initialCount).toBe(5); + expect(selection.active?.count).toBe(7); + }); + + it('does not restore a selection cleared while the count was loading', async () => { + // Arrange + const count = Promise.withResolvers(); + const selection = createAllMatchingSelection( + () => 'query', + () => count.promise + ); + selection.select(5); + + // Act + selection.clear(); + count.resolve(7); + await count.promise; + + // Assert + expect(selection.active).toBeUndefined(); + }); + + it('does not overwrite a newer selection when an earlier count finishes later', async () => { + // Arrange + const firstCount = Promise.withResolvers(); + const secondCount = Promise.withResolvers(); + const refreshCount = vi.fn<() => Promise>().mockReturnValueOnce(firstCount.promise).mockReturnValueOnce(secondCount.promise); + const selection = createAllMatchingSelection(() => 'query', refreshCount); + selection.select(5); + selection.select(6); + + // Act + secondCount.resolve(8); + await secondCount.promise; + firstCount.resolve(7); + await firstCount.promise; + + // Assert + expect(selection.active?.count).toBe(8); + }); + + it('does not apply a refreshed count to a different query', async () => { + // Arrange + let identity = 'first'; + const count = Promise.withResolvers(); + const selection = createAllMatchingSelection( + () => identity, + () => count.promise + ); + selection.select(5); + + // Act + identity = 'second'; + count.resolve(7); + await count.promise; + const activeForDifferentQuery = selection.active; + identity = 'first'; + + // Assert + expect(activeForDifferentQuery).toBeUndefined(); + expect(selection.active?.count).toBe(5); + }); + + it('keeps the displayed estimate if the refresh fails', async () => { + // Arrange + const count = Promise.withResolvers(); + const selection = createAllMatchingSelection( + () => 'query', + () => count.promise + ); + + // Act + selection.select(5); + count.reject(new Error('Count unavailable')); + await count.promise.catch(() => undefined); + + // Assert + expect(selection.active?.count).toBe(5); + }); }); diff --git a/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.ts b/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.ts index db09696882..90c869855e 100644 --- a/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.ts +++ b/src/Exceptionless.Web/ClientApp/src/lib/features/shared/all-matching-selection.svelte.ts @@ -19,9 +19,23 @@ export interface AllMatchingTarget { * for: as soon as the query identity (organization, filter, time range, saved view) changes it is no longer active, so * an action can never run against a different set of events than the user confirmed. */ -export function createAllMatchingSelection(getIdentity: () => string) { +export function createAllMatchingSelection(getIdentity: () => string, refreshCount: () => Promise) { let selection = $state(); + async function updateCount(selected: AllMatchingSelection) { + let count: number | undefined; + try { + count = await refreshCount(); + } catch { + // A failed count refresh leaves the displayed estimate in place. + return; + } + + if (count !== undefined && selection === selected && selected.identity === getIdentity()) { + selected.count = count; + } + } + return { get active(): AllMatchingSelection | undefined { return selection && selection.identity === getIdentity() ? selection : undefined; @@ -47,6 +61,7 @@ export function createAllMatchingSelection(getIdentity: () => string) { count, identity }; + void updateCount(selection); return true; } }; diff --git a/src/Exceptionless.Web/ClientApp/src/routes/(app)/event/+page.svelte b/src/Exceptionless.Web/ClientApp/src/routes/(app)/event/+page.svelte index 3ad4c4d8ed..e364be0040 100644 --- a/src/Exceptionless.Web/ClientApp/src/routes/(app)/event/+page.svelte +++ b/src/Exceptionless.Web/ClientApp/src/routes/(app)/event/+page.svelte @@ -775,13 +775,18 @@ // Gmail-style "select everything matching the filter". It belongs to the query it was made for, so a different // filter, time range, project or saved view drops it. - const allMatching = createAllMatchingSelection(() => - getAllMatchingIdentity({ - filter: eventsQueryParameters.filter, - organizationId: organization.current, - savedViewId: savedViewsState.activeSavedView?.id, - time: eventsQueryParameters.time - }) + const allMatching = createAllMatchingSelection( + () => + getAllMatchingIdentity({ + filter: eventsQueryParameters.filter, + organizationId: organization.current, + savedViewId: savedViewsState.activeSavedView?.id, + time: eventsQueryParameters.time + }), + async () => { + const result = await matchingTotalQuery.refetch(); + return result.data?.meta.total as number | undefined; + } ); const pageRowCount = $derived(table.getRowModel().rows.length); const isPageSelected = $derived(pageRowCount > 0 && table.getIsAllRowsSelected()); @@ -882,15 +887,12 @@ table.setPageIndex(0); } - async function selectAllMatching() { - // Count again right now, so the number the user confirms is current. If the filter changes while the count is - // loading, the count belongs to the old filter and must not be bound to the new one. - const identity = allMatching.identity; - const result = await matchingTotalQuery.refetch(); - const count = (result.data?.meta.total ?? totalMatching) as number | undefined; - if (count !== undefined) { - allMatching.select(count, identity); + function selectAllMatching() { + if (totalMatching === undefined) { + return; } + + allMatching.select(totalMatching); } let reconcileTotalRequested = false; diff --git a/src/Exceptionless.Web/ClientApp/src/routes/(app)/stack/+page.svelte b/src/Exceptionless.Web/ClientApp/src/routes/(app)/stack/+page.svelte index 7efab475c4..e97f6e8f39 100644 --- a/src/Exceptionless.Web/ClientApp/src/routes/(app)/stack/+page.svelte +++ b/src/Exceptionless.Web/ClientApp/src/routes/(app)/stack/+page.svelte @@ -746,13 +746,18 @@ // Gmail-style "select everything matching the filter". It belongs to the query it was made for, so a different // filter, time range, project or saved view drops it. - const allMatching = createAllMatchingSelection(() => - getAllMatchingIdentity({ - filter: eventsQueryParameters.filter, - organizationId: organization.current, - savedViewId: savedViewsState.activeSavedView?.id, - time: eventsQueryParameters.time - }) + const allMatching = createAllMatchingSelection( + () => + getAllMatchingIdentity({ + filter: eventsQueryParameters.filter, + organizationId: organization.current, + savedViewId: savedViewsState.activeSavedView?.id, + time: eventsQueryParameters.time + }), + async () => { + const result = await matchingTotalQuery.refetch(); + return result.data?.meta.total as number | undefined; + } ); const pageRowCount = $derived(table.getRowModel().rows.length); const isPageSelected = $derived(pageRowCount > 0 && table.getIsAllRowsSelected()); @@ -808,15 +813,12 @@ table.setPageIndex(0); } - async function selectAllMatching() { - // Count again right now, so the number the user confirms is current. If the filter changes while the count is - // loading, the count belongs to the old filter and must not be bound to the new one. - const identity = allMatching.identity; - const result = await matchingTotalQuery.refetch(); - const count = (result.data?.meta.total ?? totalMatching) as number | undefined; - if (count !== undefined) { - allMatching.select(count, identity); + function selectAllMatching() { + if (totalMatching === undefined) { + return; } + + allMatching.select(totalMatching); } const debouncedReconciliationRefetch = debounce(1500, () => eventsQuery.refetch());