Skip to content

Commit

Permalink
[Vega] Shim new platform - vega_fn.js -> vega_fn.js , use ExpressionF…
Browse files Browse the repository at this point in the history
…unction (elastic#42582)

* [Vega] Shim new platform - vega_fn.js -> vega_fn.js , use ExpressionFunction

* fix PR comments

* fix PR comments
  • Loading branch information
alexwizp committed Aug 9, 2019
1 parent 2f1fe83 commit 57d6702
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/legacy/core_plugins/vis_type_vega/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ import { LegacyDependenciesPlugin, LegacyDependenciesPluginSetup } from './shim'
import { Plugin as DataPublicPlugin } from '../../../../plugins/data/public';
import { VisualizationsSetup } from '../../visualizations/public';

// @ts-ignore
import { createVegaFn } from './vega_fn';
// @ts-ignore
import { createVegaTypeDefinition } from './vega_type';

/** @private */
interface VegaVisualizationDependencies extends LegacyDependenciesPluginSetup {
/** @internal */
export interface VegaVisualizationDependencies extends LegacyDependenciesPluginSetup {
uiSettings: UiSettingsClientContract;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,35 @@

import { get } from 'lodash';
import { i18n } from '@kbn/i18n';

import { ExpressionFunction, KibanaContext, Render } from '../../interpreter/types';
import { VegaVisualizationDependencies } from './plugin';
import { createVegaRequestHandler } from './vega_request_handler';

export const createVegaFn = (dependencies) => ({
name: 'vega',
const name = 'vega';
type Context = KibanaContext | null;

interface Arguments {
spec: string;
}

export type VisParams = Required<Arguments>;

interface RenderValue {
visData: Context;
visType: typeof name;
visConfig: VisParams;
}

type Return = Promise<Render<RenderValue>>;

export const createVegaFn = (
dependencies: VegaVisualizationDependencies
): ExpressionFunction<typeof name, Context, Arguments, Return> => ({
name,
type: 'render',
context: {
types: [
'kibana_context',
'null',
],
types: ['kibana_context', 'null'],
},
help: i18n.translate('visTypeVega.function.help', {
defaultMessage: 'Vega visualization',
Expand All @@ -37,25 +56,25 @@ export const createVegaFn = (dependencies) => ({
spec: {
types: ['string'],
default: '',
help: '',
},
},
async fn(context, args) {
const vegaRequestHandler = createVegaRequestHandler(dependencies);

const response = await vegaRequestHandler({
timeRange: get(context, 'timeRange', null),
query: get(context, 'query', null),
filters: get(context, 'filters', null),
timeRange: get(context, 'timeRange'),
query: get(context, 'query'),
filters: get(context, 'filters'),
visParams: { spec: args.spec },
forceFetch: true,
});

return {
type: 'render',
as: 'visualization',
value: {
visData: response,
visType: 'vega',
visType: name,
visConfig: {
spec: args.spec,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,38 @@
* specific language governing permissions and limitations
* under the License.
*/
import { timefilter } from 'ui/timefilter';
import { buildEsQuery, getEsQueryConfig } from '@kbn/es-query';
import { Filter } from '@kbn/es-query';
import { timefilter, TimeRange } from 'ui/timefilter';
import { Query } from 'src/legacy/core_plugins/data/public';

// @ts-ignore
import { buildEsQuery, getEsQueryConfig } from '@kbn/es-query';
// @ts-ignore
import { VegaParser } from './data_model/vega_parser';
// @ts-ignore
import { SearchCache } from './data_model/search_cache';
// @ts-ignore
import { TimeCache } from './data_model/time_cache';

export function createVegaRequestHandler({ uiSettings, es, serviceSettings }) {
import { VegaVisualizationDependencies } from './plugin';
import { VisParams } from './vega_fn';

interface VegaRequestHandlerParams {
query: Query;
filters: Filter;
timeRange: TimeRange;
visParams: VisParams;
}

export function createVegaRequestHandler({
es,
uiSettings,
serviceSettings,
}: VegaVisualizationDependencies) {
const searchCache = new SearchCache(es, { max: 10, maxAge: 4 * 1000 });
const timeCache = new TimeCache(timefilter, 3 * 1000);

return ({ timeRange, filters, query, visParams }) => {
return ({ timeRange, filters, query, visParams }: VegaRequestHandlerParams) => {
timeCache.setTimeRange(timeRange);

const esQueryConfigs = getEsQueryConfig(uiSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@
*/

import { i18n } from '@kbn/i18n';
import { DefaultEditorSize } from 'ui/vis/editor_size';
import { Status } from 'ui/vis/update_status';
// @ts-ignore
import { DefaultEditorSize } from 'ui/vis/editor_size';
// @ts-ignore
import { defaultFeedbackMessage } from 'ui/vis/default_feedback_message';

import vegaEditorTemplate from './vega_editor_template.html';
import { visFactory } from '../../visualizations/public';
import { VegaVisualizationDependencies } from './plugin';

import { createVegaRequestHandler } from './vega_request_handler';
// @ts-ignore
import { createVegaVisualization } from './vega_visualization';

import vegaEditorTemplate from './vega_editor_template.html';
// @ts-ignore
import defaultSpec from '!!raw-loader!./default.spec.hjson';

import { visFactory } from '../../visualizations/public';

export const createVegaTypeDefinition = (dependencies) => {
export const createVegaTypeDefinition = (dependencies: VegaVisualizationDependencies) => {
const requestHandler = createVegaRequestHandler(dependencies);
const visualization = createVegaVisualization(dependencies);

Expand Down
1 change: 1 addition & 0 deletions src/legacy/ui/public/timefilter/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { timefilter, Timefilter } from './timefilter';
export { timeHistory, TimeRange } from './time_history';

0 comments on commit 57d6702

Please sign in to comment.