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

Support multi data source in region map #614

Merged
merged 1 commit into from
Apr 26, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased 2.x](https://github.com/opensearch-project/dashboards-maps/compare/2.13...2.x)
### Features
Support multi data source display in Maps app([#611](https://github.com/opensearch-project/dashboards-maps/pull/611))
Support multi data source in Region map ([#614](https://github.com/opensearch-project/dashboards-maps/pull/614))
### Enhancements
### Bug Fixes
* Fix zoom level type error in custom layer ([#605](https://github.com/opensearch-project/dashboards-maps/pull/605))
Expand Down
12 changes: 11 additions & 1 deletion public/components/vector_upload_options.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ jest.mock('../../../../src/plugins/opensearch_dashboards_react/public', () => ({
}));

describe('vector_upload_options', () => {
const props = {};
const props = {
vis: {
data: {
indexPattern: {
dataSourceRef: {
id: 'mock-data-source-id',
},
},
},
},
};

const getIndexResponseWhenIndexIsNotPresent = {
ok: false,
Expand Down
6 changes: 4 additions & 2 deletions public/components/vector_upload_options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const VectorUploadOptions = (props: RegionMapOptionsProps) => {
return document.getElementsByName(elementName)[0];
};

const dataSourceRefId = props.vis.data.indexPattern?.dataSourceRef?.id || '';

const validateIndexName = (typedIndexName: string, isIndexNameWithSuffix: boolean) => {
const error = [];
const errorIndexNameDiv = fetchElementByName('errorIndexName');
Expand Down Expand Up @@ -175,7 +177,7 @@ const VectorUploadOptions = (props: RegionMapOptionsProps) => {

const checkIfIndexExists = async (indexName: string) => {
try {
const result = await getIndex(indexName, http);
const result = await getIndex(indexName, http, dataSourceRefId);
return result.ok;
} catch (e) {
return false;
Expand Down Expand Up @@ -280,7 +282,7 @@ const VectorUploadOptions = (props: RegionMapOptionsProps) => {
type: GEO_SHAPE_TYPE,
data: [JSON.parse(fileData || null)],
};
const result = await postGeojson(JSON.stringify(bodyData), http);
const result = await postGeojson(JSON.stringify(bodyData), http, dataSourceRefId);
// error handling logic that displays correct toasts for the end users
if (result?.ok) {
parsePostGeojsonResult(result, indexName);
Expand Down
27 changes: 18 additions & 9 deletions public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@ import { CoreStart } from '../../../src/core/public';
import { createGetterSetter } from '../../../src/plugins/opensearch_dashboards_utils/common';
import { TimefilterContract } from '../../../src/plugins/data/public';

export const postGeojson = async (requestBody: any, http: CoreStart['http']) => {
export const postGeojson = async (
requestBody: any,
http: CoreStart['http'],
dataSourceRefId: string
) => {
try {
const response = await http.post('../api/custom_import_map/_upload', {
const query = dataSourceRefId ? { dataSourceId: dataSourceRefId } : undefined;

return await http.post('../api/custom_import_map/_upload', {
body: requestBody,
...(query && { query }),
});
return response;
} catch (e) {
return e;
}
};

export const getIndex = async (indexName: string, http: CoreStart['http']) => {
export const getIndex = async (
indexName: string,
http: CoreStart['http'],
dataSourceRefId: string
) => {
try {
const response = await http.post('../api/custom_import_map/_indices', {
body: JSON.stringify({
index: indexName,
}),
const query = dataSourceRefId ? { dataSourceId: dataSourceRefId } : undefined;
return await http.post('../api/custom_import_map/_indices', {
body: JSON.stringify({ index: indexName }),
...(query && { query }),
});
return response;
} catch (e) {
return e;
}
Expand Down
10 changes: 7 additions & 3 deletions server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import { geospatial, opensearch, statsRoute } from '../server/routes';
import { mapSavedObjectsType } from './saved_objects';
import { capabilitiesProvider } from './saved_objects/capabilities_provider';
import { ConfigSchema } from '../common/config';
import GeospatialPlugin from './clusters/geospatial_plugin';

export class CustomImportMapPlugin
implements Plugin<CustomImportMapPluginSetup, CustomImportMapPluginStart>
{
implements Plugin<CustomImportMapPluginSetup, CustomImportMapPluginStart> {
private readonly logger: Logger;
private readonly globalConfig$;
private readonly config$;
Expand Down Expand Up @@ -58,7 +58,11 @@ export class CustomImportMapPlugin
const opensearchService = new OpensearchService(geospatialClient);

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

if (dataSource) {
dataSource.registerCustomApiSchema(GeospatialPlugin);
}

// Register server side APIs
geospatial(geospatialService, router);
Expand Down
1 change: 1 addition & 0 deletions server/routes/geospatial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (services, router) {
path: '/api/custom_import_map/_upload',
validate: {
body: schema.any(),
query: schema.maybe(schema.object({}, { unknowns: 'allow' })),
},
options: {
body: {
Expand Down
1 change: 1 addition & 0 deletions server/routes/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function (services, router) {
body: schema.object({
index: schema.string(),
}),
query: schema.maybe(schema.object({}, { unknowns: 'allow' })),
},
},
services.getIndex
Expand Down
20 changes: 16 additions & 4 deletions server/services/geospatial_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ export default class GeospatialService {
}

uploadGeojson = async (context, req, res) => {
const dataSourceRefId = req.query.dataSourceId;
let uploadResponse;
try {
const { callAsCurrentUser } = await this.driver.asScoped(req);
const uploadResponse = await callAsCurrentUser('geospatial.geospatialQuery', {
body: req.body,
});
if (dataSourceRefId) {
const remoteDataSourceClient = context.dataSource.opensearch.legacy.getClient(
dataSourceRefId
).callAPI;
uploadResponse = await remoteDataSourceClient('geospatial.geospatialQuery', {
body: req.body,
});
} else {
const { callAsCurrentUser } = await this.driver.asScoped(req);
uploadResponse = await callAsCurrentUser('geospatial.geospatialQuery', {
body: req.body,
});
}

return res.ok({
body: {
ok: true,
Expand Down
23 changes: 16 additions & 7 deletions server/services/opensearch_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ export default class OpensearchService {
}

getIndex = async (context, req, res) => {
const dataSourceRefId = req.query.dataSourceId;
try {
const { index } = req.body;
const { callAsCurrentUser } = this.driver.asScoped(req);
const indices = await callAsCurrentUser('cat.indices', {
index,
format: 'json',
h: 'health,index,status',
});
if (dataSourceRefId) {
const remoteDataSourceClient = context.dataSource.opensearch.legacy.getClient(
dataSourceRefId
).callAPI;
const { index } = req.body;
const indices = await remoteDataSourceClient('cat.indices', {
index,
format: 'json',
h: 'health,index,status',
});
} else {
const { callAsCurrentUser } = this.driver.asScoped(req);
const { index } = req.body;
const indices = await callAsCurrentUser;
}
return res.ok({
body: {
ok: true,
Expand Down
2 changes: 2 additions & 0 deletions server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { HomeServerPluginSetup } from '../../../src/plugins/home/server';
import { DataSourcePluginSetup } from '../../../src/plugins/data_source/server';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CustomImportMapPluginSetup {}
Expand All @@ -11,4 +12,5 @@ export interface CustomImportMapPluginStart {}

export interface AppPluginSetupDependencies {
home?: HomeServerPluginSetup;
dataSource: DataSourcePluginSetup;
}
Loading