-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 135874-change-infrastructure-pages-titles-to…
…-use-breadcrumbs
- Loading branch information
Showing
232 changed files
with
3,683 additions
and
1,418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/plugins/data/common/query/text_based_query_state_to_ast_with_validation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import { DataViewsContract } from '@kbn/data-views-plugin/common'; | ||
import { textBasedQueryStateToAstWithValidation } from './text_based_query_state_to_ast_with_validation'; | ||
|
||
describe('textBasedQueryStateToAstWithValidation', () => { | ||
it('returns undefined for a non text based query', async () => { | ||
const dataViewsService = {} as unknown as DataViewsContract; | ||
const actual = await textBasedQueryStateToAstWithValidation({ | ||
filters: [], | ||
query: { language: 'lucene', query: '' }, | ||
time: { | ||
from: 'now', | ||
to: 'now+7d', | ||
}, | ||
dataViewsService, | ||
}); | ||
|
||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('returns an object with the correct structure for an SQL query with existing dataview', async () => { | ||
const dataViewsService = { | ||
getIdsWithTitle: jest.fn(() => { | ||
return [ | ||
{ | ||
title: 'foo', | ||
id: 'bar', | ||
}, | ||
]; | ||
}), | ||
get: jest.fn(() => { | ||
return { | ||
title: 'foo', | ||
id: 'bar', | ||
timeFieldName: 'baz', | ||
}; | ||
}), | ||
} as unknown as DataViewsContract; | ||
const actual = await textBasedQueryStateToAstWithValidation({ | ||
filters: [], | ||
query: { sql: 'SELECT * FROM foo' }, | ||
time: { | ||
from: 'now', | ||
to: 'now+7d', | ||
}, | ||
dataViewsService, | ||
}); | ||
|
||
expect(actual).toHaveProperty( | ||
'chain.1.arguments.timeRange.0.chain.0.arguments', | ||
expect.objectContaining({ | ||
from: ['now'], | ||
to: ['now+7d'], | ||
}) | ||
); | ||
|
||
expect(actual).toHaveProperty( | ||
'chain.2.arguments', | ||
expect.objectContaining({ | ||
query: ['SELECT * FROM foo'], | ||
}) | ||
); | ||
}); | ||
|
||
it('returns an error for text based language with non existing dataview', async () => { | ||
const dataViewsService = { | ||
getIdsWithTitle: jest.fn(() => { | ||
return [ | ||
{ | ||
title: 'foo', | ||
id: 'bar', | ||
}, | ||
]; | ||
}), | ||
get: jest.fn(() => { | ||
return { | ||
title: 'foo', | ||
id: 'bar', | ||
timeFieldName: 'baz', | ||
}; | ||
}), | ||
} as unknown as DataViewsContract; | ||
|
||
await expect( | ||
textBasedQueryStateToAstWithValidation({ | ||
filters: [], | ||
query: { sql: 'SELECT * FROM another_dataview' }, | ||
time: { | ||
from: 'now', | ||
to: 'now+7d', | ||
}, | ||
dataViewsService, | ||
}) | ||
).rejects.toThrow('No data view found for index pattern another_dataview'); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/plugins/data/common/query/text_based_query_state_to_ast_with_validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import { | ||
isOfAggregateQueryType, | ||
getIndexPatternFromSQLQuery, | ||
Query, | ||
AggregateQuery, | ||
} from '@kbn/es-query'; | ||
import type { DataViewsContract } from '@kbn/data-views-plugin/common'; | ||
import type { QueryState } from '..'; | ||
import { textBasedQueryStateToExpressionAst } from './text_based_query_state_to_ast'; | ||
|
||
interface Args extends QueryState { | ||
dataViewsService: DataViewsContract; | ||
inputQuery?: Query; | ||
} | ||
|
||
const getIndexPatternFromAggregateQuery = (query: AggregateQuery) => { | ||
if ('sql' in query) { | ||
return getIndexPatternFromSQLQuery(query.sql); | ||
} | ||
}; | ||
|
||
/** | ||
* Converts QueryState to expression AST | ||
* @param filters array of kibana filters | ||
* @param query kibana query or aggregate query | ||
* @param time kibana time range | ||
*/ | ||
export async function textBasedQueryStateToAstWithValidation({ | ||
filters, | ||
query, | ||
inputQuery, | ||
time, | ||
dataViewsService, | ||
}: Args) { | ||
let ast; | ||
if (query && isOfAggregateQueryType(query)) { | ||
// sql query | ||
const idxPattern = getIndexPatternFromAggregateQuery(query); | ||
const idsTitles = await dataViewsService.getIdsWithTitle(); | ||
const dataViewIdTitle = idsTitles.find(({ title }) => title === idxPattern); | ||
|
||
if (dataViewIdTitle) { | ||
const dataView = await dataViewsService.get(dataViewIdTitle.id); | ||
const timeFieldName = dataView.timeFieldName; | ||
|
||
ast = textBasedQueryStateToExpressionAst({ | ||
filters, | ||
query, | ||
inputQuery, | ||
time, | ||
timeFieldName, | ||
}); | ||
} else { | ||
throw new Error(`No data view found for index pattern ${idxPattern}`); | ||
} | ||
} | ||
return ast; | ||
} |
Oops, something went wrong.