Skip to content

Commit

Permalink
[workspace] Fix not able to query legacy data when have at least 1 wo…
Browse files Browse the repository at this point in the history
…rkspace (#7254) (#7320)

* Fix not able to query legacy data when have at least 1 workspace



* fix typo



* update snapshot



---------



(cherry picked from commit 3bb4170)

Signed-off-by: Hailong Cui <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Yulong Ruan <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2024
1 parent acbfcaf commit f1ae46e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface SavedObjectCountOptions {
namespacesToInclude?: string[];
searchString?: string;
workspaces?: string[];
availableWorkspaces?: string[];
}

export async function getSavedObjectCounts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ describe('SavedObjectsTable', () => {
});
});

it('all visible workspaces in find options when not in any workspace', async () => {
it('no workspaces in find options when not in any workspace', async () => {
findObjectsMock.mockClear();
const applications = applicationServiceMock.createStartContract();
applications.capabilities = {
Expand Down Expand Up @@ -851,8 +851,8 @@ describe('SavedObjectsTable', () => {
await waitFor(() => {
expect(findObjectsMock).toBeCalledWith(
http,
expect.objectContaining({
workspaces: expect.arrayContaining(['workspace1', 'workspace2']),
expect.not.objectContaining({
workspaces,
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,14 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
}

private get workspaceIdQuery() {
const { availableWorkspaces, currentWorkspaceId, workspaceEnabled } = this.state;
const { currentWorkspaceId, workspaceEnabled } = this.state;
// workspace is turned off
if (!workspaceEnabled) {
return undefined;
} else {
// application home
// not in any workspace
if (!currentWorkspaceId) {
// public workspace is virtual at this moment
return availableWorkspaces?.map((ws) => ws.id);
return undefined;
} else {
return [currentWorkspaceId];
}
Expand Down Expand Up @@ -250,6 +249,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
typesToInclude: filteredTypes,
searchString: queryText,
workspaces: this.workspaceIdQuery,
availableWorkspaces: this.state.availableWorkspaces?.map((ws) => ws.id),
});

if (availableNamespaces.length) {
Expand Down Expand Up @@ -286,6 +286,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
typesToInclude: allowedTypes,
searchString: queryText,
workspaces: this.workspaceIdQuery,
availableWorkspaces: this.state.availableWorkspaces?.map((ws) => ws.id),
});

if (availableNamespaces.length) {
Expand Down Expand Up @@ -958,9 +959,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
if (workspaceEnabled && availableWorkspaces?.length) {
const wsCounts = savedObjectCounts.workspaces || {};
const wsFilterOptions = availableWorkspaces
.filter((ws) => {
return this.workspaceIdQuery?.includes(ws.id);
})
.filter((ws) => (currentWorkspaceId ? currentWorkspaceId === ws.id : true))
.map((ws) => {
return {
name: ws.name,
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/saved_objects_management/public/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,28 @@ describe('Utils', () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', workspaces: ['foo'] });
expect(obj).toEqual({ foo: 'bar', workspaces: ['foo'] });
});

it('formatWorkspaceIdParams with availableWorkspaces exists', async () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', availableWorkspaces: ['foo'] });
expect(obj).toEqual({ foo: 'bar', availableWorkspaces: ['foo'] });
});

it('formatWorkspaceIdParams with availableWorkspaces is empty array', async () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', availableWorkspaces: [] });
expect(obj).toEqual({ foo: 'bar' });
});

it('formatWorkspaceIdParams with availableWorkspaces is null/undefined', async () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', availableWorkspaces: null });
expect(obj).toEqual({ foo: 'bar' });
});

it('formatWorkspaceIdParams with both workspaces and availableWorkspaces are not empty', async () => {
const obj = formatWorkspaceIdParams({
foo: 'bar',
availableWorkspaces: ['foo', 'bar'],
workspaces: ['foo'],
});
expect(obj).toEqual({ foo: 'bar', availableWorkspaces: ['foo', 'bar'], workspaces: ['foo'] });
});
});
10 changes: 5 additions & 5 deletions src/plugins/saved_objects_management/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

export function formatWorkspaceIdParams<T extends { workspaces?: string[] | null }>(
obj: T
): T | Omit<T, 'workspaces'> {
const { workspaces, ...others } = obj;
if (workspaces) {
export function formatWorkspaceIdParams<
T extends { workspaces?: string[] | null; availableWorkspaces?: string[] | null }
>(obj: T): T | Omit<T, 'workspaces' | 'availableWorkspaces'> {
const { workspaces, availableWorkspaces, ...others } = obj;
if (workspaces || (availableWorkspaces && availableWorkspaces.length)) {
return obj;
}
return others;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const registerScrollForCountRoute = (router: IRouter) => {
namespacesToInclude: schema.maybe(schema.arrayOf(schema.string())),
searchString: schema.maybe(schema.string()),
workspaces: schema.maybe(schema.arrayOf(schema.string())),
availableWorkspaces: schema.maybe(schema.arrayOf(schema.string())),
}),
},
},
Expand All @@ -59,7 +60,9 @@ export const registerScrollForCountRoute = (router: IRouter) => {
const requestHasNamespaces =
Array.isArray(req.body.namespacesToInclude) && req.body.namespacesToInclude.length;

const requestHasWorkspaces = Array.isArray(req.body.workspaces) && req.body.workspaces.length;
const requestHasWorkspaces =
(Array.isArray(req.body.workspaces) && req.body.workspaces.length) ||
(Array.isArray(req.body.availableWorkspaces) && req.body.availableWorkspaces.length);

if (requestHasNamespaces) {
counts.namespaces = {};
Expand Down Expand Up @@ -114,7 +117,7 @@ export const registerScrollForCountRoute = (router: IRouter) => {
}
}

const workspacesToInclude = req.body.workspaces || [];
const workspacesToInclude = req.body.workspaces || req.body.availableWorkspaces || [];
for (const ws of workspacesToInclude) {
if (!counts.workspaces[ws]) {
counts.workspaces[ws] = 0;
Expand Down

0 comments on commit f1ae46e

Please sign in to comment.