Skip to content

Commit

Permalink
Merge branch 'main' into chart_expressions_xy-reference-line
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/plugins/chart_expressions/expression_xy/common/expression_functions/xy_vis.test.ts
#	src/plugins/chart_expressions/expression_xy/common/expression_functions/xy_vis_fn.ts
  • Loading branch information
Kuznietsov committed May 17, 2022
2 parents 37664e8 + 05b7308 commit 7f06310
Show file tree
Hide file tree
Showing 41 changed files with 452 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { validateAccessor } from '@kbn/visualizations-plugin/common/utils';
import { ExtendedDataLayerArgs, ExtendedDataLayerFn } from '../types';
import { EXTENDED_DATA_LAYER, LayerTypes } from '../constants';
import { getAccessors } from '../helpers';
import { getAccessors, normalizeTable } from '../helpers';

export const extendedDataLayerFn: ExtendedDataLayerFn['fn'] = async (data, args, context) => {
const table = args.table ?? data;
Expand All @@ -19,11 +19,13 @@ export const extendedDataLayerFn: ExtendedDataLayerFn['fn'] = async (data, args,
validateAccessor(accessors.splitAccessor, table.columns);
accessors.accessors.forEach((accessor) => validateAccessor(accessor, table.columns));

const normalizedTable = normalizeTable(table, accessors.xAccessor);

return {
type: EXTENDED_DATA_LAYER,
...args,
layerType: LayerTypes.DATA,
...accessors,
table,
table: normalizedTable,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@
*/

import { xyVisFunction } from '.';
import { Datatable } from '@kbn/expressions-plugin/common';
import { createMockExecutionContext } from '@kbn/expressions-plugin/common/mocks';
import { sampleArgs, sampleLayer } from '../__mocks__';
import { XY_VIS } from '../constants';

describe('xyVis', () => {
test('it renders with the specified data and args', async () => {
const { data, args } = sampleArgs();
const newData = {
...data,
type: 'datatable',

columns: data.columns.map((c) =>
c.id !== 'c'
? c
: {
...c,
meta: {
type: 'string',
},
}
),
} as Datatable;

const { layers, ...rest } = args;
const { layerId, layerType, table, type, ...restLayerArgs } = sampleLayer;
const result = await xyVisFunction.fn(
data,
newData,
{ ...rest, ...restLayerArgs, referenceLines: [], annotationLayers: [] },
createMockExecutionContext()
);
Expand All @@ -28,7 +45,7 @@ describe('xyVis', () => {
value: {
args: {
...rest,
layers: [{ layerType, table: data, layerId: 'dataLayers-0', type, ...restLayerArgs }],
layers: [{ layerType, table: newData, layerId: 'dataLayers-0', type, ...restLayerArgs }],
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type { Datatable } from '@kbn/expressions-plugin/common';
import { ExpressionValueVisDimension } from '@kbn/visualizations-plugin/common/expression_functions';
import { LayerTypes, XY_VIS_RENDERER, DATA_LAYER, REFERENCE_LINE } from '../constants';
import { appendLayerIds, getAccessors } from '../helpers';
import { appendLayerIds, getAccessors, normalizeTable } from '../helpers';
import { DataLayerConfigResult, XYLayerConfig, XyVisFn, XYArgs } from '../types';
import { getLayerDimensions } from '../utils';
import {
Expand All @@ -26,19 +26,23 @@ import {
validateValueLabels,
} from './validate';

const createDataLayer = (args: XYArgs, table: Datatable): DataLayerConfigResult => ({
type: DATA_LAYER,
seriesType: args.seriesType,
hide: args.hide,
columnToLabel: args.columnToLabel,
xScaleType: args.xScaleType,
isHistogram: args.isHistogram,
palette: args.palette,
yConfig: args.yConfig,
layerType: LayerTypes.DATA,
table,
...getAccessors<string | ExpressionValueVisDimension, XYArgs>(args, table),
});
const createDataLayer = (args: XYArgs, table: Datatable): DataLayerConfigResult => {
const accessors = getAccessors<string | ExpressionValueVisDimension, XYArgs>(args, table);
const normalizedTable = normalizeTable(table, accessors.xAccessor);
return {
type: DATA_LAYER,
seriesType: args.seriesType,
hide: args.hide,
columnToLabel: args.columnToLabel,
xScaleType: args.xScaleType,
isHistogram: args.isHistogram,
palette: args.palette,
yConfig: args.yConfig,
layerType: LayerTypes.DATA,
table: normalizedTable,
...accessors,
};
};

export const xyVisFn: XyVisFn['fn'] = async (data, args, handlers) => {
validateAccessor(args.splitRowAccessor, data.columns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

export { appendLayerIds, getAccessors } from './layers';
export { normalizeTable } from './table';
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 moment from 'moment';
import { Datatable } from '@kbn/expressions-plugin';
import { normalizeTable } from './table';
import { createSampleDatatableWithRows } from '../__mocks__';

describe('#normalizeTable', () => {
it('should convert row values from date string to number if xAccessor related to `date` column', () => {
const xAccessor = 'c';
const data = createSampleDatatableWithRows([
{ a: 1, b: 2, c: '2022-05-07T06:25:00.000', d: 'Foo' },
{ a: 1, b: 2, c: '2022-05-08T06:25:00.000', d: 'Foo' },
{ a: 1, b: 2, c: '2022-05-09T06:25:00.000', d: 'Foo' },
]);
const newData = {
...data,
type: 'datatable',

columns: data.columns.map((c) =>
c.id !== 'c'
? c
: {
...c,
meta: {
type: 'date',
},
}
),
} as Datatable;
const expectedData = {
...newData,
rows: newData.rows.map((row) => ({
...row,
[xAccessor as string]: moment(row[xAccessor as string]).valueOf(),
})),
};
const normalizedTable = normalizeTable(newData, xAccessor);
expect(normalizedTable).toEqual(expectedData);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 moment from 'moment';
import { getColumnByAccessor } from '@kbn/visualizations-plugin/common/utils';
import type { Datatable } from '@kbn/expressions-plugin/common';
import { ExpressionValueVisDimension } from '@kbn/visualizations-plugin/common/expression_functions';

export function normalizeTable(data: Datatable, xAccessor?: string | ExpressionValueVisDimension) {
const xColumn = xAccessor && getColumnByAccessor(xAccessor, data.columns);
if (xColumn && xColumn?.meta.type === 'date') {
const xColumnId = xColumn.id;
const rows = data.rows.reduce<Datatable['rows']>((normalizedRows, row) => {
return [
...normalizedRows,
{
...row,
[xColumnId]:
typeof row[xColumnId] === 'string' ? moment(row[xColumnId]).valueOf() : row[xColumnId],
},
];
}, []);
return { ...data, rows };
}

return data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const getXDomain = (
.filter((v) => !isUndefined(v))
.sort()
);

const [firstXValue] = xValues;
const lastXValue = xValues[xValues.length - 1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ describe('XYChart component', () => {
});

test('it uses passed in minInterval', () => {
const table1 = createSampleDatatableWithRows([{ a: 1, b: 2, c: 'I', d: 'Foo' }]);
const table1 = createSampleDatatableWithRows([
{ a: 1, b: 2, c: '2019-01-02T05:00:00.000Z', d: 'Foo' },
]);
const table2 = createSampleDatatableWithRows([]);

const component = shallow(
Expand Down Expand Up @@ -438,7 +440,7 @@ describe('XYChart component', () => {

test('should pass enabled histogram mode and min interval to endzones component', () => {
const component = shallow(
<XYChart {...defaultProps} minInterval={24 * 60 * 60 * 1000} args={defaultTimeArgs} />
<XYChart {...defaultProps} minInterval={24 * 60 * 60 * 1000} args={timeArgs} />
);

expect(component.find(XyEndzones).dive().find('Endzones').props()).toEqual(
Expand All @@ -462,7 +464,8 @@ describe('XYChart component', () => {
seriesType: 'bar',
xScaleType: 'time',
isHistogram: true,
},
table: newData,
} as DataLayerConfig,
],
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export const item: GetInfoResponse['item'] = {
csp_rule_template: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
ingest_pipeline: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const item: GetInfoResponse['item'] = {
lens: [],
ml_module: [],
osquery_pack_asset: [],
osquery_saved_query: [],
security_rule: [],
csp_rule_template: [],
tag: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('Fleet - packageToPackagePolicy', () => {
security_rule: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
ingest_pipeline: [],
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export enum KibanaAssetType {
mlModule = 'ml_module',
tag = 'tag',
osqueryPackAsset = 'osquery_pack_asset',
osquerySavedQuery = 'osquery_saved_query',
}

/*
Expand All @@ -89,6 +90,7 @@ export enum KibanaSavedObjectType {
cloudSecurityPostureRuleTemplate = 'csp-rule-template',
tag = 'tag',
osqueryPackAsset = 'osquery-pack-asset',
osquerySavedQuery = 'osquery-saved-query',
}

export enum ElasticsearchAssetType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const AssetsFacetGroup = ({ width }: Args) => {
ml_module: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
component_template: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ export const AssetTitleMap: Record<DisplayedAssetType, string> = {
security_rule: i18n.translate('xpack.fleet.epm.assetTitles.securityRules', {
defaultMessage: 'Security rules',
}),
osquery_pack_asset: i18n.translate('xpack.fleet.epm.assetTitles.osqueryPackAsset', {
osquery_pack_asset: i18n.translate('xpack.fleet.epm.assetTitles.osqueryPackAssets', {
defaultMessage: 'Osquery packs',
}),
osquery_saved_query: i18n.translate('xpack.fleet.epm.assetTitles.osquerySavedQuery', {
defaultMessage: 'Osquery saved queries',
}),
ml_module: i18n.translate('xpack.fleet.epm.assetTitles.mlModules', {
defaultMessage: 'ML modules',
}),
Expand Down Expand Up @@ -102,6 +105,7 @@ export const AssetIcons: Record<KibanaAssetType, IconType> = {
ml_module: 'mlApp',
tag: 'tagApp',
osquery_pack_asset: 'osqueryApp',
osquery_saved_query: 'osqueryApp',
};

export const ServiceIcons: Record<ServiceName, IconType> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const KibanaSavedObjectTypeMapping: Record<KibanaAssetType, KibanaSavedObjectTyp
KibanaSavedObjectType.cloudSecurityPostureRuleTemplate,
[KibanaAssetType.tag]: KibanaSavedObjectType.tag,
[KibanaAssetType.osqueryPackAsset]: KibanaSavedObjectType.osqueryPackAsset,
[KibanaAssetType.osquerySavedQuery]: KibanaSavedObjectType.osquerySavedQuery,
};

const AssetFilters: Record<string, (kibanaAssets: ArchiveAsset[]) => ArchiveAsset[]> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('storedPackagePoliciesToAgentPermissions()', () => {
ml_module: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
component_template: [],
Expand Down Expand Up @@ -184,6 +185,7 @@ describe('storedPackagePoliciesToAgentPermissions()', () => {
ml_module: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
component_template: [],
Expand Down Expand Up @@ -278,6 +280,7 @@ describe('storedPackagePoliciesToAgentPermissions()', () => {
ml_module: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
component_template: [],
Expand Down Expand Up @@ -404,6 +407,7 @@ describe('storedPackagePoliciesToAgentPermissions()', () => {
ml_module: [],
tag: [],
osquery_pack_asset: [],
osquery_saved_query: [],
},
elasticsearch: {
component_template: [],
Expand Down
Loading

0 comments on commit 7f06310

Please sign in to comment.