Skip to content

Commit

Permalink
[Obs AI Assistant] Evaluations: auto-create space (#185013)
Browse files Browse the repository at this point in the history
Auto-create space if needed when running the evaluation framework with
`--spaceId`.

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
dgieselaar and kibanamachine authored Jun 10, 2024
1 parent bfa3431 commit 8cd4858
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function runEvaluations() {
node: serviceUrls.esUrl,
});

await kibanaClient.createSpaceIfNeeded();

const connectors = await kibanaClient.getConnectors();

if (!connectors.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class KibanaClient {
});
}

private getUrl(props: { query?: UrlObject['query']; pathname: string }) {
private getUrl(props: { query?: UrlObject['query']; pathname: string; ignoreSpaceId?: boolean }) {
const parsed = parse(this.url);

const baseUrl = parsed.pathname?.replaceAll('/', '') ?? '';
Expand All @@ -108,7 +108,7 @@ export class KibanaClient {
...parsed,
pathname: `/${[
baseUrl,
...(this.spaceId ? ['s', this.spaceId] : []),
...(props.ignoreSpaceId || !this.spaceId ? [] : ['s', this.spaceId]),
props.pathname.startsWith('/') ? props.pathname.substring(1) : props.pathname,
].join('/')}`,
query: props.query,
Expand All @@ -119,7 +119,7 @@ export class KibanaClient {

callKibana<T>(
method: string,
props: { query?: UrlObject['query']; pathname: string },
props: { query?: UrlObject['query']; pathname: string; ignoreSpaceId?: boolean },
data?: any
) {
const url = this.getUrl(props);
Expand Down Expand Up @@ -181,6 +181,58 @@ export class KibanaClient {
this.log.info('Knowledge base installed');
}

async createSpaceIfNeeded() {
if (!this.spaceId) {
return;
}

this.log.debug(`Checking if space ${this.spaceId} exists`);

const spaceExistsResponse = await this.callKibana<{
id?: string;
}>('GET', {
pathname: `/api/spaces/space/${this.spaceId}`,
ignoreSpaceId: true,
}).catch((error) => {
if (isAxiosError(error) && error.response?.status === 404) {
return {
status: 404,
data: {
id: undefined,
},
};
}
throw error;
});

if (spaceExistsResponse.data.id) {
this.log.debug(`Space id ${this.spaceId} found`);
return;
}

this.log.info(`Creating space ${this.spaceId}`);

const spaceCreatedResponse = await this.callKibana<{ id: string }>(
'POST',
{
pathname: '/api/spaces/space',
ignoreSpaceId: true,
},
{
id: this.spaceId,
name: this.spaceId,
}
);

if (spaceCreatedResponse.status === 200) {
this.log.info(`Created space ${this.spaceId}`);
} else {
throw new Error(
`Error creating space: ${spaceCreatedResponse.status} - ${spaceCreatedResponse.data}`
);
}
}

createChatClient({
connectorId,
evaluationConnectorId,
Expand Down

0 comments on commit 8cd4858

Please sign in to comment.