-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[index patterns] Add pattern validation method to index patterns fetcher #90170
Changes from 7 commits
01b9979
3d99bde
8788337
bc4c790
8ed8cf7
f744acb
9b717c9
df0f7c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [IndexPatternsFetcher](./kibana-plugin-plugins-data-server.indexpatternsfetcher.md) > [validatePatternListActive](./kibana-plugin-plugins-data-server.indexpatternsfetcher.validatepatternlistactive.md) | ||
|
||
## IndexPatternsFetcher.validatePatternListActive() method | ||
|
||
Returns an index pattern list of only those index pattern strings in the given list that return indices | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
validatePatternListActive(patternList: string[]): Promise<string[]>; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| patternList | <code>string[]</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`Promise<string[]>` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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 { IndexPatternsFetcher } from '.'; | ||
import { ElasticsearchClient } from 'kibana/server'; | ||
|
||
describe('Index Pattern Fetcher - server', () => { | ||
let indexPatterns: IndexPatternsFetcher; | ||
let esClient: ElasticsearchClient; | ||
const emptyResponse = { | ||
body: { | ||
indices: [], | ||
}, | ||
}; | ||
const response = { | ||
body: { | ||
indices: ['hello', 'world'], | ||
}, | ||
}; | ||
const patternList = ['a', 'b', 'c']; | ||
beforeEach(() => { | ||
esClient = ({ | ||
transport: { | ||
request: jest.fn().mockReturnValueOnce(emptyResponse).mockReturnValue(response), | ||
}, | ||
} as unknown) as ElasticsearchClient; | ||
indexPatterns = new IndexPatternsFetcher(esClient); | ||
}); | ||
|
||
it('Removes pattern without matching indices', async () => { | ||
const result = await indexPatterns.validatePatternListActive(patternList); | ||
expect(result).toEqual(['b', 'c']); | ||
}); | ||
|
||
it('Returns all patterns when all match indices', async () => { | ||
esClient = ({ | ||
transport: { | ||
request: jest.fn().mockReturnValue(response), | ||
}, | ||
} as unknown) as ElasticsearchClient; | ||
indexPatterns = new IndexPatternsFetcher(esClient); | ||
const result = await indexPatterns.validatePatternListActive(patternList); | ||
expect(result).toEqual(patternList); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,16 +58,24 @@ export class IndexPatternsFetcher { | |
rollupIndex?: string; | ||
}): Promise<FieldDescriptor[]> { | ||
const { pattern, metaFields, fieldCapsOptions, type, rollupIndex } = options; | ||
const patternList = Array.isArray(pattern) ? pattern : pattern.split(','); | ||
let patternListActive: string[] = patternList; | ||
// if only one pattern, don't bother with validation. We let getFieldCapabilities fail if the single pattern is bad regardless | ||
if (patternList.length > 1) { | ||
patternListActive = await this.validatePatternListActive(patternList); | ||
} | ||
const fieldCapsResponse = await getFieldCapabilities( | ||
this.elasticsearchClient, | ||
pattern, | ||
// if none of the patterns are active, pass the original list to get an error | ||
patternListActive.length > 0 ? patternListActive : patternList, | ||
metaFields, | ||
{ | ||
allow_no_indices: fieldCapsOptions | ||
? fieldCapsOptions.allow_no_indices | ||
: this.allowNoIndices, | ||
} | ||
); | ||
|
||
if (type === 'rollup' && rollupIndex) { | ||
const rollupFields: FieldDescriptor[] = []; | ||
const rollupIndexCapabilities = getCapabilitiesForRollupIndices( | ||
|
@@ -118,4 +126,25 @@ export class IndexPatternsFetcher { | |
} | ||
return await getFieldCapabilities(this.elasticsearchClient, indices, metaFields); | ||
} | ||
|
||
/** | ||
* Returns an index pattern list of only those index pattern strings in the given list that return indices | ||
* | ||
* @param patternList string[] | ||
* @return {Promise<string[]>} | ||
*/ | ||
async validatePatternListActive(patternList: string[]) { | ||
const result = await Promise.all( | ||
patternList.map((pattern) => | ||
this.elasticsearchClient.count({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its annoying that the resolve endpoint works this way but this is a good choice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it is annoying. i would not have caught it if it weren't for that ML test! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also tried There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That code needs to be removed, time pattern index patterns were dropped in 7.x. |
||
index: pattern, | ||
}) | ||
) | ||
); | ||
return result.reduce( | ||
(acc: string[], { body: { count } }, patternListIndex) => | ||
count > 0 ? [...acc, patternList[patternListIndex]] : acc, | ||
[] | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if none of the patterns are active, pass the original list to get an error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to add this as a comment to the code. It definitely makes sense but its not obvious why this is being done.