-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Obs AI] get_index_info: add unit tests #256802
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4350e1
[Obs AI] get_index_info: add unit tests
6a4e694
Simplify change
1d4c1fa
Remove test
a5c805f
Simplify test
bc8d0d5
Merge branch 'main' of https://github.com/elastic/kibana into add-uni…
d18a4a5
Add fast path
db09bdb
Merge branch 'main' into add-unit-tests-index-tool
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
.../observability_agent_builder/server/tools/get_index_info/get_field_values_handler.test.ts
This file contains hidden or 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,134 @@ | ||
| /* | ||
| * 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 { | ||
| getFieldCategory, | ||
| resolveInputToConcreteFields, | ||
| type ResolvedValidField, | ||
| } from './get_field_values_handler'; | ||
|
|
||
| describe('getFieldCategory', () => { | ||
| it.each([ | ||
| ['keyword', 'keyword'], | ||
| ['constant_keyword', 'keyword'], | ||
| ['ip', 'keyword'], | ||
| ['long', 'numeric'], | ||
| ['integer', 'numeric'], | ||
| ['short', 'numeric'], | ||
| ['byte', 'numeric'], | ||
| ['double', 'numeric'], | ||
| ['float', 'numeric'], | ||
| ['half_float', 'numeric'], | ||
| ['scaled_float', 'numeric'], | ||
| ['unsigned_long', 'numeric'], | ||
| ['date', 'date'], | ||
| ['date_nanos', 'date'], | ||
| ['boolean', 'boolean'], | ||
| ['text', 'text'], | ||
| ['match_only_text', 'text'], | ||
| ] as const)('maps "%s" to "%s"', (fieldType, expectedCategory) => { | ||
| expect(getFieldCategory(fieldType)).toBe(expectedCategory); | ||
| }); | ||
|
|
||
| it.each(['geo_point', 'flattened', 'object', 'nested', 'unknown_type'])( | ||
| 'returns "unsupported" for "%s"', | ||
| (fieldType) => { | ||
| expect(getFieldCategory(fieldType)).toBe('unsupported'); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| describe('resolveInputToConcreteFields', () => { | ||
| const allFieldNames = [ | ||
| 'service.name', | ||
| 'service.environment', | ||
| 'host.name', | ||
| 'host.ip', | ||
| 'host.os', | ||
| '@timestamp', | ||
| ]; | ||
|
|
||
| const fieldNameToTypeMap: Record<string, string | undefined> = { | ||
| 'service.name': 'keyword', | ||
| 'service.environment': 'keyword', | ||
| 'host.name': 'keyword', | ||
| 'host.ip': 'ip', | ||
| 'host.os': undefined, | ||
| '@timestamp': 'date', | ||
| }; | ||
|
|
||
| describe('literal field name', () => { | ||
| it('returns a resolved field when the field exists', () => { | ||
| const result = resolveInputToConcreteFields( | ||
| 'service.name', | ||
| allFieldNames, | ||
| fieldNameToTypeMap | ||
| ); | ||
|
|
||
| expect(result).toEqual([ | ||
| { field: 'service.name', fieldType: 'keyword', category: 'keyword' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns an error when the field does not exist', () => { | ||
| const result = resolveInputToConcreteFields( | ||
| 'missing.field', | ||
| allFieldNames, | ||
| fieldNameToTypeMap | ||
| ); | ||
|
|
||
| expect(result).toEqual([ | ||
| { input: 'missing.field', error: 'Field "missing.field" not found' }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('wildcard pattern', () => { | ||
| it('matches multiple fields and returns their categories', () => { | ||
| const result = resolveInputToConcreteFields( | ||
| 'host.*', | ||
| allFieldNames, | ||
| fieldNameToTypeMap | ||
| ) as ResolvedValidField[]; | ||
|
|
||
| expect(result.map((r) => r.field)).toEqual(['host.name', 'host.ip']); | ||
| }); | ||
|
|
||
| it('excludes fields with undefined type (object/nested)', () => { | ||
| const result = resolveInputToConcreteFields('host.*', allFieldNames, fieldNameToTypeMap); | ||
|
|
||
| const fieldNames = result | ||
| .filter((r): r is ResolvedValidField => 'field' in r) | ||
| .map((r) => r.field); | ||
|
|
||
| expect(fieldNames).not.toContain('host.os'); | ||
| }); | ||
|
|
||
| it('returns an error when no fields match', () => { | ||
| const result = resolveInputToConcreteFields( | ||
| 'nonexistent.*', | ||
| allFieldNames, | ||
| fieldNameToTypeMap | ||
| ); | ||
|
|
||
| expect(result).toEqual([ | ||
| { input: 'nonexistent.*', error: 'No fields match pattern "nonexistent.*"' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('matches across different field categories', () => { | ||
| const result = resolveInputToConcreteFields('*', allFieldNames, fieldNameToTypeMap); | ||
|
|
||
| const categories = result | ||
| .filter((r): r is ResolvedValidField => 'field' in r) | ||
| .map((r) => r.category); | ||
|
|
||
| expect(categories).toContain('keyword'); | ||
| expect(categories).toContain('date'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
51 changes: 51 additions & 0 deletions
51
.../observability_agent_builder/server/tools/get_index_info/get_index_fields_handler.test.ts
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * 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 { extractFieldPaths } from './get_index_fields_handler'; | ||
|
|
||
| describe('extractFieldPaths', () => { | ||
| it('extracts top-level keys from a flat object', () => { | ||
| expect(extractFieldPaths({ a: 1, b: 'hello' })).toEqual(['a', 'b']); | ||
| }); | ||
|
|
||
| it('extracts dot-notation paths from nested objects', () => { | ||
| expect(extractFieldPaths({ a: { b: 1, c: 2 } })).toEqual(['a.b', 'a.c']); | ||
| }); | ||
|
|
||
| it('handles deeply nested objects', () => { | ||
| expect(extractFieldPaths({ a: { b: { c: { d: 1 } } } })).toEqual(['a.b.c.d']); | ||
| }); | ||
|
|
||
| it('treats arrays as leaf values', () => { | ||
| expect(extractFieldPaths({ tags: ['foo', 'bar'] })).toEqual(['tags']); | ||
| }); | ||
|
|
||
| it('treats null as a leaf value', () => { | ||
| expect(extractFieldPaths({ a: null })).toEqual(['a']); | ||
| }); | ||
|
|
||
| it('returns an empty array for an empty object', () => { | ||
| expect(extractFieldPaths({})).toEqual([]); | ||
| }); | ||
|
|
||
| it('handles a mix of nested and leaf values', () => { | ||
| const result = extractFieldPaths({ | ||
| service: { name: 'web', environment: 'prod' }, | ||
| '@timestamp': '2024-01-01', | ||
| tags: ['a'], | ||
| host: { os: { platform: 'linux' } }, | ||
| }); | ||
|
|
||
| expect(result).toEqual([ | ||
| 'service.name', | ||
| 'service.environment', | ||
| '@timestamp', | ||
| 'tags', | ||
| 'host.os.platform', | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like the refactor to have
minimatch.QQ: This change removes the fast path that existed for non-wildcard field names.
Previously, inputs like
service.namedid a direct map lookup. Now every input (including literals likeservice.name) scans the entireallFieldNamesarray throughminimatch. For indices with hundreds/thousands of fields and multiple literal field inputs, this introduces a performance regression. Can we reintroduce the fast path lookup?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.
Addressed in d18a4a5