Skip to content
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] Enable new context constants in formula #158452

Merged
merged 23 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ba70d2f
:sparkles: Add new constants features in formula
dej611 May 25, 2023
bb9391a
:bug: Fix for now fn
dej611 May 25, 2023
6cba35c
:sparkles: TSVB to Lens support
dej611 May 25, 2023
ba418db
:bugs: Fix some issues
dej611 May 26, 2023
1986128
Merge remote-tracking branch 'upstream/main' into feature/151827
dej611 May 26, 2023
9f65b46
:bugs: Fix interval + other small fixes
dej611 May 29, 2023
abd79a8
:wrench: Fix import order
dej611 May 29, 2023
f5c278a
:wrench: Minor tweaks
dej611 May 29, 2023
9e5e135
:white_check_mark: Add unit tests
dej611 May 29, 2023
a4cf88e
:white_check_mark: Fix tests
dej611 May 29, 2023
912a95a
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine May 29, 2023
a72bdcb
:white_check_mark: Add more tests
dej611 May 29, 2023
f22399e
Merge branch 'feature/151827' of https://github.com/dej611/kibana int…
dej611 May 29, 2023
700465c
:white_check_mark: Remove constant and add tests for functions
dej611 May 31, 2023
1418001
:bug: Duplicate functions for now
dej611 May 31, 2023
40dd3d0
:bug: Fix importing issues
dej611 May 31, 2023
173876b
:camera_flash: Update snapshot tests
dej611 May 31, 2023
ab163c3
Merge branch 'main' into feature/151827
dej611 May 31, 2023
7ae0789
Merge remote-tracking branch 'upstream/main' into feature/151827
dej611 Jun 1, 2023
1213bd5
Merge branch 'feature/151827' of https://github.com/dej611/kibana int…
dej611 Jun 1, 2023
297d677
Merge remote-tracking branch 'upstream/main' into feature/151827
dej611 Jun 5, 2023
18964ef
:ok_hand: Integrated feedback
dej611 Jun 5, 2023
a561f0e
Merge branch 'main' into feature/151827
stratoula Jun 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,34 @@ describe('convertMathToFormulaColumn', () => {
expect(convertMathToFormulaColumn(...input)).toEqual(expect.objectContaining(expected));
}
});

it.each`
expression | expected
${'params._interval'} | ${'interval()'}
${'params._interval + params._interval'} | ${'interval() + interval()'}
${'params._all'} | ${null}
${'params._all + params.interval'} | ${null}
${'params._timestamp'} | ${null}
${'params._timestamp + params.interval'} | ${null}
${'params._index'} | ${null}
${'params._index + params.interval'} | ${null}
`(`handle special params cases: $expression`, ({ expression, expected }) => {
expect(
convertMathToFormulaColumn({
series,
metrics: [{ ...mathMetric, script: expression }],
dataView,
})
).toEqual(
expected
? expect.objectContaining({
meta: { metricId: 'some-id-1' },
operationType: 'formula',
params: { formula: expected },
})
: expected
);
});
});

describe('convertOtherAggsToFormulaColumn', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ export const convertMathToFormulaColumn = (
return null;
}

// now replace the _interval with the new interval() formula
if (script.includes('params._interval')) {
script = script.replaceAll('params._interval', 'interval()');
}

const scripthasNoStaticNumber = isNaN(Number(script));
if (script.includes('params') || !scripthasNoStaticNumber) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ jest.mock('./dimension_panel/reference_editor', () => ({
ReferenceEditor: () => null,
}));

const nowInstant = new Date();

const fieldsOne = [
{
name: 'timestamp',
Expand Down Expand Up @@ -365,7 +367,14 @@ describe('IndexPattern Data Source', () => {
it('should generate an empty expression when no columns are selected', async () => {
const state = FormBasedDatasource.initialize();
expect(
FormBasedDatasource.toExpression(state, 'first', indexPatterns, dateRange, 'testing-seed')
FormBasedDatasource.toExpression(
state,
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
)
).toEqual(null);
});

Expand Down Expand Up @@ -395,6 +404,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
)
).toEqual({
Expand Down Expand Up @@ -450,6 +460,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
)
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -637,6 +648,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.timeFields).toEqual(['timestamp', 'another_datefield']);
Expand Down Expand Up @@ -678,6 +690,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
expect((ast.chain[1].arguments.aggs[1] as Ast).chain[0].arguments.timeShift).toEqual(['1d']);
Expand Down Expand Up @@ -891,6 +904,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
const count = (ast.chain[1].arguments.aggs[1] as Ast).chain[0];
Expand Down Expand Up @@ -961,6 +975,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.aggs[0]).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -1091,6 +1106,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
const timeScaleCalls = ast.chain.filter((fn) => fn.function === 'lens_time_scale');
Expand Down Expand Up @@ -1162,6 +1178,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
const filteredMetricAgg = (ast.chain[1].arguments.aggs[0] as Ast).chain[0].arguments;
Expand Down Expand Up @@ -1219,6 +1236,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
const formatIndex = ast.chain.findIndex((fn) => fn.function === 'lens_format_column');
Expand Down Expand Up @@ -1273,6 +1291,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.metricsAtAllLevels).toEqual([false]);
Expand Down Expand Up @@ -1318,6 +1337,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.timeFields).toEqual(['timestamp']);
Expand Down Expand Up @@ -1381,6 +1401,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
);

Expand Down Expand Up @@ -1455,6 +1476,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;

Expand Down Expand Up @@ -1525,6 +1547,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;

Expand Down Expand Up @@ -1636,6 +1659,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
// @ts-expect-error we can't isolate just the reference type
Expand Down Expand Up @@ -1675,6 +1699,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;

Expand Down Expand Up @@ -1768,6 +1793,7 @@ describe('IndexPattern Data Source', () => {
'first',
indexPatterns,
dateRange,
nowInstant,
'testing-seed'
) as Ast;
const chainLength = ast.chain.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { flatten, isEqual } from 'lodash';
import type { DataViewsPublicPluginStart, DataView } from '@kbn/data-views-plugin/public';
import type { IndexPatternFieldEditorStart } from '@kbn/data-view-field-editor-plugin/public';
import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
import { DataPublicPluginStart, ES_FIELD_TYPES } from '@kbn/data-plugin/public';
import { DataPublicPluginStart, ES_FIELD_TYPES, UI_SETTINGS } from '@kbn/data-plugin/public';
import { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public';
import { ChartsPluginSetup } from '@kbn/charts-plugin/public';
import { UiActionsStart } from '@kbn/ui-actions-plugin/public';
Expand Down Expand Up @@ -186,7 +186,7 @@ export function getFormBasedDatasource({
return loadInitialState({
persistedState,
references,
defaultIndexPatternId: core.uiSettings.get('defaultIndex'),
defaultIndexPatternId: uiSettings.get('defaultIndex'),
storage,
initialContext,
indexPatternRefs,
Expand Down Expand Up @@ -425,8 +425,16 @@ export function getFormBasedDatasource({
return fields;
},

toExpression: (state, layerId, indexPatterns, dateRange, searchSessionId) =>
toExpression(state, layerId, indexPatterns, uiSettings, dateRange, searchSessionId),
toExpression: (state, layerId, indexPatterns, dateRange, nowInstant, searchSessionId) =>
toExpression(
state,
layerId,
indexPatterns,
uiSettings,
dateRange,
nowInstant,
searchSessionId
),

renderLayerSettings(domElement, props) {
render(
Expand Down Expand Up @@ -854,7 +862,8 @@ export function getFormBasedDatasource({
layer,
columnId,
frameDatasourceAPI.dataViews.indexPatterns[layer.indexPatternId],
frameDatasourceAPI.dateRange
frameDatasourceAPI.dateRange,
uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET)
);
}
);
Expand Down
Loading