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

Resolve url dynamically when requesting EMS data #25685

Merged
merged 15 commits into from
Nov 30, 2018
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
47 changes: 27 additions & 20 deletions src/core_plugins/region_map/public/choropleth_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import d3 from 'd3';
import { i18n } from '@kbn/i18n';
import { KibanaMapLayer } from 'ui/vis/map/kibana_map_layer';
import { truncatedColorMaps } from 'ui/vislib/components/color/truncated_colormaps';
import { uiModules } from 'ui/modules';
import * as topojson from 'topojson-client';
import { toastNotifications } from 'ui/notify';
import * as colorUtil from 'ui/vis/map/color_util';
Expand All @@ -36,6 +37,14 @@ const EMPTY_STYLE = {
};


const emsServiceSettings = new Promise((resolve) => {
uiModules.get('kibana').run(($injector) => {
const serviceSttings = $injector.get('serviceSettings');
resolve(serviceSttings);
});
});


export default class ChoroplethLayer extends KibanaMapLayer {

static _doInnerJoin(sortedMetrics, sortedGeojsonFeatures, joinField) {
Expand Down Expand Up @@ -69,7 +78,7 @@ export default class ChoroplethLayer extends KibanaMapLayer {
}


constructor(geojsonUrl, attribution, format, showAllShapes, meta) {
constructor(name, attribution, format, showAllShapes, meta, layerConfig) {
super();

this._metrics = null;
Expand All @@ -79,9 +88,9 @@ export default class ChoroplethLayer extends KibanaMapLayer {
this._tooltipFormatter = () => '';
this._attribution = attribution;
this._boundsOfData = null;

this._showAllShapes = showAllShapes;
this._geojsonUrl = geojsonUrl;
this._layerName = name;
this._layerConfig = layerConfig;

this._leafletLayer = L.geoJson(null, {
onEachFeature: (feature, layer) => {
Expand Down Expand Up @@ -114,7 +123,7 @@ export default class ChoroplethLayer extends KibanaMapLayer {
this._isJoinValid = false;
this._whenDataLoaded = new Promise(async (resolve) => {
try {
const data = await this._makeJsonAjaxCall(geojsonUrl);
const data = await this._makeJsonAjaxCall();
let featureCollection;
const formatType = typeof format === 'string' ? format : format.type;
if (formatType === 'geojson') {
Expand Down Expand Up @@ -148,15 +157,15 @@ export default class ChoroplethLayer extends KibanaMapLayer {
let errorMessage;
if (e.status === 404) {
nickpeihl marked this conversation as resolved.
Show resolved Hide resolved
errorMessage = i18n.translate('regionMap.choroplethLayer.downloadingVectorData404ErrorMessage', {
defaultMessage: 'Server responding with \'404\' when attempting to fetch {geojsonUrl}. \
defaultMessage: 'Server responding with \'404\' when attempting to fetch {name}. \
Make sure the file exists at that location.',
values: { geojsonUrl },
values: { name: name },
});
} else {
errorMessage = i18n.translate('regionMap.choroplethLayer.downloadingVectorDataErrorMessage', {
defaultMessage: 'Cannot download {geojsonUrl} file. Please ensure the \
defaultMessage: 'Cannot download {name} file. Please ensure the \
CORS configuration of the server permits requests from the Kibana application on this host.',
values: { geojsonUrl },
values: { name: name },
});
}

Expand All @@ -174,11 +183,9 @@ CORS configuration of the server permits requests from the Kibana application on
}

//This method is stubbed in the tests to avoid network request during unit tests.
async _makeJsonAjaxCall(url) {
return await $.ajax({
dataType: 'json',
url: url
});
async _makeJsonAjaxCall() {
const serviceSettings = await emsServiceSettings;
return serviceSettings.getGeoJsonForRegionLayer(this._layerConfig);
}

_invalidateJoin() {
Expand Down Expand Up @@ -222,7 +229,7 @@ CORS configuration of the server permits requests from the Kibana application on
}

getUrl() {
return this._geojsonUrl;
return this._layerName;
}

setTooltipFormatter(tooltipFormatter, metricsAgg, fieldName) {
Expand All @@ -246,8 +253,8 @@ CORS configuration of the server permits requests from the Kibana application on
this._setStyle();
}

cloneChoroplethLayerForNewData(url, attribution, format, showAllData, meta) {
const clonedLayer = new ChoroplethLayer(url, attribution, format, showAllData, meta);
cloneChoroplethLayerForNewData(name, attribution, format, showAllData, meta, layerConfig) {
const clonedLayer = new ChoroplethLayer(name, attribution, format, showAllData, meta, layerConfig);
clonedLayer.setJoinField(this._joinField);
clonedLayer.setColorRamp(this._colorRamp);
clonedLayer.setLineWeight(this._lineWeight);
Expand Down Expand Up @@ -301,12 +308,12 @@ CORS configuration of the server permits requests from the Kibana application on
this._setStyle();
}

canReuseInstance(geojsonUrl, showAllShapes) {
return this._geojsonUrl === geojsonUrl && this._showAllShapes === showAllShapes;
canReuseInstance(name, showAllShapes) {
return this._layerName === name && this._showAllShapes === showAllShapes;
}

canReuseInstanceForNewMetrics(geojsonUrl, showAllShapes, newMetrics) {
if (this._geojsonUrl !== geojsonUrl) {
canReuseInstanceForNewMetrics(name, showAllShapes, newMetrics) {
if (this._layerName !== name) {
return false;
}

Expand Down
5 changes: 4 additions & 1 deletion src/core_plugins/region_map/public/region_map_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ import { truncatedColorMaps } from 'ui/vislib/components/color/truncated_colorma
import { mapToLayerWithId } from './util';
import { RegionMapsVisualizationProvider } from './region_map_visualization';
import { Status } from 'ui/vis/update_status';
import { ORIGIN } from 'ui/vis/map/origin';

VisTypesRegistryProvider.register(function RegionMapProvider(Private, regionmapsConfig, config, i18n) {

const VisFactory = Private(VisFactoryProvider);
const RegionMapsVisualization = Private(RegionMapsVisualizationProvider);

const vectorLayers = regionmapsConfig.layers.map(mapToLayerWithId.bind(null, 'self_hosted', false));
const vectorLayers = regionmapsConfig.layers.map(mapToLayerWithId.bind(null, ORIGIN.KIBANA_YML));

const selectedLayer = vectorLayers[0];
const selectedJoinField = selectedLayer ? vectorLayers[0].fields[0] : null;

Expand Down
3 changes: 2 additions & 1 deletion src/core_plugins/region_map/public/region_map_vis_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { toastNotifications } from 'ui/notify';
import regionMapVisParamsTemplate from './region_map_vis_params.html';
import { mapToLayerWithId } from './util';
import '../../tile_map/public/editors/wms_options';
import { ORIGIN } from 'ui/vis/map/origin';

uiModules.get('kibana/region_map')
.directive('regionMapVisParams', function (serviceSettings, regionmapsConfig) {
Expand All @@ -38,7 +39,7 @@ uiModules.get('kibana/region_map')
serviceSettings.getFileLayers()
.then(function (layersFromService) {

layersFromService = layersFromService.map(mapToLayerWithId.bind(null, 'elastic_maps_service', true));
layersFromService = layersFromService.map(mapToLayerWithId.bind(null, ORIGIN.EMS));
const newVectorLayers = $scope.collections.vectorLayers.slice();
for (let i = 0; i < layersFromService.length; i += 1) {
const layerFromService = layersFromService[i];
Expand Down
28 changes: 15 additions & 13 deletions src/core_plugins/region_map/public/region_map_visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function RegionMapsVisualizationProvider(Private, config, i18n) {
}

this._updateChoroplethLayerForNewMetrics(
this._vis.params.selectedLayer.url,
this._vis.params.selectedLayer.name,
this._vis.params.selectedLayer.attribution,
this._vis.params.showAllShapes,
results
Expand All @@ -96,7 +96,7 @@ export function RegionMapsVisualizationProvider(Private, config, i18n) {
}

this._updateChoroplethLayerForNewProperties(
visParams.selectedLayer.url,
visParams.selectedLayer.name,
visParams.selectedLayer.attribution,
this._vis.params.showAllShapes
);
Expand All @@ -107,40 +107,42 @@ export function RegionMapsVisualizationProvider(Private, config, i18n) {

}

_updateChoroplethLayerForNewMetrics(url, attribution, showAllData, newMetrics) {
if (this._choroplethLayer && this._choroplethLayer.canReuseInstanceForNewMetrics(url, showAllData, newMetrics)) {
_updateChoroplethLayerForNewMetrics(name, attribution, showAllData, newMetrics) {
if (this._choroplethLayer && this._choroplethLayer.canReuseInstanceForNewMetrics(name, showAllData, newMetrics)) {
return;
}
return this._recreateChoroplethLayer(url, attribution, showAllData);
return this._recreateChoroplethLayer(name, attribution, showAllData);
}

_updateChoroplethLayerForNewProperties(url, attribution, showAllData) {
if (this._choroplethLayer && this._choroplethLayer.canReuseInstance(url, showAllData)) {
_updateChoroplethLayerForNewProperties(name, attribution, showAllData) {
if (this._choroplethLayer && this._choroplethLayer.canReuseInstance(name, showAllData)) {
return;
}
return this._recreateChoroplethLayer(url, attribution, showAllData);
return this._recreateChoroplethLayer(name, attribution, showAllData);
}

_recreateChoroplethLayer(url, attribution, showAllData) {
_recreateChoroplethLayer(name, attribution, showAllData) {

this._kibanaMap.removeLayer(this._choroplethLayer);


if (this._choroplethLayer) {
this._choroplethLayer = this._choroplethLayer.cloneChoroplethLayerForNewData(
url,
name,
attribution,
this.vis.params.selectedLayer.format,
showAllData,
this.vis.params.selectedLayer.meta
this.vis.params.selectedLayer.meta,
this.vis.params.selectedLayer
);
} else {
this._choroplethLayer = new ChoroplethLayer(
url,
name,
attribution,
this.vis.params.selectedLayer.format,
showAllData,
this.vis.params.selectedLayer.meta
this.vis.params.selectedLayer.meta,
this.vis.params.selectedLayer
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/core_plugins/region_map/public/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/

import _ from 'lodash';
import { ORIGIN } from 'ui/vis/map/origin';

export function mapToLayerWithId(prefix, isEMS, layer) {
export function mapToLayerWithId(prefix, layer) {
const clonedLayer = _.cloneDeep(layer);
clonedLayer.layerId = prefix + '.' + layer.name;
clonedLayer.isEMS = isEMS;
clonedLayer.isEMS = ORIGIN.EMS === prefix ? true : false;
return clonedLayer;
}
13 changes: 11 additions & 2 deletions src/core_plugins/tile_map/public/base_maps_visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ import * as Rx from 'rxjs';
import { filter, first } from 'rxjs/operators';
import 'ui/vis/map/service_settings';
import { toastNotifications } from 'ui/notify';
import { uiModules } from 'ui/modules';

const MINZOOM = 0;
const MAXZOOM = 22;//increase this to 22. Better for WMS

const emsServiceSettings = new Promise((resolve) => {
uiModules.get('kibana').run(($injector) => {
const serviceSttings = $injector.get('serviceSettings');
resolve(serviceSttings);
});
});

export function BaseMapsVisualizationProvider(serviceSettings, i18n) {

/**
Expand Down Expand Up @@ -184,7 +192,8 @@ export function BaseMapsVisualizationProvider(serviceSettings, i18n) {
if (this._kibanaMap.getZoomLevel() > tmsLayer.maxZoom) {
this._kibanaMap.setZoomLevel(tmsLayer.maxZoom);
}
const url = tmsLayer.url;
// const url = tmsLayer.url;
const url = await (await emsServiceSettings).getUrlTemplateForTMSLayer(tmsLayer);
const options = _.cloneDeep(tmsLayer);
delete options.id;
delete options.url;
Expand All @@ -209,7 +218,7 @@ export function BaseMapsVisualizationProvider(serviceSettings, i18n) {
*/
async _updateParams() {
const mapParams = this._getMapsParams();
await this._updateBaseLayer(mapParams);
await this._updateBaseLayer();
this._kibanaMap.setLegendPosition(mapParams.legendPosition);
this._kibanaMap.setShowTooltip(mapParams.addTooltip);
this._kibanaMap.useUiStateFromVisualization(this.vis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ export function CoordinateMapsVisualizationProvider(Notifier, Private) {
});
}


_isFilteredByCollar() {
const DEFAULT = false;
const agg = this._getGeoHashAgg();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ describe('VegaParser._resolveEsQueries', () => {
function test(spec, expected, warnCount) {
return async () => {
const vp = new VegaParser(spec, { search: async () => [[42]] }, 0, 0, {
getFileLayers: async () => [{ name: 'file1', url: 'url1' }]
getFileLayers: async () => [{ name: 'file1', url: 'url1' }],
getUrlForRegionLayer: async (layer) => { return layer.url;}
});
await vp._resolveDataUrls();

Expand Down
3 changes: 2 additions & 1 deletion src/core_plugins/vega/public/data_model/ems_file_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class EmsFileParser {
}

// This URL can bypass loader sanitization at the later stage
obj.url = bypassExternalUrlCheck(foundLayer.url);
const url = await this._serviceSettings.getUrlForRegionLayer(foundLayer);
obj.url = bypassExternalUrlCheck(url);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/core_plugins/vega/public/vega_view/vega_map_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class VegaMapView extends VegaBaseView {
if (!this._$container) return;
const mapStyle = mapConfig.mapStyle === 'default' ? 'road_map' : mapConfig.mapStyle;
baseMapOpts = tmsServices.find((s) => s.id === mapStyle);
baseMapOpts = {
url: await this._serviceSettings.getUrlTemplateForTMSLayer(baseMapOpts),
...baseMapOpts
};
if (!baseMapOpts) {
this.onWarn(i18n.translate('vega.mapView.mapStyleNotFoundWarningMessage', {
defaultMessage: '{mapStyleParam} was not found',
Expand Down
Loading