Skip to content

Commit

Permalink
[Logs UI / Metrics UI] New Platform migration server followups (#51615)…
Browse files Browse the repository at this point in the history
… (#53320)
  • Loading branch information
Kerry350 authored Dec 17, 2019
1 parent 8ee2854 commit f86ae6c
Show file tree
Hide file tree
Showing 35 changed files with 278 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ export function getApmIndicesConfig(config: APMConfig): ApmIndicesConfig {
};
}

// export async function getApmIndices(context: APMRequestHandlerContext) {
// return _getApmIndices(context.core, context.config);
// }

export async function getApmIndices({
config,
savedObjectsClient
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/infra/common/http_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

export * from './log_analysis';
export * from './metadata_api';
export * from './metrics_explorer';
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import * as rt from 'io-ts';

export const METRIC_EXPLORER_AGGREGATIONS = [
'avg',
'max',
'min',
'cardinality',
'rate',
'count',
] as const;

type MetricExplorerAggregations = typeof METRIC_EXPLORER_AGGREGATIONS[number];

const metricsExplorerAggregationKeys = METRIC_EXPLORER_AGGREGATIONS.reduce<
Record<MetricExplorerAggregations, null>
>((acc, agg) => ({ ...acc, [agg]: null }), {} as Record<MetricExplorerAggregations, null>);

export const metricsExplorerAggregationRT = rt.keyof(metricsExplorerAggregationKeys);

export const metricsExplorerMetricRequiredFieldsRT = rt.type({
aggregation: metricsExplorerAggregationRT,
});

export const metricsExplorerMetricOptionalFieldsRT = rt.partial({
field: rt.union([rt.string, rt.undefined]),
});

export const metricsExplorerMetricRT = rt.intersection([
metricsExplorerMetricRequiredFieldsRT,
metricsExplorerMetricOptionalFieldsRT,
]);

export const timeRangeRT = rt.type({
field: rt.string,
from: rt.number,
to: rt.number,
interval: rt.string,
});

export const metricsExplorerRequestBodyRequiredFieldsRT = rt.type({
timerange: timeRangeRT,
indexPattern: rt.string,
metrics: rt.array(metricsExplorerMetricRT),
});

export const metricsExplorerRequestBodyOptionalFieldsRT = rt.partial({
groupBy: rt.union([rt.string, rt.null, rt.undefined]),
afterKey: rt.union([rt.string, rt.null, rt.undefined]),
limit: rt.union([rt.number, rt.null, rt.undefined]),
filterQuery: rt.union([rt.string, rt.null, rt.undefined]),
});

export const metricsExplorerRequestBodyRT = rt.intersection([
metricsExplorerRequestBodyRequiredFieldsRT,
metricsExplorerRequestBodyOptionalFieldsRT,
]);

export const metricsExplorerPageInfoRT = rt.type({
total: rt.number,
afterKey: rt.union([rt.string, rt.null]),
});

export const metricsExplorerColumnTypeRT = rt.keyof({
date: null,
number: null,
string: null,
});

export const metricsExplorerColumnRT = rt.type({
name: rt.string,
type: metricsExplorerColumnTypeRT,
});

export const metricsExplorerRowRT = rt.intersection([
rt.type({
timestamp: rt.number,
}),
rt.record(rt.string, rt.union([rt.string, rt.number, rt.null, rt.undefined])),
]);

export const metricsExplorerSeriesRT = rt.type({
id: rt.string,
columns: rt.array(metricsExplorerColumnRT),
rows: rt.array(metricsExplorerRowRT),
});

export const metricsExplorerResponseRT = rt.type({
series: rt.array(metricsExplorerSeriesRT),
pageInfo: metricsExplorerPageInfoRT,
});
8 changes: 5 additions & 3 deletions x-pack/legacy/plugins/infra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { getConfigSchema } from './server/kibana.index';
import { savedObjectMappings } from './server/saved_objects';
import { plugin, InfraServerPluginDeps } from './server/new_platform_index';
import { InfraSetup } from '../../../plugins/infra/server';
import { PluginSetupContract as FeaturesPluginSetup } from '../../../plugins/features/server';
import { SpacesPluginSetup } from '../../../plugins/spaces/server';
import { APMPluginContract } from '../../../plugins/apm/server';

const APP_ID = 'infra';
Expand Down Expand Up @@ -91,8 +93,8 @@ export function infra(kibana: any) {
indexPatternsServiceFactory: legacyServer.indexPatternsServiceFactory,
},
metrics: legacyServer.plugins.metrics,
spaces: plugins.spaces,
features: plugins.features,
spaces: plugins.spaces as SpacesPluginSetup,
features: plugins.features as FeaturesPluginSetup,
// NP_NOTE: [TSVB_GROUP] Huge hack to make TSVB (getVisData()) work with raw requests that
// originate from the New Platform router (and are very different to the old request object).
// Once TSVB has migrated over to NP, and can work with the new raw requests, or ideally just
Expand All @@ -113,7 +115,7 @@ export function infra(kibana: any) {

const libs = infraPluginInstance.getLibs();

// NP_TODO how do we replace this? Answer: return from setup function.
// NP_NOTE: Left here for now for legacy plugins to consume
legacyServer.expose(
'defineInternalSourceConfiguration',
libs.sources.defineInternalSourceConfiguration.bind(libs.sources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,43 @@ import { i18n } from '@kbn/i18n';
import React, { useCallback } from 'react';
import { MetricsExplorerAggregation } from '../../../server/routes/metrics_explorer/types';
import { MetricsExplorerOptions } from '../../containers/metrics_explorer/use_metrics_explorer_options';
import {
metricsExplorerAggregationRT,
METRIC_EXPLORER_AGGREGATIONS,
} from '../../../common/http_api/metrics_explorer';

interface Props {
options: MetricsExplorerOptions;
fullWidth: boolean;
onChange: (aggregation: MetricsExplorerAggregation) => void;
}

const isMetricsExplorerAggregation = (subject: any): subject is MetricsExplorerAggregation => {
return Object.keys(MetricsExplorerAggregation).includes(subject);
};

export const MetricsExplorerAggregationPicker = ({ options, onChange }: Props) => {
const AGGREGATION_LABELS = {
[MetricsExplorerAggregation.avg]: i18n.translate(
'xpack.infra.metricsExplorer.aggregationLables.avg',
{ defaultMessage: 'Average' }
),
[MetricsExplorerAggregation.max]: i18n.translate(
'xpack.infra.metricsExplorer.aggregationLables.max',
{ defaultMessage: 'Max' }
),
[MetricsExplorerAggregation.min]: i18n.translate(
'xpack.infra.metricsExplorer.aggregationLables.min',
{ defaultMessage: 'Min' }
),
[MetricsExplorerAggregation.cardinality]: i18n.translate(
'xpack.infra.metricsExplorer.aggregationLables.cardinality',
{ defaultMessage: 'Cardinality' }
),
[MetricsExplorerAggregation.rate]: i18n.translate(
'xpack.infra.metricsExplorer.aggregationLables.rate',
{ defaultMessage: 'Rate' }
),
[MetricsExplorerAggregation.count]: i18n.translate(
'xpack.infra.metricsExplorer.aggregationLables.count',
{ defaultMessage: 'Document count' }
),
['avg']: i18n.translate('xpack.infra.metricsExplorer.aggregationLables.avg', {
defaultMessage: 'Average',
}),
['max']: i18n.translate('xpack.infra.metricsExplorer.aggregationLables.max', {
defaultMessage: 'Max',
}),
['min']: i18n.translate('xpack.infra.metricsExplorer.aggregationLables.min', {
defaultMessage: 'Min',
}),
['cardinality']: i18n.translate('xpack.infra.metricsExplorer.aggregationLables.cardinality', {
defaultMessage: 'Cardinality',
}),
['rate']: i18n.translate('xpack.infra.metricsExplorer.aggregationLables.rate', {
defaultMessage: 'Rate',
}),
['count']: i18n.translate('xpack.infra.metricsExplorer.aggregationLables.count', {
defaultMessage: 'Document count',
}),
};

const handleChange = useCallback(
e => {
const aggregation =
(isMetricsExplorerAggregation(e.target.value) && e.target.value) ||
MetricsExplorerAggregation.avg;
(metricsExplorerAggregationRT.is(e.target.value) && e.target.value) || 'avg';
onChange(aggregation);
},
[onChange]
Expand All @@ -66,7 +59,7 @@ export const MetricsExplorerAggregationPicker = ({ options, onChange }: Props) =
})}
fullWidth
value={options.aggregation}
options={Object.keys(MetricsExplorerAggregation).map(k => ({
options={METRIC_EXPLORER_AGGREGATIONS.map(k => ({
text: AGGREGATION_LABELS[k as MetricsExplorerAggregation],
value: k,
}))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
*/

import { calculateDomain } from './calculate_domain';
import {
MetricsExplorerSeries,
MetricsExplorerAggregation,
MetricsExplorerColumnType,
} from '../../../../server/routes/metrics_explorer/types';
import { MetricsExplorerSeries } from '../../../../server/routes/metrics_explorer/types';
import { MetricsExplorerOptionsMetric } from '../../../containers/metrics_explorer/use_metrics_explorer_options';
import { MetricsExplorerColor } from '../../../../common/color_palette';
describe('calculateDomain()', () => {
const series: MetricsExplorerSeries = {
id: 'test-01',
columns: [
{ type: MetricsExplorerColumnType.date, name: 'timestamp' },
{ type: MetricsExplorerColumnType.number, name: 'metric_0' },
{ type: MetricsExplorerColumnType.number, name: 'metric_1' },
{ type: MetricsExplorerColumnType.string, name: 'groupBy' },
{ type: 'date', name: 'timestamp' },
{ type: 'number', name: 'metric_0' },
{ type: 'number', name: 'metric_1' },
{ type: 'string', name: 'groupBy' },
],
rows: [
{ timestamp: 1562860500000, metric_0: null, metric_1: null },
Expand All @@ -31,12 +27,12 @@ describe('calculateDomain()', () => {
};
const metrics: MetricsExplorerOptionsMetric[] = [
{
aggregation: MetricsExplorerAggregation.avg,
aggregation: 'avg',
field: 'system.memory.free',
color: MetricsExplorerColor.color0,
},
{
aggregation: MetricsExplorerAggregation.avg,
aggregation: 'avg',
field: 'system.memory.used.bytes',
color: MetricsExplorerColor.color1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import {
MetricsExplorerAggregation,
MetricsExplorerMetric,
} from '../../../../server/routes/metrics_explorer/types';
import { MetricsExplorerMetric } from '../../../../server/routes/metrics_explorer/types';
import { createFormatter } from '../../../utils/formatters';
import { InfraFormatterType } from '../../../lib/lib';
import { metricToFormat } from './metric_to_format';
export const createFormatterForMetric = (metric?: MetricsExplorerMetric) => {
if (metric && metric.field) {
const format = metricToFormat(metric);
if (
format === InfraFormatterType.bits &&
metric.aggregation === MetricsExplorerAggregation.rate
) {
if (format === InfraFormatterType.bits && metric.aggregation === 'rate') {
return createFormatter(InfraFormatterType.bits, '{{value}}/s');
}
return createFormatter(format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@
*/

import { createFormatterForMetric } from './create_formatter_for_metric';
import { MetricsExplorerAggregation } from '../../../../server/routes/metrics_explorer/types';
import { MetricsExplorerMetric } from '../../../../server/routes/metrics_explorer/types';

describe('createFormatterForMetric()', () => {
it('should just work for count', () => {
const metric = { aggregation: MetricsExplorerAggregation.count };
const metric: MetricsExplorerMetric = { aggregation: 'count' };
const format = createFormatterForMetric(metric);
expect(format(1291929)).toBe('1,291,929');
});
it('should just work for numerics', () => {
const metric = { aggregation: MetricsExplorerAggregation.avg, field: 'system.load.1' };
const metric: MetricsExplorerMetric = { aggregation: 'avg', field: 'system.load.1' };
const format = createFormatterForMetric(metric);
expect(format(1000.2)).toBe('1,000.2');
});
it('should just work for percents', () => {
const metric = { aggregation: MetricsExplorerAggregation.avg, field: 'system.cpu.total.pct' };
const metric: MetricsExplorerMetric = { aggregation: 'avg', field: 'system.cpu.total.pct' };
const format = createFormatterForMetric(metric);
expect(format(0.349)).toBe('34.9%');
});
it('should just work for rates', () => {
const metric = {
aggregation: MetricsExplorerAggregation.rate,
const metric: MetricsExplorerMetric = {
aggregation: 'rate',
field: 'system.network.out.bytes',
};
const format = createFormatterForMetric(metric);
expect(format(103929292)).toBe('831.4Mbit/s');
});
it('should just work for bytes', () => {
const metric = {
aggregation: MetricsExplorerAggregation.avg,
const metric: MetricsExplorerMetric = {
aggregation: 'avg',
field: 'system.network.out.bytes',
};
const format = createFormatterForMetric(metric);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/

import { createMetricLabel } from './create_metric_label';
import { MetricsExplorerAggregation } from '../../../../server/routes/metrics_explorer/types';
import { MetricsExplorerMetric } from '../../../../server/routes/metrics_explorer/types';

describe('createMetricLabel()', () => {
it('should work with metrics with fields', () => {
const metric = { aggregation: MetricsExplorerAggregation.avg, field: 'system.load.1' };
const metric: MetricsExplorerMetric = { aggregation: 'avg', field: 'system.load.1' };
expect(createMetricLabel(metric)).toBe('avg(system.load.1)');
});
it('should work with document count', () => {
const metric = { aggregation: MetricsExplorerAggregation.count };
const metric: MetricsExplorerMetric = { aggregation: 'count' };
expect(createMetricLabel(metric)).toBe('count()');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { createTSVBLink, createFilterFromOptions } from './create_tsvb_link';
import { source, options, timeRange, chartOptions } from '../../../utils/fixtures/metrics_explorer';
import uuid from 'uuid';
import { OutputBuffer } from 'uuid/interfaces';
import { MetricsExplorerAggregation } from '../../../../server/routes/metrics_explorer/types';
import {
MetricsExplorerYAxisMode,
MetricsExplorerChartType,
} from '../../../containers/metrics_explorer/use_metrics_explorer_options';
import { MetricsExplorerOptions } from '../../../containers/metrics_explorer/use_metrics_explorer_options';

jest.mock('uuid');
const mockedUuid = uuid as jest.Mocked<typeof uuid>;
Expand All @@ -28,11 +28,9 @@ describe('createTSVBLink()', () => {
});

it('should work with rates', () => {
const customOptions = {
const customOptions: MetricsExplorerOptions = {
...options,
metrics: [
{ aggregation: MetricsExplorerAggregation.rate, field: 'system.network.out.bytes' },
],
metrics: [{ aggregation: 'rate', field: 'system.network.out.bytes' }],
};
const link = createTSVBLink(source, customOptions, series, timeRange, chartOptions);
expect(link).toBe(
Expand Down
Loading

0 comments on commit f86ae6c

Please sign in to comment.