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

[Expressions] Refactor expression functions to use observables underneath #100409

Merged
merged 2 commits into from
Jun 11, 2021
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 @@ -23,7 +23,7 @@ export interface ExpressionFunctionDefinition<Name extends string, Input, Argume
| [help](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinition.help.md) | <code>string</code> | Help text displayed in the Expression editor. This text should be internationalized. |
| [inputTypes](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinition.inputtypes.md) | <code>Array&lt;TypeToString&lt;Input&gt;&gt;</code> | List of allowed type names for input value of this function. If this property is set the input of function will be cast to the first possible type in this list. If this property is missing the input will be provided to the function as-is. |
| [name](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinition.name.md) | <code>Name</code> | The name of the function, as will be used in expression. |
| [type](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinition.type.md) | <code>TypeToString&lt;UnwrapPromiseOrReturn&lt;Output&gt;&gt;</code> | Name of type of value this function outputs. |
| [type](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinition.type.md) | <code>TypeString&lt;Output&gt; &#124; UnmappedTypeStrings</code> | Name of type of value this function outputs. |

## Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Name of type of value this function outputs.
<b>Signature:</b>

```typescript
type?: TypeToString<UnwrapPromiseOrReturn<Output>>;
type?: TypeString<Output> | UnmappedTypeStrings;
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ExpressionFunctionDefinition<Name extends string, Input, Argume
| [help](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinition.help.md) | <code>string</code> | Help text displayed in the Expression editor. This text should be internationalized. |
| [inputTypes](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinition.inputtypes.md) | <code>Array&lt;TypeToString&lt;Input&gt;&gt;</code> | List of allowed type names for input value of this function. If this property is set the input of function will be cast to the first possible type in this list. If this property is missing the input will be provided to the function as-is. |
| [name](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinition.name.md) | <code>Name</code> | The name of the function, as will be used in expression. |
| [type](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinition.type.md) | <code>TypeToString&lt;UnwrapPromiseOrReturn&lt;Output&gt;&gt;</code> | Name of type of value this function outputs. |
| [type](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinition.type.md) | <code>TypeString&lt;Output&gt; &#124; UnmappedTypeStrings</code> | Name of type of value this function outputs. |

## Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Name of type of value this function outputs.
<b>Signature:</b>

```typescript
type?: TypeToString<UnwrapPromiseOrReturn<Output>>;
type?: TypeString<Output> | UnmappedTypeStrings;
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
* Side Public License, v 1.
*/

import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { Observable, defer, of, zip } from 'rxjs';
import { map } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../types';
import { Datatable, DatatableColumn, getType } from '../../expression_types';

export interface MapColumnArguments {
id?: string | null;
name: string;
expression?(datatable: Datatable): Observable<boolean | number | string | null>;
expression(datatable: Datatable): Observable<boolean | number | string | null>;
copyMetaFrom?: string | null;
}

export const mapColumn: ExpressionFunctionDefinition<
'mapColumn',
Datatable,
MapColumnArguments,
Promise<Datatable>
Observable<Datatable>
> = {
name: 'mapColumn',
aliases: ['mc'], // midnight commander. So many times I've launched midnight commander instead of moving a file.
Expand Down Expand Up @@ -80,57 +80,56 @@ export const mapColumn: ExpressionFunctionDefinition<
default: null,
},
},
fn: (input, args) => {
const expression = (...params: Parameters<Required<MapColumnArguments>['expression']>) =>
args
.expression?.(...params)
.pipe(take(1))
.toPromise() ?? Promise.resolve(null);
fn(input, args) {
const existingColumnIndex = input.columns.findIndex(({ id, name }) =>
args.id ? id === args.id : name === args.name
);
const id = input.columns[existingColumnIndex]?.id ?? args.id ?? args.name;

const columns = [...input.columns];
const existingColumnIndex = columns.findIndex(({ id, name }) => {
if (args.id) {
return id === args.id;
}
return name === args.name;
});
const columnId =
existingColumnIndex === -1 ? args.id ?? args.name : columns[existingColumnIndex].id;

const rowPromises = input.rows.map((row) => {
return expression({
type: 'datatable',
columns,
rows: [row],
}).then((val) => ({
...row,
[columnId]: val,
}));
});
return defer(() => {
const rows$ = input.rows.length
? zip(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zip will wait for all rows to emit the first value .... should we consider emitting rows as they come ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that behavior will be backward incompatible and may not work as expected without adapting the code in other plugins.

...input.rows.map((row) =>
args
.expression({
type: 'datatable',
columns: [...input.columns],
rows: [row],
})
.pipe(map((value) => ({ ...row, [id]: value })))
)
)
: of([]);

return Promise.all(rowPromises).then((rows) => {
const type = rows.length ? getType(rows[0][columnId]) : 'null';
const newColumn: DatatableColumn = {
id: columnId,
name: args.name,
meta: { type, params: { id: type } },
};
if (args.copyMetaFrom) {
const metaSourceFrom = columns.find(({ id }) => id === args.copyMetaFrom);
newColumn.meta = { ...newColumn.meta, ...(metaSourceFrom?.meta || {}) };
}
return rows$.pipe<Datatable>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to pipe here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's here because we need to process rows altogether and put them into a datatable.

map((rows) => {
const type = getType(rows[0]?.[id]);
const newColumn: DatatableColumn = {
id,
name: args.name,
meta: { type, params: { id: type } },
};
if (args.copyMetaFrom) {
const metaSourceFrom = input.columns.find(
({ id: columnId }) => columnId === args.copyMetaFrom
);
newColumn.meta = { ...newColumn.meta, ...(metaSourceFrom?.meta ?? {}) };
}

if (existingColumnIndex === -1) {
columns.push(newColumn);
} else {
columns[existingColumnIndex] = newColumn;
}
const columns = [...input.columns];
if (existingColumnIndex === -1) {
columns.push(newColumn);
} else {
columns[existingColumnIndex] = newColumn;
}

return {
type: 'datatable',
columns,
rows,
} as Datatable;
return {
columns,
rows,
type: 'datatable',
};
})
);
});
},
};
Loading