Skip to content

Commit

Permalink
Make vega parser flags more fine-grained
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Mar 29, 2023
1 parent b374d01 commit feb1d03
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 43 deletions.
16 changes: 2 additions & 14 deletions src/plugins/vis_augmenter/public/test_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,7 @@ export const TEST_RESULT_SPEC_SINGLE_VIS_LAYER = {
data: {
values: TEST_VALUES_SINGLE_VIS_LAYER,
},
config: {
...TEST_BASE_CONFIG,
kibana: {
...TEST_BASE_CONFIG.kibana,
showEvents: true,
},
},
config: TEST_BASE_CONFIG,
vconcat: [
{
layer: [
Expand Down Expand Up @@ -541,13 +535,7 @@ export const TEST_RESULT_SPEC_MULTIPLE_VIS_LAYERS = {
data: {
values: TEST_VALUES_MULTIPLE_VIS_LAYERS,
},
config: {
...TEST_BASE_CONFIG,
kibana: {
...TEST_BASE_CONFIG.kibana,
showEvents: true,
},
},
config: TEST_BASE_CONFIG,
vconcat: [
{
layer: [
Expand Down
82 changes: 72 additions & 10 deletions src/plugins/vis_augmenter/public/vega/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
OpenSearchDashboardsDatatableColumn,
} from '../../../expressions/public';
import {
enableEventsInConfig,
enableVisLayersInSpecConfig,
isVisLayerColumn,
generateVisLayerFilterString,
addMissingRowsToTableBounds,
addPointInTimeEventsLayersToTable,
addPointInTimeEventsLayersToSpec,
} from './helpers';
import { VIS_LAYER_COLUMN_TYPE } from '../';
import { VIS_LAYER_COLUMN_TYPE, VisLayerTypes, PointInTimeEventsVisLayer, VisLayer } from '../';
import {
TEST_DATATABLE_MULTIPLE_VIS_LAYERS,
TEST_DATATABLE_NO_VIS_LAYERS,
Expand All @@ -42,27 +42,89 @@ import {
} from '../test_constants';

describe('helpers', function () {
describe('enableEventsInConfig()', function () {
it('updates config with undefined showEvents field', function () {
describe('enableVisLayersInSpecConfig()', function () {
const pointInTimeEventsVisLayer = {
type: VisLayerTypes.PointInTimeEvents,
originPlugin: 'test-plugin',
pluginResource: {
type: 'test-resource-type',
id: 'test-resource-id',
name: 'test-resource-name',
urlPath: 'test-resource-url-path',
},
events: [
{
timestamp: 1234,
metadata: {
pluginResourceId: 'test-resource-id',
},
},
],
} as PointInTimeEventsVisLayer;
const invalidVisLayer = ({
type: 'something-invalid',
originPlugin: 'test-plugin',
pluginResource: {
type: 'test-resource-type',
id: 'test-resource-id',
name: 'test-resource-name',
urlPath: 'test-resource-url-path',
},
} as unknown) as VisLayer;

it('updates config with just a valid Vislayer', function () {
const baseConfig = {
kibana: {
hideWarnings: true,
},
};
const updatedConfig = enableVisLayersInSpecConfig({ config: baseConfig }, [
pointInTimeEventsVisLayer,
]);
const expectedMap = new Map<VisLayerTypes, boolean>([
[VisLayerTypes.PointInTimeEvents, true],
]);
// @ts-ignore
baseConfig.kibana.visibleVisLayers = expectedMap;
expect(updatedConfig).toStrictEqual(baseConfig);
});
it('updates config with a valid and invalid VisLayer', function () {
const baseConfig = {
kibana: {
hideWarnings: true,
},
};
const updatedConfig = enableEventsInConfig(baseConfig);
const updatedConfig = enableVisLayersInSpecConfig({ config: baseConfig }, [
pointInTimeEventsVisLayer,
invalidVisLayer,
]);
const expectedMap = new Map<VisLayerTypes, boolean>([
[VisLayerTypes.PointInTimeEvents, true],
]);
// @ts-ignore
baseConfig.kibana.showEvents = true;
baseConfig.kibana.visibleVisLayers = expectedMap;
expect(updatedConfig).toStrictEqual(baseConfig);
});
it('updates config with false showEvents field', function () {
it('does not update config if no valid VisLayer', function () {
const baseConfig = {
kibana: {
hideWarnings: true,
showEvents: false,
},
};
const updatedConfig = enableEventsInConfig(baseConfig);
baseConfig.kibana.showEvents = true;
const updatedConfig = enableVisLayersInSpecConfig({ config: baseConfig }, [invalidVisLayer]);
// @ts-ignore
baseConfig.kibana.visibleVisLayers = new Map<VisLayerTypes, boolean>();
expect(updatedConfig).toStrictEqual(baseConfig);
});
it('does not update config if empty VisLayer list', function () {
const baseConfig = {
kibana: {
hideWarnings: true,
},
};
const updatedConfig = enableVisLayersInSpecConfig({ config: baseConfig }, []);
// @ts-ignore
baseConfig.kibana.visibleVisLayers = new Map<VisLayerTypes, boolean>();
expect(updatedConfig).toStrictEqual(baseConfig);
});
});
Expand Down
20 changes: 17 additions & 3 deletions src/plugins/vis_augmenter/public/vega/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,36 @@ import {
import {
PointInTimeEvent,
PointInTimeEventsVisLayer,
isPointInTimeEventsVisLayer,
VIS_LAYER_COLUMN_TYPE,
EVENT_COLOR,
EVENT_MARK_SIZE,
EVENT_MARK_SIZE_ENLARGED,
EVENT_MARK_SHAPE,
EVENT_TIMELINE_HEIGHT,
HOVER_PARAM,
VisLayer,
VisLayers,
VisLayerTypes,
} from '../';

export const enableEventsInConfig = (config: { kibana: {} }) => {
export const enableVisLayersInSpecConfig = (spec: object, visLayers: VisLayers): {} => {
const config = get(spec, 'config', { kibana: {} });
const visibleVisLayers = new Map<VisLayerTypes, boolean>();

// Currently only support PointInTimeEventsVisLayers. Set the flag to true
// if there are any
const pointInTimeEventsVisLayers = visLayers.filter((visLayer: VisLayer) =>
isPointInTimeEventsVisLayer(visLayer)
) as PointInTimeEventsVisLayer[];
if (!isEmpty(pointInTimeEventsVisLayers)) {
visibleVisLayers.set(VisLayerTypes.PointInTimeEvents, true);
}
return {
...config,
kibana: {
...config.kibana,
showEvents: true,
visibleVisLayers,
},
};
};
Expand Down Expand Up @@ -239,7 +254,6 @@ export const addPointInTimeEventsLayersToSpec = (
spec: object
): object => {
const newSpec = cloneDeep(spec) as any;
newSpec.config = enableEventsInConfig(newSpec.config);

const xAxisId = getXAxisId(dimensions, datatable.columns);
const xAxisTitle = dimensions.x.label.replaceAll('"', '');
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions src/plugins/vis_type_vega/public/data_model/vega_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { UrlParser } from './url_parser';
import { SearchAPI } from './search_api';
import { TimeCache } from './time_cache';
import { IServiceSettings } from '../../../maps_legacy/public';
import { VisLayerTypes } from '../../../vis_augmenter/public';
import {
Bool,
Data,
Expand Down Expand Up @@ -92,7 +93,7 @@ export class VegaParser {
getServiceSettings: () => Promise<IServiceSettings>;
filters: Bool;
timeCache: TimeCache;
showEvents: boolean;
visibleVisLayers: Map<VisLayerTypes, boolean>;

constructor(
spec: VegaSpec | string,
Expand All @@ -103,7 +104,7 @@ export class VegaParser {
) {
this.spec = spec as VegaSpec;
this.hideWarnings = false;
this.showEvents = false;
this.visibleVisLayers = new Map<VisLayerTypes, boolean>();

this.error = undefined;
this.warnings = [];
Expand Down Expand Up @@ -160,7 +161,7 @@ The URL is an identifier only. OpenSearch Dashboards and your browser will never

this._config = this._parseConfig();
this.hideWarnings = !!this._config.hideWarnings;
this.showEvents = !!this._config.showEvents;
this.visibleVisLayers = this._config.visibleVisLayers;
this.useMap = this._config.type === 'map';
this.renderer = this._config.renderer === 'svg' ? 'svg' : 'canvas';
this.tooltips = this._parseTooltips();
Expand Down Expand Up @@ -193,11 +194,13 @@ The URL is an identifier only. OpenSearch Dashboards and your browser will never
contains: 'padding',
};

// If showEvents is true, it means we are showing a base vis + event vis.
// If we are showing PointInTimeEventsVisLayers, it means we are showing a base vis + event vis.
// Because this will be using a vconcat spec, we can autosize the width
// via fit-x. Note the regular 'fit' (to autosize width + height) does not work here.
// See limitations: https://vega.github.io/vega-lite/docs/size.html#limitations
const showEventsAutosize = {
const showPointInTimeEvents =
this.visibleVisLayers.get(VisLayerTypes.PointInTimeEvents) === true;
const showPointInTimeEventsAutosize = {
type: 'fit-x',
contains: 'padding',
};
Expand Down Expand Up @@ -236,8 +239,8 @@ The URL is an identifier only. OpenSearch Dashboards and your browser will never
autosize = defaultAutosize;
}

if (this.showEvents) {
autosize = showEventsAutosize;
if (showPointInTimeEvents) {
autosize = showPointInTimeEventsAutosize;
}

if (
Expand All @@ -259,7 +262,7 @@ The URL is an identifier only. OpenSearch Dashboards and your browser will never
);
}

if (useResize && !this.showEvents) {
if (useResize && !showPointInTimeEvents) {
this.spec.width = 'container';
this.spec.height = 'container';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isPointInTimeEventsVisLayer,
addPointInTimeEventsLayersToTable,
addPointInTimeEventsLayersToSpec,
enableVisLayersInSpecConfig,
} from '../../../vis_augmenter/public';
import { formatDatatable, createSpecFromDatatable } from './helpers';
import { VegaVisualizationDependencies } from '../plugin';
Expand Down Expand Up @@ -85,6 +86,7 @@ export const createLineVegaSpecFn = (

if (!isEmpty(pointInTimeEventsVisLayers) && dimensions.x !== null) {
spec = addPointInTimeEventsLayersToSpec(table, dimensions, spec);
spec.config = enableVisLayersInSpecConfig(spec, pointInTimeEventsVisLayers);
}
return JSON.stringify(spec);
},
Expand Down
Loading

0 comments on commit feb1d03

Please sign in to comment.