Skip to content

Commit

Permalink
[ml] migrate file_data_visualizer/import route to file_upload plugin (e…
Browse files Browse the repository at this point in the history
…lastic#89640)

* migrate file_upload plugin to maps_file_upload

* update plugins list

* migrate ml import endpoint

* migrate ml telemetry to file_upload plugin

* add fileUpload plugin to ml

* add TS project

* update ML to use file_upload endpoint

* move types to file_upload plugin

* ignore error

* clean up

* i18n clean-up

* remove schemas from ml

* remove usageCollection from ml

* node scripts/build_plugin_list_docs

* update telemety collector

* revert changes to ingestPipeline schema

* change name of TELEMETRY_DOC_ID to unique value

* remove ImportFile from ml/server/routes/apidoc.json

* fix typo in x=pack/tsconfig.json

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
# Conflicts:
#	src/plugins/telemetry/schema/oss_plugins.json
#	x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json
  • Loading branch information
nreese committed Feb 2, 2021
1 parent 3e2c484 commit 12e8113
Show file tree
Hide file tree
Showing 41 changed files with 324 additions and 214 deletions.
4 changes: 4 additions & 0 deletions docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ and actions.
|The features plugin enhance Kibana with a per-feature privilege system.
|{kib-repo}blob/{branch}/x-pack/plugins/file_upload[fileUpload]
|WARNING: Missing README.
|{kib-repo}blob/{branch}/x-pack/plugins/fleet/README.md[fleet]
|Fleet needs to have Elasticsearch API keys enabled, and also to have TLS enabled on kibana, (if you want to run Kibana without TLS you can provide the following config flag --xpack.fleet.agents.tlsCheckDisabled=false)
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/file_upload/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './constants';
export * from './types';
54 changes: 54 additions & 0 deletions x-pack/plugins/file_upload/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*/

export interface ImportResponse {
success: boolean;
id: string;
index?: string;
pipelineId?: string;
docCount: number;
failures: ImportFailure[];
error?: any;
ingestError?: boolean;
}

export interface ImportFailure {
item: number;
reason: string;
doc: ImportDoc;
}

export interface Doc {
message: string;
}

export type ImportDoc = Doc | string;

export interface Settings {
pipeline?: string;
index: string;
body: any[];
[key: string]: any;
}

export interface Mappings {
_meta?: {
created_by: string;
};
properties: {
[key: string]: any;
};
}

export interface IngestPipelineWrapper {
id: string;
pipeline: IngestPipeline;
}

export interface IngestPipeline {
description: string;
processors: any[];
}
11 changes: 11 additions & 0 deletions x-pack/plugins/file_upload/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../..',
roots: ['<rootDir>/x-pack/plugins/file_upload'],
};
8 changes: 8 additions & 0 deletions x-pack/plugins/file_upload/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "fileUpload",
"version": "8.0.0",
"kibanaVersion": "kibana",
"server": true,
"ui": false,
"requiredPlugins": ["usageCollection"]
}
23 changes: 23 additions & 0 deletions x-pack/plugins/file_upload/server/error_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { boomify, isBoom } from '@hapi/boom';
import { ResponseError, CustomHttpResponseOptions } from 'kibana/server';

export function wrapError(error: any): CustomHttpResponseOptions<ResponseError> {
const boom = isBoom(error)
? error
: boomify(error, { statusCode: error.status ?? error.statusCode });
const statusCode = boom.output.statusCode;
return {
body: {
message: boom,
...(statusCode !== 500 && error.body ? { attributes: { body: error.body } } : {}),
},
headers: boom.output.headers as { [key: string]: string },
statusCode,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
*/

import { IScopedClusterClient } from 'kibana/server';
import { INDEX_META_DATA_CREATED_BY } from '../../../common/constants/file_datavisualizer';
import { INDEX_META_DATA_CREATED_BY } from '../common/constants';
import {
ImportResponse,
ImportFailure,
Settings,
Mappings,
IngestPipelineWrapper,
} from '../../../common/types/file_datavisualizer';
import { InputData } from './file_data_visualizer';
} from '../common';

export type InputData = any[];

export function importDataProvider({ asCurrentUser }: IScopedClusterClient) {
async function importData(
id: string,
id: string | undefined,
index: string,
settings: Settings,
mappings: Mappings,
Expand Down Expand Up @@ -77,7 +78,7 @@ export function importDataProvider({ asCurrentUser }: IScopedClusterClient) {
} catch (error) {
return {
success: false,
id,
id: id!,
index: createdIndex,
pipelineId: createdPipelineId,
error: error.body !== undefined ? error.body : error,
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/file_upload/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 { FileUploadPlugin } from './plugin';

export const plugin = () => new FileUploadPlugin();
24 changes: 24 additions & 0 deletions x-pack/plugins/file_upload/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { CoreSetup, CoreStart, Plugin } from 'src/core/server';
import { fileUploadRoutes } from './routes';
import { initFileUploadTelemetry } from './telemetry';
import { UsageCollectionSetup } from '../../../../src/plugins/usage_collection/server';

interface SetupDeps {
usageCollection: UsageCollectionSetup;
}

export class FileUploadPlugin implements Plugin {
async setup(coreSetup: CoreSetup, plugins: SetupDeps) {
fileUploadRoutes(coreSetup.http.createRouter());

initFileUploadTelemetry(coreSetup, plugins.usageCollection);
}

start(core: CoreStart) {}
}
85 changes: 85 additions & 0 deletions x-pack/plugins/file_upload/server/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 { IRouter, IScopedClusterClient } from 'kibana/server';
import { MAX_FILE_SIZE_BYTES, IngestPipelineWrapper, Mappings, Settings } from '../common';
import { wrapError } from './error_wrapper';
import { InputData, importDataProvider } from './import_data';

import { updateTelemetry } from './telemetry';
import { importFileBodySchema, importFileQuerySchema } from './schemas';

function importData(
client: IScopedClusterClient,
id: string | undefined,
index: string,
settings: Settings,
mappings: Mappings,
ingestPipeline: IngestPipelineWrapper,
data: InputData
) {
const { importData: importDataFunc } = importDataProvider(client);
return importDataFunc(id, index, settings, mappings, ingestPipeline, data);
}

/**
* Routes for the file upload.
*/
export function fileUploadRoutes(router: IRouter) {
/**
* @apiGroup FileDataVisualizer
*
* @api {post} /api/file_upload/import Import file data
* @apiName ImportFile
* @apiDescription Imports file data into elasticsearch index.
*
* @apiSchema (query) importFileQuerySchema
* @apiSchema (body) importFileBodySchema
*/
router.post(
{
path: '/api/file_upload/import',
validate: {
query: importFileQuerySchema,
body: importFileBodySchema,
},
options: {
body: {
accepts: ['application/json'],
maxBytes: MAX_FILE_SIZE_BYTES,
},
tags: ['access:ml:canFindFileStructure'],
},
},
async (context, request, response) => {
try {
const { id } = request.query;
const { index, data, settings, mappings, ingestPipeline } = request.body;

// `id` being `undefined` tells us that this is a new import due to create a new index.
// follow-up import calls to just add additional data will include the `id` of the created
// index, we'll ignore those and don't increment the counter.
if (id === undefined) {
await updateTelemetry();
}

const result = await importData(
context.core.elasticsearch.client,
id,
index,
settings,
mappings,
// @ts-expect-error
ingestPipeline,
data
);
return response.ok({ body: result });
} catch (e) {
return response.customError(wrapError(e));
}
}
);
}
24 changes: 24 additions & 0 deletions x-pack/plugins/file_upload/server/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { schema } from '@kbn/config-schema';

export const importFileQuerySchema = schema.object({
id: schema.maybe(schema.string()),
});

export const importFileBodySchema = schema.object({
index: schema.string(),
data: schema.arrayOf(schema.any()),
settings: schema.maybe(schema.any()),
/** Mappings */
mappings: schema.any(),
/** Ingest pipeline definition */
ingestPipeline: schema.object({
id: schema.maybe(schema.string()),
pipeline: schema.maybe(schema.any()),
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { initMlTelemetry } from './ml_usage_collector';
export { initFileUploadTelemetry } from './usage_collector';
export { updateTelemetry } from './telemetry';
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import { SavedObjectsType } from 'src/core/server';
import { TELEMETRY_DOC_ID } from './telemetry';

export const mlTelemetryMappingsType: SavedObjectsType = {
export const telemetryMappingsType: SavedObjectsType = {
name: TELEMETRY_DOC_ID,
hidden: false,
namespaceType: 'agnostic',
mappings: {
properties: {
file_data_visualizer: {
file_upload: {
properties: {
index_creation_count: {
type: 'long',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('ml plugin telemetry', () => {
it('should update existing telemetry', async () => {
const internalRepo = mockInit({
attributes: {
file_data_visualizer: {
file_upload: {
index_creation_count: 2,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { ISavedObjectsRepository } from 'kibana/server';

import { getInternalRepository } from './internal_repository';

export const TELEMETRY_DOC_ID = 'ml-telemetry';
export const TELEMETRY_DOC_ID = 'file-upload-usage-collection-telemetry';

export interface Telemetry {
file_data_visualizer: {
file_upload: {
index_creation_count: number;
};
}
Expand All @@ -23,7 +23,7 @@ export interface TelemetrySavedObject {

export function initTelemetry(): Telemetry {
return {
file_data_visualizer: {
file_upload: {
index_creation_count: 0,
},
};
Expand Down Expand Up @@ -74,8 +74,8 @@ export async function updateTelemetry(internalRepo?: ISavedObjectsRepository) {

function incrementCounts(telemetry: Telemetry) {
return {
file_data_visualizer: {
index_creation_count: telemetry.file_data_visualizer.index_creation_count + 1,
file_upload: {
index_creation_count: telemetry.file_upload.index_creation_count + 1,
},
};
}
Loading

0 comments on commit 12e8113

Please sign in to comment.