Skip to content

Commit

Permalink
[8.14] [Obs AI Assistant] Evaluations: auto-create space (#185013) (#…
Browse files Browse the repository at this point in the history
…185675)

# Backport

This will backport the following commits from `main` to `8.14`:
- [[Obs AI Assistant] Evaluations: auto-create space
(#185013)](#185013)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Dario
Gieselaar","email":"dario.gieselaar@elastic.co"},"sourceCommit":{"committedDate":"2024-06-10T08:27:39Z","message":"[Obs
AI Assistant] Evaluations: auto-create space (#185013)\n\nAuto-create
space if needed when running the evaluation framework
with\r\n`--spaceId`.\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"8cd4858d2fbde97d3513feb58ee07cca304898cb","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Obs
AI
Assistant","ci:project-deploy-observability","v8.15.0","v8.14.1"],"title":"[Obs
AI Assistant] Evaluations: auto-create
space","number":185013,"url":"#185013
AI Assistant] Evaluations: auto-create space (#185013)\n\nAuto-create
space if needed when running the evaluation framework
with\r\n`--spaceId`.\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"8cd4858d2fbde97d3513feb58ee07cca304898cb"}},"sourceBranch":"main","suggestedTargetBranches":["8.14"],"targetPullRequestStates":[{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"#185013
AI Assistant] Evaluations: auto-create space (#185013)\n\nAuto-create
space if needed when running the evaluation framework
with\r\n`--spaceId`.\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"8cd4858d2fbde97d3513feb58ee07cca304898cb"}},{"branch":"8.14","label":"v8.14.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Dario Gieselaar <dario.gieselaar@elastic.co>
  • Loading branch information
kibanamachine and dgieselaar committed Jun 10, 2024
1 parent 7a22f1f commit a9f97eb
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 a9f97eb

Please sign in to comment.