Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into kbn-179458-es-secon…
Browse files Browse the repository at this point in the history
…dary-auth
  • Loading branch information
pgayvallet committed Jun 10, 2024
2 parents ca2d6d3 + 8cd4858 commit fd77242
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,21 @@ const mvZipDefinition: FunctionDefinition = {
}),
alias: undefined,
signatures: [
{
params: [
{
name: 'string1',
type: 'string',
optional: false,
},
{
name: 'string2',
type: 'string',
optional: false,
},
],
returnType: 'string',
},
{
params: [
{
Expand Down Expand Up @@ -2549,6 +2564,37 @@ const powDefinition: FunctionDefinition = {
],
};

const repeatDefinition: FunctionDefinition = {
type: 'eval',
name: 'repeat',
description: i18n.translate('kbn-esql-validation-autocomplete.esql.definitions.repeat', {
defaultMessage:
'Returns a string constructed by concatenating `string` with itself the specified `number` of times.',
}),
alias: undefined,
signatures: [
{
params: [
{
name: 'string',
type: 'string',
optional: false,
},
{
name: 'number',
type: 'number',
optional: false,
},
],
returnType: 'string',
},
],
supportedCommands: ['stats', 'eval', 'where', 'row', 'sort'],
supportedOptions: ['by'],
validate: undefined,
examples: ['ROW a = "Hello!"\n| EVAL triple_a = REPEAT(a, 3);'],
};

const replaceDefinition: FunctionDefinition = {
type: 'eval',
name: 'replace',
Expand Down Expand Up @@ -4795,6 +4841,7 @@ export const evalFunctionDefinitions = [
nowDefinition,
piDefinition,
powDefinition,
repeatDefinition,
replaceDefinition,
rightDefinition,
roundDefinition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5077,6 +5077,33 @@ describe('validation logic', () => {
);
testErrorsAndWarnings('from a_index | eval mv_zip(null, null, null)', []);
testErrorsAndWarnings('row nullVar = null | eval mv_zip(nullVar, nullVar, nullVar)', []);
testErrorsAndWarnings('row var = mv_zip(to_string(true), to_string(true))', []);
testErrorsAndWarnings(
'from a_index | where length(mv_zip(stringField, stringField)) > 0',
[]
);

testErrorsAndWarnings(
'from a_index | where length(mv_zip(booleanField, booleanField)) > 0',
[
'Argument of [mv_zip] must be [string], found value [booleanField] type [boolean]',
'Argument of [mv_zip] must be [string], found value [booleanField] type [boolean]',
]
);

testErrorsAndWarnings('from a_index | eval var = mv_zip(stringField, stringField)', []);

testErrorsAndWarnings(
'from a_index | eval var = mv_zip(to_string(booleanField), to_string(booleanField))',
[]
);

testErrorsAndWarnings('from a_index | eval mv_zip(booleanField, booleanField)', [
'Argument of [mv_zip] must be [string], found value [booleanField] type [boolean]',
'Argument of [mv_zip] must be [string], found value [booleanField] type [boolean]',
]);

testErrorsAndWarnings('from a_index | sort mv_zip(stringField, stringField)', []);
});

describe('now', () => {
Expand Down Expand Up @@ -10334,6 +10361,51 @@ describe('validation logic', () => {
testErrorsAndWarnings('from a_index | eval mv_append(null, null)', []);
testErrorsAndWarnings('row nullVar = null | eval mv_append(nullVar, nullVar)', []);
});

describe('repeat', () => {
testErrorsAndWarnings('row var = repeat("a", 5)', []);
testErrorsAndWarnings('row repeat("a", 5)', []);
testErrorsAndWarnings('row var = repeat(to_string(true), to_integer(true))', []);

testErrorsAndWarnings('row var = repeat(true, true)', [
'Argument of [repeat] must be [string], found value [true] type [boolean]',
'Argument of [repeat] must be [number], found value [true] type [boolean]',
]);

testErrorsAndWarnings(
'from a_index | where length(repeat(stringField, numberField)) > 0',
[]
);

testErrorsAndWarnings(
'from a_index | where length(repeat(booleanField, booleanField)) > 0',
[
'Argument of [repeat] must be [string], found value [booleanField] type [boolean]',
'Argument of [repeat] must be [number], found value [booleanField] type [boolean]',
]
);

testErrorsAndWarnings('from a_index | eval var = repeat(stringField, numberField)', []);
testErrorsAndWarnings('from a_index | eval repeat(stringField, numberField)', []);

testErrorsAndWarnings(
'from a_index | eval var = repeat(to_string(booleanField), to_integer(booleanField))',
[]
);

testErrorsAndWarnings('from a_index | eval repeat(booleanField, booleanField)', [
'Argument of [repeat] must be [string], found value [booleanField] type [boolean]',
'Argument of [repeat] must be [number], found value [booleanField] type [boolean]',
]);

testErrorsAndWarnings('from a_index | eval repeat(stringField, numberField, extraArg)', [
'Error: [repeat] function expects exactly 2 arguments, got 3.',
]);

testErrorsAndWarnings('from a_index | sort repeat(stringField, numberField)', []);
testErrorsAndWarnings('from a_index | eval repeat(null, null)', []);
testErrorsAndWarnings('row nullVar = null | eval repeat(nullVar, nullVar)', []);
});
});
});

Expand Down
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 fd77242

Please sign in to comment.