Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fix/top_hit_agg
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.test.ts
  • Loading branch information
sulemanof committed Feb 5, 2020
2 parents a1d0ae3 + 367086b commit 0476a0a
Show file tree
Hide file tree
Showing 79 changed files with 857 additions and 1,032 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('telemetry_usage_collector', () => {
const collectorOptions = createTelemetryUsageCollector(usageCollector, server);

expect(collectorOptions.type).toBe('static_telemetry');
expect(await collectorOptions.fetch()).toEqual(expectedObject);
expect(await collectorOptions.fetch({} as any)).toEqual(expectedObject); // Sending any as the callCluster client because it's not needed in this collector but TS requires it when calling it.
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,27 @@
* under the License.
*/

import { get, omit } from 'lodash';
import { omit } from 'lodash';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';

export function handleKibanaStats(server, response) {
export interface KibanaUsageStats {
kibana: {
index: string;
};
kibana_stats: {
os: {
platform: string;
platformRelease: string;
distro?: string;
distroRelease?: string;
};
};

[plugin: string]: any;
}

export function handleKibanaStats(server: any, response?: KibanaUsageStats) {
if (!response) {
server.log(
['warning', 'telemetry', 'local-stats'],
Expand All @@ -30,8 +48,17 @@ export function handleKibanaStats(server, response) {

const { kibana, kibana_stats: kibanaStats, ...plugins } = response;

const platform = get(kibanaStats, 'os.platform', 'unknown');
const platformRelease = get(kibanaStats, 'os.platformRelease', 'unknown');
const os = {
platform: 'unknown',
platformRelease: 'unknown',
...kibanaStats.os,
};
const formattedOsStats = Object.entries(os).reduce((acc, [key, value]) => {
return {
...acc,
[`${key}s`]: [{ [key]: value, count: 1 }],
};
}, {});

const version = server
.config()
Expand All @@ -44,16 +71,16 @@ export function handleKibanaStats(server, response) {
...omit(kibana, 'index'), // discard index
count: 1,
indices: 1,
os: {
platforms: [{ platform, count: 1 }],
platformReleases: [{ platformRelease, count: 1 }],
},
os: formattedOsStats,
versions: [{ version, count: 1 }],
plugins,
};
}

export async function getKibana(usageCollection, callWithInternalUser) {
export async function getKibana(
usageCollection: UsageCollectionSetup,
callWithInternalUser: CallCluster
): Promise<KibanaUsageStats> {
const usage = await usageCollection.bulkFetch(callWithInternalUser);
return usageCollection.toObject(usage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@ import { get, omit } from 'lodash';
import { getClusterInfo } from './get_cluster_info';
import { getClusterStats } from './get_cluster_stats';
// @ts-ignore
import { getKibana, handleKibanaStats } from './get_kibana';
import { getKibana, handleKibanaStats, KibanaUsageStats } from './get_kibana';
import { StatsGetter } from '../collection_manager';

/**
* Handle the separate local calls by combining them into a single object response that looks like the
* "cluster_stats" document from X-Pack monitoring.
*
* @param {Object} server ??
* @param {Object} clusterInfo Cluster info (GET /)
* @param {Object} clusterStats Cluster stats (GET /_cluster/stats)
* @param {Object} kibana The Kibana Usage stats
* @return {Object} A combined object containing the different responses.
*/
export function handleLocalStats(server: any, clusterInfo: any, clusterStats: any, kibana: any) {
export function handleLocalStats(
server: any,
clusterInfo: any,
clusterStats: any,
kibana: KibanaUsageStats
) {
return {
timestamp: new Date().toISOString(),
cluster_uuid: get(clusterInfo, 'cluster_uuid'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

import { Field } from 'src/plugins/data/public';
import { VisState } from 'src/legacy/core_plugins/visualizations/public';
import { IAggConfig, AggParam, EditorConfig } from '../legacy_imports';
import { IAggConfig, AggParam } from '../legacy_imports';
import { ComboBoxGroupedOptions } from '../utils';
import { EditorConfig } from './utils';

// NOTE: we cannot export the interface with export { InterfaceName }
// as there is currently a bug on babel typescript transform plugin for it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ const mockEditorConfig = {
};

jest.mock('ui/new_platform');
jest.mock('ui/vis/config', () => ({
editorConfigProviders: {
getConfigForAgg: jest.fn(() => mockEditorConfig),
},
jest.mock('./utils', () => ({
getEditorConfig: jest.fn(() => mockEditorConfig),
}));
jest.mock('./agg_params_helper', () => ({
getAggParamsToRender: jest.fn(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ import { i18n } from '@kbn/i18n';
import useUnmount from 'react-use/lib/useUnmount';

import { IndexPattern } from 'src/plugins/data/public';
import {
IAggConfig,
AggGroupNames,
editorConfigProviders,
FixedParam,
TimeIntervalParam,
EditorParamConfig,
} from '../legacy_imports';
import { IAggConfig, AggGroupNames } from '../legacy_imports';

import { DefaultEditorAggSelect } from './agg_select';
import { DefaultEditorAggParam } from './agg_param';
Expand All @@ -46,6 +39,7 @@ import {
initAggParamsState,
} from './agg_params_state';
import { DefaultEditorCommonProps } from './agg_common_props';
import { EditorParamConfig, TimeIntervalParam, FixedParam, getEditorConfig } from './utils';

const FIXED_VALUE_PROP = 'fixedValue';
const DEFAULT_PROP = 'default';
Expand Down Expand Up @@ -93,10 +87,12 @@ function DefaultEditorAggParams({
values: { schema: agg.schema.title },
})
: '';

const editorConfig = useMemo(() => editorConfigProviders.getConfigForAgg(indexPattern, agg), [
const aggTypeName = agg.type?.name;
const fieldName = agg.params?.field?.name;
const editorConfig = useMemo(() => getEditorConfig(indexPattern, aggTypeName, fieldName), [
indexPattern,
agg,
aggTypeName,
fieldName,
]);
const params = useMemo(() => getAggParamsToRender({ agg, editorConfig, metricAggs, state }), [
agg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

import { IndexPattern } from 'src/plugins/data/public';
import { VisState } from 'src/legacy/core_plugins/visualizations/public';
import { IAggConfig, IAggType, AggGroupNames, BUCKET_TYPES, EditorConfig } from '../legacy_imports';
import { IAggConfig, IAggType, AggGroupNames, BUCKET_TYPES } from '../legacy_imports';
import {
getAggParamsToRender,
getAggTypeOptions,
isInvalidParamsTouched,
} from './agg_params_helper';
import { FieldParamEditor, OrderByParamEditor } from './controls';
import { EditorConfig } from './utils';

jest.mock('../utils', () => ({
groupAndSortBy: jest.fn(() => ['indexedFields']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import {
AggParam,
IFieldParamType,
IAggType,
EditorConfig,
} from '../legacy_imports';
import { EditorConfig } from './utils';

interface ParamInstanceBase {
agg: IAggConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/

import { VisState } from 'src/legacy/core_plugins/visualizations/public';
import { IAggConfig, AggParam, EditorConfig } from '../../legacy_imports';
import { IAggConfig, AggParam } from '../../legacy_imports';
import { EditorConfig } from '../utils';

export const aggParamCommonPropsMock = {
agg: {} as IAggConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function TimeIntervalParamEditor({

const onChange = (opts: EuiComboBoxOptionProps[]) => {
const selectedOpt: ComboBoxOption = get(opts, '0');
setValue(selectedOpt ? selectedOpt.key : selectedOpt);
setValue(selectedOpt ? selectedOpt.key : '');

if (selectedOpt) {
agg.write();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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 { i18n } from '@kbn/i18n';
import { IndexPattern } from 'src/plugins/data/public';

/**
* A hidden parameter can be hidden from the UI completely.
*/
interface Param {
hidden?: boolean;
help?: string;
}

/**
* A fixed parameter has a fixed value for a specific field.
* It can optionally also be hidden.
*/
export type FixedParam = Partial<Param> & {
fixedValue: any;
};

/**
* Numeric interval parameters must always be set in the editor to a multiple of
* the specified base. It can optionally also be hidden.
*/
export type NumericIntervalParam = Partial<Param> & {
base: number;
};

/**
* Time interval parameters must always be set in the editor to a multiple of
* the specified base. It can optionally also be hidden.
*/
export type TimeIntervalParam = Partial<Param> & {
default: string;
timeBase: string;
};

export type EditorParamConfig = NumericIntervalParam | TimeIntervalParam | FixedParam | Param;

export interface EditorConfig {
[paramName: string]: EditorParamConfig;
}

export function getEditorConfig(
indexPattern: IndexPattern,
aggTypeName: string,
fieldName: string
): EditorConfig {
const aggRestrictions = indexPattern.getAggregationRestrictions();

if (!aggRestrictions || !aggTypeName || !fieldName) {
return {};
}

// Exclude certain param options for terms:
// otherBucket, missingBucket, orderBy, orderAgg
if (aggTypeName === 'terms') {
return {
otherBucket: {
hidden: true,
},
missingBucket: {
hidden: true,
},
};
}

const fieldAgg = aggRestrictions[aggTypeName] && aggRestrictions[aggTypeName][fieldName];

if (!fieldAgg) {
return {};
}

// Set interval and base interval for histograms based on agg restrictions
if (aggTypeName === 'histogram') {
const interval = fieldAgg.interval;
return interval
? {
intervalBase: {
fixedValue: interval,
},
interval: {
base: interval,
help: i18n.translate('visDefaultEditor.editorConfig.histogram.interval.helpText', {
defaultMessage: 'Must be a multiple of configuration interval: {interval}',
values: { interval },
}),
},
}
: {};
}

// Set date histogram time zone based on agg restrictions
if (aggTypeName === 'date_histogram') {
// Interval is deprecated on date_histogram rollups, but may still be present
// See https://github.com/elastic/kibana/pull/36310
const interval = fieldAgg.calendar_interval || fieldAgg.fixed_interval;
return {
useNormalizedEsInterval: {
fixedValue: false,
},
interval: {
default: interval,
timeBase: interval,
help: i18n.translate(
'visDefaultEditor.editorConfig.dateHistogram.customInterval.helpText',
{
defaultMessage: 'Must be a multiple of configuration interval: {interval}',
values: { interval },
}
),
},
};
}

return {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
* under the License.
*/

export { editorConfigProviders, EditorConfigProviderRegistry } from './editor_config_providers';
export * from './types';
export * from './editor_config';
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ export { getDocLink } from 'ui/documentation_links';
export { documentationLinks } from 'ui/documentation_links/documentation_links';
export { move } from 'ui/utils/collection';
export * from 'ui/vis/lib';
export * from 'ui/vis/config';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class TruncateFormatEditor extends DefaultFormatEditor {
}

render() {
const { formatParams } = this.props;
const { formatParams, onError } = this.props;
const { error, samples } = this.state;

return (
Expand All @@ -55,8 +55,15 @@ export class TruncateFormatEditor extends DefaultFormatEditor {
>
<EuiFieldNumber
defaultValue={formatParams.fieldLength}
min={1}
onChange={e => {
this.onChange({ fieldLength: e.target.value ? Number(e.target.value) : null });
if (e.target.checkValidity()) {
this.onChange({
fieldLength: e.target.value ? Number(e.target.value) : null,
});
} else {
onError(e.target.validationMessage);
}
}}
isInvalid={!!error}
/>
Expand Down
Loading

0 comments on commit 0476a0a

Please sign in to comment.