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

[Backport 2.x] Add maps saved object for sample datasets #249

Merged
merged 1 commit into from
Feb 16, 2023
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
33 changes: 33 additions & 0 deletions cypress/integration/add_saved_object.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
2 changes: 1 addition & 1 deletion opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"server": true,
"ui": true,
"requiredPlugins": ["regionMap", "opensearchDashboardsReact", "navigation", "savedObjects", "data"],
"optionalPlugins": []
"optionalPlugins": ["home"]
}
25 changes: 23 additions & 2 deletions server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,53 @@ 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<CustomImportMapPluginSetup, CustomImportMapPluginStart> {
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<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));
}

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
const geospatialService = new GeospatialService(geospatialClient);
const opensearchService = new OpensearchService(geospatialClient);

const router = core.http.createRouter();
const { home } = plugins;

// Register server side APIs
geospatial(geospatialService, router);
opensearch(opensearchService, router);
Expand All @@ -50,6 +69,8 @@ export class CustomImportMapPlugin
// Register capabilities
core.capabilities.registerProvider(capabilitiesProvider);

if (home) this.addMapsSavedObjects(home, config);

return {};
}

Expand Down
90 changes: 90 additions & 0 deletions server/services/sample_data/flights_saved_objects.ts
Original file line number Diff line number Diff line change
@@ -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: [],
},
];
};
5 changes: 5 additions & 0 deletions server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}