Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/7.x' into backport/7.x/pr-76528
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Sep 7, 2020
2 parents 9ca86fb + 293124c commit 92ca404
Show file tree
Hide file tree
Showing 132 changed files with 6,738 additions and 1,412 deletions.
2 changes: 1 addition & 1 deletion src/legacy/server/logging/log_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class TransformObjStream extends Stream.Transform {
method: event.method || '',
headers: event.headers,
remoteAddress: source.remoteAddress,
userAgent: source.remoteAddress,
userAgent: source.userAgent,
referer: source.referer,
};

Expand Down
4 changes: 3 additions & 1 deletion src/legacy/server/logging/log_format_json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ describe('KbnLoggerJsonFormat', () => {
},
};
const result = await createPromiseFromStreams([createListStream([event]), format]);
const { type, method, statusCode, message } = JSON.parse(result);
const { type, method, statusCode, message, req } = JSON.parse(result);

expect(type).toBe('response');
expect(method).toBe('GET');
expect(statusCode).toBe(200);
expect(message).toBe('GET /path/to/resource 200 12000ms - 13.0B');
expect(req.remoteAddress).toBe('127.0.0.1');
expect(req.userAgent).toBe('Test Thing');
});

it('ops', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ export interface QueryState {
// Warning: (ae-missing-release-tag) "QueryStringInput" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export const QueryStringInput: React.FC<Pick<Props_3, "query" | "prepend" | "size" | "placeholder" | "onChange" | "onBlur" | "onSubmit" | "indexPatterns" | "dataTestSubj" | "screenTitle" | "disableAutoFocus" | "persistedLog" | "bubbleSubmitEvent" | "languageSwitcherPopoverAnchorPosition" | "onChangeQueryInputFocus">>;
export const QueryStringInput: React.FC<Pick<Props_3, "query" | "prepend" | "size" | "className" | "placeholder" | "onChange" | "onBlur" | "onSubmit" | "indexPatterns" | "dataTestSubj" | "screenTitle" | "disableAutoFocus" | "persistedLog" | "bubbleSubmitEvent" | "languageSwitcherPopoverAnchorPosition" | "onChangeQueryInputFocus">>;

// @public (undocumented)
export type QuerySuggestion = QuerySuggestionBasic | QuerySuggestionField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import React, { Component, RefObject, createRef } from 'react';
import { i18n } from '@kbn/i18n';

import classNames from 'classnames';
import {
EuiTextArea,
EuiOutsideClickDetector,
Expand Down Expand Up @@ -62,6 +62,7 @@ interface Props {
onSubmit?: (query: Query) => void;
dataTestSubj?: string;
size?: SuggestionsListSize;
className?: string;
}

interface State {
Expand Down Expand Up @@ -586,9 +587,12 @@ export class QueryStringInputUI extends Component<Props, State> {
'aria-owns': 'kbnTypeahead__items',
};
const ariaCombobox = { ...isSuggestionsVisible, role: 'combobox' };

const className = classNames(
'euiFormControlLayout euiFormControlLayout--group kbnQueryBar__wrap',
this.props.className
);
return (
<div className="euiFormControlLayout euiFormControlLayout--group kbnQueryBar__wrap">
<div className={className}>
{this.props.prepend}
<EuiOutsideClickDetector onOutsideClick={this.onOutsideClick}>
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
/*
* 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 angular from 'angular';
import 'angular-mocks';
import $ from 'jquery';

import sinon from 'sinon';

import { PrivateProvider, initAngularBootstrap } from '../../../../../kibana_legacy/public';
import { FixedScrollProvider } from './fixed_scroll';
import { DebounceProviderTimeout } from './debounce/debounce';

const testModuleName = 'fixedScroll';

angular
.module(testModuleName, [])
.provider('Private', PrivateProvider)
.service('debounce', ['$timeout', DebounceProviderTimeout])
.directive('fixedScroll', FixedScrollProvider);

describe('FixedScroll directive', function () {
const sandbox = sinon.createSandbox();
let mockWidth;
let mockHeight;
let currentWidth = 120;
let currentHeight = 120;
let currentJqLiteWidth = 120;
let spyScrollWidth;

let compile;
let flushPendingTasks;
const trash = [];

beforeAll(() => {
mockWidth = jest.spyOn($.prototype, 'width').mockImplementation(function (width) {
if (width === undefined) {
return currentWidth;
} else {
currentWidth = width;
return this;
}
});
mockHeight = jest.spyOn($.prototype, 'height').mockImplementation(function (height) {
if (height === undefined) {
return currentHeight;
} else {
currentHeight = height;
return this;
}
});
angular.element.prototype.width = jest.fn(function (width) {
if (width === undefined) {
return currentJqLiteWidth;
} else {
currentJqLiteWidth = width;
return this;
}
});
angular.element.prototype.offset = jest.fn(() => ({ top: 0 }));
});

beforeEach(() => {
currentJqLiteWidth = 120;
initAngularBootstrap();

angular.mock.module(testModuleName);
angular.mock.inject(($compile, $rootScope, $timeout) => {
flushPendingTasks = function flushPendingTasks() {
$rootScope.$digest();
$timeout.flush();
};

compile = function (ratioY, ratioX) {
if (ratioX == null) ratioX = ratioY;

// since the directive works at the sibling level we create a
// parent for everything to happen in
const $parent = $('<div>').css({
position: 'fixed',
top: 0,
left: 0,
right: 0,
bottom: 0,
});

$parent.appendTo(document.body);
trash.push($parent);

const $el = $('<div fixed-scroll></div>')
.css({
'overflow-x': 'auto',
width: $parent.width(),
})
.appendTo($parent);

spyScrollWidth = jest.spyOn(window.HTMLElement.prototype, 'scrollWidth', 'get');
spyScrollWidth.mockReturnValue($parent.width() * ratioX);
angular.element.prototype.height = jest.fn(() => $parent.height() * ratioY);

const $content = $('<div>')
.css({
width: $parent.width() * ratioX,
height: $parent.height() * ratioY,
})
.appendTo($el);

$compile($parent)($rootScope);
flushPendingTasks();

return {
$container: $el,
$content: $content,
$scroller: $parent.find('.fixed-scroll-scroller'),
};
};
});
});

afterEach(function () {
trash.splice(0).forEach(function ($el) {
$el.remove();
});

sandbox.restore();
spyScrollWidth.mockRestore();
});

afterAll(() => {
mockWidth.mockRestore();
mockHeight.mockRestore();
delete angular.element.prototype.width;
delete angular.element.prototype.height;
delete angular.element.prototype.offset;
});

test('does nothing when not needed', function () {
let els = compile(0.5, 1.5);
expect(els.$scroller).toHaveLength(0);

els = compile(1.5, 0.5);
expect(els.$scroller).toHaveLength(0);
});

test('attaches a scroller below the element when the content is larger then the container', function () {
const els = compile(1.5);
expect(els.$scroller.length).toBe(1);
});

test('copies the width of the container', function () {
const els = compile(1.5);
expect(els.$scroller.width()).toBe(els.$container.width());
});

test('mimics the scrollWidth of the element', function () {
const els = compile(1.5);
expect(els.$scroller.prop('scrollWidth')).toBe(els.$container.prop('scrollWidth'));
});

describe('scroll event handling / tug of war prevention', function () {
test('listens when needed, unlistens when not needed', function (done) {
const on = sandbox.spy($.fn, 'on');
const off = sandbox.spy($.fn, 'off');
const jqLiteOn = sandbox.spy(angular.element.prototype, 'on');
const jqLiteOff = sandbox.spy(angular.element.prototype, 'off');

const els = compile(1.5);
expect(on.callCount).toBe(1);
expect(jqLiteOn.callCount).toBe(1);
checkThisVals('$.fn.on', on, jqLiteOn);

expect(off.callCount).toBe(0);
expect(jqLiteOff.callCount).toBe(0);
currentJqLiteWidth = els.$container.prop('scrollWidth');
flushPendingTasks();
expect(off.callCount).toBe(1);
expect(jqLiteOff.callCount).toBe(1);
checkThisVals('$.fn.off', off, jqLiteOff);
done();

function checkThisVals(namejQueryFn, spyjQueryFn, spyjqLiteFn) {
// the this values should be different
expect(spyjQueryFn.thisValues[0].is(spyjqLiteFn.thisValues[0])).toBeFalsy();
// but they should be either $scroller or $container
const el = spyjQueryFn.thisValues[0];

if (el.is(els.$scroller) || el.is(els.$container)) return;

done.fail('expected ' + namejQueryFn + ' to be called with $scroller or $container');
}
});

// Turn off this row because tests failed.
// Scroll event is not catched in fixed_scroll.
// As container is jquery element in test but inside fixed_scroll it's a jqLite element.
// it would need jquery in jest to make this work.
[
//{ from: '$container', to: '$scroller' },
{ from: '$scroller', to: '$container' },
].forEach(function (names) {
describe('scroll events ' + JSON.stringify(names), function () {
let spyJQueryScrollLeft;
let spyJQLiteScrollLeft;
let els;
let $from;
let $to;

beforeEach(function () {
spyJQueryScrollLeft = sandbox.spy($.fn, 'scrollLeft');
spyJQLiteScrollLeft = sandbox.stub();
angular.element.prototype.scrollLeft = spyJQLiteScrollLeft;
els = compile(1.5);
$from = els[names.from];
$to = els[names.to];
});

test('transfers the scrollLeft', function () {
expect(spyJQueryScrollLeft.callCount).toBe(0);
expect(spyJQLiteScrollLeft.callCount).toBe(0);
$from.scroll();
expect(spyJQueryScrollLeft.callCount).toBe(1);
expect(spyJQLiteScrollLeft.callCount).toBe(1);

// first call should read the scrollLeft from the $container
const firstCall = spyJQueryScrollLeft.getCall(0);
expect(firstCall.args).toEqual([]);

// second call should be setting the scrollLeft on the $scroller
const secondCall = spyJQLiteScrollLeft.getCall(0);
expect(secondCall.args).toEqual([firstCall.returnValue]);
});

/**
* In practice, calling $el.scrollLeft() causes the "scroll" event to trigger,
* but the browser seems to be very careful about triggering the event too much
* and I can't reliably recreate the browsers behavior in a test. So... faking it!
*/
test('prevents tug of war by ignoring echo scroll events', function () {
$from.scroll();
expect(spyJQueryScrollLeft.callCount).toBe(1);
expect(spyJQLiteScrollLeft.callCount).toBe(1);

spyJQueryScrollLeft.resetHistory();
spyJQLiteScrollLeft.resetHistory();
$to.scroll();
expect(spyJQueryScrollLeft.callCount).toBe(0);
expect(spyJQLiteScrollLeft.callCount).toBe(0);
});
});
});
});
});
58 changes: 58 additions & 0 deletions src/plugins/telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,61 @@ Telemetry allows Kibana features to have usage tracked in the wild. The general
3. Viewing usage data in the Kibana instance of the telemetry cluster (Viewing).

This plugin is responsible for sending usage data to the telemetry cluster. For collecting usage data, use the [`usageCollection` plugin](../usage_collection/README.md)

## Telemetry Plugin public API

### Setup

The `setup` function exposes the following interface:

- `getTelemetryUrl: () => Promise<URL>`:
An async function that resolves into the telemetry Url used to send telemetry. The url is wrapped with node's [URL constructor](https://nodejs.org/api/url.html). Here is an example on how to grab the url origin:
```
const telemetryUrl = await getTelemetryUrl();
> telemetryUrl.origin; // 'https://telemetry.elastic.co'
```
Note that the telemetry URL is a kibana.yml configuration hence it is recommended to call the `getTelemetryUrl` everytime before using the actual url.

### Start

The `start` function exposes the following interface:

- `async getIsOptedIn(): Promise<boolean>`:
An async function that resolves into `true` if the user has opted into send Elastic usage data.
Resolves to `false` if the user explicitly opted out of sending usage data to Elastic or did not choose
to opt-in or out yet after a minor or major upgrade (only when previously opted out).

### Usage

To use the exposed plugin start and setup contracts:

1. Make sure `telemetry` is in your `optionalPlugins` in the `kibana.json` file:

```json5
// <plugin>/kibana.json
{
"id": "...",
"optionalPlugins": ["telemetry"]
}
```

2. Use the exposed contracts:
```ts
// <plugin>/server/plugin.ts

import { TelemetryPluginsStart } from '../telemetry/server`;

interface MyPlyginStartDeps {
telemetry?: TelemetryPluginsStart;
}

class MyPlugin {
public async start(
core: CoreStart,
{ telemetry }: MyPlyginStartDeps
) {
const isOptedIn = await telemetry?.getIsOptedIn();
...
}
}
```
Loading

0 comments on commit 92ca404

Please sign in to comment.