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

Improve Interpreter TypeScript typings #47212

Merged
merged 22 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
24616b2
fix: 🐛 correct createError() implementation and typings
streamich Oct 3, 2019
3599078
feat: 🎸 improve expressions service TypeScript types
streamich Oct 3, 2019
e368990
perf: ⚡️ improve ExpressionLoader class
streamich Oct 3, 2019
9a7b226
feat: 🎸 improve Interpreter types
streamich Oct 3, 2019
d9c1a31
Merge remote-tracking branch 'upstream/master' into interpreter-typings
streamich Oct 3, 2019
bec55db
feat: 🎸 improve Interpreter function typings
streamich Oct 3, 2019
0f9e24d
feat: 🎸 improve Interepreter test TypeScript typings
streamich Oct 4, 2019
119ede3
feat: 🎸 TypeScriptify kibana_context expression function
streamich Oct 4, 2019
b30e38a
feat: 🎸 TypeScriptify "kibana" expressions functions
streamich Oct 4, 2019
f504566
feat: 🎸 TypeScriptify "visualization" function
streamich Oct 4, 2019
ffa35c8
feat: 🎸 TypeScriptify Interpreter initialization
streamich Oct 4, 2019
54defdb
test: 💍 fix interpreter tests
streamich Oct 4, 2019
55a96e5
feat: 🎸 add TypeScript typing to some of the executor internals
streamich Oct 4, 2019
d76ddfc
fix: 🐛 this fixes visualizations; why?
streamich Oct 4, 2019
70ca5c4
chore: 🤖 delete redundant files and fix TypeScript error
streamich Oct 4, 2019
0a17241
Merge remote-tracking branch 'upstream/master' into interpreter-typings
streamich Oct 7, 2019
2ba1371
perf: ⚡️ use not null assertion for .dataHandler
streamich Oct 7, 2019
88684f4
refactor: 💡 stricten AnyExpressionFunction type
streamich Oct 7, 2019
fdf880f
fix: 🐛 set correct initial pipeline exec context
streamich Oct 7, 2019
254fccc
feat: 🎸 improve expression type typings
streamich Oct 7, 2019
7f4a4d0
Merge remote-tracking branch 'upstream/master' into interpreter-typings
streamich Oct 7, 2019
3a3c576
test: 💍 call interpreter with correct input from func tests
streamich Oct 7, 2019
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 @@ -27,11 +27,11 @@ import { getInspector } from '../services';

export class ExpressionLoader {
data$: Observable<Data>;
update$: Observable<any>;
render$: Observable<RenderId>;
events$: Observable<any>;
update$: ExpressionRenderHandler['update$'];
render$: ExpressionRenderHandler['render$'];
events$: ExpressionRenderHandler['events$'];

private dataHandler: ExpressionDataHandler;
private dataHandler!: ExpressionDataHandler;
private renderHandler: ExpressionRenderHandler;
private dataSubject: Subject<Data>;
private data: Data;
Expand All @@ -58,8 +58,6 @@ export class ExpressionLoader {
});

this.execute(expression, params);
// @ts-ignore
this.dataHandler = this.dataHandler;
}

destroy() {}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/legacy/core_plugins/interpreter/public/functions/clog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
* under the License.
*/

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

const name = 'clog';

type Context = any;
type ClogExpressionFunction = ExpressionFunction<typeof name, Context, {}, Context>;

export const clog = (): ClogExpressionFunction => ({
name,
args: {},
help: 'Outputs the context to the console',
fn: (context: any) => {
fn: context => {
console.log(context); // eslint-disable-line no-console
return context;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { font } from './font';
import { functionWrapper } from '../../test_helpers';

describe('font', () => {
const fn: any = functionWrapper(font);
const fn = functionWrapper(font);

describe('default output', () => {
const result = fn(null);
Expand Down
21 changes: 11 additions & 10 deletions src/legacy/core_plugins/interpreter/public/functions/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import {
} from '../types';

interface Arguments {
align: TextAlignment;
color: string;
family: FontFamily;
italic: boolean;
lHeight: number | null;
size: number;
underline: boolean;
weight: FontWeight;
align?: TextAlignment;
color?: string;
family?: FontFamily;
italic?: boolean;
lHeight?: number | null;
size?: number;
underline?: boolean;
weight?: FontWeight;
}

export function font(): ExpressionFunction<'font', null, Arguments, Style> {
Expand Down Expand Up @@ -88,6 +88,7 @@ export function font(): ExpressionFunction<'font', null, Arguments, Style> {
types: ['boolean'],
},
lHeight: {
default: null,
aliases: ['lineHeight'],
help: i18n.translate('interpreter.functions.font.args.lHeightHelpText', {
defaultMessage: 'The line height in pixels',
Expand Down Expand Up @@ -126,7 +127,7 @@ export function font(): ExpressionFunction<'font', null, Arguments, Style> {
},
},
fn: (_context, args) => {
if (!Object.values(FontWeight).includes(args.weight)) {
if (!Object.values(FontWeight).includes(args.weight!)) {
throw new Error(
i18n.translate('interpreter.functions.font.invalidFontWeightErrorMessage', {
defaultMessage: "Invalid font weight: '{weight}'",
Expand All @@ -136,7 +137,7 @@ export function font(): ExpressionFunction<'font', null, Arguments, Style> {
})
);
}
if (!Object.values(TextAlignment).includes(args.align)) {
if (!Object.values(TextAlignment).includes(args.align!)) {
throw new Error(
i18n.translate('interpreter.functions.font.invalidTextAlignmentErrorMessage', {
defaultMessage: "Invalid text alignment: '{align}'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@

import { functionWrapper } from '../../test_helpers';
import { kibana } from './kibana';
import { KibanaContext, FunctionHandlers } from '../../types';

describe('interpreter/functions#kibana', () => {
const fn = functionWrapper(kibana);
let context: any;
let initialContext: any;
let handlers: any;
let context: Partial<KibanaContext>;
let initialContext: KibanaContext;
let handlers: FunctionHandlers;

beforeEach(() => {
context = { timeRange: { from: '0', to: '1' } };
initialContext = {
type: 'kibana_context',
query: { language: 'lucene', query: 'geo.src:US' },
filters: [{ meta: { disabled: false }, query: { match: {} } }],
filters: [
{
meta: {
disabled: false,
negate: false,
alias: null,
},
query: { match: {} },
},
],
timeRange: { from: '2', to: '3' },
};
handlers = {
Expand All @@ -57,11 +68,34 @@ describe('interpreter/functions#kibana', () => {
});

it('combines filters from context with initialContext', () => {
context.filters = [{ meta: { disabled: true }, query: { match: {} } }];
context.filters = [
{
meta: {
disabled: true,
negate: false,
alias: null,
},
query: { match: {} },
},
];
const actual = fn(context, {}, handlers);
expect(actual.filters).toEqual([
{ meta: { disabled: false }, query: { match: {} } },
{ meta: { disabled: true }, query: { match: {} } },
{
meta: {
disabled: false,
negate: false,
alias: null,
},
query: { match: {} },
},
{
meta: {
disabled: true,
negate: false,
alias: null,
},
query: { match: {} },
},
]);
});
});
24 changes: 18 additions & 6 deletions src/legacy/core_plugins/interpreter/public/functions/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunction, KibanaContext } from '../../types';

export const kibana = () => ({
export type ExpressionFunctionKibana = ExpressionFunction<
'kibana',
KibanaContext | null,
object,
KibanaContext
>;

export const kibana = (): ExpressionFunctionKibana => ({
name: 'kibana',
type: 'kibana_context',
context: {},

context: {
types: ['kibana_context', 'null'],
},

help: i18n.translate('interpreter.functions.kibana.help', {
defaultMessage: 'Gets kibana global context',
}),
args: {},
fn(context: any, args: any, handlers: any) {
fn(context, args, handlers) {
const initialContext = handlers.getInitialContext ? handlers.getInitialContext() : {};

if (context.query) {
if (context && context.query) {
initialContext.query = initialContext.query.concat(context.query);
}

if (context.filters) {
if (context && context.filters) {
initialContext.filters = initialContext.filters.concat(context.filters);
}

const timeRange = initialContext.timeRange || context.timeRange;
const timeRange = initialContext.timeRange || (context ? context.timeRange : undefined);

return {
...context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,23 @@

import chrome from 'ui/chrome';
import { i18n } from '@kbn/i18n';
import { ExpressionFunction, KibanaContext } from '../../types';

export const kibanaContext = () => ({
interface Arguments {
q?: string | null;
filters?: string | null;
timeRange?: string | null;
savedSearchId?: string | null;
}

export type ExpressionFunctionKibanaContext = ExpressionFunction<
'kibana_context',
KibanaContext | null,
Arguments,
Promise<KibanaContext>
>;

export const kibanaContext = (): ExpressionFunctionKibanaContext => ({
name: 'kibana_context',
type: 'kibana_context',
context: {
Expand All @@ -34,21 +49,33 @@ export const kibanaContext = () => ({
types: ['string', 'null'],
aliases: ['query', '_'],
default: null,
help: i18n.translate('interpreter.functions.kibana_context.q.help', {
defaultMessage: 'Specify Kibana free form text query',
}),
},
filters: {
types: ['string', 'null'],
default: '"[]"',
help: i18n.translate('interpreter.functions.kibana_context.filters.help', {
defaultMessage: 'Specify Kibana generic filters',
}),
},
timeRange: {
types: ['string', 'null'],
default: null,
help: i18n.translate('interpreter.functions.kibana_context.timeRange.help', {
defaultMessage: 'Specify Kibana time range filter',
}),
},
savedSearchId: {
types: ['string', 'null'],
default: null,
help: i18n.translate('interpreter.functions.kibana_context.savedSearchId.help', {
defaultMessage: 'Specify saved search ID to be used for queries and filters',
}),
},
},
async fn(context: any, args: any) {
async fn(context, args, handlers) {
const $injector = await chrome.dangerouslyGetActiveInjector();
const savedSearches = $injector.get('savedSearches') as any;
const queryArg = args.q ? JSON.parse(args.q) : [];
Expand All @@ -63,15 +90,19 @@ export const kibanaContext = () => ({
filters = filters.concat(searchFilters);
}

if (context.query) {
if (context && context.query) {
queries = queries.concat(context.query);
}

if (context.filters) {
if (context && context.filters) {
filters = filters.concat(context.filters).filter((f: any) => !f.meta.disabled);
}

const timeRange = args.timeRange ? JSON.parse(args.timeRange) : context.timeRange;
const timeRange = args.timeRange
? JSON.parse(args.timeRange)
: context
? context.timeRange
: undefined;

return {
type: 'kibana_context',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunction, KibanaDatatable } from '../../types';

export const visDimension = () => ({
const name = 'visdimension';

type Context = KibanaDatatable | null;

interface Arguments {
accessor: string | number;
format?: string;
formatParams?: string;
}

type Return = any;

export const visDimension = (): ExpressionFunction<typeof name, Context, Arguments, Return> => ({
name: 'visdimension',
help: i18n.translate('interpreter.function.visDimension.help', {
defaultMessage: 'Generates visConfig dimension object',
Expand All @@ -39,16 +52,23 @@ export const visDimension = () => ({
format: {
types: ['string'],
default: 'string',
help: i18n.translate('interpreter.function.visDimension.format.help', {
defaultMessage: 'Format',
}),
},
formatParams: {
types: ['string'],
default: '"{}"',
help: i18n.translate('interpreter.function.visDimension.formatParams.help', {
defaultMessage: 'Format params',
}),
},
},
fn: (context: any, args: any) => {
const accessor = Number.isInteger(args.accessor)
? args.accessor
: context.columns.find((c: any) => c.id === args.accessor);
fn: (context, args) => {
const accessor =
typeof args.accessor === 'number'
? args.accessor
: context!.columns.find(c => c.id === args.accessor);
if (accessor === undefined) {
throw new Error(
i18n.translate('interpreter.function.visDimension.error.accessor', {
Expand All @@ -62,7 +82,7 @@ export const visDimension = () => ({
accessor,
format: {
id: args.format,
params: JSON.parse(args.formatParams),
params: JSON.parse(args.formatParams!),
},
};
},
Expand Down
Loading