Skip to content

Commit

Permalink
Add maps saved object for sample datasets (#240)
Browse files Browse the repository at this point in the history
* Add Default Saved object for Maps

Signed-off-by: Naveen Tatikonda <[email protected]>

* Consume config and output result

Signed-off-by: Kawika Avilla <[email protected]>

* Refactor the code

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
Signed-off-by: Kawika Avilla <[email protected]>
Co-authored-by: Kawika Avilla <[email protected]>
(cherry picked from commit 816ce79)
  • Loading branch information
naveentatikonda authored and github-actions[bot] committed Feb 14, 2023
1 parent 0e87a44 commit e4ed7a0
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
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;
}

0 comments on commit e4ed7a0

Please sign in to comment.