66
77import Boom from 'boom' ;
88import fs from 'fs' ;
9+ import { RequestHandlerContext } from 'kibana/server' ;
910import os from 'os' ;
10- const util = require ( 'util' ) ;
11- // const readFile = util.promisify(fs.readFile);
11+ import util from 'util' ;
12+
1213const readdir = util . promisify ( fs . readdir ) ;
1314const 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,
0 commit comments