Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -7,145 +7,31 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { v4 as uuidv4 } from 'uuid';
import { pick } from 'lodash';

import type { Query } from '@kbn/es-query';
import {
type ControlGroupChainingSystem,
type ControlLabelPosition,
type ControlPanelsState,
type SerializedControlState,
DEFAULT_AUTO_APPLY_SELECTIONS,
DEFAULT_CONTROL_CHAINING,
DEFAULT_CONTROL_GROW,
DEFAULT_CONTROL_LABEL_POSITION,
DEFAULT_CONTROL_WIDTH,
DEFAULT_IGNORE_PARENT_SETTINGS,
} from '@kbn/controls-plugin/common';
import { SerializedSearchSourceFields, parseSearchSourceJSON } from '@kbn/data-plugin/common';

import type { SavedObject, SavedObjectReference } from '@kbn/core-saved-objects-api-server';
import type {
ControlGroupAttributes,
DashboardAttributes,
DashboardGetOut,
DashboardItem,
DashboardOptions,
ItemAttrsToSavedObjectAttrsReturn,
PartialDashboardItem,
SavedObjectToItemReturn,
} from './types';
import type {
DashboardSavedObjectAttributes,
SavedDashboardPanel,
} from '../../dashboard_saved_object';
import type { DashboardSavedObjectAttributes } from '../../dashboard_saved_object';
import type {
ControlGroupAttributes as ControlGroupAttributesV2,
DashboardCrudTypes as DashboardCrudTypesV2,
} from '../../../common/content_management/v2';
import { DEFAULT_DASHBOARD_OPTIONS } from '../../../common/content_management';

function controlGroupInputOut(
controlGroupInput?: DashboardSavedObjectAttributes['controlGroupInput']
): ControlGroupAttributes | undefined {
if (!controlGroupInput) {
return;
}
const {
panelsJSON,
ignoreParentSettingsJSON,
controlStyle = DEFAULT_CONTROL_LABEL_POSITION,
chainingSystem = DEFAULT_CONTROL_CHAINING,
showApplySelections = !DEFAULT_AUTO_APPLY_SELECTIONS,
} = controlGroupInput;
const controls = panelsJSON
? Object.entries(JSON.parse(panelsJSON) as ControlPanelsState<SerializedControlState>).map(
([
id,
{
explicitInput,
type,
grow = DEFAULT_CONTROL_GROW,
width = DEFAULT_CONTROL_WIDTH,
order,
},
]) => ({
controlConfig: explicitInput,
id,
grow,
order,
type,
width,
})
)
: [];

const {
ignoreFilters = DEFAULT_IGNORE_PARENT_SETTINGS.ignoreFilters,
ignoreQuery = DEFAULT_IGNORE_PARENT_SETTINGS.ignoreQuery,
ignoreTimerange = DEFAULT_IGNORE_PARENT_SETTINGS.ignoreTimerange,
ignoreValidations = DEFAULT_IGNORE_PARENT_SETTINGS.ignoreValidations,
} = ignoreParentSettingsJSON ? JSON.parse(ignoreParentSettingsJSON) : {};

// try to maintain a consistent (alphabetical) order of keys
return {
autoApplySelections: !showApplySelections,
chainingSystem: chainingSystem as ControlGroupChainingSystem,
controls,
labelPosition: controlStyle as ControlLabelPosition,
ignoreParentSettings: { ignoreFilters, ignoreQuery, ignoreTimerange, ignoreValidations },
};
}

function kibanaSavedObjectMetaOut(
kibanaSavedObjectMeta: DashboardSavedObjectAttributes['kibanaSavedObjectMeta']
): DashboardAttributes['kibanaSavedObjectMeta'] {
const { searchSourceJSON } = kibanaSavedObjectMeta;
if (!searchSourceJSON) {
return {};
}
// Dashboards do not yet support ES|QL (AggregateQuery) in the search source
return {
searchSource: parseSearchSourceJSON(searchSourceJSON) as Omit<
SerializedSearchSourceFields,
'query'
> & { query?: Query },
};
}

function optionsOut(optionsJSON: string): DashboardAttributes['options'] {
const {
hidePanelTitles = DEFAULT_DASHBOARD_OPTIONS.hidePanelTitles,
useMargins = DEFAULT_DASHBOARD_OPTIONS.useMargins,
syncColors = DEFAULT_DASHBOARD_OPTIONS.syncColors,
syncCursor = DEFAULT_DASHBOARD_OPTIONS.syncCursor,
syncTooltips = DEFAULT_DASHBOARD_OPTIONS.syncTooltips,
} = JSON.parse(optionsJSON) as DashboardOptions;
return {
hidePanelTitles,
useMargins,
syncColors,
syncCursor,
syncTooltips,
};
}

function panelsOut(panelsJSON: string): DashboardAttributes['panels'] {
const panels = JSON.parse(panelsJSON) as SavedDashboardPanel[];
return panels.map(
({ embeddableConfig, gridData, id, panelIndex, panelRefName, title, type, version }) => ({
gridData,
id,
panelConfig: embeddableConfig,
panelIndex,
panelRefName,
title,
type,
version,
})
);
}
import {
transformControlGroupIn,
transformControlGroupOut,
transformOptionsOut,
transformPanelsIn,
transformPanelsOut,
transformSearchSourceIn,
transformSearchSourceOut,
} from './transforms';

export function dashboardAttributesOut(
attributes: DashboardSavedObjectAttributes | Partial<DashboardSavedObjectAttributes>
Expand All @@ -165,13 +51,13 @@ export function dashboardAttributesOut(
} = attributes;
// try to maintain a consistent (alphabetical) order of keys
return {
...(controlGroupInput && { controlGroupInput: controlGroupInputOut(controlGroupInput) }),
...(controlGroupInput && { controlGroupInput: transformControlGroupOut(controlGroupInput) }),
...(description && { description }),
...(kibanaSavedObjectMeta && {
kibanaSavedObjectMeta: kibanaSavedObjectMetaOut(kibanaSavedObjectMeta),
kibanaSavedObjectMeta: transformSearchSourceOut(kibanaSavedObjectMeta),
}),
...(optionsJSON && { options: optionsOut(optionsJSON) }),
...(panelsJSON && { panels: panelsOut(panelsJSON) }),
...(optionsJSON && { options: transformOptionsOut(optionsJSON) }),
...(panelsJSON && { panels: transformPanelsOut(panelsJSON) }),
...(refreshInterval && {
refreshInterval: { pause: refreshInterval.pause, value: refreshInterval.value },
}),
Expand All @@ -183,54 +69,6 @@ export function dashboardAttributesOut(
};
}

function controlGroupInputIn(
controlGroupInput?: ControlGroupAttributes
): DashboardSavedObjectAttributes['controlGroupInput'] | undefined {
if (!controlGroupInput) {
return;
}
const { controls, ignoreParentSettings, labelPosition, chainingSystem, autoApplySelections } =
controlGroupInput;
const updatedControls = Object.fromEntries(
controls.map(({ controlConfig, id = uuidv4(), ...restOfControl }) => {
return [id, { ...restOfControl, explicitInput: controlConfig }];
})
);
return {
chainingSystem,
controlStyle: labelPosition,
ignoreParentSettingsJSON: JSON.stringify(ignoreParentSettings),
panelsJSON: JSON.stringify(updatedControls),
showApplySelections: !autoApplySelections,
};
}

function panelsIn(
panels: DashboardAttributes['panels']
): DashboardSavedObjectAttributes['panelsJSON'] {
const updatedPanels = panels.map(({ panelIndex, gridData, panelConfig, ...restPanel }) => {
const idx = panelIndex ?? uuidv4();
return {
...restPanel,
embeddableConfig: panelConfig,
panelIndex: idx,
gridData: {
...gridData,
i: idx,
},
};
});

return JSON.stringify(updatedPanels);
}

function kibanaSavedObjectMetaIn(
kibanaSavedObjectMeta: DashboardAttributes['kibanaSavedObjectMeta']
) {
const { searchSource } = kibanaSavedObjectMeta;
return { searchSourceJSON: JSON.stringify(searchSource ?? {}) };
}

export const getResultV3ToV2 = (result: DashboardGetOut): DashboardCrudTypesV2['GetOut'] => {
const { meta, item } = result;
const { attributes, ...rest } = item;
Expand All @@ -250,14 +88,14 @@ export const getResultV3ToV2 = (result: DashboardGetOut): DashboardCrudTypesV2['

const v2Attributes = {
...(controlGroupInput && {
controlGroupInput: controlGroupInputIn(controlGroupInput) as ControlGroupAttributesV2,
controlGroupInput: transformControlGroupIn(controlGroupInput) as ControlGroupAttributesV2,
}),
description,
...(kibanaSavedObjectMeta && {
kibanaSavedObjectMeta: kibanaSavedObjectMetaIn(kibanaSavedObjectMeta),
kibanaSavedObjectMeta: transformSearchSourceIn(kibanaSavedObjectMeta),
}),
...(options && { optionsJSON: JSON.stringify(options) }),
panelsJSON: panels ? panelsIn(panels) : '[]',
panelsJSON: panels ? transformPanelsIn(panels) : '[]',
refreshInterval,
...(timeFrom && { timeFrom }),
timeRestore,
Expand All @@ -282,16 +120,16 @@ export const itemAttrsToSavedObjectAttrs = (
const soAttributes = {
...rest,
...(controlGroupInput && {
controlGroupInput: controlGroupInputIn(controlGroupInput),
controlGroupInput: transformControlGroupIn(controlGroupInput),
}),
...(options && {
optionsJSON: JSON.stringify(options),
}),
...(panels && {
panelsJSON: panelsIn(panels),
panelsJSON: transformPanelsIn(panels),
}),
...(kibanaSavedObjectMeta && {
kibanaSavedObjectMeta: kibanaSavedObjectMetaIn(kibanaSavedObjectMeta),
kibanaSavedObjectMeta: transformSearchSourceIn(kibanaSavedObjectMeta),
}),
};
return { attributes: soAttributes, error: null };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { transformControlGroupIn } from './control_group_in_transforms';
import { ControlGroupAttributes } from '../../types';
import { CONTROL_WIDTH_OPTIONS } from '@kbn/controls-plugin/common';

jest.mock('uuid', () => ({
v4: jest.fn(() => 'mock-uuid'),
}));

describe('transformControlGroupIn', () => {
const mockControlGroupInput: ControlGroupAttributes = {
chainingSystem: 'NONE',
labelPosition: 'oneLine',
autoApplySelections: true,
ignoreParentSettings: {
ignoreFilters: true,
ignoreQuery: true,
ignoreTimerange: true,
ignoreValidations: true,
},
controls: [
{
id: 'control1',
type: 'type1',
width: CONTROL_WIDTH_OPTIONS.SMALL,
controlConfig: { bizz: 'buzz' },
order: 0,
grow: false,
},
{
type: 'type2',
grow: true,
width: CONTROL_WIDTH_OPTIONS.SMALL,
controlConfig: { boo: 'bear' },
order: 1,
},
],
};

it('should return undefined if controlGroupInput is undefined', () => {
const result = transformControlGroupIn(undefined);
expect(result).toBeUndefined();
});

it('should transform controlGroupInput correctly', () => {
const result = transformControlGroupIn(mockControlGroupInput);

expect(result).toEqual({
chainingSystem: 'NONE',
controlStyle: 'oneLine',
showApplySelections: false,
ignoreParentSettingsJSON: JSON.stringify({
ignoreFilters: true,
ignoreQuery: true,
ignoreTimerange: true,
ignoreValidations: true,
}),
panelsJSON: JSON.stringify({
control1: {
type: 'type1',
width: 'small',
order: 0,
grow: false,
explicitInput: { bizz: 'buzz' },
},
'mock-uuid': {
type: 'type2',
grow: true,
width: 'small',
order: 1,
explicitInput: { boo: 'bear' },
},
}),
});
});

it('should handle empty controls array', () => {
const controlGroupInput: ControlGroupAttributes = {
...mockControlGroupInput,
controls: [],
};

const result = transformControlGroupIn(controlGroupInput);

expect(result).toEqual({
chainingSystem: 'NONE',
controlStyle: 'oneLine',
showApplySelections: false,
ignoreParentSettingsJSON: JSON.stringify({
ignoreFilters: true,
ignoreQuery: true,
ignoreTimerange: true,
ignoreValidations: true,
}),
panelsJSON: JSON.stringify({}),
});
});
});
Loading