Skip to content

Commit

Permalink
Merge branch 'main' into version-enable-rule-api
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho authored Jul 24, 2024
2 parents c303127 + 317154d commit 4bd3fca
Show file tree
Hide file tree
Showing 44 changed files with 295 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/alert-failed-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
name: Alert on failed test
if: |
!github.event.issue.pull_request
&& github.event.comment.user.login == 'kibanamachine'
&& (github.event.comment.user.login == 'kibanamachine' || github.event.comment.user.login == 'elasticmachine')
runs-on: ubuntu-latest
steps:
- name: Checkout kibana-operations
Expand Down
7 changes: 3 additions & 4 deletions packages/kbn-esql-ast/src/walker/walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,7 @@ describe('Walker.params', () => {
});

test('can collect all params from grouping functions', () => {
const query =
'ROW x=1, time=2024-07-10 | stats z = avg(x) by bucket(time, 20, ?earliest,?latest)';
const query = 'ROW x=1, time=2024-07-10 | stats z = avg(x) by bucket(time, 20, ?start,?end)';
const { ast } = getAstAndSyntaxErrors(query);
const params = Walker.params(ast);

Expand All @@ -602,13 +601,13 @@ describe('Walker.params', () => {
type: 'literal',
literalType: 'param',
paramType: 'named',
value: 'earliest',
value: 'start',
},
{
type: 'literal',
literalType: 'param',
paramType: 'named',
value: 'latest',
value: 'end',
},
]);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-esql-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export {
getESQLQueryColumnsRaw,
getESQLResults,
getTimeFieldFromESQLQuery,
getEarliestLatestParams,
hasEarliestLatestParams,
getStartEndParams,
hasStartEndParams,
TextBasedLanguages,
} from './src';

Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-esql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export {
getESQLQueryColumns,
getESQLQueryColumnsRaw,
getESQLResults,
getEarliestLatestParams,
hasEarliestLatestParams,
getStartEndParams,
hasStartEndParams,
} from './utils/run_query';
10 changes: 4 additions & 6 deletions packages/kbn-esql-utils/src/utils/query_parsing_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,25 @@ describe('esql query helpers', () => {
});

it('should return the time field if there is at least one time param', () => {
expect(getTimeFieldFromESQLQuery('from a | eval b = 1 | where time >= ?earliest')).toBe(
'time'
);
expect(getTimeFieldFromESQLQuery('from a | eval b = 1 | where time >= ?start')).toBe('time');
});

it('should return undefined if there is one named param but is not ?earliest or ?latest', () => {
it('should return undefined if there is one named param but is not ?start or ?end', () => {
expect(
getTimeFieldFromESQLQuery('from a | eval b = 1 | where time >= ?late')
).toBeUndefined();
});

it('should return undefined if there is one named param but is used without a time field', () => {
expect(
getTimeFieldFromESQLQuery('from a | eval b = DATE_TRUNC(1 day, ?earliest)')
getTimeFieldFromESQLQuery('from a | eval b = DATE_TRUNC(1 day, ?start)')
).toBeUndefined();
});

it('should return the time field if there is at least one time param in the bucket function', () => {
expect(
getTimeFieldFromESQLQuery(
'from a | stats meow = avg(bytes) by bucket(event.timefield, 200, ?earliest, ?latest)'
'from a | stats meow = avg(bytes) by bucket(event.timefield, 200, ?start, ?end)'
)
).toBe('event.timefield');
});
Expand Down
6 changes: 2 additions & 4 deletions packages/kbn-esql-utils/src/utils/query_parsing_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function removeDropCommandsFromESQLQuery(esql?: string): string {
}

/**
* When the ?earliest and ?latest params are used, we want to retrieve the timefield from the query.
* When the ?start and ?end params are used, we want to retrieve the timefield from the query.
* @param esql:string
* @returns string
*/
Expand All @@ -69,9 +69,7 @@ export const getTimeFieldFromESQLQuery = (esql: string) => {
});

const params = Walker.params(ast);
const timeNamedParam = params.find(
(param) => param.value === 'earliest' || param.value === 'latest'
);
const timeNamedParam = params.find((param) => param.value === 'start' || param.value === 'end');
if (!timeNamedParam || !functions.length) {
return undefined;
}
Expand Down
32 changes: 16 additions & 16 deletions packages/kbn-esql-utils/src/utils/run_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { getEarliestLatestParams } from './run_query';
import { getStartEndParams } from './run_query';

describe('getEarliestLatestParams', () => {
describe('getStartEndParams', () => {
it('should return an empty array if there are no time params', () => {
const time = { from: 'now-15m', to: 'now' };
const query = 'FROM foo';
const params = getEarliestLatestParams(query, time);
const params = getStartEndParams(query, time);
expect(params).toEqual([]);
});

it('should return an array with the earliest param if exists at the query', () => {
it('should return an array with the start param if exists at the query', () => {
const time = { from: 'Jul 5, 2024 @ 08:03:56.849', to: 'Jul 5, 2024 @ 10:03:56.849' };
const query = 'FROM foo | where time > ?earliest';
const params = getEarliestLatestParams(query, time);
const query = 'FROM foo | where time > ?start';
const params = getStartEndParams(query, time);
expect(params).toHaveLength(1);
expect(params[0]).toHaveProperty('earliest');
expect(params[0]).toHaveProperty('start');
});

it('should return an array with the latest param if exists at the query', () => {
it('should return an array with the end param if exists at the query', () => {
const time = { from: 'Jul 5, 2024 @ 08:03:56.849', to: 'Jul 5, 2024 @ 10:03:56.849' };
const query = 'FROM foo | where time < ?latest';
const params = getEarliestLatestParams(query, time);
const query = 'FROM foo | where time < ?end';
const params = getStartEndParams(query, time);
expect(params).toHaveLength(1);
expect(params[0]).toHaveProperty('latest');
expect(params[0]).toHaveProperty('end');
});

it('should return an array with the latest and earliest params if exist at the query', () => {
it('should return an array with the end and start params if exist at the query', () => {
const time = { from: 'Jul 5, 2024 @ 08:03:56.849', to: 'Jul 5, 2024 @ 10:03:56.849' };
const query = 'FROM foo | where time < ?latest amd time > ?earliest';
const params = getEarliestLatestParams(query, time);
const query = 'FROM foo | where time < ?end amd time > ?start';
const params = getStartEndParams(query, time);
expect(params).toHaveLength(2);
expect(params[0]).toHaveProperty('earliest');
expect(params[1]).toHaveProperty('latest');
expect(params[0]).toHaveProperty('start');
expect(params[1]).toHaveProperty('end');
});
});
26 changes: 13 additions & 13 deletions packages/kbn-esql-utils/src/utils/run_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import { esFieldTypeToKibanaFieldType } from '@kbn/field-types';
import type { ESQLColumn, ESQLSearchResponse, ESQLSearchParams } from '@kbn/es-types';
import { lastValueFrom } from 'rxjs';

export const hasEarliestLatestParams = (query: string) => /\?earliest|\?latest/i.test(query);
export const hasStartEndParams = (query: string) => /\?start|\?end/i.test(query);

export const getEarliestLatestParams = (query: string, time?: TimeRange) => {
const earliestNamedParams = /\?earliest/i.test(query);
const latestNamedParams = /\?latest/i.test(query);
if (time && (earliestNamedParams || latestNamedParams)) {
export const getStartEndParams = (query: string, time?: TimeRange) => {
const startNamedParams = /\?start/i.test(query);
const endNamedParams = /\?end/i.test(query);
if (time && (startNamedParams || endNamedParams)) {
const timeParams = {
earliest: earliestNamedParams ? dateMath.parse(time.from)?.toISOString() : undefined,
latest: latestNamedParams ? dateMath.parse(time.to)?.toISOString() : undefined,
start: startNamedParams ? dateMath.parse(time.from)?.toISOString() : undefined,
end: endNamedParams ? dateMath.parse(time.to)?.toISOString() : undefined,
};
const namedParams = [];
if (timeParams?.earliest) {
namedParams.push({ earliest: timeParams.earliest });
if (timeParams?.start) {
namedParams.push({ start: timeParams.start });
}
if (timeParams?.latest) {
namedParams.push({ latest: timeParams.latest });
if (timeParams?.end) {
namedParams.push({ end: timeParams.end });
}
return namedParams;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function getESQLQueryColumnsRaw({
timeRange?: TimeRange;
}): Promise<ESQLColumn[]> {
try {
const namedParams = getEarliestLatestParams(esqlQuery, timeRange);
const namedParams = getStartEndParams(esqlQuery, timeRange);
const response = await lastValueFrom(
search(
{
Expand Down Expand Up @@ -135,7 +135,7 @@ export async function getESQLResults({
response: ESQLSearchResponse;
params: ESQLSearchParams;
}> {
const namedParams = getEarliestLatestParams(esqlQuery, timeRange);
const namedParams = getStartEndParams(esqlQuery, timeRange);
const result = await lastValueFrom(
search(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const allFunctions = statsAggregationFunctionDefinitions
.concat(evalFunctionDefinitions)
.concat(groupingFunctionDefinitions);

export const TIME_SYSTEM_PARAMS = ['?earliest', '?latest'];
export const TIME_SYSTEM_PARAMS = ['?start', '?end'];

export const TRIGGER_SUGGESTION_COMMAND = {
title: 'Trigger Suggestion Dialog',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ test('should allow param inside agg function argument', async () => {
test('allow params in WHERE command expressions', async () => {
const { validate } = await setup();

const res1 = await validate('FROM index | WHERE stringField >= ?earliest');
const res1 = await validate('FROM index | WHERE stringField >= ?start');
const res2 = await validate(`
FROM index
| WHERE stringField >= ?earliest
| WHERE stringField >= ?start
| WHERE stringField <= ?0
| WHERE stringField == ?
`);
const res3 = await validate(`
FROM index
| WHERE stringField >= ?earliest
| WHERE stringField >= ?start
AND stringField <= ?0
AND stringField == ?
`);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/search/expressions/esql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { i18n } from '@kbn/i18n';
import type { IKibanaSearchResponse, IKibanaSearchRequest } from '@kbn/search-types';
import type { Datatable, ExpressionFunctionDefinition } from '@kbn/expressions-plugin/common';
import { RequestAdapter } from '@kbn/inspector-plugin/common';
import { getEarliestLatestParams } from '@kbn/esql-utils';
import { getStartEndParams } from '@kbn/esql-utils';

import { zipObject } from 'lodash';
import { Observable, defer, throwError } from 'rxjs';
Expand Down Expand Up @@ -154,7 +154,7 @@ export const getEsqlFn = ({ getStartDependencies }: EsqlFnArguments) => {
uiSettings as Parameters<typeof getEsQueryConfig>[0]
);

const namedParams = getEarliestLatestParams(query, input.timeRange);
const namedParams = getStartEndParams(query, input.timeRange);

if (namedParams.length) {
params.params = namedParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('getEsqlDataView', () => {
});

it('returns an adhoc dataview if it is adhoc with named params and query index pattern is the same as the dataview index pattern', async () => {
const query = { esql: 'from data-view-ad-hoc-title | where time >= ?earliest' };
const query = { esql: 'from data-view-ad-hoc-title | where time >= ?start' };
const dataView = await getEsqlDataView(query, dataViewAdHocNoAtTimestamp, services);
expect(dataView.timeFieldName).toBe('time');
});
Expand Down
4 changes: 2 additions & 2 deletions test/functional/apps/discover/esql/_esql_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
expect(await testSubjects.exists('unifiedHistogramChart')).to.be(false);
});

it('should render the histogram for indices with no @timestamp field when the ?earliest, ?latest params are in the query', async function () {
it('should render the histogram for indices with no @timestamp field when the ?start, ?end params are in the query', async function () {
await PageObjects.discover.selectTextBaseLang();
await PageObjects.unifiedFieldList.waitUntilSidebarHasLoaded();

const testQuery = `from kibana_sample_data_flights | limit 10 | where timestamp >= ?earliest and timestamp <= ?latest`;
const testQuery = `from kibana_sample_data_flights | limit 10 | where timestamp >= ?start and timestamp <= ?end`;

await monacoEditor.setCodeEditorValue(testQuery);
await testSubjects.click('querySubmitButton');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { updateApiKeyParamsSchema } from './schemas/latest';
export type { UpdateApiKeyParams } from './types/latest';

export { updateApiKeyParamsSchema as updateApiKeyParamsSchemaV1 } from './schemas/v1';
export type { UpdateApiKeyParams as UpdateApiKeyParamsV1 } from './types/v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

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

export const updateApiKeyParamsSchema = schema.object({
id: schema.string(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { TypeOf } from '@kbn/config-schema';
import { updateApiKeyParamsSchemaV1 } from '..';

export type UpdateApiKeyParams = TypeOf<typeof updateApiKeyParamsSchemaV1>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export type { updateApiKeyParamsSchema } from './schemas';
export type { UpdateApiKeyParams } from './types';

export { updateRuleApiKey } from './update_rule_api_key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './update_rule_api_key_schemas';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

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

export const updateApiKeyParamsSchema = schema.object({
id: schema.string(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './update_rule_api_key_types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { TypeOf } from '@kbn/config-schema';
import { updateApiKeyParamsSchema } from '../schemas';

export type UpdateApiKeyParams = TypeOf<typeof updateApiKeyParamsSchema>;
Loading

0 comments on commit 4bd3fca

Please sign in to comment.