Skip to content

Commit

Permalink
Merge branch 'master' into update-apm-index-pattern-master
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Jan 7, 2020
2 parents 2717b6f + 58cb24a commit 38a8586
Show file tree
Hide file tree
Showing 80 changed files with 2,755 additions and 1,469 deletions.
3 changes: 2 additions & 1 deletion src/legacy/core_plugins/console/public/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"version": "kibana",
"server": true,
"ui": true,
"requiredPlugins": ["home"]
"requiredPlugins": ["home"],
"optionalPlugins": ["usageCollection"]
}
10 changes: 6 additions & 4 deletions src/legacy/core_plugins/console/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import { I18nContext } from 'ui/i18n';
import chrome from 'ui/chrome';
import { FeatureCatalogueCategory } from 'ui/registry/feature_catalogue';

import { plugin } from './np_ready';
import { DevToolsSetup } from '../../../../plugins/dev_tools/public';
import { HomePublicPluginSetup } from '../../../../plugins/home/public';
import { UsageCollectionSetup } from '../../../../plugins/usage_collection/public';

export interface XPluginSet {
usageCollection: UsageCollectionSetup;
dev_tools: DevToolsSetup;
home: HomePublicPluginSetup;
__LEGACY: {
Expand All @@ -32,10 +38,6 @@ export interface XPluginSet {
};
}

import { plugin } from './np_ready';
import { DevToolsSetup } from '../../../../plugins/dev_tools/public';
import { HomePublicPluginSetup } from '../../../../plugins/home/public';

const pluginInstance = plugin({} as any);

(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jest.mock('../../../../contexts/editor_context/editor_registry.ts', () => ({
setInputEditor: () => {},
getInputEditor: () => ({
getRequestsInRange: async () => [{ test: 'test' }],
getCoreEditor: () => ({ getCurrentPosition: jest.fn() }),
}),
},
}));
Expand Down Expand Up @@ -52,3 +53,6 @@ jest.mock('../../../../models/sense_editor', () => {
jest.mock('../../../../hooks/use_send_current_request_to_es/send_request_to_es', () => ({
sendRequestToES: jest.fn(),
}));
jest.mock('../../../../../lib/autocomplete/get_endpoint_from_position', () => ({
getEndpointFromPosition: jest.fn(),
}));
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ import {
ServicesContextProvider,
EditorContextProvider,
RequestContextProvider,
ContextValue,
} from '../../../../contexts';

// Mocked functions
import { sendRequestToES } from '../../../../hooks/use_send_current_request_to_es/send_request_to_es';
import { getEndpointFromPosition } from '../../../../../lib/autocomplete/get_endpoint_from_position';

import * as consoleMenuActions from '../console_menu_actions';
import { Editor } from './editor';

describe('Legacy (Ace) Console Editor Component Smoke Test', () => {
let mockedAppContextValue: any;
let mockedAppContextValue: ContextValue;
const sandbox = sinon.createSandbox();

const doMount = () =>
Expand All @@ -58,22 +62,28 @@ describe('Legacy (Ace) Console Editor Component Smoke Test', () => {
beforeEach(() => {
document.queryCommandSupported = sinon.fake(() => true);
mockedAppContextValue = {
elasticsearchUrl: 'test',
services: {
trackUiMetric: { count: () => {}, load: () => {} },
settings: {} as any,
storage: {} as any,
history: {
getSavedEditorState: () => null,
getSavedEditorState: () => ({} as any),
updateCurrentState: jest.fn(),
},
} as any,
notifications: notificationServiceMock.createSetupContract(),
},
docLinkVersion: 'NA',
};
});

afterEach(() => {
jest.clearAllMocks();
sandbox.restore();
});

it('calls send current request to ES', async () => {
(getEndpointFromPosition as jest.Mock).mockReturnValue({ patterns: [] });
(sendRequestToES as jest.Mock).mockRejectedValue({});
const editor = doMount();
act(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* under the License.
*/

// @ts-ignore
import { getEndpointFromPosition } from '../../../../lib/autocomplete/autocomplete';
import { getEndpointFromPosition } from '../../../../lib/autocomplete/get_endpoint_from_position';
import { SenseEditor } from '../../../models/sense_editor';

export async function autoIndent(editor: SenseEditor, event: Event) {
Expand All @@ -40,7 +39,7 @@ export function getDocumentation(
}
const position = requests[0].range.end;
position.column = position.column - 1;
const endpoint = getEndpointFromPosition(editor, position, editor.parser);
const endpoint = getEndpointFromPosition(editor.getCoreEditor(), position, editor.parser);
if (endpoint && endpoint.documentation && endpoint.documentation.indexOf('http') !== -1) {
return endpoint.documentation
.replace('/master/', `/${docLinkVersion}/`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

export { useServicesContext, ServicesContextProvider } from './services_context';
export { useServicesContext, ServicesContextProvider, ContextValue } from './services_context';

export {
useRequestActionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import React, { createContext, useContext } from 'react';
import { NotificationsSetup } from 'kibana/public';
import { History, Storage, Settings } from '../../services';
import { MetricsTracker } from '../../types';

interface ContextValue {
export interface ContextValue {
services: {
history: History;
storage: Storage;
settings: Settings;
notifications: NotificationsSetup;
trackUiMetric: MetricsTracker;
};
elasticsearchUrl: string;
docLinkVersion: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export interface ESRequestResult {
}

let CURRENT_REQ_ID = 0;
export function sendRequestToES({ requests }: EsRequestArgs): Promise<ESRequestResult[]> {
export function sendRequestToES(args: EsRequestArgs): Promise<ESRequestResult[]> {
const requests = args.requests.slice();
return new Promise((resolve, reject) => {
const reqId = ++CURRENT_REQ_ID;
const results: ESRequestResult[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

import { SenseEditor } from '../../models/sense_editor';
import { getEndpointFromPosition } from '../../../lib/autocomplete/get_endpoint_from_position';
import { MetricsTracker } from '../../../types';

export const track = (requests: any[], editor: SenseEditor, trackUiMetric: MetricsTracker) => {
const coreEditor = editor.getCoreEditor();
// `getEndpointFromPosition` gets values from the server-side generated JSON files which
// are a combination of JS, automatically generated JSON and manual overrides. That means
// the metrics reported from here will be tied to the definitions in those files.
// See src/legacy/core_plugins/console/server/api_server/spec
const endpointDescription = getEndpointFromPosition(
coreEditor,
coreEditor.getCurrentPosition(),
editor.parser
);

if (requests[0] && endpointDescription) {
const eventName = `${requests[0].method}_${endpointDescription.id ?? 'unknown'}`;
trackUiMetric.count(eventName);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
import { i18n } from '@kbn/i18n';
import { useCallback } from 'react';
import { instance as registry } from '../../contexts/editor_context/editor_registry';
import { useServicesContext } from '../../contexts';
import { useRequestActionContext, useServicesContext } from '../../contexts';
import { sendRequestToES } from './send_request_to_es';
import { useRequestActionContext } from '../../contexts';
import { track } from './track';

// @ts-ignore
import mappings from '../../../lib/mappings/mappings';

export const useSendCurrentRequestToES = () => {
const {
services: { history, settings, notifications },
services: { history, settings, notifications, trackUiMetric },
} = useServicesContext();

const dispatch = useRequestActionContext();
Expand All @@ -45,9 +46,10 @@ export const useSendCurrentRequestToES = () => {
return;
}

const results = await sendRequestToES({
requests,
});
// Fire and forget
setTimeout(() => track(requests, editor, trackUiMetric), 0);

const results = await sendRequestToES({ requests });

results.forEach(({ request: { path, method, data } }) => {
history.addToHistory(path, method, data);
Expand Down Expand Up @@ -82,5 +84,5 @@ export const useSendCurrentRequestToES = () => {
});
}
}
}, [dispatch, settings, history, notifications]);
}, [dispatch, settings, history, notifications, trackUiMetric]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { NotificationsSetup } from 'kibana/public';
import { ServicesContextProvider, EditorContextProvider, RequestContextProvider } from './contexts';
import { Main } from './containers';
import { createStorage, createHistory, createSettings, Settings } from '../services';
import { createUsageTracker } from '../services/tracker';

let settingsRef: Settings;
export function legacyBackDoorToSettings() {
Expand All @@ -36,6 +37,9 @@ export function boot(deps: {
}) {
const { I18nContext, notifications, docLinkVersion, elasticsearchUrl } = deps;

const trackUiMetric = createUsageTracker();
trackUiMetric.load('opened_app');

const storage = createStorage({
engine: window.localStorage,
prefix: 'sense:',
Expand All @@ -50,7 +54,13 @@ export function boot(deps: {
value={{
elasticsearchUrl,
docLinkVersion,
services: { storage, history, settings, notifications },
services: {
storage,
history,
settings,
notifications,
trackUiMetric,
},
}}
>
<RequestContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './create';
export * from '../legacy_core_editor/create_readonly';
export { MODE } from '../../../lib/row_parser';
export { SenseEditor } from './sense_editor';
export { getEndpointFromPosition } from '../../../lib/autocomplete/get_endpoint_from_position';
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { URL_PATH_END_MARKER } from './components/index';
import { createTokenIterator } from '../../application/factories';

import { Position, Token, Range, CoreEditor } from '../../types';
import { SenseEditor } from '../../application/models/sense_editor';

let LAST_EVALUATED_TOKEN: any = null;

Expand All @@ -54,11 +53,20 @@ function isUrlParamsToken(token: any) {
return false;
}
}
function getCurrentMethodAndTokenPaths(

/**
* Get the method and token paths for a specific position in the current editor buffer.
*
* This function can be used for getting autocomplete information or for getting more information
* about the endpoint associated with autocomplete. In future, these concerns should be better
* separated.
*
*/
export function getCurrentMethodAndTokenPaths(
editor: CoreEditor,
pos: Position,
parser: any,
forceEndOfUrl?: boolean
forceEndOfUrl?: boolean /* Flag for indicating whether we want to avoid early escape optimization. */
) {
const tokenIter = createTokenIterator({
editor,
Expand Down Expand Up @@ -186,7 +194,7 @@ function getCurrentMethodAndTokenPaths(
}
}

if (walkedSomeBody && (!bodyTokenPath || bodyTokenPath.length === 0)) {
if (walkedSomeBody && (!bodyTokenPath || bodyTokenPath.length === 0) && !forceEndOfUrl) {
// we had some content and still no path -> the cursor is position after a closed body -> no auto complete
return {};
}
Expand Down Expand Up @@ -298,20 +306,6 @@ function getCurrentMethodAndTokenPaths(
}
return ret;
}
export function getEndpointFromPosition(senseEditor: SenseEditor, pos: Position, parser: any) {
const editor = senseEditor.getCoreEditor();
const context = {
...getCurrentMethodAndTokenPaths(
editor,
{ column: pos.column, lineNumber: pos.lineNumber },
parser,
true
),
};
const components = getTopLevelUrlCompleteComponents(context.method);
populateContext(context.urlTokenPath, context, editor, true, components);
return context.endpoint;
}

// eslint-disable-next-line
export default function({ coreEditor: editor, parser }: { coreEditor: CoreEditor; parser: any }) {
Expand Down Expand Up @@ -812,7 +806,6 @@ export default function({ coreEditor: editor, parser }: { coreEditor: CoreEditor
if (!ret.urlTokenPath) {
// zero length tokenPath is true

// console.log("Can't extract a valid url token path.");
return context;
}

Expand All @@ -825,13 +818,11 @@ export default function({ coreEditor: editor, parser }: { coreEditor: CoreEditor
);

if (!context.endpoint) {
// console.log("couldn't resolve an endpoint.");
return context;
}

if (!ret.urlParamsTokenPath) {
// zero length tokenPath is true
// console.log("Can't extract a valid urlParams token path.");
return context;
}
let tokenPath: any[] = [];
Expand Down Expand Up @@ -859,7 +850,6 @@ export default function({ coreEditor: editor, parser }: { coreEditor: CoreEditor
context.requestStartRow = ret.requestStartRow;
if (!ret.urlTokenPath) {
// zero length tokenPath is true
// console.log("Can't extract a valid url token path.");
return context;
}

Expand All @@ -875,7 +865,6 @@ export default function({ coreEditor: editor, parser }: { coreEditor: CoreEditor
if (!ret.bodyTokenPath) {
// zero length tokenPath is true

// console.log("Can't extract a valid body token path.");
return context;
}

Expand Down
Loading

0 comments on commit 38a8586

Please sign in to comment.