Skip to content

Commit

Permalink
[Obs AI Assistant] Evaluations: auto-create space (elastic#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 <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 8cd4858)
  • Loading branch information
dgieselaar committed Jun 10, 2024
1 parent 7a22f1f commit ae7175b
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 @@ -77,6 +77,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 @@ -95,7 +95,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 @@ -104,7 +104,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 @@ -115,7 +115,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 @@ -162,6 +162,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 ae7175b

Please sign in to comment.