Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[data / search session] BWCA all the routes #158981

Merged
merged 20 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/plugins/data/public/search/session/sessions_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { PublicContract } from '@kbn/utility-types';
import { HttpSetup } from '@kbn/core/public';
import type {
SavedObject,
SavedObjectsFindResponse,
SavedObjectsUpdateResponse,
SavedObjectsFindOptions,
} from '@kbn/core/server';
Expand All @@ -24,6 +23,9 @@ export interface SessionsClientDeps {
http: HttpSetup;
}

const version = '1';
const options = { version };

/**
* CRUD Search Session SO
*/
Expand All @@ -35,7 +37,7 @@ export class SessionsClient {
}

public get(sessionId: string): Promise<SearchSessionSavedObject> {
return this.http.get(`/internal/session/${encodeURIComponent(sessionId)}`);
return this.http.get(`/internal/session/${encodeURIComponent(sessionId)}`, options);
}

public create({
Expand All @@ -54,6 +56,7 @@ export class SessionsClient {
sessionId: string;
}): Promise<SearchSessionSavedObject> {
return this.http.post(`/internal/session`, {
version,
body: JSON.stringify({
name,
appId,
Expand All @@ -65,9 +68,10 @@ export class SessionsClient {
});
}

public find(options: Omit<SavedObjectsFindOptions, 'type'>): Promise<SearchSessionsFindResponse> {
public find(opts: Omit<SavedObjectsFindOptions, 'type'>): Promise<SearchSessionsFindResponse> {
return this.http!.post(`/internal/session/_find`, {
body: JSON.stringify(options),
version,
body: JSON.stringify(opts),
});
}

Expand All @@ -76,27 +80,23 @@ export class SessionsClient {
attributes: unknown
): Promise<SavedObjectsUpdateResponse<SearchSessionSavedObjectAttributes>> {
return this.http!.put(`/internal/session/${encodeURIComponent(sessionId)}`, {
version,
body: JSON.stringify(attributes),
});
}

public rename(
sessionId: string,
newName: string
): Promise<SavedObjectsUpdateResponse<Pick<SearchSessionSavedObjectAttributes, 'name'>>> {
return this.update(sessionId, { name: newName });
public async rename(sessionId: string, newName: string): Promise<void> {
await this.update(sessionId, { name: newName });
}

public extend(
sessionId: string,
expires: string
): Promise<SavedObjectsFindResponse<SearchSessionSavedObjectAttributes>> {
return this.http!.post(`/internal/session/${encodeURIComponent(sessionId)}/_extend`, {
public async extend(sessionId: string, expires: string): Promise<void> {
await this.http!.post(`/internal/session/${encodeURIComponent(sessionId)}/_extend`, {
version,
body: JSON.stringify({ expires }),
});
}

public delete(sessionId: string): Promise<void> {
return this.http!.delete(`/internal/session/${encodeURIComponent(sessionId)}`);
return this.http!.delete(`/internal/session/${encodeURIComponent(sessionId)}`, options);
}
}
7 changes: 6 additions & 1 deletion src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ export type {
DataRequestHandlerContext,
AsyncSearchStatusResponse,
} from './search';
export { shimHitsTotal, SearchSessionService, NoSearchIdInSessionError } from './search';
export {
shimHitsTotal,
SearchSessionService,
NoSearchIdInSessionError,
INITIAL_SEARCH_SESSION_REST_VERSION,
} from './search';

// Search namespace
export const search = {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { usageProvider, searchUsageObserver } from './collectors/search';
export * from './aggs';
export * from './session';
export * from './errors/no_search_id_in_session';
export { INITIAL_SEARCH_SESSION_REST_VERSION } from './routes/session';
72 changes: 72 additions & 0 deletions src/plugins/data/server/search/routes/response_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema } from '@kbn/config-schema';

const searchSessionRequestInfoSchema = schema.object({
id: schema.string(),
strategy: schema.string(),
});

const serializeableSchema = schema.mapOf(schema.string(), schema.any());

const searchSessionAttrSchema = schema.object({
sessionId: schema.string(),
name: schema.maybe(schema.string()),
appId: schema.maybe(schema.string()),
created: schema.string(),
expires: schema.string(),
locatorId: schema.maybe(schema.string()),
initialState: schema.maybe(serializeableSchema),
restoreState: schema.maybe(serializeableSchema),
idMapping: schema.mapOf(schema.string(), searchSessionRequestInfoSchema),
realmType: schema.maybe(schema.string()),
realmName: schema.maybe(schema.string()),
username: schema.maybe(schema.string()),
version: schema.string(),
isCanceled: schema.maybe(schema.boolean()),
});

export const searchSessionSchema = schema.object({
id: schema.string(),
attributes: searchSessionAttrSchema,
});

export const searchSessionStatusSchema = schema.object({
status: schema.oneOf([
schema.literal('in_progress'),
schema.literal('error'),
schema.literal('complete'),
schema.literal('cancelled'),
schema.literal('expired'),
]),
errors: schema.maybe(schema.arrayOf(schema.string())),
});

export const searchSessionsFindSchema = schema.object({
total: schema.number(),
saved_objects: schema.arrayOf(searchSessionSchema),
statuses: schema.recordOf(schema.string(), searchSessionStatusSchema),
});

const referencesSchema = schema.arrayOf(
schema.object({ id: schema.string(), type: schema.string(), name: schema.string() })
);

export const searchSessionsUpdateSchema = schema.object({
id: schema.string(),
type: schema.string(),
updated_at: schema.maybe(schema.string()),
version: schema.maybe(schema.string()),
namespaces: schema.maybe(schema.arrayOf(schema.string())),
references: schema.maybe(referencesSchema),
attributes: schema.object({
name: schema.maybe(schema.string()),
expires: schema.maybe(schema.string()),
}),
});
65 changes: 65 additions & 0 deletions src/plugins/data/server/search/routes/response_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { SerializableRecord } from '@kbn/utility-types';

interface SearchSessionAttrRestResponse {
sessionId: string;
name?: string;
appId?: string;
created: string;
expires: string;
locatorId?: string;
initialState?: SerializableRecord;
restoreState?: SerializableRecord;
idMapping: Record<string, SearchSessionRequestInfoRestResponse>;
realmType?: string;
realmName?: string;
username?: string;
version: string;
isCanceled?: boolean;
}

interface SearchSessionRequestInfoRestResponse {
id: string;
strategy: string;
}

export interface SearchSessionRestResponse {
id: string;
attributes: SearchSessionAttrRestResponse;
}

export interface SearchSessionStatusRestResponse {
status: StatusRestRespone;
errors?: string[];
}

type StatusRestRespone = 'in_progress' | 'error' | 'complete' | 'cancelled' | 'expired';

export interface SearchSessionsFindRestResponse {
saved_objects: SearchSessionRestResponse[];
total: number;
/**
* Map containing calculated statuses of search sessions from the find response
*/
statuses: Record<string, SearchSessionStatusRestResponse>;
}

export interface SearchSessionsUpdateRestResponse {
id: string;
type: string;
updated_at?: string;
version?: string;
namespaces?: string[];
references?: Array<{ id: string; type: string; name: string }>;
attributes: {
name?: string;
expires?: string;
};
}
29 changes: 21 additions & 8 deletions src/plugins/data/server/search/routes/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { dataPluginMock } from '../../mocks';

import { registerSessionRoutes } from './session';

enum GetHandlerIndex {
ID,
STATUS,
}

enum PostHandlerIndex {
SAVE,
FIND,
Expand Down Expand Up @@ -43,7 +48,8 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [, saveHandler] = mockRouter.post.mock.calls[PostHandlerIndex.SAVE];
const [[, saveHandler]] =
mockRouter.versioned.post.mock.results[PostHandlerIndex.SAVE].value.addVersion.mock.calls;

await saveHandler(mockContext, mockRequest, mockResponse);

Expand All @@ -58,7 +64,7 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [[, getHandler]] = mockRouter.get.mock.calls;
const [[, getHandler]] = mockRouter.versioned.get.mock.results[0].value.addVersion.mock.calls;

await getHandler(mockContext, mockRequest, mockResponse);

Expand All @@ -73,10 +79,12 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [[], [, statusHandler]] = mockRouter.get.mock.calls;
const [[, statusHandler]] =
mockRouter.versioned.get.mock.results[GetHandlerIndex.STATUS].value.addVersion.mock.calls;

await statusHandler(mockContext, mockRequest, mockResponse);

expect(mockContext.search!.getSessionStatus).toHaveBeenCalled();
expect(mockContext.search!.getSessionStatus).toHaveBeenCalledWith(id);
});

Expand All @@ -92,7 +100,8 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [, findHandler] = mockRouter.post.mock.calls[PostHandlerIndex.FIND];
const [[, findHandler]] =
mockRouter.versioned.post.mock.results[PostHandlerIndex.FIND].value.addVersion.mock.calls;

await findHandler(mockContext, mockRequest, mockResponse);

Expand All @@ -110,7 +119,8 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [, updateHandler] = mockRouter.put.mock.calls[0];
const [[, updateHandler]] =
mockRouter.versioned.put.mock.results[0].value.addVersion.mock.calls;

await updateHandler(mockContext, mockRequest, mockResponse);

Expand All @@ -125,7 +135,8 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [, cancelHandler] = mockRouter.post.mock.calls[PostHandlerIndex.CANCEL];
const [[, cancelHandler]] =
mockRouter.versioned.post.mock.results[PostHandlerIndex.CANCEL].value.addVersion.mock.calls;

await cancelHandler(mockContext, mockRequest, mockResponse);

Expand All @@ -140,7 +151,8 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [, deleteHandler] = mockRouter.delete.mock.calls[0];
const [[, deleteHandler]] =
mockRouter.versioned.delete.mock.results[0].value.addVersion.mock.calls;

await deleteHandler(mockContext, mockRequest, mockResponse);

Expand All @@ -157,7 +169,8 @@ describe('registerSessionRoutes', () => {
const mockResponse = httpServerMock.createResponseFactory();

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const [, extendHandler] = mockRouter.post.mock.calls[PostHandlerIndex.EXTEND];
const [[, extendHandler]] =
mockRouter.versioned.post.mock.results[PostHandlerIndex.EXTEND].value.addVersion.mock.calls;

await extendHandler(mockContext, mockRequest, mockResponse);

Expand Down
Loading