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

refactor: simplify sample data saved object id prefix logic #1

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 @@ -33,12 +33,7 @@ import { i18n } from '@osd/i18n';
import { getSavedObjects } from './saved_objects';
import { fieldMappings } from './field_mappings';
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types';
import {
appendDataSourceId,
appendWorkspaceAndDataSourceId,
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
} from '../util';
import { addPrefixTo } from '../util';

const ecommerceName = i18n.translate('home.sampleData.ecommerceSpecTitle', {
defaultMessage: 'Sample eCommerce orders',
Expand All @@ -59,13 +54,11 @@ export const ecommerceSpecProvider = function (): SampleDatasetSchema {
previewImagePath: '/plugins/home/assets/sample_data_resources/ecommerce/dashboard.png',
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/ecommerce/dashboard_dark.png',
overviewDashboard: DASHBOARD_ID,
getWorkspaceAndDataSourceIntegratedDashboard: appendWorkspaceAndDataSourceId(DASHBOARD_ID),
getDashboardWithPrefix: addPrefixTo(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: DEFAULT_INDEX,
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX),
getDataSourceIntegratedDefaultIndex: addPrefixTo(DEFAULT_INDEX),
savedObjects: getSavedObjects(),
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
dataIndices: [
{
id: 'ecommerce',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ import { i18n } from '@osd/i18n';
import { getSavedObjects } from './saved_objects';
import { fieldMappings } from './field_mappings';
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types';
import {
appendDataSourceId,
appendWorkspaceAndDataSourceId,
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
} from '../util';
import { addPrefixTo } from '../util';

const flightsName = i18n.translate('home.sampleData.flightsSpecTitle', {
defaultMessage: 'Sample flight data',
Expand All @@ -59,13 +54,11 @@ export const flightsSpecProvider = function (): SampleDatasetSchema {
previewImagePath: '/plugins/home/assets/sample_data_resources/flights/dashboard.png',
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/flights/dashboard_dark.png',
overviewDashboard: DASHBOARD_ID,
getWorkspaceAndDataSourceIntegratedDashboard: appendWorkspaceAndDataSourceId(DASHBOARD_ID),
getDashboardWithPrefix: addPrefixTo(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: DEFAULT_INDEX,
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX),
getDataSourceIntegratedDefaultIndex: addPrefixTo(DEFAULT_INDEX),
savedObjects: getSavedObjects(),
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
dataIndices: [
{
id: 'flights',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ import { i18n } from '@osd/i18n';
import { getSavedObjects } from './saved_objects';
import { fieldMappings } from './field_mappings';
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types';
import {
appendDataSourceId,
appendWorkspaceAndDataSourceId,
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
} from '../util';
import { addPrefixTo } from '../util';

const logsName = i18n.translate('home.sampleData.logsSpecTitle', {
defaultMessage: 'Sample web logs',
Expand All @@ -59,13 +54,11 @@ export const logsSpecProvider = function (): SampleDatasetSchema {
previewImagePath: '/plugins/home/assets/sample_data_resources/logs/dashboard.png',
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/logs/dashboard_dark.png',
overviewDashboard: DASHBOARD_ID,
getWorkspaceAndDataSourceIntegratedDashboard: appendWorkspaceAndDataSourceId(DASHBOARD_ID),
getDashboardWithPrefix: addPrefixTo(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: DEFAULT_INDEX,
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX),
getDataSourceIntegratedDefaultIndex: addPrefixTo(DEFAULT_INDEX),
savedObjects: getSavedObjects(),
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
dataIndices: [
{
id: 'logs',
Expand Down
25 changes: 10 additions & 15 deletions src/plugins/home/server/services/sample_data/data_sets/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import { SavedObject } from 'opensearch-dashboards/server';
import { cloneDeep } from 'lodash';

const generateIdWithPrefix = (id: string, prefix?: string) => {
return [...(prefix ? [prefix] : []), id].join('_');
const withPrefix = (...args: Array<string | undefined>) => (id: string) => {
const prefix = args.filter(Boolean).join('_');
if (prefix) {
return `${prefix}_${id}`;
}
return id;
};

export const appendDataSourceId = (id: string) => {
return (dataSourceId?: string) => generateIdWithPrefix(id, dataSourceId);
export const addPrefixTo = (id: string) => (...args: Array<string | undefined>) => {
return withPrefix(...args)(id);
};

const overrideSavedObjectId = (savedObject: SavedObject, idGenerator: (id: string) => string) => {
Expand Down Expand Up @@ -62,9 +66,8 @@ export const getDataSourceIntegratedSavedObjects = (
): SavedObject[] => {
savedObjectList = cloneDeep(savedObjectList);
if (dataSourceId) {
const idGeneratorWithDataSource = (id: string) => generateIdWithPrefix(id, dataSourceId);
return savedObjectList.map((savedObject) => {
overrideSavedObjectId(savedObject, idGeneratorWithDataSource);
overrideSavedObjectId(savedObject, withPrefix(dataSourceId));

// update reference
if (savedObject.type === 'index-pattern') {
Expand Down Expand Up @@ -94,22 +97,14 @@ export const getDataSourceIntegratedSavedObjects = (
return savedObjectList;
};

export const appendWorkspaceId = (id: string) => (workspaceId?: string) =>
generateIdWithPrefix(id, workspaceId);

export const appendWorkspaceAndDataSourceId = (id: string) => (workspaceId?: string) => (
dataSourceId?: string
) => appendDataSourceId(appendWorkspaceId(id)(workspaceId))(dataSourceId);

export const getWorkspaceIntegratedSavedObjects = (
savedObjectList: SavedObject[],
workspaceId?: string
) => {
savedObjectList = cloneDeep(savedObjectList);
const generateWithWorkspaceId = (id: string) => appendWorkspaceId(id)(workspaceId);

savedObjectList.forEach((savedObject) => {
overrideSavedObjectId(savedObject, generateWithWorkspaceId);
overrideSavedObjectId(savedObject, withPrefix(workspaceId));
});
return savedObjectList;
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export interface SampleDatasetSchema<T = unknown> {

// saved object id of main dashboard for sample data set
overviewDashboard: string;
getWorkspaceAndDataSourceIntegratedDashboard: (
workspaceId?: string
) => (dataSourceId?: string) => string;
getDashboardWithPrefix: (...args: Array<string | undefined>) => string;
appLinks: AppLinkSchema[];

// saved object id of default index-pattern for sample data set
Expand All @@ -100,15 +98,6 @@ export interface SampleDatasetSchema<T = unknown> {
// OpenSearch Dashboards saved objects (index patter, visualizations, dashboard, ...)
// Should provide a nice demo of OpenSearch Dashboards's functionality with the sample data set
savedObjects: Array<SavedObject<T>>;
getDataSourceIntegratedSavedObjects: (
savedObjects: Array<SavedObject<T>>,
dataSourceId?: string,
dataSourceTitle?: string
) => Array<SavedObject<T>>;
getWorkspaceIntegratedSavedObjects: (
savedObjects: Array<SavedObject<T>>,
workspaceId?: string
) => Array<SavedObject<T>>;
dataIndices: DataIndexSchema[];
status?: string | undefined;
statusMsg?: unknown;
Expand Down
15 changes: 8 additions & 7 deletions src/plugins/home/server/services/sample_data/routes/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import {
} from '../lib/translate_timestamp';
import { loadData } from '../lib/load_data';
import { SampleDataUsageTracker } from '../usage/usage';
import {
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
} from '../data_sets/util';

const insertDataIntoIndex = (
dataIndexConfig: any,
Expand Down Expand Up @@ -201,19 +205,16 @@ export function createInstallRoute(

let createResults;
let savedObjectsList = sampleDataset.savedObjects;
if (workspaceId) {
savedObjectsList = getWorkspaceIntegratedSavedObjects(savedObjectsList, workspaceId);
}
if (dataSourceId) {
savedObjectsList = sampleDataset.getDataSourceIntegratedSavedObjects(
savedObjectsList = getDataSourceIntegratedSavedObjects(
savedObjectsList,
dataSourceId,
dataSourceTitle
);
}
if (workspaceId) {
savedObjectsList = sampleDataset.getWorkspaceIntegratedSavedObjects(
savedObjectsList,
workspaceId
);
}

try {
createResults = await context.core.savedObjects.client.bulkCreate(
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/home/server/services/sample_data/routes/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export const createListRoute = (router: IRouter, sampleDatasets: SampleDatasetSc
description: sampleDataset.description,
previewImagePath: sampleDataset.previewImagePath,
darkPreviewImagePath: sampleDataset.darkPreviewImagePath,
overviewDashboard: sampleDataset.getWorkspaceAndDataSourceIntegratedDashboard(
workspaceId
)(dataSourceId),
overviewDashboard: sampleDataset.getDashboardWithPrefix(dataSourceId, workspaceId),
appLinks: sampleDataset.appLinks,
defaultIndex: sampleDataset.getDataSourceIntegratedDefaultIndex(dataSourceId),
dataIndices: sampleDataset.dataIndices.map(({ id }) => ({ id })),
Expand Down
18 changes: 8 additions & 10 deletions src/plugins/home/server/services/sample_data/routes/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import { IRouter } from 'src/core/server';
import { SampleDatasetSchema } from '../lib/sample_dataset_registry_types';
import { createIndexName } from '../lib/create_index_name';
import { SampleDataUsageTracker } from '../usage/usage';
import {
getDataSourceIntegratedSavedObjects,
getWorkspaceIntegratedSavedObjects,
} from '../data_sets/util';

export function createUninstallRoute(
router: IRouter,
Expand Down Expand Up @@ -81,17 +85,11 @@ export function createUninstallRoute(
}

let savedObjectsList = sampleDataset.savedObjects;
if (dataSourceId) {
savedObjectsList = sampleDataset.getDataSourceIntegratedSavedObjects(
savedObjectsList,
dataSourceId
);
}
if (workspaceId) {
savedObjectsList = sampleDataset.getWorkspaceIntegratedSavedObjects(
savedObjectsList,
workspaceId
);
savedObjectsList = getWorkspaceIntegratedSavedObjects(savedObjectsList, workspaceId);
}
if (dataSourceId) {
savedObjectsList = getDataSourceIntegratedSavedObjects(savedObjectsList, dataSourceId);
wanglam marked this conversation as resolved.
Show resolved Hide resolved
}

const deletePromises = savedObjectsList.map(({ type, id }) =>
Expand Down
Loading