Skip to content

Commit

Permalink
[WIP] Add inspector for VEGA
Browse files Browse the repository at this point in the history
Closes: #31189
  • Loading branch information
alexwizp committed Jul 8, 2020
1 parent 1c91b1c commit 009fdcc
Show file tree
Hide file tree
Showing 38 changed files with 636 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/plugins/data/public/search/aggs/buckets/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const getTermsBucketAgg = () =>

const response = await nestedSearchSource.fetch({ abortSignal });
request
.stats(getResponseInspectorStats(nestedSearchSource, response))
.stats(getResponseInspectorStats(response, nestedSearchSource))
.ok({ json: response });
resp = mergeOtherBucketAggResponse(aggConfigs, resp, response, aggConfig, filterAgg());
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const handleCourierRequest = async ({

(searchSource as any).lastQuery = queryHash;

request.stats(getResponseInspectorStats(searchSource, response)).ok({ json: response });
request.stats(getResponseInspectorStats(response, searchSource)).ok({ json: response });

(searchSource as any).rawResponse = response;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ export function getRequestInspectorStats(searchSource: ISearchSource) {

/** @public */
export function getResponseInspectorStats(
searchSource: ISearchSource,
resp: SearchResponse<unknown>
resp: SearchResponse<unknown>,
searchSource?: ISearchSource
) {
const lastRequest = searchSource.history && searchSource.history[searchSource.history.length - 1];
const lastRequest =
searchSource?.history && searchSource.history[searchSource.history.length - 1];
const stats: RequestStatistics = {};

if (resp && resp.took) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ function discoverController(
}

function onResults(resp) {
inspectorRequest.stats(getResponseInspectorStats($scope.searchSource, resp)).ok({ json: resp });
inspectorRequest.stats(getResponseInspectorStats(resp, $scope.searchSource)).ok({ json: resp });

if (getTimeField()) {
const tabifiedData = tabifyAggResponse($scope.vis.data.aggs, resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class SearchEmbeddable extends Embeddable<SearchInput, SearchOutput>
this.updateOutput({ loading: false, error: undefined });

// Log response to inspector
inspectorRequest.stats(getResponseInspectorStats(searchSource, resp)).ok({ json: resp });
inspectorRequest.stats(getResponseInspectorStats(resp, searchSource)).ok({ json: resp });

// Apply the changes to the angular scope
this.searchScope.$apply(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/expressions/common/execution/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createExecutionContainer, ExecutionContainer } from './container';
import { createError } from '../util';
import { Defer, now } from '../../../kibana_utils/common';
import { toPromise } from '../../../data/common/utils/abort_utils';
import { RequestAdapter, DataAdapter } from '../../../inspector/common';
import { RequestAdapter, DataAdapter, Adapters } from '../../../inspector/common';
import { isExpressionValueError, ExpressionValueError } from '../expression_types/specs/error';
import {
ExpressionAstExpression,
Expand Down Expand Up @@ -70,7 +70,7 @@ export class Execution<
ExtraContext extends Record<string, unknown> = Record<string, unknown>,
Input = unknown,
Output = unknown,
InspectorAdapters = ExtraContext['inspectorAdapters'] extends object
InspectorAdapters extends Adapters = ExtraContext['inspectorAdapters'] extends object
? ExtraContext['inspectorAdapters']
: DefaultInspectorAdapters
> {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/expressions/common/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/

import { ExpressionType } from '../expression_types';
import { DataAdapter, RequestAdapter } from '../../../inspector/common';
import { Adapters, DataAdapter, RequestAdapter } from '../../../inspector/common';
import { TimeRange, Query, Filter } from '../../../data/common';
import { SavedObject, SavedObjectAttributes } from '../../../../core/public';

/**
* `ExecutionContext` is an object available to all functions during a single execution;
* it provides various methods to perform side-effects.
*/
export interface ExecutionContext<Input = unknown, InspectorAdapters = DefaultInspectorAdapters> {
export interface ExecutionContext<Input = unknown, InspectorAdapters extends Adapters = Adapters> {
/**
* Get initial input with which execution started.
*/
Expand Down Expand Up @@ -75,7 +75,7 @@ export interface ExecutionContext<Input = unknown, InspectorAdapters = DefaultIn
/**
* Default inspector adapters created if inspector adapters are not set explicitly.
*/
export interface DefaultInspectorAdapters {
export interface DefaultInspectorAdapters extends Adapters {
requests: RequestAdapter;
data: DataAdapter;
}
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/inspector/common/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@
* under the License.
*/

export { Adapters } from './types';
export { DataAdapter, FormattedData } from './data';
export { RequestAdapter, RequestStatistic, RequestStatistics, RequestStatus } from './request';
export {
RequestAdapter,
RequestStatistic,
RequestStatistics,
RequestStatus,
RequestResponder,
} from './request';
1 change: 1 addition & 0 deletions src/plugins/inspector/common/adapters/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export { RequestStatistic, RequestStatistics, RequestStatus } from './types';
export { RequestAdapter } from './request_adapter';
export { RequestResponder } from './request_responder';
25 changes: 25 additions & 0 deletions src/plugins/inspector/common/adapters/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* The interface that the adapters used to open an inspector have to fullfill.
*/
export interface Adapters {
[key: string]: any;
}
3 changes: 2 additions & 1 deletion src/plugins/inspector/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import * as React from 'react';
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/public';
import { toMountPoint } from '../../kibana_react/public';
import { InspectorViewRegistry } from './view_registry';
import { Adapters, InspectorOptions, InspectorSession } from './types';
import { InspectorOptions, InspectorSession } from './types';
import { InspectorPanel } from './ui/inspector_panel';
import { Adapters } from '../common';

import { getRequestsViewDescription, getDataViewDescription } from './views';

Expand Down
12 changes: 3 additions & 9 deletions src/plugins/inspector/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@
*/

import { OverlayRef } from '../../../core/public';

/**
* The interface that the adapters used to open an inspector have to fullfill.
*/
export interface Adapters {
[key: string]: any;
}
import { Adapters } from '../common';

/**
* The props interface that a custom inspector view component, that will be passed
* to {@link InspectorViewDescription#component}, must use.
*/
export interface InspectorViewProps {
export interface InspectorViewProps<TAdapters extends Adapters = Adapters> {
/**
* Adapters used to open the inspector.
*/
adapters: Adapters;
adapters: TAdapters;
/**
* The title that the inspector is currently using e.g. a visualization name.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/inspector/public/ui/inspector_panel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import React from 'react';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { InspectorPanel } from './inspector_panel';
import { Adapters, InspectorViewDescription } from '../types';
import { InspectorViewDescription } from '../types';
import { Adapters } from '../../common';

describe('InspectorPanel', () => {
let adapters: Adapters;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/inspector/public/ui/inspector_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { i18n } from '@kbn/i18n';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { EuiFlexGroup, EuiFlexItem, EuiFlyoutHeader, EuiTitle, EuiFlyoutBody } from '@elastic/eui';
import { Adapters, InspectorViewDescription } from '../types';
import { InspectorViewDescription } from '../types';
import { Adapters } from '../../common';
import { InspectorViewChooser } from './inspector_view_chooser';

function hasAdaptersChanged(oldAdapters: Adapters, newAdapters: Adapters) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/inspector/public/view_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { InspectorViewRegistry } from './view_registry';
import { InspectorViewDescription } from './types';

import { Adapters } from './types';
import { Adapters } from '../common';

function createMockView(
params: {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/inspector/public/view_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/

import { EventEmitter } from 'events';
import { Adapters, InspectorViewDescription } from './types';
import { InspectorViewDescription } from './types';
import { Adapters } from '../common';

/**
* @callback viewShouldShowFunc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
} from '@elastic/eui';

import { DataTableFormat } from './data_table';
import { InspectorViewProps, Adapters } from '../../../types';
import { InspectorViewProps } from '../../../types';
import { Adapters } from '../../../../common';
import {
TabularLoaderOptions,
TabularData,
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/inspector/public/views/data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import React from 'react';
import { i18n } from '@kbn/i18n';

import { DataViewComponent } from './components/data_view';
import { Adapters, InspectorViewDescription, InspectorViewProps } from '../../types';
import { InspectorViewDescription, InspectorViewProps } from '../../types';
import { Adapters } from '../../../common';
import { IUiSettingsClient } from '../../../../../core/public';

export const getDataViewDescription = (
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/inspector/public/views/requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import { i18n } from '@kbn/i18n';

import { RequestsViewComponent } from './components/requests_view';
import { Adapters, InspectorViewDescription } from '../../types';
import { InspectorViewDescription } from '../../types';
import { Adapters } from '../../../common';

export const getRequestsViewDescription = (): InspectorViewDescription => ({
title: i18n.translate('inspector.requests.requestsTitle', {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/vis_type_vega/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"version": "kibana",
"server": true,
"ui": true,
"requiredPlugins": ["data", "visualizations", "mapsLegacy", "expressions"]
"requiredPlugins": ["data", "visualizations", "mapsLegacy", "expressions", "inspector"]
}
40 changes: 36 additions & 4 deletions src/plugins/vis_type_vega/public/data_model/search_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
*/

import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { map, tap } from 'rxjs/operators';
import { CoreStart, IUiSettingsClient } from 'kibana/public';
import {
getSearchParamsFromRequest,
SearchRequest,
DataPublicPluginStart,
IEsSearchResponse,
} from '../../../data/public';
import { search as dataPluginSearch } from '../../../data/public';
import { VegaInspectorAdapters } from '../vega_inspector';
import { RequestResponder } from '../../../inspector/public';

export interface SearchAPIDependencies {
uiSettings: IUiSettingsClient;
Expand All @@ -35,26 +39,54 @@ export interface SearchAPIDependencies {
export class SearchAPI {
constructor(
private readonly dependencies: SearchAPIDependencies,
private readonly abortSignal?: AbortSignal
private readonly abortSignal?: AbortSignal,
public readonly inspectorAdapters?: VegaInspectorAdapters
) {}

search(searchRequests: SearchRequest[]) {
const { search } = this.dependencies.search;
const requestResponders: any = {};

if (this.inspectorAdapters) {
this.inspectorAdapters.requests.reset();
}

return combineLatest(
searchRequests.map((request, index) => {
const requestId = index.toString();
const params = getSearchParamsFromRequest(request, {
uiSettings: this.dependencies.uiSettings,
injectedMetadata: this.dependencies.injectedMetadata,
});

if (this.inspectorAdapters) {
requestResponders[requestId] = this.inspectorAdapters.requests.start(
`#${requestId}`,
request
);
requestResponders[requestId].json(params.body);
}

return search({ params }, { signal: this.abortSignal }).pipe(
map((data) => ({
id: index,
id: requestId,
rawResponse: data.rawResponse,
}))
);
})
);
).pipe(tap((data) => this.inspectSearchResults(data, requestResponders)));
}

private inspectSearchResults(
requests: IEsSearchResponse[],
requestResponders: Record<string, RequestResponder>
) {
requests.forEach((request) => {
if (request.id && requestResponders[request.id]) {
requestResponders[request.id]
.stats(dataPluginSearch.getResponseInspectorStats(request.rawResponse))
.ok({ json: request.rawResponse });
}
});
}
}
3 changes: 2 additions & 1 deletion src/plugins/vis_type_vega/public/data_model/vega_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ export class VegaParser {
this.hideWarnings = false;
this.error = undefined;
this.warnings = [];
this.searchAPI = searchAPI;

const onWarn = this._onWarning.bind(this);
this._urlParsers = {
elasticsearch: new EsQueryParser(timeCache, searchAPI, filters, onWarn),
elasticsearch: new EsQueryParser(timeCache, this.searchAPI, filters, onWarn),
emsfile: new EmsFileParser(serviceSettings),
url: new UrlParser(onWarn),
};
Expand Down
Loading

0 comments on commit 009fdcc

Please sign in to comment.