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

[Region Map] Shim new Platform #40966

Merged
merged 15 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
44 changes: 44 additions & 0 deletions src/legacy/core_plugins/region_map/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

import { resolve } from 'path';
import { Legacy } from 'kibana';

import { LegacyPluginApi, LegacyPluginInitializer } from '../../../../src/legacy/types';

const regionMapPluginInitializer: LegacyPluginInitializer = ({ Plugin }: LegacyPluginApi) =>
new Plugin({
id: 'region_map',
require: ['kibana', 'elasticsearch', 'visualizations', 'interpreter', 'data'],
publicDir: resolve(__dirname, 'public'),
uiExports: {
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
hacks: [resolve(__dirname, 'public/legacy')],
injectDefaultVars: server => ({}),
},
init: (server: Legacy.Server) => ({}),
config(Joi: any) {
return Joi.object({
enabled: Joi.boolean().default(true),
}).default();
},
} as Legacy.PluginSpecOptions);

// eslint-disable-next-line import/no-default-export
export default regionMapPluginInitializer;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import _ from 'lodash';
import { RegionMapsVisualizationProvider } from '../region_map_visualization';
import ChoroplethLayer from '../choropleth_layer';
import LogstashIndexPatternStubProvider from 'fixtures/stubbed_logstash_index_pattern';
import * as visModule from 'ui/vis';
Expand All @@ -40,7 +39,10 @@ import afterdatachangePng from './afterdatachange.png';
import afterdatachangeandresizePng from './afterdatachangeandresize.png';
import aftercolorchangePng from './aftercolorchange.png';
import changestartupPng from './changestartup.png';
import { visualizations } from '../../../visualizations/public';

import { createRegionMapVisualization } from '../region_map_visualization';
import { createRegionMapTypeDefinition } from '../region_map_type';

const THRESHOLD = 0.45;
const PIXEL_DIFF = 96;
Expand All @@ -52,6 +54,7 @@ describe('RegionMapsVisualizationTests', function () {
let Vis;
let indexPattern;
let vis;
let dependencies;

let imageComparator;

Expand Down Expand Up @@ -85,9 +88,23 @@ describe('RegionMapsVisualizationTests', function () {

let getManifestStub;
beforeEach(ngMock.inject((Private, $injector) => {
const serviceSettings = $injector.get('serviceSettings');
const regionmapsConfig = $injector.get('regionmapsConfig');
const uiSettings = $injector.get('config');

dependencies = {
serviceSettings,
$injector,
regionmapsConfig,
uiSettings
};

visualizations.types.VisTypesRegistryProvider.register(() =>
createRegionMapTypeDefinition(dependencies)
);

Vis = Private(visModule.VisProvider);
RegionMapsVisualization = Private(RegionMapsVisualizationProvider);
RegionMapsVisualization = createRegionMapVisualization(dependencies);
indexPattern = Private(LogstashIndexPatternStubProvider);

ChoroplethLayer.prototype._makeJsonAjaxCall = async function () {
Expand All @@ -99,7 +116,6 @@ describe('RegionMapsVisualizationTests', function () {
});
};

const serviceSettings = $injector.get('serviceSettings');
getManifestStub = serviceSettings.__debugStubManifestCalls(async (url) => {
//simulate network calls
if (url.startsWith('https://foobar')) {
Expand All @@ -118,16 +134,13 @@ describe('RegionMapsVisualizationTests', function () {
}
}
});

}));


afterEach(function () {
ChoroplethLayer.prototype._makeJsonAjaxCall = _makeJsonAjaxCallOld;
getManifestStub.removeStub();
});


describe('RegionMapVisualization - basics', function () {

beforeEach(async function () {
Expand Down Expand Up @@ -260,6 +273,7 @@ describe('RegionMapsVisualizationTests', function () {
});

const newTableGroup = _.cloneDeep(dummyTableGroup);

newTableGroup.rows.pop();//remove one shape

await regionMapsVisualization.render(newTableGroup, vis.params, {
Expand All @@ -269,10 +283,10 @@ describe('RegionMapsVisualizationTests', function () {
data: true,
uiState: false
});
const mismatchedPixelsAfterDataChange = await compareImage(afterdatachangePng);


const mismatchedPixelsAfterDataChange = await compareImage(afterdatachangePng);
const anotherTableGroup = _.cloneDeep(newTableGroup);

anotherTableGroup.rows.pop();//remove one shape
domNode.style.width = '412px';
domNode.style.height = '112px';
Expand All @@ -288,11 +302,9 @@ describe('RegionMapsVisualizationTests', function () {
regionMapsVisualization.destroy();
expect(mismatchedPixelsAfterDataChange).to.be.lessThan(PIXEL_DIFF);
expect(mismatchedPixelsAfterDataChangeAndResize).to.be.lessThan(PIXEL_DIFF);

});

it('should redo data and color ramp', async function () {

const regionMapsVisualization = new RegionMapsVisualization(domNode, vis);
await regionMapsVisualization.render(dummyTableGroup, vis.params, {
resize: false,
Expand All @@ -316,12 +328,9 @@ describe('RegionMapsVisualizationTests', function () {

regionMapsVisualization.destroy();
expect(mismatchedPixelsAfterDataAndColorChange).to.be.lessThan(PIXEL_DIFF);

});


it('should zoom and center elsewhere', async function () {

vis.params.mapZoom = 4;
vis.params.mapCenter = [36, -85];
const regionMapsVisualization = new RegionMapsVisualization(domNode, vis);
Expand All @@ -337,21 +346,17 @@ describe('RegionMapsVisualizationTests', function () {
regionMapsVisualization.destroy();

expect(mismatchedPixels).to.be.lessThan(PIXEL_DIFF);

});


});


async function compareImage(expectedImageSource) {
const elementList = domNode.querySelectorAll('canvas');
expect(elementList.length).to.equal(1);
const firstCanvasOnMap = elementList[0];
return imageComparator.compareImage(firstCanvasOnMap, expectedImageSource, THRESHOLD);
}


function setupDOM(width, height) {
domNode = document.createElement('div');
domNode.style.top = '0';
Expand All @@ -368,6 +373,4 @@ describe('RegionMapsVisualizationTests', function () {
domNode.innerHTML = '';
document.body.removeChild(domNode);
}

});

21 changes: 5 additions & 16 deletions src/legacy/core_plugins/region_map/public/choropleth_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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,15 +35,6 @@ const EMPTY_STYLE = {
fillOpacity: 0
};


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 @@ -78,10 +68,10 @@ export default class ChoroplethLayer extends KibanaMapLayer {
}


constructor(name, attribution, format, showAllShapes, meta, layerConfig) {
constructor(name, attribution, format, showAllShapes, meta, layerConfig, serviceSettings) {

super();

this._serviceSettings = serviceSettings;
this._metrics = null;
this._joinField = null;
this._colorRamp = truncatedColorMaps[Object.keys(truncatedColorMaps)[0]].value;
Expand Down Expand Up @@ -193,8 +183,7 @@ 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() {
const serviceSettings = await emsServiceSettings;
return serviceSettings.getJsonForRegionLayer(this._layerConfig);
return this._serviceSettings.getJsonForRegionLayer(this._layerConfig);
}

_invalidateJoin() {
Expand Down Expand Up @@ -262,8 +251,8 @@ CORS configuration of the server permits requests from the Kibana application on
this._setStyle();
}

cloneChoroplethLayerForNewData(name, attribution, format, showAllData, meta, layerConfig) {
const clonedLayer = new ChoroplethLayer(name, attribution, format, showAllData, meta, layerConfig);
cloneChoroplethLayerForNewData(name, attribution, format, showAllData, meta, layerConfig, serviceSettings) {
const clonedLayer = new ChoroplethLayer(name, attribution, format, showAllData, meta, layerConfig, serviceSettings);
clonedLayer.setJoinField(this._joinField);
clonedLayer.setColorRamp(this._colorRamp);
clonedLayer.setLineWeight(this._lineWeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@
* under the License.
*/

import { resolve } from 'path';

export default function (kibana) {

return new kibana.Plugin({
uiExports: {
visTypes: ['plugins/region_map/region_map_vis'],
interpreter: ['plugins/region_map/region_map_fn'],
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
}
});
import { PluginInitializerContext } from '../../../../core/public';
import { RegionMapPlugin as Plugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new Plugin(initializerContext);
}
40 changes: 40 additions & 0 deletions src/legacy/core_plugins/region_map/public/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

import { PluginInitializerContext } from 'kibana/public';
import { npSetup, npStart } from 'ui/new_platform';

import { visualizations } from '../../visualizations/public';
import { RegionMapPluginSetupDependencies } from './plugin';
import { LegacyDependenciesPlugin } from './shim';
import { plugin } from '.';

const plugins: Readonly<RegionMapPluginSetupDependencies> = {
visualizations,
data: npSetup.plugins.data,

// Temporary solution
// It will be removed when all dependent services are migrated to the new platform.
__LEGACY: new LegacyDependenciesPlugin(),
};

const pluginInstance = plugin({} as PluginInitializerContext);

export const setup = pluginInstance.setup(npSetup.core, plugins);
export const start = pluginInstance.start(npStart.core);
75 changes: 75 additions & 0 deletions src/legacy/core_plugins/region_map/public/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.
*/
import {
CoreSetup,
CoreStart,
Plugin,
PluginInitializerContext,
UiSettingsClientContract,
} from '../../../../core/public';
import { Plugin as DataPublicPlugin } from '../../../../plugins/data/public';
import { VisualizationsSetup } from '../../visualizations/public';

import { LegacyDependenciesPlugin, LegacyDependenciesPluginSetup } from './shim';

// @ts-ignore
import { createRegionMapFn } from './region_map_fn';
// @ts-ignore
import { createRegionMapTypeDefinition } from './region_map_type';

/** @private */
interface RegionMapVisualizationDependencies extends LegacyDependenciesPluginSetup {
uiSettings: UiSettingsClientContract;
}

/** @internal */
export interface RegionMapPluginSetupDependencies {
data: ReturnType<DataPublicPlugin['setup']>;
visualizations: VisualizationsSetup;
__LEGACY: LegacyDependenciesPlugin;
}

/** @internal */
export class RegionMapPlugin implements Plugin<Promise<void>, void> {
initializerContext: PluginInitializerContext;

constructor(initializerContext: PluginInitializerContext) {
this.initializerContext = initializerContext;
}

public async setup(
core: CoreSetup,
{ data, visualizations, __LEGACY }: RegionMapPluginSetupDependencies
) {
const visualizationDependencies: Readonly<RegionMapVisualizationDependencies> = {
uiSettings: core.uiSettings,
...(await __LEGACY.setup()),
};

data.expressions.registerFunction(createRegionMapFn);

visualizations.types.VisTypesRegistryProvider.register(() =>
createRegionMapTypeDefinition(visualizationDependencies)
);
}

public start(core: CoreStart) {
// nothing to do here yet
}
}
Loading