Skip to content

Commit a987cef

Browse files
authored
preparing datatable for unifying with kibana_datatable (#73654)
1 parent ea54531 commit a987cef

22 files changed

Lines changed: 144 additions & 81 deletions

File tree

src/plugins/expressions/common/expression_types/specs/boolean.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export const boolean: ExpressionTypeDefinition<'boolean', boolean> = {
4141
},
4242
datatable: (value): Datatable => ({
4343
type: 'datatable',
44-
columns: [{ name: 'value', type: name }],
44+
meta: {},
45+
columns: [{ id: 'value', name: 'value', meta: { type: name } }],
4546
rows: [{ value }],
4647
}),
4748
},

src/plugins/expressions/common/expression_types/specs/datatable.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ import { ExpressionTypeDefinition } from '../types';
2323
import { PointSeries, PointSeriesColumn } from './pointseries';
2424
import { ExpressionValueRender } from './render';
2525

26+
type State = string | number | boolean | null | undefined | SerializableState;
27+
28+
/** @internal **/
29+
export interface SerializableState {
30+
[key: string]: State | State[];
31+
}
32+
2633
const name = 'datatable';
2734

2835
/**
@@ -42,19 +49,31 @@ export type DatatableColumnType = 'string' | 'number' | 'boolean' | 'date' | 'nu
4249
*/
4350
export type DatatableRow = Record<string, any>;
4451

52+
export interface DatatableColumnMeta {
53+
type: DatatableColumnType;
54+
field?: string;
55+
params?: SerializableState;
56+
}
4557
/**
4658
* This type represents the shape of a column in a `Datatable`.
4759
*/
4860
export interface DatatableColumn {
61+
id: string;
4962
name: string;
50-
type: DatatableColumnType;
63+
meta: DatatableColumnMeta;
64+
}
65+
66+
export interface DatatableMeta {
67+
type?: string;
68+
source?: string;
5169
}
5270

5371
/**
5472
* A `Datatable` in Canvas is a unique structure that represents tabulated data.
5573
*/
5674
export interface Datatable {
5775
type: typeof name;
76+
meta?: DatatableMeta;
5877
columns: DatatableColumn[];
5978
rows: DatatableRow[];
6079
}
@@ -103,14 +122,16 @@ export const datatable: ExpressionTypeDefinition<typeof name, Datatable, Seriali
103122
from: {
104123
null: () => ({
105124
type: name,
125+
meta: {},
106126
rows: [],
107127
columns: [],
108128
}),
109129
pointseries: (value: PointSeries) => ({
110130
type: name,
131+
meta: {},
111132
rows: value.rows,
112133
columns: map(value.columns, (val: PointSeriesColumn, colName) => {
113-
return { name: colName, type: val.type };
134+
return { id: colName, name: colName, meta: { type: val.type } };
114135
}),
115136
}),
116137
},
@@ -127,13 +148,13 @@ export const datatable: ExpressionTypeDefinition<typeof name, Datatable, Seriali
127148
}),
128149
pointseries: (table: Datatable): PointSeries => {
129150
const validFields = ['x', 'y', 'color', 'size', 'text'];
130-
const columns = table.columns.filter((column) => validFields.includes(column.name));
151+
const columns = table.columns.filter((column) => validFields.includes(column.id));
131152
const rows = table.rows.map((row) => pick(row, validFields));
132153
return {
133154
type: 'pointseries',
134155
columns: columns.reduce<Record<string, PointSeries['columns']>>((acc, column) => {
135156
acc[column.name] = {
136-
type: column.type,
157+
type: column.meta.type,
137158
expression: column.name,
138159
role: 'dimension',
139160
};

src/plugins/expressions/common/expression_types/specs/num.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export const num: ExpressionTypeDefinition<'num', ExpressionValueNum> = {
7373
},
7474
datatable: ({ value }): Datatable => ({
7575
type: 'datatable',
76-
columns: [{ name: 'value', type: 'number' }],
76+
meta: {},
77+
columns: [{ id: 'value', name: 'value', meta: { type: 'number' } }],
7778
rows: [{ value }],
7879
}),
7980
},

src/plugins/expressions/common/expression_types/specs/number.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export const number: ExpressionTypeDefinition<typeof name, number> = {
5555
},
5656
datatable: (value): Datatable => ({
5757
type: 'datatable',
58-
columns: [{ name: 'value', type: 'number' }],
58+
meta: {},
59+
columns: [{ id: 'value', name: 'value', meta: { type: 'number' } }],
5960
rows: [{ value }],
6061
}),
6162
},

src/plugins/expressions/common/expression_types/specs/string.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export const string: ExpressionTypeDefinition<typeof name, string> = {
4040
},
4141
datatable: (value): Datatable => ({
4242
type: 'datatable',
43-
columns: [{ name: 'value', type: 'string' }],
43+
meta: {},
44+
columns: [{ id: 'value', name: 'value', meta: { type: 'string' } }],
4445
rows: [{ value }],
4546
}),
4647
},

x-pack/plugins/canvas/canvas_plugin_src/functions/browser/location.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { getFunctionHelp } from '../../../i18n';
1010
const noop = () => {};
1111

1212
interface Return extends Datatable {
13-
columns: [{ name: 'latitude'; type: 'number' }, { name: 'longitude'; type: 'number' }];
13+
columns: [
14+
{ id: 'latitude'; name: 'latitude'; meta: { type: 'number' } },
15+
{ id: 'longitude'; name: 'longitude'; meta: { type: 'number' } }
16+
];
1417
rows: [{ latitude: number; longitude: number }];
1518
}
1619

@@ -30,8 +33,8 @@ export function location(): ExpressionFunctionDefinition<'location', null, {}, P
3033
return resolve({
3134
type: 'datatable',
3235
columns: [
33-
{ name: 'latitude', type: 'number' },
34-
{ name: 'longitude', type: 'number' },
36+
{ id: 'latitude', name: 'latitude', meta: { type: 'number' } },
37+
{ id: 'longitude', name: 'longitude', meta: { type: 'number' } },
3538
],
3639
rows: [{ latitude, longitude }],
3740
});

x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_tables.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@ const testTable: Datatable = {
1616
type: 'datatable',
1717
columns: [
1818
{
19+
id: 'name',
1920
name: 'name',
20-
type: 'string',
21+
meta: { type: 'string' },
2122
},
2223
{
24+
id: 'time',
2325
name: 'time',
24-
type: 'date',
26+
meta: { type: 'date' },
2527
},
2628
{
29+
id: 'price',
2730
name: 'price',
28-
type: 'number',
31+
meta: { type: 'number' },
2932
},
3033
{
34+
id: 'quantity',
3135
name: 'quantity',
32-
type: 'number',
36+
meta: { type: 'number' },
3337
},
3438
{
39+
id: 'in_stock',
3540
name: 'in_stock',
36-
type: 'boolean',
41+
meta: { type: 'boolean' },
3742
},
3843
],
3944
rows: [
@@ -107,24 +112,29 @@ const stringTable: Datatable = {
107112
type: 'datatable',
108113
columns: [
109114
{
115+
id: 'name',
110116
name: 'name',
111-
type: 'string',
117+
meta: { type: 'string' },
112118
},
113119
{
120+
id: 'time',
114121
name: 'time',
115-
type: 'string',
122+
meta: { type: 'string' },
116123
},
117124
{
125+
id: 'price',
118126
name: 'price',
119-
type: 'string',
127+
meta: { type: 'string' },
120128
},
121129
{
130+
id: 'quantity',
122131
name: 'quantity',
123-
type: 'string',
132+
meta: { type: 'string' },
124133
},
125134
{
135+
id: 'in_stock',
126136
name: 'in_stock',
127-
type: 'string',
137+
meta: { type: 'string' },
128138
},
129139
],
130140
rows: [

x-pack/plugins/canvas/canvas_plugin_src/functions/common/alterColumn.test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('alterColumn', () => {
3838
const arbitraryRowIndex = 6;
3939

4040
expect(newColumn.name).not.toBe(originalColumn.name);
41-
expect(newColumn.type).not.toBe(originalColumn.type);
41+
expect(newColumn.meta.type).not.toBe(originalColumn.meta.type);
4242
expect(typeof dateToString.rows[arbitraryRowIndex].timeISO).toBe('string');
4343
expect(new Date(dateToString.rows[arbitraryRowIndex].timeISO)).toEqual(
4444
new Date(testTable.rows[arbitraryRowIndex].time)
@@ -60,7 +60,7 @@ describe('alterColumn', () => {
6060
it('converts the column to the specified type', () => {
6161
const dateToString = fn(testTable, { column: 'time', type: 'string', name: 'timeISO' });
6262

63-
expect(typeof dateToString.columns[timeColumnIndex].type).toBe('string');
63+
expect(typeof dateToString.columns[timeColumnIndex].meta.type).toBe('string');
6464
expect(typeof dateToString.rows[timeColumnIndex].timeISO).toBe('string');
6565
expect(new Date(dateToString.rows[timeColumnIndex].timeISO)).toEqual(
6666
new Date(testTable.rows[timeColumnIndex].time)
@@ -69,10 +69,10 @@ describe('alterColumn', () => {
6969

7070
it('does not change column if type is not specified', () => {
7171
const unconvertedColumn = fn(testTable, { column: 'price', name: 'foo' });
72-
const originalType = testTable.columns[priceColumnIndex].type;
72+
const originalType = testTable.columns[priceColumnIndex].meta.type;
7373
const arbitraryRowIndex = 2;
7474

75-
expect(unconvertedColumn.columns[priceColumnIndex].type).toBe(originalType);
75+
expect(unconvertedColumn.columns[priceColumnIndex].meta.type).toBe(originalType);
7676
expect(typeof unconvertedColumn.rows[arbitraryRowIndex].foo).toBe(originalType);
7777
});
7878

@@ -99,7 +99,7 @@ describe('alterColumn', () => {
9999
const arbitraryRowIndex = 5;
100100

101101
expect(newColumn.name).not.toBe(originalColumn.name);
102-
expect(newColumn.type).not.toBe(originalColumn.type);
102+
expect(newColumn.meta.type).not.toBe(originalColumn.meta.type);
103103
expect(typeof overwriteName.rows[arbitraryRowIndex].name).toBe('string');
104104
expect(new Date(overwriteName.rows[arbitraryRowIndex].name)).toEqual(
105105
new Date(testTable.rows[arbitraryRowIndex].time)
@@ -122,7 +122,7 @@ describe('alterColumn', () => {
122122
const numberToString = fn(testTable, { column: 'price', type: 'string' });
123123

124124
expect(numberToString.columns[priceColumnIndex]).toHaveProperty('name', 'price');
125-
expect(numberToString.columns[priceColumnIndex]).toHaveProperty('type', 'string');
125+
expect(numberToString.columns[priceColumnIndex].meta).toHaveProperty('type', 'string');
126126

127127
expect(typeof numberToString.rows[arbitraryRowIndex].price).toBe('string');
128128
expect(numberToString.rows[arbitraryRowIndex].price).toBe(
@@ -132,7 +132,7 @@ describe('alterColumn', () => {
132132
const stringToNumber = fn(numberToString, { column: 'price', type: 'number' });
133133

134134
expect(stringToNumber.columns[priceColumnIndex]).toHaveProperty('name', 'price');
135-
expect(stringToNumber.columns[priceColumnIndex]).toHaveProperty('type', 'number');
135+
expect(stringToNumber.columns[priceColumnIndex].meta).toHaveProperty('type', 'number');
136136

137137
expect(typeof stringToNumber.rows[arbitraryRowIndex].price).toBe('number');
138138

@@ -146,7 +146,7 @@ describe('alterColumn', () => {
146146
const dateToString = fn(testTable, { column: 'time', type: 'string' });
147147

148148
expect(dateToString.columns[timeColumnIndex]).toHaveProperty('name', 'time');
149-
expect(dateToString.columns[timeColumnIndex]).toHaveProperty('type', 'string');
149+
expect(dateToString.columns[timeColumnIndex].meta).toHaveProperty('type', 'string');
150150

151151
expect(typeof dateToString.rows[arbitraryRowIndex].time).toBe('string');
152152
expect(new Date(dateToString.rows[arbitraryRowIndex].time)).toEqual(
@@ -156,7 +156,7 @@ describe('alterColumn', () => {
156156
const stringToDate = fn(dateToString, { column: 'time', type: 'date' });
157157

158158
expect(stringToDate.columns[timeColumnIndex]).toHaveProperty('name', 'time');
159-
expect(stringToDate.columns[timeColumnIndex]).toHaveProperty('type', 'date');
159+
expect(stringToDate.columns[timeColumnIndex].meta).toHaveProperty('type', 'date');
160160
expect(new Date(stringToDate.rows[timeColumnIndex].time)).toBeInstanceOf(Date);
161161

162162
expect(new Date(stringToDate.rows[timeColumnIndex].time)).toEqual(
@@ -169,7 +169,7 @@ describe('alterColumn', () => {
169169
const arbitraryRowIndex = 1;
170170

171171
expect(dateToNumber.columns[timeColumnIndex]).toHaveProperty('name', 'time');
172-
expect(dateToNumber.columns[timeColumnIndex]).toHaveProperty('type', 'number');
172+
expect(dateToNumber.columns[timeColumnIndex].meta).toHaveProperty('type', 'number');
173173

174174
expect(typeof dateToNumber.rows[arbitraryRowIndex].time).toBe('number');
175175
expect(dateToNumber.rows[arbitraryRowIndex].time).toEqual(
@@ -179,7 +179,7 @@ describe('alterColumn', () => {
179179
const numberToDate = fn(dateToNumber, { column: 'time', type: 'date' });
180180

181181
expect(numberToDate.columns[timeColumnIndex]).toHaveProperty('name', 'time');
182-
expect(numberToDate.columns[timeColumnIndex]).toHaveProperty('type', 'date');
182+
expect(numberToDate.columns[timeColumnIndex].meta).toHaveProperty('type', 'date');
183183

184184
expect(new Date(numberToDate.rows[arbitraryRowIndex].time)).toBeInstanceOf(Date);
185185
expect(new Date(numberToDate.rows[arbitraryRowIndex].time)).toEqual(
@@ -192,7 +192,7 @@ describe('alterColumn', () => {
192192
const arbitraryRowIndex = 7;
193193

194194
expect(booleanToNumber.columns[inStockColumnIndex]).toHaveProperty('name', 'in_stock');
195-
expect(booleanToNumber.columns[inStockColumnIndex]).toHaveProperty('type', 'number');
195+
expect(booleanToNumber.columns[inStockColumnIndex].meta).toHaveProperty('type', 'number');
196196

197197
expect(typeof booleanToNumber.rows[arbitraryRowIndex].in_stock).toBe('number');
198198
expect(booleanToNumber.rows[arbitraryRowIndex].in_stock).toEqual(
@@ -202,7 +202,7 @@ describe('alterColumn', () => {
202202
const numberToBoolean = fn(booleanToNumber, { column: 'in_stock', type: 'boolean' });
203203

204204
expect(numberToBoolean.columns[inStockColumnIndex]).toHaveProperty('name', 'in_stock');
205-
expect(numberToBoolean.columns[inStockColumnIndex]).toHaveProperty('type', 'boolean');
205+
expect(numberToBoolean.columns[inStockColumnIndex].meta).toHaveProperty('type', 'boolean');
206206

207207
expect(typeof numberToBoolean.rows[arbitraryRowIndex].in_stock).toBe('boolean');
208208
expect(numberToBoolean.rows[arbitraryRowIndex].in_stock).toEqual(
@@ -216,7 +216,7 @@ describe('alterColumn', () => {
216216

217217
expect(stringToNull.columns[nameColumnIndex]).toHaveProperty('name', 'name');
218218

219-
expect(stringToNull.columns[nameColumnIndex]).toHaveProperty('type', 'null');
219+
expect(stringToNull.columns[nameColumnIndex].meta).toHaveProperty('type', 'null');
220220

221221
expect(stringToNull.rows[arbitraryRowIndex].name).toBe(null);
222222
});

x-pack/plugins/canvas/canvas_plugin_src/functions/common/alterColumn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ export function alterColumn(): ExpressionFunctionDefinition<
5757
}
5858

5959
const name = args.name || column.name;
60-
const type = args.type || column.type;
60+
const type = args.type || column.meta.type;
6161

6262
const columns = input.columns.reduce((all: DatatableColumn[], col) => {
6363
if (col.name !== args.name) {
6464
if (col.name !== column.name) {
6565
all.push(col);
6666
} else {
67-
all.push({ name, type });
67+
all.push({ id: name, name, meta: { type } });
6868
}
6969
}
7070
return all;
@@ -76,7 +76,7 @@ export function alterColumn(): ExpressionFunctionDefinition<
7676
handler = (function getHandler() {
7777
switch (type) {
7878
case 'string':
79-
if (column.type === 'date') {
79+
if (column.meta.type === 'date') {
8080
return (v: string) => new Date(v).toISOString();
8181
}
8282
return String;

x-pack/plugins/canvas/canvas_plugin_src/functions/common/as.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ describe('as', () => {
1313
it('returns a datatable with a single column and single row', () => {
1414
expect(fn('foo', { name: 'bar' })).toEqual({
1515
type: 'datatable',
16-
columns: [{ name: 'bar', type: 'string' }],
16+
columns: [{ id: 'bar', name: 'bar', meta: { type: 'string' } }],
1717
rows: [{ bar: 'foo' }],
1818
});
1919

2020
expect(fn(2, { name: 'num' })).toEqual({
2121
type: 'datatable',
22-
columns: [{ name: 'num', type: 'number' }],
22+
columns: [{ id: 'num', name: 'num', meta: { type: 'number' } }],
2323
rows: [{ num: 2 }],
2424
});
2525

2626
expect(fn(true, { name: 'bool' })).toEqual({
2727
type: 'datatable',
28-
columns: [{ name: 'bool', type: 'boolean' }],
28+
columns: [{ id: 'bool', name: 'bool', meta: { type: 'boolean' } }],
2929
rows: [{ bool: true }],
3030
});
3131
});

0 commit comments

Comments
 (0)