Skip to content

Commit

Permalink
version avec acces es par routes server
Browse files Browse the repository at this point in the history
  • Loading branch information
bjacomy committed Nov 30, 2020
1 parent 56789e3 commit c95f9a2
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 11 deletions.
2 changes: 1 addition & 1 deletion kibana.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "transformVis",
"version": "7.9.1",
"server": false,
"server": true,
"ui": true,
"requiredPlugins": ["navigation","data","kibanaReact","kibanaUtils", "visualizations", "expressions"],
"optionalPlugins": [],
Expand Down
33 changes: 24 additions & 9 deletions public/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { TransformVisData } from './types';

import { getData} from './services';
import { DataPublicPluginStart } from '../../../src/plugins/data/public';
import axios from 'axios'
import value from '@elastic/eui/dist/eui_theme_*.json';

const babelTransform = (code: string) => {
return transform(code, {
Expand All @@ -36,8 +38,8 @@ export function getTransformRequestHandler({
query: Query | null;
visParams: VisParams;
}): Promise<TransformVisData> => {
const data : DataPublicPluginStart = getData();
const es = data.search.__LEGACY.esClient;
//const data : DataPublicPluginStart = getData();
//const es = data.search.__LEGACY.esClient;
const settings = uiSettings;
const options : InjectedMetadataSetup["getInjectedVar"]= (name= 'transformVisOptions', defaultValue= 'undefined')=> {};
const _timeRange: TimeRange = timeRange || settings.get('timepicker:timeDefaults');
Expand All @@ -60,7 +62,7 @@ export function getTransformRequestHandler({
bindme.timefilter = timeFilter;
bindme.timeRange = _timeRange;
bindme.buildEsQuery = esQuery.buildEsQuery;
bindme.es = es;
//bindme.es = es;
bindme.response = {};

const parseMultiqueryDsl = (_multiquerydsltext: string | undefined): Record<string, any> => {
Expand Down Expand Up @@ -112,21 +114,34 @@ export function getTransformRequestHandler({
}
delete body.previousContextSource;
}
return es
const requestSearch = {
body: body
};
try{

const headers = {
'Content-Type': 'application/json',
'kbn-xsrf': 'reporting'
}

response = await axios.post(`../api/transform_vis/${index}/_search`,requestSearch,{headers: headers});
/* return es
.search({
index,
body,
})
.then(function (response: { error: any; }) {
.then(function (response: { error: any; }) {*/
// @ts-ignore
if (response.error) throw response.error;
response = response.data.data;
//if (response.status) throw response.error;
if (queryName === '_single_') {
bindme.response = Object.assign(bindme.response, response);
} else {
bindme.response = Object.assign(bindme.response, { [queryName]: response });
}
})
.catch((error: any) => logError('Elasticsearch Query Error', [ `"${queryName}" query:\nGET ${index}/_search\n${JSON.stringify(body, null, 2)}`, error ]));
}
catch(error: any) { logError('Elasticsearch Query Error', [ `"${queryName}" query:\nGET ${index}/_search\n${JSON.stringify(body, null, 2)}`, error ])};
return response;
};

const evalMeta = (response?: any) => {
Expand Down Expand Up @@ -160,7 +175,7 @@ export function getTransformRequestHandler({
return {
transform: Mustache.render(formula, { ...bindme, meta: awaitContext }),
meta: bindme.meta,
es,
//es,
context,
timeFilter,
timeRange,
Expand Down
2 changes: 1 addition & 1 deletion public/transform_vis_controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TransformVisComponent extends React.Component<TransformVisComponentProps>
},
size: [root.host.clientWidth, root.host.clientHeight],
},
es: this.props.es,
//es: this.props.es,
context: this.props.context,
timeRange: this.props.timeRange,
timefilter: this.props.data.query.timefilter.timefilter,
Expand Down
11 changes: 11 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PluginInitializerContext } from '../../../src/core/server';
import { TransformPlugin } from './plugin';

// This exports static code and TypeScript types,
// as well as, Kibana Platform `plugin()` initializer.

export function plugin(initializerContext: PluginInitializerContext) {
return new TransformPlugin(initializerContext);
}

export { TransformPluginSetup, TransformPluginStart } from './types';
38 changes: 38 additions & 0 deletions server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
PluginInitializerContext,
CoreSetup,
CoreStart,
Plugin,
Logger,
} from '../../../src/core/server';

import { TransformPluginSetup, TransformPluginStart } from './types';
import { defineRoutes } from './routes';
import { Server } from 'http';

export class TransformPlugin
implements Plugin<TransformPluginSetup, TransformPluginStart> {
private readonly logger: Logger;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
}

public setup(core: CoreSetup) {
this.logger.debug('transform-vis: Setup');
console.log('transform-vis: Setup');
const router = core.http.createRouter();
// Register server side APIs
defineRoutes(router);

return {};
}

public start(core: CoreStart) {
console.log('transform-vis: Start');
this.logger.debug('transform-vis: Started');
return {};
}

public stop() {}
}
211 changes: 211 additions & 0 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@

import { schema } from '@kbn/config-schema';
import { IRouter} from '../../../../src/core/server';

export function defineRoutes(router: IRouter) {
router.get(
{
path: '/api/transform_vis/example',
validate: false,
},
async (context, request, response) => {
return response.ok({
body: {
time: new Date().toISOString(),
},
});
}
);
router.get(
{
path: '/api/transform_vis/cluster/_health',
validate: false,
},
async (context, request, response) => {
const data = await context.core.elasticsearch.legacy.client.callAsCurrentUser('cluster.health')
//console.log('****************** ', data)
return response.ok({
body: {
time: Object.keys(data.status)
},
});
}
);
router.get(
{
path: '/api/transform_vis/cat/indices',
validate: false,
},
async (context, request, response) => {
const data = await context.core.elasticsearch.legacy.client.callAsCurrentUser('cat.indices')
//console.log('****************** ', data)
return response.ok({
body: {
data: data,
time: new Date().toISOString(),
},
});
}
);

//Create a mapping for a selected index
router.post(
{
path: '/api/transform_vis/{index}/_search',
validate: {
body: schema.any(),
params: schema.any(),
//query: schema.any()
}
},
async (context, request, response) => {
try {
//console.log("bjacomy*************** ",request.params.index);
//console.log("bjacomy*************** ",request.body);
const data = await await context.core.elasticsearch.legacy.client.callAsCurrentUser('search', {
index: request.params.index,
body: request.body.body
});
//console.log('****************** ', data)
return response.ok({
body: {
data,
time: new Date().toISOString(),
},
});
} catch (error) {
console.log('****************** ', error);
return response.ok({
body: {
message: error
}
});
}
});

router.post(
{
path: '/api/transform_vis/create/indice/{index}',
//validate: false
validate: {
//body: schema.any(),
params: schema.any()
}
},


async (context, request, response) => {
try {
//console.log('****************** create indice',request);
const data = await context.core.elasticsearch.legacy.client.callAsCurrentUser('indices.create',{index: request.params.index});
//console.log('****************** ', data)
return response.ok({
body: {
data: data,
time: new Date().toISOString(),
},
});
} catch (error) {
//console.log('****************** ', error);
return response.ok({
body: {
message: error,
}
});
}
}
);

//Create a mapping for a selected index
router.post(
{
path: '/api/transform_vis/{index}/_mapping',
validate: {
body: schema.any(),
params: schema.any()
}
},
async (context, request, response) => {
try {
//console.log('****************** create indice mapping',request);
const data = await context.core.elasticsearch.legacy.client.callAsCurrentUser('indices.putMapping',{index: request.params.index, body: {"properties":request.body.body}});
//console.log('****************** ', data)
return response.ok({
body: {
data,
time: new Date().toISOString(),
},
});
} catch (error) {
//console.log('****************** ', error);
return response.ok({
body: {
message: "error",
}
});
}
});

//Create a mapping for a selected index
router.post(
{
path: '/api/transform_vis/{index}/_bulk',
validate: {
body: schema.any(),
params: schema.any(),
query: schema.any()
}
},
async (context, request, response) => {
try {
//console.log('****************** bulk indice',request);
const pipeline = request.query.pipeline || false;
const data = await await context.core.elasticsearch.legacy.client.callAsCurrentUser('bulk', {
...(pipeline && { pipeline }),
body: request.body
});
//console.log('****************** ', data)
return response.ok({
body: {
data,
time: new Date().toISOString(),
},
});
} catch (error) {
//console.log('****************** ', error);
return response.ok({
body: {
message: error
}
});
}

});

//checking index
router.post(
{
path: '/api/transform_vis/{index}/_exists',
validate: {
params: schema.any(),
}
},
async (context, request, response) => {
try {
const data = await context.core.elasticsearch.legacy.client.callAsCurrentUser('indices.get',{index: request.params.index});
return response.ok({
body: {
data: data,
time: new Date().toISOString(),
},
});
} catch (error) {
//console.log('****************** ', error);
return response.ok({
body: {
message: error,
}
});
}
});
}
4 changes: 4 additions & 0 deletions server/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TransformPluginSetup {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TransformPluginStart {}

0 comments on commit c95f9a2

Please sign in to comment.