Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move interfaces for interpreter types to OSS #37753

Merged
merged 19 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
* under the License.
*/

export const image = () => ({
name: 'image',
to: {
render: input => {
return {
type: 'render',
as: 'image',
value: input,
};
},
},
});
export {
Datatable,
DatatableColumn,
DatatableRow,
DatatableColumnType,
ExpressionImage,
Filter,
InterpreterErrorType,
isDatatable,
KibanaContext,
KibanaDatatable,
PointSeries,
PointSeriesColumns,
PointSeriesColumn,
PointSeriesColumnName,
Render,
Style,
} from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@
* under the License.
*/

export const boolean = () => ({
name: 'boolean',
import { ExpressionType } from '../../types';
import { Datatable } from './datatable';
import { Render } from './render';

const name = 'boolean';

export const boolean = (): ExpressionType<typeof name, boolean> => ({
name,
from: {
null: () => false,
number: n => Boolean(n),
string: s => Boolean(s),
},
to: {
render: value => {
render: (value): Render<{ text: string }> => {
const text = `${value}`;
return {
type: 'render',
as: 'text',
value: { text },
};
},
datatable: value => ({
datatable: (value): Datatable => ({
type: 'datatable',
columns: [{ name: 'value', type: 'boolean' }],
columns: [{ name: 'value', type: name }],
rows: [{ value }],
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,128 @@

import { map, pick, zipObject } from 'lodash';

export const datatable = () => ({
name: 'datatable',
validate: datatable => {
import { ExpressionType } from '../../types';
import { PointSeries } from './pointseries';
import { Render } from './render';

const name = 'datatable';

/**
* A Utility function that Typescript can use to determine if an object is a Datatable.
* @param datatable
*/
export const isDatatable = (datatable: any): datatable is Datatable =>
!!datatable && datatable.type === 'datatable';

/**
* This type represents the `type` of any `DatatableColumn` in a `Datatable`.
*/
export type DatatableColumnType = 'string' | 'number' | 'boolean' | 'date' | 'null';

/**
* This type represents a `DatatableRow` in a `Datatable`.
*/
export type DatatableRow = Record<string, any>;

/**
* This type represents the shape of a column in a `Datatable`.
*/
export interface DatatableColumn {
name: string;
type: DatatableColumnType;
}

/**
* A `Datatable` in Canvas is a unique structure that represents tabulated data.
*/
export interface Datatable {
type: typeof name;
columns: DatatableColumn[];
rows: DatatableRow[];
}

interface SerializedDatatable extends Datatable {
rows: string[][];
}

interface RenderedDatatable {
datatable: Datatable;
paginate: boolean;
perPage: number;
showHeader: boolean;
}

export const datatable = (): ExpressionType<typeof name, Datatable, SerializedDatatable> => ({
name,
validate: table => {
// TODO: Check columns types. Only string, boolean, number, date, allowed for now.
if (!datatable.columns) {
if (!table.columns) {
throw new Error('datatable must have a columns array, even if it is empty');
}

if (!datatable.rows) throw new Error('datatable must have a rows array, even if it is empty');
if (!table.rows) {
throw new Error('datatable must have a rows array, even if it is empty');
}
},
serialize: datatable => {
const { columns, rows } = datatable;
serialize: table => {
const { columns, rows } = table;
return {
...datatable,
...table,
rows: rows.map(row => {
return columns.map(column => row[column.name]);
}),
};
},
deserialize: datatable => {
const { columns, rows } = datatable;
deserialize: table => {
const { columns, rows } = table;
return {
...datatable,
rows: rows.map(row => {
...table,
rows: rows.map((row: any) => {
return zipObject(map(columns, 'name'), row);
}),
};
},
from: {
null: () => {
return {
type: 'datatable',
type: name,
rows: [],
columns: [],
};
},
pointseries: context => {
pointseries: (context: PointSeries) => {
return {
type: 'datatable',
type: name,
rows: context.rows,
columns: map(context.columns, (val, name) => {
return { name: name, type: val.type, role: val.role };
columns: map(context.columns, (val, colName) => {
return { name: colName!, type: val.type };
}),
};
},
},
to: {
render: datatable => {
render: (table): Render<RenderedDatatable> => {
return {
type: 'render',
as: 'table',
value: {
datatable,
datatable: table,
paginate: true,
perPage: 10,
showHeader: true,
},
};
},
pointseries: datatable => {
pointseries: (table): PointSeries => {
// datatable columns are an array that looks like [{ name: "one", type: "string" }, { name: "two", type: "string" }]
// rows look like [{ one: 1, two: 2}, { one: 3, two: 4}, ...]
const validFields = ['x', 'y', 'color', 'size', 'text'];
const columns = datatable.columns.filter(column => validFields.includes(column.name));
const rows = datatable.rows.map(row => pick(row, validFields));
const columns = table.columns.filter(column => validFields.includes(column.name));
const rows = table.rows.map(row => pick(row, validFields));

return {
type: 'pointseries',
columns: columns.reduce((acc, column) => {
columns: columns.reduce((acc: Record<string, any>, column) => {
/* pointseries columns are an object that looks like this
* {
* x: { type: "string", expression: "x", role: "dimension" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@
* under the License.
*/

export const pointseries = () => ({
name: 'pointseries',
from: {
null: () => {
return {
type: 'pointseries',
rows: [],
columns: {},
};
},
},
import { ExpressionType } from '../../types';
import { Render } from './render';

const name = 'error';

// TODO: Improve typings on this interface [#38553]
export interface InterpreterErrorType {
type: typeof name;
error: unknown;
info: unknown;
}

export const error = (): ExpressionType<typeof name, InterpreterErrorType> => ({
name,
to: {
render: (pointseries, types) => {
const datatable = types.datatable.from(pointseries, types);
render: (input): Render<Pick<InterpreterErrorType, 'error' | 'info'>> => {
return {
type: 'render',
as: 'table',
as: name,
value: {
datatable,
showHeader: true,
error: input.error,
info: input.info,
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@
* under the License.
*/

export const filter = () => ({
name: 'filter',
import { ExpressionType } from '../../types';

const name = 'filter';

/**
* Represents an object that is a Filter.
*/
export interface Filter {
type?: string;
value?: string;
column?: string;
and: Filter[];
to?: string;
from?: string;
query?: string | null;
}

export const filter = (): ExpressionType<typeof name, Filter> => ({
name,
from: {
null: () => {
return {
type: 'filter',
type: name,
// Any meta data you wish to pass along.
meta: {},
// And filters. If you need an "or", create a filter type for it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@
* under the License.
*/

export const error = () => ({
name: 'error',
import { ExpressionType } from '../../types';
import { Render } from './render';

const name = 'image';

export interface ExpressionImage {
type: 'image';
mode: string;
dataurl: string;
}

export const image = (): ExpressionType<typeof name, ExpressionImage> => ({
name,
to: {
render: input => {
const { error, info } = input;
render: (input): Render<Pick<ExpressionImage, 'mode' | 'dataurl'>> => {
return {
type: 'render',
as: 'error',
value: {
error,
info,
},
as: 'image',
value: input,
};
},
},
Expand Down
24 changes: 10 additions & 14 deletions src/legacy/core_plugins/interpreter/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,17 @@
* under the License.
*/

// @ts-ignore
import { boolean } from './boolean';
// @ts-ignore
import { datatable } from './datatable';
// @ts-ignore
import { error } from './error';
// @ts-ignore
import { filter } from './filter';
// @ts-ignore
import { image } from './image';
// @ts-ignore
import { nullType } from './null';
// @ts-ignore
import { number } from './number';
// @ts-ignore
import { pointseries } from './pointseries';
// @ts-ignore
import { render } from './render';
// @ts-ignore
import { shape } from './shape';
// @ts-ignore
import { string } from './string';
// @ts-ignore
import { style } from './style';
import { kibanaContext } from './kibana_context';
import { kibanaDatatable } from './kibana_datatable';
Expand All @@ -61,5 +49,13 @@ export const typeSpecs = [
kibanaDatatable,
];

export { KibanaContext } from './kibana_context';
export { KibanaDatatable } from './kibana_datatable';
// Types
export * from './datatable';
export * from './error';
export * from './filter';
export * from './image';
export * from './kibana_context';
export * from './kibana_datatable';
export * from './pointseries';
export * from './render';
export * from './style';
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
* under the License.
*/

export const nullType = () => ({
name: 'null',
import { ExpressionType } from '../../types';

const name = 'null';

export const nullType = (): ExpressionType<typeof name, null> => ({
name,
from: {
'*': () => null,
},
Expand Down
Loading