Skip to content

Commit

Permalink
Update types
Browse files Browse the repository at this point in the history
  • Loading branch information
sulemanof committed Nov 18, 2020
1 parent ea6c8a5 commit bdfb4b4
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/plugins/vis_type_timeseries/common/panel_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

import { $Values } from '@kbn/utility-types';

export const PANEL_TYPES = {
TABLE: 'table',
GAUGE: 'gauge',
Expand All @@ -25,3 +27,5 @@ export const PANEL_TYPES = {
TIMESERIES: 'timeseries',
METRIC: 'metric',
};

export type PANEL_TYPES = $Values<typeof PANEL_TYPES>;
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ import { PersistedState } from 'src/plugins/visualizations/public';
// @ts-expect-error
import { ErrorComponent } from './error';
import { TimeseriesVisTypes } from './vis_types';
import { TimeseriesVisParams } from '../../metrics_fn';
import { TimeseriesVisData } from '../../types';

interface TimeseriesVisualizationProps {
className?: string;
dateFormat: string;
getConfig: IUiSettingsClient['get'];
handlers: IInterpreterRenderHandlers;
model: {
id: string;
type: keyof typeof TimeseriesVisTypes;
};
visData: any;
model: TimeseriesVisParams;
visData: TimeseriesVisData;
uiState: PersistedState;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import React from 'react';
import { IUiSettingsClient } from 'src/core/public';
import { PersistedState } from 'src/plugins/visualizations/public';

/**
* TSVB visualizations are not typed yet.
*/

// @ts-expect-error
import { TimeseriesVisualization as timeseries } from './timeseries/vis';
// @ts-expect-error
Expand All @@ -34,6 +38,8 @@ import { TableVis as table } from './table/vis';
import { gauge } from './gauge/vis';
// @ts-expect-error
import { MarkdownVisualization as markdown } from './markdown/vis';
import { TimeseriesVisParams } from '../../../metrics_fn';
import { TimeseriesVisData } from '../../../types';

export const TimeseriesVisTypes: Record<string, React.ComponentType<TimeseriesVisProps>> = {
timeseries,
Expand All @@ -45,7 +51,7 @@ export const TimeseriesVisTypes: Record<string, React.ComponentType<TimeseriesVi
};

export interface TimeseriesVisProps {
model: any;
model: TimeseriesVisParams;
onBrush: (gte: string, lte: string) => void;
onUiState: (
field: string,
Expand All @@ -55,7 +61,7 @@ export interface TimeseriesVisProps {
}
) => void;
uiState: PersistedState;
visData: any;
visData: TimeseriesVisData;
dateFormat: string;
getConfig: IUiSettingsClient['get'];
}
11 changes: 3 additions & 8 deletions src/plugins/vis_type_timeseries/public/metrics_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ExpressionFunctionDefinition, Render } from '../../expressions/public';

import { PanelSchema } from '../common/types';
import { metricsRequestHandler } from './request_handler';
import { TimeseriesVisData } from './types';

type Input = KibanaContext | null;
type Output = Promise<Render<TimeseriesRenderValue>>;
Expand All @@ -35,10 +36,8 @@ interface Arguments {
export type TimeseriesVisParams = PanelSchema;

export interface TimeseriesRenderValue {
visType: 'metrics';
visData: Input;
visData: TimeseriesVisData | {};
visParams: TimeseriesVisParams;
uiState: any;
}

export type TimeseriesExpressionFunctionDefinition = ExpressionFunctionDefinition<
Expand Down Expand Up @@ -68,7 +67,7 @@ export const createMetricsFn = (): TimeseriesExpressionFunctionDefinition => ({
},
},
async fn(input, args) {
const visParams: PanelSchema = JSON.parse(args.params);
const visParams: TimeseriesVisParams = JSON.parse(args.params);
const uiState = JSON.parse(args.uiState);

const response = await metricsRequestHandler({
Expand All @@ -77,14 +76,10 @@ export const createMetricsFn = (): TimeseriesExpressionFunctionDefinition => ({
uiState,
});

response.visType = 'metrics';

return {
type: 'render',
as: 'timeseries_vis',
value: {
uiState,
visType: 'metrics',
visParams,
visData: response,
},
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/vis_type_timeseries/public/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ import { KibanaContext } from '../../data/public';
import { getTimezone, validateInterval } from './application';
import { getUISettings, getDataStart, getCoreStart } from './services';
import { MAX_BUCKETS_SETTING, ROUTES } from '../common/constants';
import { PanelSchema } from '../common/types';
import { TimeseriesVisParams } from './metrics_fn';
import { TimeseriesVisData } from './types';

interface MetricsRequestHandlerParams {
input: KibanaContext | null;
uiState: Record<string, any>;
visParams: PanelSchema;
visParams: TimeseriesVisParams;
}

export const metricsRequestHandler = async ({
input,
uiState,
visParams,
}: MetricsRequestHandlerParams) => {
}: MetricsRequestHandlerParams): Promise<TimeseriesVisData | {}> => {
const config = getUISettings();
const timezone = getTimezone(config);
const uiStateObj = uiState[visParams.type] ?? {};
Expand Down
15 changes: 10 additions & 5 deletions src/plugins/vis_type_timeseries/public/timeseries_vis_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ import { render, unmountComponentAtNode } from 'react-dom';
import { IUiSettingsClient } from 'kibana/public';
import { VisualizationContainer } from '../../visualizations/public';
import { ExpressionRenderDefinition } from '../../expressions/common/expression_renderers';
import { TimeseriesRenderValue } from './metrics_fn';
import { TimeseriesRenderValue, TimeseriesVisParams } from './metrics_fn';
import { TimeseriesVisData } from './types';

const TimeseriesVisualization = lazy(
() => import('./application/components/timeseries_visualization')
);

const checkIfDataExists = (visData: any, model: any) => {
const data = visData.type === 'table' ? visData.series : visData[model.id]?.series;
return data?.length > 0;
const checkIfDataExists = (visData: TimeseriesVisData | {}, model: TimeseriesVisParams) => {
if ('type' in visData) {
const data = visData.type === 'table' ? visData.series : visData?.[model.id]?.series;
return Boolean(data?.length);
}

return false;
};

export const getTimeseriesVisRenderer: (deps: {
Expand All @@ -58,7 +63,7 @@ export const getTimeseriesVisRenderer: (deps: {
getConfig={uiSettings.get.bind(uiSettings)}
handlers={handlers}
model={config.visParams}
visData={config.visData}
visData={config.visData as TimeseriesVisData}
uiState={handlers.uiState!}
/>
</VisualizationContainer>,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vis_type_timeseries/public/to_ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import { buildExpression, buildExpressionFunction } from '../../expressions/public';
import { Vis } from '../../visualizations/public';
import { TimeseriesExpressionFunctionDefinition } from './metrics_fn';
import { TimeseriesExpressionFunctionDefinition, TimeseriesVisParams } from './metrics_fn';

export const toExpressionAst = (vis: Vis<any>) => {
export const toExpressionAst = (vis: Vis<TimeseriesVisParams>) => {
const timeseries = buildExpressionFunction<TimeseriesExpressionFunctionDefinition>('tsvb', {
params: JSON.stringify(vis.params),
uiState: JSON.stringify(vis.uiState),
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/vis_type_timeseries/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,40 @@
import React from 'react';
import { EuiDraggable } from '@elastic/eui';

import type { PANEL_TYPES } from '../common/panel_types';
import { TimeseriesUIRestrictions } from '../common/ui_restrictions';

type PropsOf<T> = T extends React.ComponentType<infer ComponentProps> ? ComponentProps : never;
type FirstArgumentOf<Func> = Func extends (arg1: infer FirstArgument, ...rest: any[]) => any
? FirstArgument
: never;
export type DragHandleProps = FirstArgumentOf<
Exclude<PropsOf<typeof EuiDraggable>['children'], React.ReactElement>
>['dragHandleProps'];

/**
* series data is not typed yet.
* Should be in sync with returned data from server [GetVisDataPanel] in src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts
*/
interface SeriesData {
[key: string]: {
annotations: {
[key: string]: unknown[];
};
id: string;
series: unknown[];
error?: unknown;
};
}

export type TimeseriesVisData = SeriesData & {
dateFormat: string;
scaledDataFormat: Array<[string, string]>;
timezone: string;
type: PANEL_TYPES;
uiRestrictions: TimeseriesUIRestrictions;
/**
* series array is responsible only for "table" vis type
*/
series?: unknown[];
};

0 comments on commit bdfb4b4

Please sign in to comment.