2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
|
|
|
|
2020-09-29 19:00:29 +02:00
|
|
|
import './expression.scss';
|
2020-11-02 19:15:59 +01:00
|
|
|
import { I18nProvider } from '@kbn/i18n/react';
|
2019-09-17 20:57:53 +02:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2019-10-25 22:32:59 +02:00
|
|
|
import {
|
2020-02-11 19:47:36 +01:00
|
|
|
ExpressionFunctionDefinition,
|
|
|
|
ExpressionRenderDefinition,
|
2019-10-25 22:32:59 +02:00
|
|
|
IInterpreterRenderHandlers,
|
2020-04-15 12:22:37 +02:00
|
|
|
} from '../../../../../src/plugins/expressions/public';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { MetricConfig } from './types';
|
2020-03-25 16:16:11 +01:00
|
|
|
import { FormatFactory, LensMultiTable } from '../types';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { AutoScale } from './auto_scale';
|
2019-09-25 17:11:13 +02:00
|
|
|
import { VisualizationContainer } from '../visualization_container';
|
2020-10-07 10:36:56 +02:00
|
|
|
import { EmptyPlaceholder } from '../shared_components';
|
|
|
|
import { LensIconChartMetric } from '../assets/chart_metric';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
export interface MetricChartProps {
|
|
|
|
data: LensMultiTable;
|
|
|
|
args: MetricConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MetricRender {
|
|
|
|
type: 'render';
|
|
|
|
as: 'lens_metric_chart_renderer';
|
|
|
|
value: MetricChartProps;
|
|
|
|
}
|
|
|
|
|
2020-02-11 19:47:36 +01:00
|
|
|
export const metricChart: ExpressionFunctionDefinition<
|
2019-09-17 20:57:53 +02:00
|
|
|
'lens_metric_chart',
|
|
|
|
LensMultiTable,
|
2020-02-11 19:47:36 +01:00
|
|
|
Omit<MetricConfig, 'layerId'>,
|
2019-09-17 20:57:53 +02:00
|
|
|
MetricRender
|
2020-02-11 19:47:36 +01:00
|
|
|
> = {
|
2019-09-17 20:57:53 +02:00
|
|
|
name: 'lens_metric_chart',
|
|
|
|
type: 'render',
|
|
|
|
help: 'A metric chart',
|
|
|
|
args: {
|
|
|
|
title: {
|
|
|
|
types: ['string'],
|
|
|
|
help: 'The chart title.',
|
|
|
|
},
|
2020-10-01 18:02:37 +02:00
|
|
|
description: {
|
|
|
|
types: ['string'],
|
|
|
|
help: '',
|
|
|
|
},
|
|
|
|
metricTitle: {
|
|
|
|
types: ['string'],
|
|
|
|
help: 'The title of the metric shown.',
|
|
|
|
},
|
2019-09-17 20:57:53 +02:00
|
|
|
accessor: {
|
|
|
|
types: ['string'],
|
|
|
|
help: 'The column whose value is being displayed',
|
|
|
|
},
|
|
|
|
mode: {
|
|
|
|
types: ['string'],
|
|
|
|
options: ['reduced', 'full'],
|
|
|
|
default: 'full',
|
|
|
|
help:
|
|
|
|
'The display mode of the chart - reduced will only show the metric itself without min size',
|
|
|
|
},
|
|
|
|
},
|
2020-02-11 19:47:36 +01:00
|
|
|
inputTypes: ['lens_multitable'],
|
|
|
|
fn(data, args) {
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
|
|
|
type: 'render',
|
|
|
|
as: 'lens_metric_chart_renderer',
|
|
|
|
value: {
|
|
|
|
data,
|
|
|
|
args,
|
|
|
|
},
|
2020-02-11 19:47:36 +01:00
|
|
|
} as MetricRender;
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
2020-02-11 19:47:36 +01:00
|
|
|
};
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
export const getMetricChartRenderer = (
|
2020-03-25 16:16:11 +01:00
|
|
|
formatFactory: Promise<FormatFactory>
|
2020-02-11 19:47:36 +01:00
|
|
|
): ExpressionRenderDefinition<MetricChartProps> => ({
|
2019-09-17 20:57:53 +02:00
|
|
|
name: 'lens_metric_chart_renderer',
|
2019-10-25 18:09:41 +02:00
|
|
|
displayName: 'Metric chart',
|
|
|
|
help: 'Metric chart renderer',
|
2020-02-11 19:47:36 +01:00
|
|
|
validate: () => undefined,
|
2019-09-17 20:57:53 +02:00
|
|
|
reuseDomNode: true,
|
2020-03-25 16:16:11 +01:00
|
|
|
render: async (
|
|
|
|
domNode: Element,
|
|
|
|
config: MetricChartProps,
|
|
|
|
handlers: IInterpreterRenderHandlers
|
|
|
|
) => {
|
|
|
|
const resolvedFormatFactory = await formatFactory;
|
|
|
|
ReactDOM.render(
|
2020-11-02 19:15:59 +01:00
|
|
|
<I18nProvider>
|
|
|
|
<MetricChart {...config} formatFactory={resolvedFormatFactory} />
|
|
|
|
</I18nProvider>,
|
2020-03-25 16:16:11 +01:00
|
|
|
domNode,
|
|
|
|
() => {
|
|
|
|
handlers.done();
|
|
|
|
}
|
|
|
|
);
|
2019-10-25 22:32:59 +02:00
|
|
|
handlers.onDestroy(() => ReactDOM.unmountComponentAtNode(domNode));
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export function MetricChart({
|
|
|
|
data,
|
|
|
|
args,
|
|
|
|
formatFactory,
|
|
|
|
}: MetricChartProps & { formatFactory: FormatFactory }) {
|
2020-10-01 18:02:37 +02:00
|
|
|
const { metricTitle, title, description, accessor, mode } = args;
|
2019-09-17 20:57:53 +02:00
|
|
|
const firstTable = Object.values(data.tables)[0];
|
2020-03-17 14:57:52 +01:00
|
|
|
if (!accessor) {
|
|
|
|
return (
|
2020-10-01 18:02:37 +02:00
|
|
|
<VisualizationContainer
|
|
|
|
reportTitle={title}
|
|
|
|
reportDescription={description}
|
|
|
|
className="lnsMetricExpression__container"
|
|
|
|
/>
|
2020-03-17 14:57:52 +01:00
|
|
|
);
|
|
|
|
}
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-10-07 10:36:56 +02:00
|
|
|
if (!firstTable) {
|
|
|
|
return <EmptyPlaceholder icon={LensIconChartMetric} />;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 10:36:56 +02:00
|
|
|
const column = firstTable.columns[0];
|
|
|
|
const row = firstTable.rows[0];
|
|
|
|
|
|
|
|
// NOTE: Cardinality and Sum never receives "null" as value, but always 0, even for empty dataset.
|
|
|
|
// Mind falsy values here as 0!
|
|
|
|
const shouldShowResults = row[accessor] != null;
|
|
|
|
|
|
|
|
if (!shouldShowResults) {
|
|
|
|
return <EmptyPlaceholder icon={LensIconChartMetric} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value =
|
2020-10-16 18:46:51 +02:00
|
|
|
column && column.meta?.params
|
|
|
|
? formatFactory(column.meta?.params).convert(row[accessor])
|
2020-10-07 10:36:56 +02:00
|
|
|
: Number(Number(row[accessor]).toFixed(3)).toString();
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return (
|
2020-10-01 18:02:37 +02:00
|
|
|
<VisualizationContainer
|
|
|
|
reportTitle={title}
|
|
|
|
reportDescription={description}
|
|
|
|
className="lnsMetricExpression__container"
|
|
|
|
>
|
2019-09-17 20:57:53 +02:00
|
|
|
<AutoScale>
|
|
|
|
<div data-test-subj="lns_metric_value" style={{ fontSize: '60pt', fontWeight: 600 }}>
|
|
|
|
{value}
|
|
|
|
</div>
|
|
|
|
{mode === 'full' && (
|
|
|
|
<div data-test-subj="lns_metric_title" style={{ fontSize: '24pt' }}>
|
2020-10-01 18:02:37 +02:00
|
|
|
{metricTitle}
|
2019-09-17 20:57:53 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</AutoScale>
|
2019-09-25 17:11:13 +02:00
|
|
|
</VisualizationContainer>
|
2019-09-17 20:57:53 +02:00
|
|
|
);
|
|
|
|
}
|