Skip to content

Commit

Permalink
[ML] change import endpoint call to fileupload plugin, update file an…
Browse files Browse the repository at this point in the history
…alyzer endpoint
  • Loading branch information
darnautov committed Feb 6, 2020
1 parent ea7c78d commit a4fe581
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import chrome from 'ui/chrome';

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

const basePath = chrome.addBasePath('/api/ml');
const basePath = chrome.addBasePath('/api');

export const fileDatavisualizer = {
analyzeFile(obj, params = {}) {
Expand All @@ -22,7 +22,7 @@ export const fileDatavisualizer = {
}
}
return http({
url: `${basePath}/file_data_visualizer/analyze_file${paramString}`,
url: `${basePath}/ml/file_data_visualizer/analyze_file${paramString}`,
method: 'POST',
data: obj,
});
Expand All @@ -33,7 +33,7 @@ export const fileDatavisualizer = {
const { index, data, settings, mappings, ingestPipeline } = obj;

return http({
url: `${basePath}/file_data_visualizer/import${paramString}`,
url: `${basePath}/fileupload/import${paramString}`,
method: 'POST',
data: {
index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@

import Boom from 'boom';
import fs from 'fs';
import { RequestHandlerContext } from 'kibana/server';
import os from 'os';
const util = require('util');
// const readFile = util.promisify(fs.readFile);
import util from 'util';

const readdir = util.promisify(fs.readdir);
const writeFile = util.promisify(fs.writeFile);

export function fileDataVisualizerProvider(callWithRequest) {
async function analyzeFile(data, overrides) {
export interface InputData {
[key: string]: any;
}

export interface InputOverrides {
[key: string]: string;
}

export type FormattedOverrides = InputOverrides & {
column_names: string[];
has_header_row: boolean;
should_trim_fields: boolean;
};

export function fileDataVisualizerProvider(context: RequestHandlerContext) {
async function analyzeFile(data: any, overrides: any) {
let cached = false;
let results = [];

try {
results = await callWithRequest('ml.fileStructure', { body: data, ...overrides });
results = await context.ml!.mlClient.callAsCurrentUser('ml.fileStructure', {
body: data,
...overrides,
});
if (false) {
// disabling caching for now
cached = await cacheData(data);
Expand All @@ -37,7 +55,7 @@ export function fileDataVisualizerProvider(callWithRequest) {
};
}

async function cacheData(data) {
async function cacheData(data: InputData) {
const outputPath = `${os.tmpdir()}/kibana-ml`;
const tempFile = 'es-ml-tempFile';
const tempFilePath = `${outputPath}/${tempFile}`;
Expand All @@ -52,13 +70,13 @@ export function fileDataVisualizerProvider(callWithRequest) {
}
}

function createOutputDir(dir) {
function createOutputDir(dir: string) {
if (fs.existsSync(dir) === false) {
fs.mkdirSync(dir);
}
}

async function deleteOutputFiles(outputPath) {
async function deleteOutputFiles(outputPath: string) {
const files = await readdir(outputPath);
files.forEach(f => {
fs.unlinkSync(`${outputPath}/${f}`);
Expand All @@ -70,28 +88,30 @@ export function fileDataVisualizerProvider(callWithRequest) {
};
}

function formatOverrides(overrides) {
function formatOverrides(overrides: InputOverrides) {
let hasOverrides = false;

const reducedOverrides = Object.keys(overrides).reduce((p, c) => {
if (overrides[c] !== '') {
p[c] = overrides[c];
hasOverrides = true;
}
return p;
}, {});
const reducedOverrides: FormattedOverrides = Object.keys(overrides).reduce((acc, overrideKey) => {
const overrideValue: string = overrides[overrideKey];
if (overrideValue !== '') {
acc[overrideKey] = overrideValue;

if (reducedOverrides.column_names !== undefined) {
reducedOverrides.column_names = reducedOverrides.column_names.split(',');
}
if (overrideKey === 'column_names') {
acc.column_names = overrideValue.split(',');
}

if (reducedOverrides.has_header_row !== undefined) {
reducedOverrides.has_header_row = reducedOverrides.has_header_row === 'true';
}
if (overrideKey === 'has_header_row') {
acc.has_header_row = overrideValue === 'true';
}

if (reducedOverrides.should_trim_fields !== undefined) {
reducedOverrides.should_trim_fields = reducedOverrides.should_trim_fields === 'true';
}
if (overrideKey === 'should_trim_fields') {
acc.should_trim_fields = overrideValue === 'true';
}

hasOverrides = true;
}
return acc;
}, {} as FormattedOverrides);

return {
reducedOverrides,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { fileDataVisualizerProvider } from './file_data_visualizer';
export { importDataProvider } from './import_data';
export { fileDataVisualizerProvider, InputOverrides, InputData } from './file_data_visualizer';
69 changes: 0 additions & 69 deletions x-pack/legacy/plugins/ml/server/routes/file_data_visualizer.js

This file was deleted.

41 changes: 41 additions & 0 deletions x-pack/legacy/plugins/ml/server/routes/file_data_visualizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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';
import { RequestHandlerContext } from 'kibana/server';
import { wrapError } from '../client/error_wrapper';
import {
InputOverrides,
InputData,
fileDataVisualizerProvider,
} from '../models/file_data_visualizer';

import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
import { RouteInitialization } from '../new_platform/plugin';

function analyzeFiles(context: RequestHandlerContext, data: InputData, overrides: InputOverrides) {
const { analyzeFile } = fileDataVisualizerProvider(context);
return analyzeFile(data, overrides);
}

export function fileDataVisualizerRoutes({ router, xpackMainPlugin }: RouteInitialization) {
router.post(
{
path: '/api/ml/file_data_visualizer/analyze_file',
validate: {
body: schema.any(),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const result = await analyzeFiles(context, request.body, request.query);
return response.ok({ body: result });
} catch (e) {
return response.customError(wrapError(e));
}
})
);
}

0 comments on commit a4fe581

Please sign in to comment.