Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Apr 3, 2019
1 parent db113de commit d1205fb
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/legacy/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export default () => Joi.object({
}).default(),
map: Joi.object({
includeElasticMapsService: Joi.boolean().default(true),
useCORSForElasticMapsService: Joi.boolean().default(true),
tilemap: Joi.object({
url: Joi.string(),
options: Joi.object({
Expand Down
21 changes: 21 additions & 0 deletions src/legacy/ui/public/vis/map/ems_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


export { EMSClient } from '../../../../core_plugins/tile_map/common/ems_client';
56 changes: 56 additions & 0 deletions x-pack/plugins/maps/common/ems_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/


import { GIS_API_PATH } from './constants';

export async function getEMSResources(emsClient, includeElasticMapsService, licenseUid, useCors) {

if (!includeElasticMapsService) {
return {
fileLayers: [],
tmsServices: []
};
}

emsClient.addQueryParams({ license: licenseUid });
const fileLayerObjs = await emsClient.getFileLayers();
const tmsServicesObjs = await emsClient.getTMSServices();

const fileLayers = fileLayerObjs.map(fileLayer => {
//backfill to static settings
const format = fileLayer.getDefaultFormatType();
const meta = fileLayer.getDefaultFormatMeta();

return {
name: fileLayer.getDisplayName(),
origin: fileLayer.getOrigin(),
id: fileLayer.getId(),
created_at: fileLayer.getCreatedAt(),
attribution: fileLayer.getHTMLAttribution(),
attributions: fileLayer.getAttributions(),
fields: fileLayer.getFieldsInLanguage(),
url: useCors ? fileLayer.getDefaultFormatUrl() : `../${GIS_API_PATH}/data/ems?id=${encodeURIComponent(fileLayer.getId())}`,
format: format, //legacy: format and meta are split up
meta: meta, //legacy, format and meta are split up,
emsLink: fileLayer.getEMSHotLink()
};
});

const tmsServices = tmsServicesObjs.map(tmsService => {
return {
origin: tmsService.getOrigin(),
id: tmsService.getId(),
minZoom: tmsService.getMinZoom(),
maxZoom: tmsService.getMaxZoom(),
attribution: tmsService.getHTMLAttribution(),
attributionMarkdown: tmsService.getMarkdownAttribution(),
url: tmsService.getUrlTemplate()
};
});

return { fileLayers, tmsServices };
}
5 changes: 5 additions & 0 deletions x-pack/plugins/maps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ export function maps(kibana) {
injectDefaultVars(server) {
const serverConfig = server.config();
const mapConfig = serverConfig.get('map');

return {
showMapsInspectorAdapter: serverConfig.get('xpack.maps.showMapsInspectorAdapter'),
isEmsEnabled: mapConfig.includeElasticMapsService,
useCORSForElasticMapsService: mapConfig.useCORSForElasticMapsService,
emsManifestServiceUrl: mapConfig.manifestServiceUrl,
emsLandingPageUrl: mapConfig.manifestServiceUrl,
kbnPkgVersion: serverConfig.get('pkg.version')
};
},
embeddableFactories: [
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/maps/public/kibana_services.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import { uiModules } from 'ui/modules';
import { SearchSourceProvider } from 'ui/courier';
import { timefilter } from 'ui/timefilter/timefilter';
import { getRequestInspectorStats, getResponseInspectorStats } from 'ui/courier/utils/courier_inspector_utils';
import { XPackInfoProvider } from 'plugins/xpack_main/services/xpack_info';

export const timeService = timefilter;
export let indexPatternService;
export let SearchSource;
export let xpackInfo;


export async function fetchSearchSourceAndRecordWithInspector({ searchSource, requestId, requestName, requestDesc, inspectorAdapters }) {
const inspectorRequest = inspectorAdapters.requests.start(
Expand Down Expand Up @@ -39,4 +42,5 @@ uiModules.get('app/maps').run(($injector) => {
indexPatternService = $injector.get('indexPatterns');
const Private = $injector.get('Private');
SearchSource = Private(SearchSourceProvider);
xpackInfo = Private(XPackInfoProvider);
});
23 changes: 22 additions & 1 deletion x-pack/plugins/maps/public/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

import { GIS_API_PATH } from '../common/constants';
import _ from 'lodash';
import { getEMSResources } from '../common/ems_util';
import chrome from 'ui/chrome';
import { i18n } from '@kbn/i18n';
import { EMSClient } from 'ui/vis/map/ems_client';

const GIS_API_RELATIVE = `../${GIS_API_PATH}`;

let meta = null;
let loadingMetaPromise = null;
let isLoaded = false;


export async function getDataSources() {
if (meta) {
return meta;
Expand All @@ -26,10 +32,25 @@ export async function getDataSources() {
try {
const response = await fetch(`${GIS_API_RELATIVE}/meta`);
const metaJson = await response.json();
const useCors = chrome.getInjected('useCORSForElasticMapsService', true);
const isEmsEnabled = chrome.getInjected('isEmsEnabled', true);
if (useCors) {
const emsClient = new EMSClient({
language: i18n.getLocale(),
kbnVersion: chrome.getInjected('kbnPkgVersion'),
manifestServiceUrl: chrome.getInjected('emsManifestServiceUrl'),
landingPageUrl: chrome.getInjected('emsLandingPageUrl')
});
const emsResponse = await getEMSResources(emsClient, isEmsEnabled, 'todo_use_licenseuid', true);
metaJson.data_sources.ems = {
file: emsResponse.fileLayers,
tms: emsResponse.tmsServices
};
}
isLoaded = true;
meta = metaJson.data_sources;
resolve(meta);
} catch(e) {
} catch (e) {
reject(e);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { AbstractVectorSource } from '../vector_source';
import React from 'react';
import { GIS_API_PATH, EMS_FILE } from '../../../../../common/constants';
import { EMS_FILE } from '../../../../../common/constants';
import { getEmsVectorFilesMeta } from '../../../../meta';
import { EMSFileCreateSourceEditor } from './create_source_editor';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -58,7 +58,7 @@ export class EMSFileSource extends AbstractVectorSource {
const featureCollection = await AbstractVectorSource.getGeoJson({
format: emsVectorFileMeta.format,
featureCollectionPath: 'data',
fetchUrl: `../${GIS_API_PATH}/data/ems?id=${encodeURIComponent(this._descriptor.id)}`
fetchUrl: emsVectorFileMeta.url
});
return {
data: featureCollection,
Expand Down
123 changes: 67 additions & 56 deletions x-pack/plugins/maps/server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GIS_API_PATH } from '../common/constants';
import fetch from 'node-fetch';
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { getEMSResources } from '../common/ems_util';

const ROOT = `/${GIS_API_PATH}`;

Expand All @@ -34,7 +35,7 @@ export function initRoutes(server, licenseUid) {
return null;
}

const ems = await getEMSResources(licenseUid);
const ems = await getEMSResources(emsClient, mapConfig.includeElasticMapsService, licenseUid, false);

const layer = ems.fileLayers.find(layer => layer.id === request.query.id);
if (!layer) {
Expand All @@ -52,15 +53,22 @@ export function initRoutes(server, licenseUid) {
path: `${ROOT}/meta`,
handler: async () => {

// console.log('mapConfig', mapCon)
let ems;
try {
ems = await getEMSResources(licenseUid);
} catch (e) {
server.log('warning', `Cannot connect to EMS, error: ${e}`);
ems = {
fileLayers: [],
tmsServices: []
};
const emptyResponse = {
fileLayers: [],
tmsServices: []
};

if (mapConfig.useCORSForElasticMapsService) {
ems = emptyResponse;
} else {
try {
ems = await getEMSResources(emsClient, mapConfig.includeElasticMapsService, licenseUid, false);
} catch (e) {
server.log('warning', `Cannot connect to EMS, error: ${e}`);
ems = emptyResponse;
}
}

return ({
Expand All @@ -78,51 +86,54 @@ export function initRoutes(server, licenseUid) {
}
});

async function getEMSResources(licenseUid) {

if (!mapConfig.includeElasticMapsService) {
return {
fileLayers: [],
tmsServices: []
};
}

emsClient.addQueryParams({ license: licenseUid });
const fileLayerObjs = await emsClient.getFileLayers();
const tmsServicesObjs = await emsClient.getTMSServices();

const fileLayers = fileLayerObjs.map(fileLayer => {
//backfill to static settings
const format = fileLayer.getDefaultFormatType();
const meta = fileLayer.getDefaultFormatMeta();

return {
name: fileLayer.getDisplayName(),
origin: fileLayer.getOrigin(),
id: fileLayer.getId(),
created_at: fileLayer.getCreatedAt(),
attribution: fileLayer.getHTMLAttribution(),
attributions: fileLayer.getAttributions(),
fields: fileLayer.getFieldsInLanguage(),
url: fileLayer.getDefaultFormatUrl(),
format: format, //legacy: format and meta are split up
meta: meta, //legacy, format and meta are split up,
emsLink: fileLayer.getEMSHotLink()
};
});

const tmsServices = tmsServicesObjs.map(tmsService => {
return {
origin: tmsService.getOrigin(),
id: tmsService.getId(),
minZoom: tmsService.getMinZoom(),
maxZoom: tmsService.getMaxZoom(),
attribution: tmsService.getHTMLAttribution(),
attributionMarkdown: tmsService.getMarkdownAttribution(),
url: tmsService.getUrlTemplate()
};
});

return { fileLayers, tmsServices };
}
// async function getEMSResources(licenseUid) {
//
// // if (!mapConfig.includeElasticMapsService) {
// // return {
// // fileLayers: [],
// // tmsServices: []
// // };
// // }
//
//
//
//
// emsClient.addQueryParams({ license: licenseUid });
// const fileLayerObjs = await emsClient.getFileLayers();
// const tmsServicesObjs = await emsClient.getTMSServices();
//
// const fileLayers = fileLayerObjs.map(fileLayer => {
// //backfill to static settings
// const format = fileLayer.getDefaultFormatType();
// const meta = fileLayer.getDefaultFormatMeta();
//
// return {
// name: fileLayer.getDisplayName(),
// origin: fileLayer.getOrigin(),
// id: fileLayer.getId(),
// created_at: fileLayer.getCreatedAt(),
// attribution: fileLayer.getHTMLAttribution(),
// attributions: fileLayer.getAttributions(),
// fields: fileLayer.getFieldsInLanguage(),
// url: fileLayer.getDefaultFormatUrl(),
// format: format, //legacy: format and meta are split up
// meta: meta, //legacy, format and meta are split up,
// emsLink: fileLayer.getEMSHotLink()
// };
// });
//
// const tmsServices = tmsServicesObjs.map(tmsService => {
// return {
// origin: tmsService.getOrigin(),
// id: tmsService.getId(),
// minZoom: tmsService.getMinZoom(),
// maxZoom: tmsService.getMaxZoom(),
// attribution: tmsService.getHTMLAttribution(),
// attributionMarkdown: tmsService.getMarkdownAttribution(),
// url: tmsService.getUrlTemplate()
// };
// });
//
// return { fileLayers, tmsServices };
// }
}

0 comments on commit d1205fb

Please sign in to comment.