-
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
[Lens] Fix context formula functions #172710
Merged
drewdaemon
merged 20 commits into
elastic:main
from
drewdaemon:170762/fix-formula-context-functions
Dec 19, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7c4466d
create custom expression functions
drewdaemon e35718f
Merge branch 'main' of github.com:elastic/kibana into 170762/fix-form…
drewdaemon c8e86c2
use now from now provider
drewdaemon 7fd2fe2
fix some tests
drewdaemon 4770d5f
fix i18n strings
drewdaemon 34e56b7
fix context variables typing
drewdaemon 6d1b438
Fix test types, now test
drewdaemon 9ec0405
fix test nondeterminism
drewdaemon c067bdf
remove unused translations
drewdaemon c3f9fb1
remove unused function
drewdaemon 2d7a938
Merge branch 'main' of github.com:elastic/kibana into 170762/fix-form…
drewdaemon d1bb537
make import type import
drewdaemon b368086
improve bundle size by splitting out expression functions
drewdaemon 98f3c31
improve bundle size by splitting out expression functions
drewdaemon bf89fce
Merge branch '170762/fix-formula-context-functions' of github.com:dre…
drewdaemon 737b703
Merge branch 'main' of github.com:elastic/kibana into 170762/fix-form…
drewdaemon e815c36
Merge branch 'main' into 170762/fix-formula-context-functions
kibanamachine e32f78f
Merge branch 'main' into 170762/fix-formula-context-functions
kibanamachine eea236f
register functions server-side as well
drewdaemon f051cb6
clarify check
drewdaemon 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
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
1 change: 1 addition & 0 deletions
1
src/plugins/data/common/search/expressions/__snapshots__/kibana.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
91 changes: 91 additions & 0 deletions
91
x-pack/plugins/lens/common/expressions/formula_context/context_fns.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,91 @@ | ||
/* | ||
* 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 { ExecutionContext } from '@kbn/expressions-plugin/common'; | ||
import { Adapters } from '@kbn/inspector-plugin/common'; | ||
import { formulaIntervalFn, formulaNowFn, formulaTimeRangeFn } from './context_fns'; | ||
|
||
describe('interval', () => { | ||
it('should return 0 if no time range available', () => { | ||
// (not sure if this case is actually possible) | ||
const result = formulaIntervalFn.fn(undefined, { targetBars: 100 }, { | ||
getSearchContext: () => ({ | ||
/* no time range */ | ||
}), | ||
} as ExecutionContext<Adapters>); | ||
expect(result).toEqual(0); | ||
}); | ||
|
||
it('should return 0 if no targetBars is passed', () => { | ||
const result = formulaIntervalFn.fn( | ||
undefined, | ||
{ | ||
/* no targetBars */ | ||
}, | ||
{ | ||
getSearchContext: () => ({ | ||
timeRange: { | ||
from: 'now-15m', | ||
to: 'now', | ||
}, | ||
}), | ||
} as ExecutionContext<Adapters> | ||
); | ||
expect(result).toEqual(0); | ||
}); | ||
|
||
it('should return a valid value > 0 if both timeRange and targetBars is passed', () => { | ||
const result = formulaIntervalFn.fn(undefined, { targetBars: 100 }, { | ||
getSearchContext: () => ({ | ||
timeRange: { | ||
from: 'now-15m', | ||
to: 'now', | ||
}, | ||
}), | ||
} as ExecutionContext<Adapters>); | ||
expect(result).toEqual(10000); | ||
}); | ||
}); | ||
|
||
describe('time range', () => { | ||
it('should return 0 if no time range is available', () => { | ||
// (not sure if this case is actually possible) | ||
const result = formulaTimeRangeFn.fn(undefined, {}, { | ||
getSearchContext: () => ({ | ||
/* no time range */ | ||
}), | ||
} as ExecutionContext<Adapters>); | ||
expect(result).toEqual(0); | ||
}); | ||
|
||
it('should return a valid value > 0 if time range is available', () => { | ||
const result = formulaTimeRangeFn.fn(undefined, {}, { | ||
getSearchContext: () => ({ | ||
timeRange: { | ||
from: 'now-15m', | ||
to: 'now', | ||
}, | ||
now: 1000000, // important to provide this to make the result consistent | ||
}), | ||
} as ExecutionContext<Adapters>); | ||
|
||
expect(result).toBe(900000); | ||
}); | ||
}); | ||
|
||
describe('now', () => { | ||
it('should return the now value when passed', () => { | ||
const now = 123456789; | ||
expect( | ||
formulaNowFn.fn(undefined, {}, { | ||
getSearchContext: () => ({ | ||
now, | ||
}), | ||
} as ExecutionContext<Adapters>) | ||
).toEqual(now); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
x-pack/plugins/lens/common/expressions/formula_context/context_fns.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,97 @@ | ||
/* | ||
* 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 { getAbsoluteTimeRange, calcAutoIntervalNear } from '@kbn/data-plugin/common'; | ||
import type { TimeRange } from '@kbn/es-query'; | ||
import type { ExpressionFunctionDefinition } from '@kbn/expressions-plugin/common'; | ||
import moment from 'moment'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
export type ExpressionFunctionFormulaTimeRange = ExpressionFunctionDefinition< | ||
'formula_time_range', | ||
undefined, | ||
object, | ||
number | ||
>; | ||
|
||
const getTimeRangeAsNumber = (timeRange: TimeRange | undefined, now: number | undefined) => { | ||
if (!timeRange) return 0; | ||
const absoluteTimeRange = getAbsoluteTimeRange( | ||
timeRange, | ||
now != null ? { forceNow: new Date(now) } : {} | ||
); | ||
return timeRange ? moment(absoluteTimeRange.to).diff(moment(absoluteTimeRange.from)) : 0; | ||
}; | ||
|
||
export const formulaTimeRangeFn: ExpressionFunctionFormulaTimeRange = { | ||
name: 'formula_time_range', | ||
|
||
help: i18n.translate('xpack.lens.formula.timeRange.help', { | ||
defaultMessage: 'The specified time range, in milliseconds (ms).', | ||
}), | ||
|
||
args: {}, | ||
|
||
fn(_input, _args, { getSearchContext }) { | ||
const { timeRange, now } = getSearchContext(); | ||
return getTimeRangeAsNumber(timeRange, now); | ||
}, | ||
}; | ||
|
||
export type ExpressionFunctionFormulaInterval = ExpressionFunctionDefinition< | ||
'formula_interval', | ||
undefined, | ||
{ | ||
targetBars?: number; | ||
}, | ||
number | ||
>; | ||
|
||
export const formulaIntervalFn: ExpressionFunctionFormulaInterval = { | ||
name: 'formula_interval', | ||
|
||
help: i18n.translate('xpack.lens.formula.interval.help', { | ||
defaultMessage: 'The specified minimum interval for the date histogram, in milliseconds (ms).', | ||
}), | ||
|
||
args: { | ||
targetBars: { | ||
types: ['number'], | ||
help: i18n.translate('xpack.lens.formula.interval.targetBars.help', { | ||
defaultMessage: 'The target number of bars for the date histogram.', | ||
}), | ||
}, | ||
}, | ||
|
||
fn(_input, args, { getSearchContext }) { | ||
const { timeRange, now } = getSearchContext(); | ||
return timeRange && args.targetBars | ||
? calcAutoIntervalNear(args.targetBars, getTimeRangeAsNumber(timeRange, now)).asMilliseconds() | ||
: 0; | ||
}, | ||
}; | ||
|
||
export type ExpressionFunctionFormulaNow = ExpressionFunctionDefinition< | ||
'formula_now', | ||
undefined, | ||
object, | ||
number | ||
>; | ||
|
||
export const formulaNowFn: ExpressionFunctionFormulaNow = { | ||
name: 'formula_now', | ||
|
||
help: i18n.translate('xpack.lens.formula.now.help', { | ||
defaultMessage: 'The current now moment used in Kibana expressed in milliseconds (ms).', | ||
}), | ||
|
||
args: {}, | ||
|
||
fn(_input, _args, { getSearchContext }) { | ||
return getSearchContext().now ?? Date.now(); | ||
}, | ||
}; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/lens/common/expressions/formula_context/index.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,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 './context_fns'; |
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
Oops, something went wrong.
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.
Required a limit bump because of the new expression functions which are imported sync.
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.
Only went up about 1KB, so we don't have to be this generous....