Skip to content

Commit

Permalink
refactor: simplify sample data saved object id prefix logic
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl committed Sep 12, 2023
1 parent 1cd52d1 commit 3bda7bd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 75 deletions.
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 { appendPrefixTo } 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: appendPrefixTo(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: DEFAULT_INDEX,
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX),
getDataSourceIntegratedDefaultIndex: appendPrefixTo(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 { appendPrefixTo } 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: appendPrefixTo(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: DEFAULT_INDEX,
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX),
getDataSourceIntegratedDefaultIndex: appendPrefixTo(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 { appendPrefixTo } 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: appendPrefixTo(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: DEFAULT_INDEX,
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX),
getDataSourceIntegratedDefaultIndex: appendPrefixTo(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 appendPrefixTo = (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
11 changes: 6 additions & 5 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 @@ -202,17 +206,14 @@ export function createInstallRoute(
let createResults;
let savedObjectsList = sampleDataset.savedObjects;
if (dataSourceId) {
savedObjectsList = sampleDataset.getDataSourceIntegratedSavedObjects(
savedObjectsList = getDataSourceIntegratedSavedObjects(
savedObjectsList,
dataSourceId,
dataSourceTitle
);
}
if (workspaceId) {
savedObjectsList = sampleDataset.getWorkspaceIntegratedSavedObjects(
savedObjectsList,
workspaceId
);
savedObjectsList = getWorkspaceIntegratedSavedObjects(savedObjectsList, workspaceId);
}

try {
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);
}

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

0 comments on commit 3bda7bd

Please sign in to comment.