diff --git a/cypress/integration/add_saved_object.spec.js b/cypress/integration/add_saved_object.spec.js new file mode 100644 index 00000000..12dc6528 --- /dev/null +++ b/cypress/integration/add_saved_object.spec.js @@ -0,0 +1,33 @@ +/* + * 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); + }); + + 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'); + }); +}); diff --git a/opensearch_dashboards.json b/opensearch_dashboards.json index 98566270..0df63693 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..cae4e8ce 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -12,27 +12,44 @@ 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'; 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(); + } + + // Adds dashboards-maps saved objects to existing sample datasets using home plugin + private addMapsSavedObjects(home: HomeServerPluginSetup, config: ConfigSchema) { + home.sampleData.addSavedObjectsToSampleDataset('flights', getFlightsSavedObjects(config)); } - public async setup(core: CoreSetup) { + 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 @@ -40,6 +57,8 @@ export class CustomImportMapPlugin const opensearchService = new OpensearchService(geospatialClient); const router = core.http.createRouter(); + const { home } = plugins; + // Register server side APIs geospatial(geospatialService, router); opensearch(opensearchService, router); @@ -50,6 +69,8 @@ export class CustomImportMapPlugin // Register capabilities core.capabilities.registerProvider(capabilitiesProvider); + if (home) this.addMapsSavedObjects(home, config); + return {}; } 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..af8eb499 --- /dev/null +++ b/server/services/sample_data/flights_saved_objects.ts @@ -0,0 +1,90 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { i18n } from '@osd/i18n'; +import { ConfigSchema } from '../../../common/config'; + +const layerList = (config: ConfigSchema) => [ + { + name: 'Default map', + description: '', + type: 'opensearch_vector_tile_map', + id: 'cad56fcc-be02-43ea-a1a6-1d17f437acf7', + zoomRange: [0, 22], + opacity: 100, + visibility: 'visible', + source: { + dataURL: config.opensearchVectorTileDataUrl, + }, + style: { + styleURL: config.opensearchVectorTileStyleUrl, + }, + }, + { + 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 = (config: ConfigSchema) => { + 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(config)), + 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; +}