Skip to content

Commit

Permalink
the merge
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Mar 27, 2020
2 parents 6c782f5 + 5a537d1 commit 17e451d
Show file tree
Hide file tree
Showing 62 changed files with 674 additions and 341 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ module.exports = {
'x-pack/dev-tools/mocha/setup_mocha.js',
'x-pack/scripts/*.js',
],
excludedFiles: ['**/integration_tests/**/*'],
rules: {
'import/no-commonjs': 'off',
'prefer-object-spread/prefer-object-spread': 'off',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
| Function | Description |
| --- | --- |
| [getDefaultSearchParams(config)](./kibana-plugin-plugins-data-server.getdefaultsearchparams.md) | |
| [getTotalLoaded({ total, failed, successful })](./kibana-plugin-plugins-data-server.gettotalloaded.md) | |
| [parseInterval(interval)](./kibana-plugin-plugins-data-server.parseinterval.md) | |
| [plugin(initializerContext)](./kibana-plugin-plugins-data-server.plugin.md) | Static code to be shared externally |
| [shouldReadFieldFromDocValues(aggregatable, esType)](./kibana-plugin-plugins-data-server.shouldreadfieldfromdocvalues.md) | |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@
"deepmerge": "^4.2.2",
"del": "^5.1.0",
"elastic-apm-node": "^3.2.0",
"elasticsearch": "^16.5.0",
"elasticsearch-browser": "^16.5.0",
"elasticsearch": "^16.7.0",
"elasticsearch-browser": "^16.7.0",
"execa": "^4.0.0",
"expiry-js": "0.1.7",
"fast-deep-equal": "^3.1.1",
Expand Down
173 changes: 28 additions & 145 deletions packages/kbn-plugin-generator/integration_tests/generate_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,163 +17,46 @@
* under the License.
*/

/* eslint-disable no-restricted-syntax */
import { spawn } from 'child_process';
import Fs from 'fs';
import { resolve } from 'path';
import util from 'util';
import { stat, readFileSync } from 'fs';
import { snakeCase } from 'lodash';
import { promisify } from 'util';

import del from 'del';
import { ProcRunner, ToolingLog } from '@kbn/dev-utils';
import { createLegacyEsTestCluster } from '@kbn/test';
import execa from 'execa';
import { snakeCase } from 'lodash';

const statP = util.promisify(stat);
const statAsync = promisify(Fs.stat);
const ROOT_DIR = resolve(__dirname, '../../../');
const oneMinute = 60000;

describe(`running the plugin-generator via 'node scripts/generate_plugin.js plugin-name' with default config`, () => {
const pluginName = 'ispec-plugin';
const snakeCased = snakeCase(pluginName);
const generatedPath = resolve(ROOT_DIR, `plugins/${snakeCased}`);
const collect = xs => data => xs.push(data + ''); // Coerce from Buffer to String

beforeAll(() => {
jest.setTimeout(oneMinute * 10);
});

beforeAll(done => {
const create = spawn(process.execPath, ['scripts/generate_plugin.js', pluginName], {
cwd: ROOT_DIR,
});
create.stdout.on('data', function selectDefaults() {
create.stdin.write('\n'); // Generate a plugin with default options.
});
create.on('close', done);
});

afterAll(() => {
del.sync(generatedPath, { force: true });
});

it(`should succeed on creating a plugin in a directory named 'plugins/${snakeCased}`, async () => {
const stats = await statP(generatedPath);
expect(stats.isDirectory()).toBe(true);
});

// skipped until internationalization is re-introduced
it.skip(`should create an internationalization config file with a blank line appended to satisfy the parser`, async () => {
// Link to the error that happens when the blank line is not there:
// https://github.com/elastic/kibana/pull/45044#issuecomment-530092627
const intlFile = `${generatedPath}/.i18nrc.json`;
expect(readFileSync(intlFile, 'utf8').endsWith('\n\n')).toBe(true);
});

describe(`then running`, () => {
it(`'yarn test:karma' should exit 0`, async () => {
await execa('yarn', ['test:karma'], {
cwd: generatedPath,
env: {
DISABLE_JUNIT_REPORTER: '1',
},
});
});

it.skip(`'yarn build' should exit 0`, async () => {
await execa('yarn', ['build'], { cwd: generatedPath });
});
const pluginName = 'ispec-plugin';
const snakeCased = snakeCase(pluginName);
const generatedPath = resolve(ROOT_DIR, `plugins/${snakeCased}`);

describe('with es instance', () => {
const log = new ToolingLog({
level: 'verbose',
writeTo: process.stdout,
});
const pr = new ProcRunner(log);

const es = createLegacyEsTestCluster({ license: 'basic', log });
beforeAll(es.start);
afterAll(es.stop);
afterAll(() => pr.teardown());
beforeAll(async () => {
await del(generatedPath, { force: true });
});

it(`'yarn start' should result in the spec plugin being initialized on kibana's stdout`, async () => {
await pr.run('kibana', {
cmd: 'yarn',
args: [
'start',
'--optimize.enabled=false',
'--logging.json=false',
'--logging.verbose=true',
'--migrations.skip=true',
],
cwd: generatedPath,
wait: new RegExp('\\[ispecPlugin\\]\\[plugins\\] Setting up plugin'),
});
await pr.stop('kibana');
});
});
afterAll(async () => {
await del(generatedPath, { force: true });
});

it(`'yarn preinstall' should exit 0`, async () => {
await execa('yarn', ['preinstall'], { cwd: generatedPath });
it('generates a plugin', async () => {
await new Promise((resolve, reject) => {
const proc = spawn(process.execPath, ['scripts/generate_plugin.js', pluginName], {
cwd: ROOT_DIR,
stdio: 'pipe',
});

it.skip(`'yarn lint' should exit 0`, async () => {
await execa('yarn', ['lint'], { cwd: generatedPath });
proc.stdout.on('data', function selectDefaults() {
proc.stdin.write('\n'); // Generate a plugin with default options.
});

it(`'yarn kbn --help' should print out the kbn help msg`, done => {
const helpMsg = `
usage: kbn <command> [<args>]
By default commands are run for Kibana itself, all packages in the 'packages/'
folder and for all plugins in './plugins' and '../kibana-extra'.
Available commands:
bootstrap - Install dependencies and crosslink projects
clean - Remove the node_modules and target directories from all projects.
run - Run script defined in package.json in each package that contains that script.
watch - Runs \`kbn:watch\` script for every project.
Global options:
-e, --exclude Exclude specified project. Can be specified multiple times to exclude multiple projects, e.g. '-e kibana -e @kbn/pm'.
-i, --include Include only specified projects. If left unspecified, it defaults to including all projects.
--oss Do not include the x-pack when running command.
--skip-kibana-plugins Filter all plugins in ./plugins and ../kibana-extra when running command.
`;
const outData = [];
const kbnHelp = spawn('yarn', ['kbn', '--help'], { cwd: generatedPath });
kbnHelp.stdout.on('data', collect(outData));
kbnHelp.on('close', () => {
expect(outData.join('\n')).toContain(helpMsg);
done();
});
});

it(`'yarn es --help' should print out the es help msg`, done => {
const helpMsg = `
usage: es <command> [<args>]
Assists with running Elasticsearch for Kibana development
Available commands:
snapshot - Downloads and run from a nightly snapshot
source - Build and run from source
archive - Install and run from an Elasticsearch tar
build_snapshots - Build and collect ES snapshots
Global options:
--help
`;
const outData = [];
const kbnHelp = spawn('yarn', ['es', '--help'], { cwd: generatedPath });
kbnHelp.stdout.on('data', collect(outData));
kbnHelp.on('close', () => {
expect(outData.join('\n')).toContain(helpMsg);
done();
});
});
proc.on('close', resolve);
proc.on('error', reject);
});

const stats = await statAsync(generatedPath);
if (!stats.isDirectory()) {
throw new Error(`Expected [${generatedPath}] to be a directory`);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ export function setServices(newServices: any) {
services = newServices;
}

export const [getUrlTracker, setUrlTracker] = createGetterSetter<{
setTrackedUrl: (url: string) => void;
}>('urlTracker');

// EXPORT legacy static dependencies, should be migrated when available in a new version;
export { angular };
export { wrapInI18nContext } from 'ui/i18n';
import { search } from '../../../../../plugins/data/public';
import { createGetterSetter } from '../../../../../plugins/kibana_utils/common';
export const { getRequestInspectorStats, getResponseInspectorStats, tabifyAggResponse } = search;
export {
unhashUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
getRequestInspectorStats,
getResponseInspectorStats,
getServices,
getUrlTracker,
unhashUrl,
subscribeWithScope,
tabifyAggResponse,
Expand Down Expand Up @@ -160,6 +161,9 @@ app.config($routeProvider => {
'/management/kibana/objects/savedSearches/' + $route.current.params.id,
},
toastNotifications,
onBeforeRedirect() {
getUrlTracker().setTrackedUrl('/discover');
},
})
),
});
Expand Down
10 changes: 8 additions & 2 deletions src/legacy/core_plugins/kibana/public/discover/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { registerFeature } from './np_ready/register_feature';
import './kibana_services';
import { EmbeddableStart, EmbeddableSetup } from '../../../../../plugins/embeddable/public';
import { getInnerAngularModule, getInnerAngularModuleEmbeddable } from './get_inner_angular';
import { setAngularModule, setServices } from './kibana_services';
import { setAngularModule, setServices, setUrlTracker } from './kibana_services';
import { NavigationPublicPluginStart as NavigationStart } from '../../../../../plugins/navigation/public';
import { ChartsPluginStart } from '../../../../../plugins/charts/public';
import { buildServices } from './build_services';
Expand Down Expand Up @@ -92,7 +92,12 @@ export class DiscoverPlugin implements Plugin<void, void> {
public initializeServices?: () => Promise<{ core: CoreStart; plugins: DiscoverStartPlugins }>;

setup(core: CoreSetup<DiscoverStartPlugins, void>, plugins: DiscoverSetupPlugins) {
const { appMounted, appUnMounted, stop: stopUrlTracker } = createKbnUrlTracker({
const {
appMounted,
appUnMounted,
stop: stopUrlTracker,
setActiveUrl: setTrackedUrl,
} = createKbnUrlTracker({
baseUrl: core.http.basePath.prepend('/app/kibana'),
defaultSubUrl: '#/discover',
storageKey: 'lastUrl:discover',
Expand All @@ -113,6 +118,7 @@ export class DiscoverPlugin implements Plugin<void, void> {
},
],
});
setUrlTracker({ setTrackedUrl });
this.stopUrlTracking = () => {
stopUrlTracker();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ export function initVisualizeApp(app, deps) {
'/management/kibana/objects/savedVisualizations/' + $route.current.params.id,
},
toastNotifications,
onBeforeRedirect() {
deps.setActiveUrl(VisualizeConstants.LANDING_PAGE_PATH);
},
})
);
},
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export {
ISearchContext,
TSearchStrategyProvider,
getDefaultSearchParams,
getTotalLoaded,
} from './search';

// Search namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { APICaller } from 'kibana/server';
import { SearchResponse } from 'elasticsearch';
import { ES_SEARCH_STRATEGY } from '../../../common/search';
import { ISearchStrategy, TSearchStrategyProvider } from '../i_search_strategy';
import { getDefaultSearchParams, ISearchContext } from '..';
import { getDefaultSearchParams, getTotalLoaded, ISearchContext } from '..';

export const esSearchStrategyProvider: TSearchStrategyProvider<typeof ES_SEARCH_STRATEGY> = (
context: ISearchContext,
Expand All @@ -46,9 +46,7 @@ export const esSearchStrategyProvider: TSearchStrategyProvider<typeof ES_SEARCH_

// The above query will either complete or timeout and throw an error.
// There is no progress indication on this api.
const { total, failed, successful } = rawResponse._shards;
const loaded = failed + successful;
return { total, loaded, rawResponse };
return { rawResponse, ...getTotalLoaded(rawResponse._shards) };
},
};
};
36 changes: 36 additions & 0 deletions src/plugins/data/server/search/es_search/get_total_loaded.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { getTotalLoaded } from './get_total_loaded';

describe('getTotalLoaded', () => {
it('returns the total/loaded, not including skipped', () => {
const result = getTotalLoaded({
successful: 10,
failed: 5,
skipped: 5,
total: 100,
});

expect(result).toEqual({
total: 100,
loaded: 15,
});
});
});
30 changes: 30 additions & 0 deletions src/plugins/data/server/search/es_search/get_total_loaded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 { ShardsResponse } from 'elasticsearch';

/**
* Get the `total`/`loaded` for this response (see `IKibanaSearchResponse`). Note that `skipped` is
* not included as it is already included in `successful`.
* @internal
*/
export function getTotalLoaded({ total, failed, successful }: ShardsResponse) {
const loaded = failed + successful;
return { total, loaded };
}
1 change: 1 addition & 0 deletions src/plugins/data/server/search/es_search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
export { ES_SEARCH_STRATEGY, IEsSearchRequest, IEsSearchResponse } from '../../../common/search';
export { esSearchStrategyProvider } from './es_search_strategy';
export { getDefaultSearchParams } from './get_default_search_params';
export { getTotalLoaded } from './get_total_loaded';
Loading

0 comments on commit 17e451d

Please sign in to comment.