Skip to content

Commit

Permalink
Improve Interpreter TypeScript typings (#47212) (#47485)
Browse files Browse the repository at this point in the history
* fix: 🐛 correct createError() implementation and typings

* feat: 🎸 improve expressions service TypeScript types

* perf: ⚡️ improve ExpressionLoader class

* feat: 🎸 improve Interpreter types

* feat: 🎸 improve Interpreter function typings

* feat: 🎸 improve Interepreter test TypeScript typings

* feat: 🎸 TypeScriptify kibana_context expression function

* feat: 🎸 TypeScriptify "kibana" expressions functions

* feat: 🎸 TypeScriptify "visualization" function

* feat: 🎸 TypeScriptify Interpreter initialization

* test: 💍 fix interpreter tests

* feat: 🎸 add TypeScript typing to some of the executor internals

* fix: 🐛 this fixes visualizations; why?

* chore: 🤖 delete redundant files and fix TypeScript error

* perf: ⚡️ use not null assertion for .dataHandler

* refactor: 💡 stricten AnyExpressionFunction type

* fix: 🐛 set correct initial pipeline exec context

* feat: 🎸 improve expression type typings

* test: 💍 call interpreter with correct input from func tests
  • Loading branch information
streamich authored Oct 7, 2019
1 parent 8e8a6ef commit 831d708
Show file tree
Hide file tree
Showing 36 changed files with 415 additions and 239 deletions.
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 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
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

0 comments on commit 831d708

Please sign in to comment.