Skip to content

Commit a4fe581

Browse files
committed
[ML] change import endpoint call to fileupload plugin, update file analyzer endpoint
1 parent ea7c78d commit a4fe581

5 files changed

Lines changed: 90 additions & 99 deletions

File tree

x-pack/legacy/plugins/ml/public/application/services/ml_api_service/datavisualizer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import chrome from 'ui/chrome';
88

99
import { http } from '../http_service';
1010

11-
const basePath = chrome.addBasePath('/api/ml');
11+
const basePath = chrome.addBasePath('/api');
1212

1313
export const fileDatavisualizer = {
1414
analyzeFile(obj, params = {}) {
@@ -22,7 +22,7 @@ export const fileDatavisualizer = {
2222
}
2323
}
2424
return http({
25-
url: `${basePath}/file_data_visualizer/analyze_file${paramString}`,
25+
url: `${basePath}/ml/file_data_visualizer/analyze_file${paramString}`,
2626
method: 'POST',
2727
data: obj,
2828
});
@@ -33,7 +33,7 @@ export const fileDatavisualizer = {
3333
const { index, data, settings, mappings, ingestPipeline } = obj;
3434

3535
return http({
36-
url: `${basePath}/file_data_visualizer/import${paramString}`,
36+
url: `${basePath}/fileupload/import${paramString}`,
3737
method: 'POST',
3838
data: {
3939
index,

x-pack/legacy/plugins/ml/server/models/file_data_visualizer/file_data_visualizer.js renamed to x-pack/legacy/plugins/ml/server/models/file_data_visualizer/file_data_visualizer.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,37 @@
66

77
import Boom from 'boom';
88
import fs from 'fs';
9+
import { RequestHandlerContext } from 'kibana/server';
910
import os from 'os';
10-
const util = require('util');
11-
// const readFile = util.promisify(fs.readFile);
11+
import util from 'util';
12+
1213
const readdir = util.promisify(fs.readdir);
1314
const writeFile = util.promisify(fs.writeFile);
1415

15-
export function fileDataVisualizerProvider(callWithRequest) {
16-
async function analyzeFile(data, overrides) {
16+
export interface InputData {
17+
[key: string]: any;
18+
}
19+
20+
export interface InputOverrides {
21+
[key: string]: string;
22+
}
23+
24+
export type FormattedOverrides = InputOverrides & {
25+
column_names: string[];
26+
has_header_row: boolean;
27+
should_trim_fields: boolean;
28+
};
29+
30+
export function fileDataVisualizerProvider(context: RequestHandlerContext) {
31+
async function analyzeFile(data: any, overrides: any) {
1732
let cached = false;
1833
let results = [];
1934

2035
try {
21-
results = await callWithRequest('ml.fileStructure', { body: data, ...overrides });
36+
results = await context.ml!.mlClient.callAsCurrentUser('ml.fileStructure', {
37+
body: data,
38+
...overrides,
39+
});
2240
if (false) {
2341
// disabling caching for now
2442
cached = await cacheData(data);
@@ -37,7 +55,7 @@ export function fileDataVisualizerProvider(callWithRequest) {
3755
};
3856
}
3957

40-
async function cacheData(data) {
58+
async function cacheData(data: InputData) {
4159
const outputPath = `${os.tmpdir()}/kibana-ml`;
4260
const tempFile = 'es-ml-tempFile';
4361
const tempFilePath = `${outputPath}/${tempFile}`;
@@ -52,13 +70,13 @@ export function fileDataVisualizerProvider(callWithRequest) {
5270
}
5371
}
5472

55-
function createOutputDir(dir) {
73+
function createOutputDir(dir: string) {
5674
if (fs.existsSync(dir) === false) {
5775
fs.mkdirSync(dir);
5876
}
5977
}
6078

61-
async function deleteOutputFiles(outputPath) {
79+
async function deleteOutputFiles(outputPath: string) {
6280
const files = await readdir(outputPath);
6381
files.forEach(f => {
6482
fs.unlinkSync(`${outputPath}/${f}`);
@@ -70,28 +88,30 @@ export function fileDataVisualizerProvider(callWithRequest) {
7088
};
7189
}
7290

73-
function formatOverrides(overrides) {
91+
function formatOverrides(overrides: InputOverrides) {
7492
let hasOverrides = false;
7593

76-
const reducedOverrides = Object.keys(overrides).reduce((p, c) => {
77-
if (overrides[c] !== '') {
78-
p[c] = overrides[c];
79-
hasOverrides = true;
80-
}
81-
return p;
82-
}, {});
94+
const reducedOverrides: FormattedOverrides = Object.keys(overrides).reduce((acc, overrideKey) => {
95+
const overrideValue: string = overrides[overrideKey];
96+
if (overrideValue !== '') {
97+
acc[overrideKey] = overrideValue;
8398

84-
if (reducedOverrides.column_names !== undefined) {
85-
reducedOverrides.column_names = reducedOverrides.column_names.split(',');
86-
}
99+
if (overrideKey === 'column_names') {
100+
acc.column_names = overrideValue.split(',');
101+
}
87102

88-
if (reducedOverrides.has_header_row !== undefined) {
89-
reducedOverrides.has_header_row = reducedOverrides.has_header_row === 'true';
90-
}
103+
if (overrideKey === 'has_header_row') {
104+
acc.has_header_row = overrideValue === 'true';
105+
}
91106

92-
if (reducedOverrides.should_trim_fields !== undefined) {
93-
reducedOverrides.should_trim_fields = reducedOverrides.should_trim_fields === 'true';
94-
}
107+
if (overrideKey === 'should_trim_fields') {
108+
acc.should_trim_fields = overrideValue === 'true';
109+
}
110+
111+
hasOverrides = true;
112+
}
113+
return acc;
114+
}, {} as FormattedOverrides);
95115

96116
return {
97117
reducedOverrides,

x-pack/legacy/plugins/ml/server/models/file_data_visualizer/index.js renamed to x-pack/legacy/plugins/ml/server/models/file_data_visualizer/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
export { fileDataVisualizerProvider } from './file_data_visualizer';
8-
export { importDataProvider } from './import_data';
7+
export { fileDataVisualizerProvider, InputOverrides, InputData } from './file_data_visualizer';

x-pack/legacy/plugins/ml/server/routes/file_data_visualizer.js

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { schema } from '@kbn/config-schema';
8+
import { RequestHandlerContext } from 'kibana/server';
9+
import { wrapError } from '../client/error_wrapper';
10+
import {
11+
InputOverrides,
12+
InputData,
13+
fileDataVisualizerProvider,
14+
} from '../models/file_data_visualizer';
15+
16+
import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
17+
import { RouteInitialization } from '../new_platform/plugin';
18+
19+
function analyzeFiles(context: RequestHandlerContext, data: InputData, overrides: InputOverrides) {
20+
const { analyzeFile } = fileDataVisualizerProvider(context);
21+
return analyzeFile(data, overrides);
22+
}
23+
24+
export function fileDataVisualizerRoutes({ router, xpackMainPlugin }: RouteInitialization) {
25+
router.post(
26+
{
27+
path: '/api/ml/file_data_visualizer/analyze_file',
28+
validate: {
29+
body: schema.any(),
30+
},
31+
},
32+
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
33+
try {
34+
const result = await analyzeFiles(context, request.body, request.query);
35+
return response.ok({ body: result });
36+
} catch (e) {
37+
return response.customError(wrapError(e));
38+
}
39+
})
40+
);
41+
}

0 commit comments

Comments
 (0)