-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[ml] migrate file_data_visualizer/import route to file_upload plugin #89640
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
Changes from 20 commits
f19cece
005170b
be1e122
1b12e4c
179e7ff
1da2023
89bbe88
4d9680c
d1073bf
044ec4f
de0ce16
8e1c90e
dd57ecd
7edbade
1079c41
ed0e477
3bc38e2
d7c3811
3b87cbf
baeb6db
0b31512
26e05b2
0473f5a
c4d050e
6d3bf3a
82b4a1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; |
| 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[]; | ||
| } |
| 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'], | ||
| }; |
| 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"] | ||
| } |
| 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 |
|---|---|---|
| @@ -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(); |
| 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) {} | ||
| } |
| 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)); | ||
| } | ||
| } | ||
| ); | ||
| } |
| 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.string(), | ||
| pipeline: schema.any(), | ||
| }), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ import { getInternalRepository } from './internal_repository'; | |
| export const TELEMETRY_DOC_ID = 'ml-telemetry'; | ||
|
|
||
| export interface Telemetry { | ||
| file_data_visualizer: { | ||
| file_upload: { | ||
| index_creation_count: number; | ||
| }; | ||
| } | ||
|
|
@@ -23,7 +23,7 @@ export interface TelemetrySavedObject { | |
|
|
||
| export function initTelemetry(): Telemetry { | ||
| return { | ||
| file_data_visualizer: { | ||
| file_upload: { | ||
| index_creation_count: 0, | ||
| }, | ||
| }; | ||
|
|
@@ -74,8 +74,8 @@ export async function updateTelemetry(internalRepo?: ISavedObjectsRepository) { | |
|
|
||
| function incrementCounts(telemetry: Telemetry) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not using Recently we've added support to increment multiple fields with custom increments:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Bamieh This PR is migrating code from ML to a separate plugin, trying to change as little as possible. I can create an issue to track using SavedObjectsRepository.incrementCounter but would like to leave any refactoring out of this initial file_upload creation pull request. |
||
| 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, | ||
| }, | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.