Skip to content

Commit

Permalink
Merge branch 'master' into poc/filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Feb 3, 2020
2 parents 6515f82 + 38dc1cb commit a040f0b
Show file tree
Hide file tree
Showing 89 changed files with 3,258 additions and 610 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function getSettingsFromFile(log: ToolingLog, path: string, settingOverrid
return transformDeprecations(settingsWithDefaults, logDeprecation);
}

export async function readConfigFile(log: ToolingLog, path: string, settingOverrides: any) {
export async function readConfigFile(log: ToolingLog, path: string, settingOverrides: any = {}) {
return new Config({
settings: await getSettingsFromFile(log, path, settingOverrides),
primary: true,
Expand Down
88 changes: 88 additions & 0 deletions src/core/public/application/ui/app_container.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 React from 'react';
import { mount } from 'enzyme';

import { AppContainer } from './app_container';
import { Mounter, AppMountParameters, AppStatus } from '../types';

describe('AppContainer', () => {
const appId = 'someApp';
const setAppLeaveHandler = jest.fn();

const flushPromises = async () => {
await new Promise(async resolve => {
setImmediate(() => resolve());
});
};

const createResolver = (): [Promise<void>, () => void] => {
let resolve: () => void | undefined;
const promise = new Promise<void>(r => {
resolve = r;
});
return [promise, resolve!];
};

const createMounter = (promise: Promise<void>): Mounter => ({
appBasePath: '/base-path',
appRoute: '/some-route',
unmountBeforeMounting: false,
mount: async ({ element }: AppMountParameters) => {
await promise;
const container = document.createElement('div');
container.innerHTML = 'some-content';
element.appendChild(container);
return () => container.remove();
},
});

it('should hide the "not found" page before mounting the route', async () => {
const [waitPromise, resolvePromise] = createResolver();
const mounter = createMounter(waitPromise);

const wrapper = mount(
<AppContainer
appId={appId}
appStatus={AppStatus.inaccessible}
mounter={mounter}
setAppLeaveHandler={setAppLeaveHandler}
/>
);

expect(wrapper.text()).toContain('Application Not Found');

wrapper.setProps({
appId,
setAppLeaveHandler,
mounter,
appStatus: AppStatus.accessible,
});
wrapper.update();

expect(wrapper.text()).toEqual('');

resolvePromise();
await flushPromises();
wrapper.update();

expect(wrapper.text()).toContain('some-content');
});
});
18 changes: 10 additions & 8 deletions src/core/public/application/ui/app_container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,27 @@ export const AppContainer: FunctionComponent<Props> = ({
unmountRef.current = null;
}
};
const mount = async () => {
if (!mounter || appStatus !== AppStatus.accessible) {
return setAppNotFound(true);
}

if (mounter.unmountBeforeMounting) {
unmount();
}
if (!mounter || appStatus !== AppStatus.accessible) {
return setAppNotFound(true);
}
setAppNotFound(false);

if (mounter.unmountBeforeMounting) {
unmount();
}

const mount = async () => {
unmountRef.current =
(await mounter.mount({
appBasePath: mounter.appBasePath,
element: elementRef.current!,
onAppLeave: handler => setAppLeaveHandler(appId, handler),
})) || null;
setAppNotFound(false);
};

mount();

return unmount;
}, [appId, appStatus, mounter, setAppLeaveHandler]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ import Fs from 'fs';
import { createGunzip, createGzip, Z_BEST_COMPRESSION } from 'zlib';
import { promisify } from 'util';
import globby from 'globby';
import { ToolingLog } from '@kbn/dev-utils';

import { createPromiseFromStreams } from '../../legacy/utils';

const unlinkAsync = promisify(Fs.unlink);

export async function editAction({ prefix, dataDir, log, handler }) {
export async function editAction({
prefix,
dataDir,
log,
handler,
}: {
prefix: string;
dataDir: string;
log: ToolingLog;
handler: () => Promise<any>;
}) {
const archives = (
await globby('**/*.gz', {
cwd: prefix ? resolve(dataDir, prefix) : dataDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/

import { Client } from 'elasticsearch';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';

import { migrateKibanaIndex, deleteKibanaIndices, createStats } from '../lib';

export async function emptyKibanaIndexAction({ client, log, kbnClient }) {
export async function emptyKibanaIndexAction({
client,
log,
kbnClient,
}: {
client: Client;
log: ToolingLog;
kbnClient: KbnClient;
}) {
const stats = createStats('emptyKibanaIndex', log);
const kibanaPluginIds = await kbnClient.plugins.getEnabledIds();

await deleteKibanaIndices({ client, stats });
await migrateKibanaIndex({ client, log, stats, kibanaPluginIds });
await deleteKibanaIndices({ client, stats, log });
await migrateKibanaIndex({ client, log, kibanaPluginIds });
return stats;
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import { resolve } from 'path';
import { createReadStream } from 'fs';
import { Readable } from 'stream';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';
import { Client } from 'elasticsearch';

import { createPromiseFromStreams, concatStreamProviders } from '../../legacy/utils';

Expand All @@ -38,12 +41,26 @@ import {
// pipe a series of streams into each other so that data and errors
// flow from the first stream to the last. Errors from the last stream
// are not listened for
const pipeline = (...streams) =>
const pipeline = (...streams: Readable[]) =>
streams.reduce((source, dest) =>
source.once('error', error => dest.emit('error', error)).pipe(dest)
source.once('error', error => dest.emit('error', error)).pipe(dest as any)
);

export async function loadAction({ name, skipExisting, client, dataDir, log, kbnClient }) {
export async function loadAction({
name,
skipExisting,
client,
dataDir,
log,
kbnClient,
}: {
name: string;
skipExisting: boolean;
client: Client;
dataDir: string;
log: ToolingLog;
kbnClient: KbnClient;
}) {
const inputDir = resolve(dataDir, name);
const stats = createStats(name, log);
const files = prioritizeMappings(await readDirectory(inputDir));
Expand All @@ -64,20 +81,20 @@ export async function loadAction({ name, skipExisting, client, dataDir, log, kbn
{ objectMode: true }
);

const progress = new Progress('load progress');
const progress = new Progress();
progress.activate(log);

await createPromiseFromStreams([
recordStream,
createCreateIndexStream({ client, stats, skipExisting, log, kibanaPluginIds }),
createCreateIndexStream({ client, stats, skipExisting, log }),
createIndexDocRecordsStream(client, stats, progress),
]);

progress.deactivate();
const result = stats.toJSON();

for (const [index, { docs }] of Object.entries(result)) {
if (!docs && docs.indexed > 0) {
if (docs && docs.indexed > 0) {
log.info('[%s] Indexed %d docs into %j', name, docs.indexed, index);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
*/

import { resolve, dirname, relative } from 'path';

import { stat, rename, createReadStream, createWriteStream } from 'fs';

import { Readable, Writable } from 'stream';
import { fromNode } from 'bluebird';
import { ToolingLog } from '@kbn/dev-utils';

import { createPromiseFromStreams } from '../../legacy/utils';

import {
prioritizeMappings,
readDirectory,
Expand All @@ -33,12 +32,20 @@ import {
createFormatArchiveStreams,
} from '../lib';

async function isDirectory(path) {
async function isDirectory(path: string): Promise<boolean> {
const stats = await fromNode(cb => stat(path, cb));
return stats.isDirectory();
}

export async function rebuildAllAction({ dataDir, log, rootDir = dataDir }) {
export async function rebuildAllAction({
dataDir,
log,
rootDir = dataDir,
}: {
dataDir: string;
log: ToolingLog;
rootDir?: string;
}) {
const childNames = prioritizeMappings(await readDirectory(dataDir));
for (const childName of childNames) {
const childPath = resolve(dataDir, childName);
Expand All @@ -58,11 +65,11 @@ export async function rebuildAllAction({ dataDir, log, rootDir = dataDir }) {
const tempFile = childPath + (gzip ? '.rebuilding.gz' : '.rebuilding');

await createPromiseFromStreams([
createReadStream(childPath),
createReadStream(childPath) as Readable,
...createParseArchiveStreams({ gzip }),
...createFormatArchiveStreams({ gzip }),
createWriteStream(tempFile),
]);
] as [Readable, ...Writable[]]);

await fromNode(cb => rename(tempFile, childPath, cb));
log.info(`${archiveName} Rebuilt ${childName}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import { resolve } from 'path';
import { createWriteStream, mkdirSync } from 'fs';
import { Readable, Writable } from 'stream';
import { Client } from 'elasticsearch';
import { ToolingLog } from '@kbn/dev-utils';

import { createListStream, createPromiseFromStreams } from '../../legacy/utils';

import {
createStats,
createGenerateIndexRecordsStream,
Expand All @@ -30,7 +32,21 @@ import {
Progress,
} from '../lib';

export async function saveAction({ name, indices, client, dataDir, log, raw }) {
export async function saveAction({
name,
indices,
client,
dataDir,
log,
raw,
}: {
name: string;
indices: string | string[];
client: Client;
dataDir: string;
log: ToolingLog;
raw: boolean;
}) {
const outputDir = resolve(dataDir, name);
const stats = createStats(name, log);

Expand All @@ -48,15 +64,15 @@ export async function saveAction({ name, indices, client, dataDir, log, raw }) {
createGenerateIndexRecordsStream(client, stats),
...createFormatArchiveStreams(),
createWriteStream(resolve(outputDir, 'mappings.json')),
]),
] as [Readable, ...Writable[]]),

// export all documents from matching indexes into data.json.gz
createPromiseFromStreams([
createListStream(indices),
createGenerateDocRecordsStream(client, stats, progress),
...createFormatArchiveStreams({ gzip: !raw }),
createWriteStream(resolve(outputDir, `data.json${raw ? '' : '.gz'}`)),
]),
] as [Readable, ...Writable[]]),
]);

progress.deactivate();
Expand Down
Loading

0 comments on commit a040f0b

Please sign in to comment.