From 51be26cbc1ab0d320d2253f43110079978f7942c Mon Sep 17 00:00:00 2001 From: Naveen Tatikonda Date: Fri, 3 Feb 2023 12:09:12 -0600 Subject: [PATCH 1/4] Add Default Saved object for Maps Signed-off-by: Naveen Tatikonda --- cypress/integration/add_saved_object.spec.js | 39 ++++++++ opensearch_dashboards.json | 2 +- server/plugin.ts | 24 ++++- .../sample_data/flights_saved_objects.ts | 98 +++++++++++++++++++ server/types.ts | 5 + 5 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 cypress/integration/add_saved_object.spec.js create mode 100644 server/services/sample_data/flights_saved_objects.ts diff --git a/cypress/integration/add_saved_object.spec.js b/cypress/integration/add_saved_object.spec.js new file mode 100644 index 00000000..ee37bca1 --- /dev/null +++ b/cypress/integration/add_saved_object.spec.js @@ -0,0 +1,39 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { BASE_PATH } from '../utils/constants'; + +describe('Add flights dataset saved object', () => { + before(() => { + cy.visit(`${BASE_PATH}/app/maps-dashboards`, { + retryOnStatusCodeFailure: true, + timeout: 60000, + }); + cy.get('div[data-test-subj="indexPatternEmptyState"]', { timeout: 60000 }) + .contains(/Add sample data/) + .click(); + cy.get('div[data-test-subj="sampleDataSetCardflights"]', { timeout: 60000 }) + .contains(/Add data/) + .click(); + cy.wait(60000); + }); + + it('check if maps saved object of flights dataset can be found and open', () => { + cy.visit(`${BASE_PATH}/app/maps-dashboards`); + cy.contains('[Flights] Maps Cancelled Flights Destination Location').click(); + cy.get('[data-test-subj="layerControlPanel"]').should('contain', 'Cancelled flights'); + }); + + after(() => { + cy.visit(`${BASE_PATH}/app/home#/tutorial_directory/sampleData`, { + retryOnStatusCodeFailure: true, + timeout: 60000, + }); + cy.get('div[data-test-subj="sampleDataSetCardflights"]', { timeout: 60000 }) + .contains(/Remove/) + .click(); + cy.wait(60000); + }); +}); diff --git a/opensearch_dashboards.json b/opensearch_dashboards.json index 1723bfde..306f3d37 100644 --- a/opensearch_dashboards.json +++ b/opensearch_dashboards.json @@ -5,5 +5,5 @@ "server": true, "ui": true, "requiredPlugins": ["regionMap", "opensearchDashboardsReact", "navigation", "savedObjects", "data"], - "optionalPlugins": [] + "optionalPlugins": ["home"] } diff --git a/server/plugin.ts b/server/plugin.ts index 0be30228..6ede8225 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -12,7 +12,14 @@ import { Logger, } from '../../../src/core/server'; -import { CustomImportMapPluginSetup, CustomImportMapPluginStart } from './types'; +import { HomeServerPluginSetup } from '../../../src/plugins/home/server'; +import { getFlightsSavedObjects } from './services/sample_data/flights_saved_objects'; + +import { + AppPluginSetupDependencies, + CustomImportMapPluginSetup, + CustomImportMapPluginStart, +} from './types'; import { createGeospatialCluster } from './clusters'; import { GeospatialService, OpensearchService } from './services'; import { geospatial, opensearch } from '../server/routes'; @@ -23,13 +30,16 @@ export class CustomImportMapPlugin implements Plugin { private readonly logger: Logger; private readonly globalConfig$; - constructor(initializerContext: PluginInitializerContext) { this.logger = initializerContext.logger.get(); this.globalConfig$ = initializerContext.config.legacy.globalConfig$; } - public async setup(core: CoreSetup) { + addMapsSavedObjects(home: HomeServerPluginSetup) { + home.sampleData.addSavedObjectsToSampleDataset('flights', getFlightsSavedObjects()); + } + + public async setup(core: CoreSetup, plugins: AppPluginSetupDependencies) { this.logger.debug('customImportMap: Setup'); // @ts-ignore const globalConfig = await this.globalConfig$.pipe(first()).toPromise(); @@ -40,10 +50,18 @@ export class CustomImportMapPlugin const opensearchService = new OpensearchService(geospatialClient); const router = core.http.createRouter(); + const { home } = plugins; + // const mapConfig = configSchema; + // const mapConfig: ConfigSchema = { + // ...this._initializerContext.config.get(), + // }; + // Register server side APIs geospatial(geospatialService, router); opensearch(opensearchService, router); + if (home) this.addMapsSavedObjects(home); + // Register saved object types core.savedObjects.registerType(mapSavedObjectsType); diff --git a/server/services/sample_data/flights_saved_objects.ts b/server/services/sample_data/flights_saved_objects.ts new file mode 100644 index 00000000..ded5641f --- /dev/null +++ b/server/services/sample_data/flights_saved_objects.ts @@ -0,0 +1,98 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +// import { SavedObject } from 'opensearch-dashboards/server'; + +import { i18n } from '@osd/i18n'; +// import { ConfigSchema } from '../../../common/config'; + +// const layerList = (mapConfig: ConfigSchema) => [ + +// const urls = (mapConfig: ConfigSchema) => ({ +// dataUrl: mapConfig.opensearchVectorTileDataUrl, +// styleUrl: mapConfig.opensearchVectorTileStyleUrl, +// }); +const layerList = [ + { + name: 'Default map', + description: '', + type: 'opensearch_vector_tile_map', + id: 'cad56fcc-be02-43ea-a1a6-1d17f437acf7', + zoomRange: [0, 22], + opacity: 100, + visibility: 'visible', + source: { + dataURL: 'https://tiles.maps.opensearch.org/data/v1.json', + }, + style: { + styleURL: 'https://tiles.maps.opensearch.org/styles/basic.json', + }, + }, + { + name: 'Cancelled flights', + description: 'Shows cancelled flights', + type: 'documents', + id: 'f3ae28ce-2494-4e50-ae31-4603cfcbfd7d', + zoomRange: [2, 22], + opacity: 70, + visibility: 'visible', + source: { + indexPatternRefName: 'opensearch_dashboards_sample_data_flights', + geoFieldType: 'geo_point', + geoFieldName: 'DestLocation', + documentRequestNumber: 1000, + tooltipFields: ['Carrier', 'Cancelled'], + showTooltips: true, + indexPatternId: 'd3d7af60-4c81-11e8-b3d7-01146121b73d', + useGeoBoundingBoxFilter: true, + filters: [ + { + meta: { + index: 'd3d7af60-4c81-11e8-b3d7-01146121b73d', + alias: null, + negate: false, + disabled: false, + }, + query: { + match_phrase: { + Cancelled: true, + }, + }, + $state: { + store: 'appState', + }, + }, + ], + }, + style: { + fillColor: '#CA8EAE', + borderColor: '#CA8EAE', + borderThickness: 1, + markerSize: 5, + }, + }, +]; + +export const getFlightsSavedObjects = () => { + return [ + { + id: '122713b0-9e70-11ed-9463-35a6f30dbef6', + type: 'map', + updated_at: '2023-01-27T18:26:09.643Z', + version: 'WzIzLDFd', + migrationVersion: {}, + attributes: { + title: i18n.translate('home.sampleData.flightsSpec.mapsCancelledFlights', { + defaultMessage: '[Flights] Maps Cancelled Flights Destination Location', + }), + description: 'Sample map to show cancelled flights location at destination', + layerList: JSON.stringify(layerList), + mapState: + '{"timeRange":{"from":"now-15d","to":"now"},"query":{"query":"","language":"kuery"},"refreshInterval":{"pause":true,"value":12000}}', + }, + references: [], + }, + ]; +}; diff --git a/server/types.ts b/server/types.ts index f506de95..0cc8bb06 100644 --- a/server/types.ts +++ b/server/types.ts @@ -2,8 +2,13 @@ * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ +import { HomeServerPluginSetup } from '../../../src/plugins/home/server'; // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface CustomImportMapPluginSetup {} // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface CustomImportMapPluginStart {} + +export interface AppPluginSetupDependencies { + home?: HomeServerPluginSetup; +} From 729dd73088bb019e6595568cca3b04bafd8c23ae Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Thu, 9 Feb 2023 20:02:16 +0000 Subject: [PATCH 2/4] Consume config and output result Signed-off-by: Kawika Avilla --- server/plugin.ts | 12 +++++++++--- server/services/sample_data/flights_saved_objects.ts | 6 ++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/plugin.ts b/server/plugin.ts index 6ede8225..b2e48656 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -25,24 +25,30 @@ import { GeospatialService, OpensearchService } from './services'; import { geospatial, opensearch } from '../server/routes'; import { mapSavedObjectsType } from './saved_objects'; import { capabilitiesProvider } from './saved_objects/capabilities_provider'; +import { ConfigSchema } from '../common/config'; export class CustomImportMapPlugin implements Plugin { private readonly logger: Logger; private readonly globalConfig$; + private readonly config$; + constructor(initializerContext: PluginInitializerContext) { this.logger = initializerContext.logger.get(); this.globalConfig$ = initializerContext.config.legacy.globalConfig$; + this.config$ = initializerContext.config.create(); } - addMapsSavedObjects(home: HomeServerPluginSetup) { - home.sampleData.addSavedObjectsToSampleDataset('flights', getFlightsSavedObjects()); + addMapsSavedObjects(home: HomeServerPluginSetup, config: ConfigSchema) { + home.sampleData.addSavedObjectsToSampleDataset('flights', getFlightsSavedObjects(config)); } public async setup(core: CoreSetup, plugins: AppPluginSetupDependencies) { this.logger.debug('customImportMap: Setup'); // @ts-ignore const globalConfig = await this.globalConfig$.pipe(first()).toPromise(); + // @ts-ignore + const config = await this.config$.pipe(first()).toPromise() as ConfigSchema; const geospatialClient = createGeospatialCluster(core, globalConfig); // Initialize services @@ -60,7 +66,7 @@ export class CustomImportMapPlugin geospatial(geospatialService, router); opensearch(opensearchService, router); - if (home) this.addMapsSavedObjects(home); + if (home) this.addMapsSavedObjects(home, config); // Register saved object types core.savedObjects.registerType(mapSavedObjectsType); diff --git a/server/services/sample_data/flights_saved_objects.ts b/server/services/sample_data/flights_saved_objects.ts index ded5641f..f2fe6737 100644 --- a/server/services/sample_data/flights_saved_objects.ts +++ b/server/services/sample_data/flights_saved_objects.ts @@ -6,7 +6,7 @@ // import { SavedObject } from 'opensearch-dashboards/server'; import { i18n } from '@osd/i18n'; -// import { ConfigSchema } from '../../../common/config'; +import { ConfigSchema } from '../../../common/config'; // const layerList = (mapConfig: ConfigSchema) => [ @@ -75,7 +75,9 @@ const layerList = [ }, ]; -export const getFlightsSavedObjects = () => { +export const getFlightsSavedObjects = (config: ConfigSchema) => { + console.log('CONFIGVALUES:') + console.log(JSON.stringify(config, null, 2)); return [ { id: '122713b0-9e70-11ed-9463-35a6f30dbef6', From c9cad2ae7dc8d262dca285d174a3c0a1fffb3b52 Mon Sep 17 00:00:00 2001 From: Naveen Tatikonda Date: Thu, 9 Feb 2023 17:56:59 -0600 Subject: [PATCH 3/4] Refactor the code Signed-off-by: Naveen Tatikonda --- cypress/integration/add_saved_object.spec.js | 10 ++-------- server/plugin.ts | 9 +++------ .../sample_data/flights_saved_objects.ts | 18 ++++-------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/cypress/integration/add_saved_object.spec.js b/cypress/integration/add_saved_object.spec.js index ee37bca1..8ef7af23 100644 --- a/cypress/integration/add_saved_object.spec.js +++ b/cypress/integration/add_saved_object.spec.js @@ -27,13 +27,7 @@ describe('Add flights dataset saved object', () => { }); after(() => { - cy.visit(`${BASE_PATH}/app/home#/tutorial_directory/sampleData`, { - retryOnStatusCodeFailure: true, - timeout: 60000, - }); - cy.get('div[data-test-subj="sampleDataSetCardflights"]', { timeout: 60000 }) - .contains(/Remove/) - .click(); - cy.wait(60000); + cy.visit(`${BASE_PATH}/app/home#/tutorial_directory`); + cy.get('button[data-test-subj="removeSampleDataSetflights"]').should('be.visible').click(); }); }); diff --git a/server/plugin.ts b/server/plugin.ts index b2e48656..cf4840ee 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -39,7 +39,8 @@ export class CustomImportMapPlugin this.config$ = initializerContext.config.create(); } - addMapsSavedObjects(home: HomeServerPluginSetup, config: ConfigSchema) { + // Adds dashboards-maps saved objects to existing sample datasets using home plugin + private addMapsSavedObjects(home: HomeServerPluginSetup, config: ConfigSchema) { home.sampleData.addSavedObjectsToSampleDataset('flights', getFlightsSavedObjects(config)); } @@ -48,7 +49,7 @@ export class CustomImportMapPlugin // @ts-ignore const globalConfig = await this.globalConfig$.pipe(first()).toPromise(); // @ts-ignore - const config = await this.config$.pipe(first()).toPromise() as ConfigSchema; + const config = (await this.config$.pipe(first()).toPromise()) as ConfigSchema; const geospatialClient = createGeospatialCluster(core, globalConfig); // Initialize services @@ -57,10 +58,6 @@ export class CustomImportMapPlugin const router = core.http.createRouter(); const { home } = plugins; - // const mapConfig = configSchema; - // const mapConfig: ConfigSchema = { - // ...this._initializerContext.config.get(), - // }; // Register server side APIs geospatial(geospatialService, router); diff --git a/server/services/sample_data/flights_saved_objects.ts b/server/services/sample_data/flights_saved_objects.ts index f2fe6737..af8eb499 100644 --- a/server/services/sample_data/flights_saved_objects.ts +++ b/server/services/sample_data/flights_saved_objects.ts @@ -3,18 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -// import { SavedObject } from 'opensearch-dashboards/server'; - import { i18n } from '@osd/i18n'; import { ConfigSchema } from '../../../common/config'; -// const layerList = (mapConfig: ConfigSchema) => [ - -// const urls = (mapConfig: ConfigSchema) => ({ -// dataUrl: mapConfig.opensearchVectorTileDataUrl, -// styleUrl: mapConfig.opensearchVectorTileStyleUrl, -// }); -const layerList = [ +const layerList = (config: ConfigSchema) => [ { name: 'Default map', description: '', @@ -24,10 +16,10 @@ const layerList = [ opacity: 100, visibility: 'visible', source: { - dataURL: 'https://tiles.maps.opensearch.org/data/v1.json', + dataURL: config.opensearchVectorTileDataUrl, }, style: { - styleURL: 'https://tiles.maps.opensearch.org/styles/basic.json', + styleURL: config.opensearchVectorTileStyleUrl, }, }, { @@ -76,8 +68,6 @@ const layerList = [ ]; export const getFlightsSavedObjects = (config: ConfigSchema) => { - console.log('CONFIGVALUES:') - console.log(JSON.stringify(config, null, 2)); return [ { id: '122713b0-9e70-11ed-9463-35a6f30dbef6', @@ -90,7 +80,7 @@ export const getFlightsSavedObjects = (config: ConfigSchema) => { defaultMessage: '[Flights] Maps Cancelled Flights Destination Location', }), description: 'Sample map to show cancelled flights location at destination', - layerList: JSON.stringify(layerList), + layerList: JSON.stringify(layerList(config)), mapState: '{"timeRange":{"from":"now-15d","to":"now"},"query":{"query":"","language":"kuery"},"refreshInterval":{"pause":true,"value":12000}}', }, From 55ee6d43b8c3a725516f0a8434adeb59ac8af674 Mon Sep 17 00:00:00 2001 From: Naveen Tatikonda Date: Mon, 13 Feb 2023 22:24:00 -0600 Subject: [PATCH 4/4] Address Review Comments Signed-off-by: Naveen Tatikonda --- cypress/integration/add_saved_object.spec.js | 10 +++++----- server/plugin.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cypress/integration/add_saved_object.spec.js b/cypress/integration/add_saved_object.spec.js index 8ef7af23..12dc6528 100644 --- a/cypress/integration/add_saved_object.spec.js +++ b/cypress/integration/add_saved_object.spec.js @@ -20,14 +20,14 @@ describe('Add flights dataset saved object', () => { cy.wait(60000); }); + after(() => { + cy.visit(`${BASE_PATH}/app/home#/tutorial_directory`); + cy.get('button[data-test-subj="removeSampleDataSetflights"]').should('be.visible').click(); + }); + it('check if maps saved object of flights dataset can be found and open', () => { cy.visit(`${BASE_PATH}/app/maps-dashboards`); cy.contains('[Flights] Maps Cancelled Flights Destination Location').click(); cy.get('[data-test-subj="layerControlPanel"]').should('contain', 'Cancelled flights'); }); - - after(() => { - cy.visit(`${BASE_PATH}/app/home#/tutorial_directory`); - cy.get('button[data-test-subj="removeSampleDataSetflights"]').should('be.visible').click(); - }); }); diff --git a/server/plugin.ts b/server/plugin.ts index cf4840ee..cae4e8ce 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -63,14 +63,14 @@ export class CustomImportMapPlugin geospatial(geospatialService, router); opensearch(opensearchService, router); - if (home) this.addMapsSavedObjects(home, config); - // Register saved object types core.savedObjects.registerType(mapSavedObjectsType); // Register capabilities core.capabilities.registerProvider(capabilitiesProvider); + if (home) this.addMapsSavedObjects(home, config); + return {}; }