Skip to content

Commit

Permalink
[ML] remove caching code, address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Feb 6, 2020
1 parent 8c77b27 commit e5e6098
Showing 1 changed file with 5 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
*/

import Boom from 'boom';
import fs from 'fs';
import { RequestHandlerContext } from 'kibana/server';
import os from 'os';
import util from 'util';

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

export interface InputData {
[key: string]: any;
Expand All @@ -28,10 +22,6 @@ export type FormattedOverrides = InputOverrides & {
};

export interface AnalysisResult {
/**
* Indicates if the result has been cached
*/
cached: boolean;
results: {
charset: string;
has_header_row: boolean;
Expand All @@ -57,22 +47,18 @@ export interface AnalysisResult {
num_lines_analyzed: number;
column_names: string[];
};
overrides?: FormattedOverrides;
}

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

try {
results = await context.ml!.mlClient.callAsCurrentUser('ml.fileStructure', {
body: data,
...overrides,
});
if (false) {
// disabling caching for now
cached = await cacheData(data);
}
} catch (error) {
const err = error.message !== undefined ? error.message : error;
throw Boom.badRequest(err);
Expand All @@ -82,39 +68,10 @@ export function fileDataVisualizerProvider(context: RequestHandlerContext) {

return {
...(hasOverrides && { overrides: reducedOverrides }),
cached,
results,
};
}

async function cacheData(data: InputData) {
const outputPath = `${os.tmpdir()}/kibana-ml`;
const tempFile = 'es-ml-tempFile';
const tempFilePath = `${outputPath}/${tempFile}`;

try {
createOutputDir(outputPath);
await deleteOutputFiles(outputPath);
await writeFile(tempFilePath, data);
return true;
} catch (error) {
return false;
}
}

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

async function deleteOutputFiles(outputPath: string) {
const files = await readdir(outputPath);
files.forEach(f => {
fs.unlinkSync(`${outputPath}/${f}`);
});
}

return {
analyzeFile,
};
Expand All @@ -126,18 +83,14 @@ function formatOverrides(overrides: InputOverrides) {
const reducedOverrides: FormattedOverrides = Object.keys(overrides).reduce((acc, overrideKey) => {
const overrideValue: string = overrides[overrideKey];
if (overrideValue !== '') {
acc[overrideKey] = overrideValue;

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

if (overrideKey === 'has_header_row') {
} else if (overrideKey === 'has_header_row') {
acc.has_header_row = overrideValue === 'true';
}

if (overrideKey === 'should_trim_fields') {
} else if (overrideKey === 'should_trim_fields') {
acc.should_trim_fields = overrideValue === 'true';
} else {
acc[overrideKey] = overrideValue;
}

hasOverrides = true;
Expand Down

0 comments on commit e5e6098

Please sign in to comment.