From 671bc942a5201b0a5f9d5a1772e30b30b882531b Mon Sep 17 00:00:00 2001 From: Daniil Date: Mon, 25 Jan 2021 14:11:59 +0300 Subject: [PATCH] [Data table] Add telemetry for table vis split mode (#88604) * Add telemetry for table vis * Update telemetry schema * Add unit tests * Update license * Use soClient instead of esClient, update tests Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- src/plugins/telemetry/schema/oss_plugins.json | 30 +++++++ src/plugins/vis_type_table/common/index.ts | 9 ++ src/plugins/vis_type_table/common/types.ts | 28 ++++++ src/plugins/vis_type_table/jest.config.js | 1 + .../public/components/table_vis_options.tsx | 2 +- .../components/table_vis_options_lazy.tsx | 2 +- .../vis_type_table/public/components/utils.ts | 2 +- .../public/legacy/table_vis_legacy_fn.ts | 5 +- .../public/legacy/table_vis_legacy_type.ts | 4 +- .../vis_type_table/public/table_vis_fn.ts | 5 +- .../vis_type_table/public/table_vis_type.ts | 4 +- .../vis_type_table/public/to_ast.test.ts | 2 +- src/plugins/vis_type_table/public/to_ast.ts | 3 +- src/plugins/vis_type_table/public/types.ts | 19 +---- .../public/utils/use/use_formatted_columns.ts | 3 +- .../public/utils/use/use_pagination.ts | 2 +- src/plugins/vis_type_table/server/index.ts | 10 ++- .../server/usage_collector/get_stats.test.ts | 67 +++++++++++++++ .../server/usage_collector/get_stats.ts | 85 +++++++++++++++++++ .../server/usage_collector/index.ts | 9 ++ .../register_usage_collector.test.ts | 56 ++++++++++++ .../register_usage_collector.ts | 32 +++++++ src/plugins/vis_type_table/tsconfig.json | 1 + src/plugins/visualizations/common/index.ts | 10 +++ src/plugins/visualizations/common/types.ts | 32 +++++++ src/plugins/visualizations/public/index.ts | 5 +- .../public/legacy/vis_update_state.d.ts | 2 +- .../saved_visualization_references.test.ts | 3 +- src/plugins/visualizations/public/types.ts | 16 +--- src/plugins/visualizations/public/vis.ts | 5 +- 30 files changed, 398 insertions(+), 56 deletions(-) create mode 100644 src/plugins/vis_type_table/common/index.ts create mode 100644 src/plugins/vis_type_table/common/types.ts create mode 100644 src/plugins/vis_type_table/server/usage_collector/get_stats.test.ts create mode 100644 src/plugins/vis_type_table/server/usage_collector/get_stats.ts create mode 100644 src/plugins/vis_type_table/server/usage_collector/index.ts create mode 100644 src/plugins/vis_type_table/server/usage_collector/register_usage_collector.test.ts create mode 100644 src/plugins/vis_type_table/server/usage_collector/register_usage_collector.ts create mode 100644 src/plugins/visualizations/common/index.ts create mode 100644 src/plugins/visualizations/common/types.ts diff --git a/src/plugins/telemetry/schema/oss_plugins.json b/src/plugins/telemetry/schema/oss_plugins.json index 50a08d96de951..27d9b5ce83203 100644 --- a/src/plugins/telemetry/schema/oss_plugins.json +++ b/src/plugins/telemetry/schema/oss_plugins.json @@ -5247,6 +5247,36 @@ } } }, + "vis_type_table": { + "properties": { + "total": { + "type": "long" + }, + "total_split": { + "type": "long" + }, + "split_columns": { + "properties": { + "total": { + "type": "long" + }, + "enabled": { + "type": "long" + } + } + }, + "split_rows": { + "properties": { + "total": { + "type": "long" + }, + "enabled": { + "type": "long" + } + } + } + } + }, "vis_type_vega": { "properties": { "vega_lib_specs_total": { diff --git a/src/plugins/vis_type_table/common/index.ts b/src/plugins/vis_type_table/common/index.ts new file mode 100644 index 0000000000000..cc54db82d37e7 --- /dev/null +++ b/src/plugins/vis_type_table/common/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +export * from './types'; diff --git a/src/plugins/vis_type_table/common/types.ts b/src/plugins/vis_type_table/common/types.ts new file mode 100644 index 0000000000000..3380e730770c3 --- /dev/null +++ b/src/plugins/vis_type_table/common/types.ts @@ -0,0 +1,28 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +export const VIS_TYPE_TABLE = 'table'; + +export enum AggTypes { + SUM = 'sum', + AVG = 'avg', + MIN = 'min', + MAX = 'max', + COUNT = 'count', +} + +export interface TableVisParams { + perPage: number | ''; + showPartialRows: boolean; + showMetricsAtAllLevels: boolean; + showToolbar: boolean; + showTotal: boolean; + totalFunc: AggTypes; + percentageCol: string; + row?: boolean; +} diff --git a/src/plugins/vis_type_table/jest.config.js b/src/plugins/vis_type_table/jest.config.js index 4e5ddbcf8d7c5..3a7906f6ec543 100644 --- a/src/plugins/vis_type_table/jest.config.js +++ b/src/plugins/vis_type_table/jest.config.js @@ -11,4 +11,5 @@ module.exports = { rootDir: '../../..', roots: ['/src/plugins/vis_type_table'], testRunner: 'jasmine2', + collectCoverageFrom: ['/src/plugins/vis_type_table/**/*.{js,ts,tsx}'], }; diff --git a/src/plugins/vis_type_table/public/components/table_vis_options.tsx b/src/plugins/vis_type_table/public/components/table_vis_options.tsx index eb76659a601d6..a70ecb43f1be7 100644 --- a/src/plugins/vis_type_table/public/components/table_vis_options.tsx +++ b/src/plugins/vis_type_table/public/components/table_vis_options.tsx @@ -19,7 +19,7 @@ import { NumberInputOption, VisOptionsProps, } from '../../../vis_default_editor/public'; -import { TableVisParams } from '../types'; +import { TableVisParams } from '../../common'; import { totalAggregations } from './utils'; const { tabifyGetColumns } = search; diff --git a/src/plugins/vis_type_table/public/components/table_vis_options_lazy.tsx b/src/plugins/vis_type_table/public/components/table_vis_options_lazy.tsx index fb0044a986f5e..716b77e9c91d2 100644 --- a/src/plugins/vis_type_table/public/components/table_vis_options_lazy.tsx +++ b/src/plugins/vis_type_table/public/components/table_vis_options_lazy.tsx @@ -9,7 +9,7 @@ import React, { lazy, Suspense } from 'react'; import { EuiLoadingSpinner } from '@elastic/eui'; import { VisOptionsProps } from 'src/plugins/vis_default_editor/public'; -import { TableVisParams } from '../types'; +import { TableVisParams } from '../../common'; const TableOptionsComponent = lazy(() => import('./table_vis_options')); diff --git a/src/plugins/vis_type_table/public/components/utils.ts b/src/plugins/vis_type_table/public/components/utils.ts index f11d7bc4b7f33..8f30788c76468 100644 --- a/src/plugins/vis_type_table/public/components/utils.ts +++ b/src/plugins/vis_type_table/public/components/utils.ts @@ -7,7 +7,7 @@ */ import { i18n } from '@kbn/i18n'; -import { AggTypes } from '../types'; +import { AggTypes } from '../../common'; const totalAggregations = [ { diff --git a/src/plugins/vis_type_table/public/legacy/table_vis_legacy_fn.ts b/src/plugins/vis_type_table/public/legacy/table_vis_legacy_fn.ts index cec16eefb360c..db0b92154d2dd 100644 --- a/src/plugins/vis_type_table/public/legacy/table_vis_legacy_fn.ts +++ b/src/plugins/vis_type_table/public/legacy/table_vis_legacy_fn.ts @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n'; import { ExpressionFunctionDefinition, Datatable, Render } from 'src/plugins/expressions/public'; import { tableVisLegacyResponseHandler, TableContext } from './table_vis_legacy_response_handler'; import { TableVisConfig } from '../types'; +import { VIS_TYPE_TABLE } from '../../common'; export type Input = Datatable; @@ -19,7 +20,7 @@ interface Arguments { export interface TableVisRenderValue { visData: TableContext; - visType: 'table'; + visType: typeof VIS_TYPE_TABLE; visConfig: TableVisConfig; } @@ -53,7 +54,7 @@ export const createTableVisLegacyFn = (): TableExpressionFunctionDefinition => ( as: 'table_vis', value: { visData: convertedData, - visType: 'table', + visType: VIS_TYPE_TABLE, visConfig, }, }; diff --git a/src/plugins/vis_type_table/public/legacy/table_vis_legacy_type.ts b/src/plugins/vis_type_table/public/legacy/table_vis_legacy_type.ts index a1ceee8c741d4..3e1140275593d 100644 --- a/src/plugins/vis_type_table/public/legacy/table_vis_legacy_type.ts +++ b/src/plugins/vis_type_table/public/legacy/table_vis_legacy_type.ts @@ -12,11 +12,11 @@ import { BaseVisTypeOptions } from '../../../visualizations/public'; import { TableOptions } from '../components/table_vis_options_lazy'; import { VIS_EVENT_TO_TRIGGER } from '../../../visualizations/public'; +import { TableVisParams, VIS_TYPE_TABLE } from '../../common'; import { toExpressionAst } from '../to_ast'; -import { TableVisParams } from '../types'; export const tableVisLegacyTypeDefinition: BaseVisTypeOptions = { - name: 'table', + name: VIS_TYPE_TABLE, title: i18n.translate('visTypeTable.tableVisTitle', { defaultMessage: 'Data table', }), diff --git a/src/plugins/vis_type_table/public/table_vis_fn.ts b/src/plugins/vis_type_table/public/table_vis_fn.ts index a45f1e828fc47..99fee424b8bea 100644 --- a/src/plugins/vis_type_table/public/table_vis_fn.ts +++ b/src/plugins/vis_type_table/public/table_vis_fn.ts @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n'; import { tableVisResponseHandler, TableContext } from './table_vis_response_handler'; import { ExpressionFunctionDefinition, Datatable, Render } from '../../expressions/public'; import { TableVisConfig } from './types'; +import { VIS_TYPE_TABLE } from '../common'; export type Input = Datatable; @@ -19,7 +20,7 @@ interface Arguments { export interface TableVisRenderValue { visData: TableContext; - visType: 'table'; + visType: typeof VIS_TYPE_TABLE; visConfig: TableVisConfig; } @@ -56,7 +57,7 @@ export const createTableVisFn = (): TableExpressionFunctionDefinition => ({ as: 'table_vis', value: { visData: convertedData, - visType: 'table', + visType: VIS_TYPE_TABLE, visConfig, }, }; diff --git a/src/plugins/vis_type_table/public/table_vis_type.ts b/src/plugins/vis_type_table/public/table_vis_type.ts index 8cd45b54c6ced..ef6d85db103b3 100644 --- a/src/plugins/vis_type_table/public/table_vis_type.ts +++ b/src/plugins/vis_type_table/public/table_vis_type.ts @@ -10,13 +10,13 @@ import { i18n } from '@kbn/i18n'; import { AggGroupNames } from '../../data/public'; import { BaseVisTypeOptions } from '../../visualizations/public'; +import { TableVisParams, VIS_TYPE_TABLE } from '../common'; import { TableOptions } from './components/table_vis_options_lazy'; import { VIS_EVENT_TO_TRIGGER } from '../../../plugins/visualizations/public'; import { toExpressionAst } from './to_ast'; -import { TableVisParams } from './types'; export const tableVisTypeDefinition: BaseVisTypeOptions = { - name: 'table', + name: VIS_TYPE_TABLE, title: i18n.translate('visTypeTable.tableVisTitle', { defaultMessage: 'Data table', }), diff --git a/src/plugins/vis_type_table/public/to_ast.test.ts b/src/plugins/vis_type_table/public/to_ast.test.ts index 1ca62475b7af0..f0aed7199a2f2 100644 --- a/src/plugins/vis_type_table/public/to_ast.test.ts +++ b/src/plugins/vis_type_table/public/to_ast.test.ts @@ -8,7 +8,7 @@ import { Vis } from 'src/plugins/visualizations/public'; import { toExpressionAst } from './to_ast'; -import { AggTypes, TableVisParams } from './types'; +import { AggTypes, TableVisParams } from '../common'; const mockSchemas = { metric: [{ accessor: 1, format: { id: 'number' }, params: {}, label: 'Count', aggType: 'count' }], diff --git a/src/plugins/vis_type_table/public/to_ast.ts b/src/plugins/vis_type_table/public/to_ast.ts index 9d9f23d31d802..1cbe9832e4c98 100644 --- a/src/plugins/vis_type_table/public/to_ast.ts +++ b/src/plugins/vis_type_table/public/to_ast.ts @@ -12,8 +12,9 @@ import { } from '../../data/public'; import { buildExpression, buildExpressionFunction } from '../../expressions/public'; import { getVisSchemas, Vis, BuildPipelineParams } from '../../visualizations/public'; +import { TableVisParams } from '../common'; import { TableExpressionFunctionDefinition } from './table_vis_fn'; -import { TableVisConfig, TableVisParams } from './types'; +import { TableVisConfig } from './types'; const buildTableVisConfig = ( schemas: ReturnType, diff --git a/src/plugins/vis_type_table/public/types.ts b/src/plugins/vis_type_table/public/types.ts index 75d48f4f53ac7..03cf8bb3395d6 100644 --- a/src/plugins/vis_type_table/public/types.ts +++ b/src/plugins/vis_type_table/public/types.ts @@ -8,14 +8,7 @@ import { IFieldFormat } from 'src/plugins/data/public'; import { SchemaConfig } from 'src/plugins/visualizations/public'; - -export enum AggTypes { - SUM = 'sum', - AVG = 'avg', - MIN = 'min', - MAX = 'max', - COUNT = 'count', -} +import { TableVisParams } from '../common'; export interface Dimensions { buckets: SchemaConfig[]; @@ -44,16 +37,6 @@ export interface TableVisUseUiStateProps { setColumnsWidth: (column: ColumnWidthData) => void; } -export interface TableVisParams { - perPage: number | ''; - showPartialRows: boolean; - showMetricsAtAllLevels: boolean; - showToolbar: boolean; - showTotal: boolean; - totalFunc: AggTypes; - percentageCol: string; -} - export interface TableVisConfig extends TableVisParams { title: string; dimensions: Dimensions; diff --git a/src/plugins/vis_type_table/public/utils/use/use_formatted_columns.ts b/src/plugins/vis_type_table/public/utils/use/use_formatted_columns.ts index 5398aa908f6eb..3a733e7a9a4dc 100644 --- a/src/plugins/vis_type_table/public/utils/use/use_formatted_columns.ts +++ b/src/plugins/vis_type_table/public/utils/use/use_formatted_columns.ts @@ -9,8 +9,9 @@ import { useMemo } from 'react'; import { chain, findIndex } from 'lodash'; +import { AggTypes } from '../../../common'; import { Table } from '../../table_vis_response_handler'; -import { FormattedColumn, TableVisConfig, AggTypes } from '../../types'; +import { FormattedColumn, TableVisConfig } from '../../types'; import { getFormatService } from '../../services'; import { addPercentageColumn } from '../add_percentage_column'; diff --git a/src/plugins/vis_type_table/public/utils/use/use_pagination.ts b/src/plugins/vis_type_table/public/utils/use/use_pagination.ts index 1573a3c6b7b88..7e55e63f9249c 100644 --- a/src/plugins/vis_type_table/public/utils/use/use_pagination.ts +++ b/src/plugins/vis_type_table/public/utils/use/use_pagination.ts @@ -7,7 +7,7 @@ */ import { useCallback, useEffect, useMemo, useState } from 'react'; -import { TableVisParams } from '../../types'; +import { TableVisParams } from '../../../common'; export const usePagination = (visParams: TableVisParams, rowCount: number) => { const [pagination, setPagination] = useState({ diff --git a/src/plugins/vis_type_table/server/index.ts b/src/plugins/vis_type_table/server/index.ts index 75068c646f501..39618d687168e 100644 --- a/src/plugins/vis_type_table/server/index.ts +++ b/src/plugins/vis_type_table/server/index.ts @@ -6,9 +6,11 @@ * Public License, v 1. */ -import { PluginConfigDescriptor } from 'kibana/server'; +import { CoreSetup, PluginConfigDescriptor } from 'kibana/server'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { configSchema, ConfigSchema } from '../config'; +import { registerVisTypeTableUsageCollector } from './usage_collector'; export const config: PluginConfigDescriptor = { exposeToBrowser: { @@ -21,6 +23,10 @@ export const config: PluginConfigDescriptor = { }; export const plugin = () => ({ - setup() {}, + setup(core: CoreSetup, plugins: { usageCollection?: UsageCollectionSetup }) { + if (plugins.usageCollection) { + registerVisTypeTableUsageCollector(plugins.usageCollection); + } + }, start() {}, }); diff --git a/src/plugins/vis_type_table/server/usage_collector/get_stats.test.ts b/src/plugins/vis_type_table/server/usage_collector/get_stats.test.ts new file mode 100644 index 0000000000000..55daa5c64349a --- /dev/null +++ b/src/plugins/vis_type_table/server/usage_collector/get_stats.test.ts @@ -0,0 +1,67 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +import { SavedObjectsClientContract } from 'kibana/server'; +import { getStats } from './get_stats'; + +const mockVisualizations = { + saved_objects: [ + { + attributes: { + visState: + '{"type": "table","aggs": [{ "schema": "metric" }, { "schema": "bucket" }, { "schema": "split", "enabled": true }], "params": { "row": true }}', + }, + }, + { + attributes: { + visState: + '{"type": "table","aggs": [{ "schema": "metric" }, { "schema": "bucket" }, { "schema": "split", "enabled": false }], "params": { "row": true }}', + }, + }, + { + attributes: { + visState: + '{"type": "table","aggs": [{ "schema": "metric" }, { "schema": "split", "enabled": true }], "params": { "row": false }}', + }, + }, + { + attributes: { + visState: '{"type": "table","aggs": [{ "schema": "metric" }, { "schema": "bucket" }]}', + }, + }, + { + attributes: { visState: '{"type": "histogram"}' }, + }, + ], +}; + +describe('vis_type_table getStats', () => { + const mockSoClient = ({ + find: jest.fn().mockResolvedValue(mockVisualizations), + } as unknown) as SavedObjectsClientContract; + + test('Returns stats from saved objects for table vis only', async () => { + const result = await getStats(mockSoClient); + expect(mockSoClient.find).toHaveBeenCalledWith({ + type: 'visualization', + perPage: 10000, + }); + expect(result).toEqual({ + total: 4, + total_split: 3, + split_columns: { + total: 1, + enabled: 1, + }, + split_rows: { + total: 2, + enabled: 1, + }, + }); + }); +}); diff --git a/src/plugins/vis_type_table/server/usage_collector/get_stats.ts b/src/plugins/vis_type_table/server/usage_collector/get_stats.ts new file mode 100644 index 0000000000000..bd3e1d2f089e2 --- /dev/null +++ b/src/plugins/vis_type_table/server/usage_collector/get_stats.ts @@ -0,0 +1,85 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server'; +import { + SavedVisState, + VisualizationSavedObjectAttributes, +} from 'src/plugins/visualizations/common'; +import { TableVisParams, VIS_TYPE_TABLE } from '../../common'; + +export interface VisTypeTableUsage { + /** + * Total number of table type visualizations + */ + total: number; + /** + * Total number of table visualizations, using "Split table" agg + */ + total_split: number; + /** + * Split table by columns stats + */ + split_columns: { + total: number; + enabled: number; + }; + /** + * Split table by rows stats + */ + split_rows: { + total: number; + enabled: number; + }; +} + +/* + * Parse the response data into telemetry payload + */ +export async function getStats( + soClient: SavedObjectsClientContract | ISavedObjectsRepository +): Promise { + const visualizations = await soClient.find({ + type: 'visualization', + perPage: 10000, + }); + + const tableVisualizations = visualizations.saved_objects + .map>(({ attributes }) => JSON.parse(attributes.visState)) + .filter(({ type }) => type === VIS_TYPE_TABLE); + + const defaultStats = { + total: tableVisualizations.length, + total_split: 0, + split_columns: { + total: 0, + enabled: 0, + }, + split_rows: { + total: 0, + enabled: 0, + }, + }; + + return tableVisualizations.reduce((acc, { aggs, params }) => { + const hasSplitAgg = aggs.find((agg) => agg.schema === 'split'); + + if (hasSplitAgg) { + acc.total_split += 1; + + const isSplitRow = params.row; + const isSplitEnabled = hasSplitAgg.enabled; + + const container = isSplitRow ? acc.split_rows : acc.split_columns; + container.total += 1; + container.enabled = isSplitEnabled ? container.enabled + 1 : container.enabled; + } + + return acc; + }, defaultStats); +} diff --git a/src/plugins/vis_type_table/server/usage_collector/index.ts b/src/plugins/vis_type_table/server/usage_collector/index.ts new file mode 100644 index 0000000000000..090ed3077b27c --- /dev/null +++ b/src/plugins/vis_type_table/server/usage_collector/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +export { registerVisTypeTableUsageCollector } from './register_usage_collector'; diff --git a/src/plugins/vis_type_table/server/usage_collector/register_usage_collector.test.ts b/src/plugins/vis_type_table/server/usage_collector/register_usage_collector.test.ts new file mode 100644 index 0000000000000..cbf39a4d937a7 --- /dev/null +++ b/src/plugins/vis_type_table/server/usage_collector/register_usage_collector.test.ts @@ -0,0 +1,56 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +jest.mock('./get_stats', () => ({ + getStats: jest.fn().mockResolvedValue({ somestat: 1 }), +})); + +import { createUsageCollectionSetupMock } from 'src/plugins/usage_collection/server/usage_collection.mock'; +import { createCollectorFetchContextMock } from 'src/plugins/usage_collection/server/mocks'; + +import { registerVisTypeTableUsageCollector } from './register_usage_collector'; +import { getStats } from './get_stats'; + +describe('registerVisTypeTableUsageCollector', () => { + it('Usage collector configs fit the shape', () => { + const mockCollectorSet = createUsageCollectionSetupMock(); + registerVisTypeTableUsageCollector(mockCollectorSet); + expect(mockCollectorSet.makeUsageCollector).toBeCalledTimes(1); + expect(mockCollectorSet.registerCollector).toBeCalledTimes(1); + expect(mockCollectorSet.makeUsageCollector).toHaveBeenCalledWith({ + type: 'vis_type_table', + isReady: expect.any(Function), + fetch: expect.any(Function), + schema: { + total: { type: 'long' }, + total_split: { type: 'long' }, + split_columns: { + total: { type: 'long' }, + enabled: { type: 'long' }, + }, + split_rows: { + total: { type: 'long' }, + enabled: { type: 'long' }, + }, + }, + }); + const usageCollectorConfig = mockCollectorSet.makeUsageCollector.mock.calls[0][0]; + expect(usageCollectorConfig.isReady()).toBe(true); + }); + + it('Usage collector config.fetch calls getStats', async () => { + const mockCollectorSet = createUsageCollectionSetupMock(); + registerVisTypeTableUsageCollector(mockCollectorSet); + const usageCollector = mockCollectorSet.makeUsageCollector.mock.results[0].value; + const mockCollectorFetchContext = createCollectorFetchContextMock(); + const fetchResult = await usageCollector.fetch(mockCollectorFetchContext); + expect(getStats).toBeCalledTimes(1); + expect(getStats).toBeCalledWith(mockCollectorFetchContext.soClient); + expect(fetchResult).toEqual({ somestat: 1 }); + }); +}); diff --git a/src/plugins/vis_type_table/server/usage_collector/register_usage_collector.ts b/src/plugins/vis_type_table/server/usage_collector/register_usage_collector.ts new file mode 100644 index 0000000000000..2ac4ce22a47e4 --- /dev/null +++ b/src/plugins/vis_type_table/server/usage_collector/register_usage_collector.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; + +import { getStats, VisTypeTableUsage } from './get_stats'; + +export function registerVisTypeTableUsageCollector(collectorSet: UsageCollectionSetup) { + const collector = collectorSet.makeUsageCollector({ + type: 'vis_type_table', + isReady: () => true, + schema: { + total: { type: 'long' }, + total_split: { type: 'long' }, + split_columns: { + total: { type: 'long' }, + enabled: { type: 'long' }, + }, + split_rows: { + total: { type: 'long' }, + enabled: { type: 'long' }, + }, + }, + fetch: ({ soClient }) => getStats(soClient), + }); + collectorSet.registerCollector(collector); +} diff --git a/src/plugins/vis_type_table/tsconfig.json b/src/plugins/vis_type_table/tsconfig.json index bda86d06c0ff7..ccff3c349cf21 100644 --- a/src/plugins/vis_type_table/tsconfig.json +++ b/src/plugins/vis_type_table/tsconfig.json @@ -8,6 +8,7 @@ "declarationMap": true }, "include": [ + "common/**/*", "public/**/*", "server/**/*", "*.ts" diff --git a/src/plugins/visualizations/common/index.ts b/src/plugins/visualizations/common/index.ts new file mode 100644 index 0000000000000..d4133eb9b7163 --- /dev/null +++ b/src/plugins/visualizations/common/index.ts @@ -0,0 +1,10 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +/** @public types */ +export * from './types'; diff --git a/src/plugins/visualizations/common/types.ts b/src/plugins/visualizations/common/types.ts new file mode 100644 index 0000000000000..4881b82a0e8d3 --- /dev/null +++ b/src/plugins/visualizations/common/types.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * and the Server Side Public License, v 1; you may not use this file except in + * compliance with, at your election, the Elastic License or the Server Side + * Public License, v 1. + */ + +import { SavedObjectAttributes } from 'kibana/server'; +import { AggConfigOptions } from 'src/plugins/data/common'; + +export interface VisParams { + [key: string]: any; +} + +export interface SavedVisState { + title: string; + type: string; + params: TVisParams; + aggs: AggConfigOptions[]; +} + +export interface VisualizationSavedObjectAttributes extends SavedObjectAttributes { + description: string; + kibanaSavedObjectMeta: { + searchSourceJSON: string; + }; + title: string; + version: number; + visState: string; + uiStateJSON: string; +} diff --git a/src/plugins/visualizations/public/index.ts b/src/plugins/visualizations/public/index.ts index d1976cc84acec..0bf8aa6e5c418 100644 --- a/src/plugins/visualizations/public/index.ts +++ b/src/plugins/visualizations/public/index.ts @@ -34,7 +34,7 @@ export type { Schema, ISchemas, } from './vis_types'; -export { VisParams, SerializedVis, SerializedVisData, VisData } from './vis'; +export { SerializedVis, SerializedVisData, VisData } from './vis'; export type VisualizeEmbeddableFactoryContract = PublicContract; export type VisualizeEmbeddableContract = PublicContract; export { VisualizeInput } from './embeddable'; @@ -46,12 +46,13 @@ export { PersistedState } from './persisted_state'; export { VisualizationControllerConstructor, VisualizationController, - SavedVisState, ISavedVis, VisSavedObject, VisResponseValue, VisToExpressionAst, + VisParams, } from './types'; export { ExprVisAPIEvents } from './expressions/vis'; export { VisualizationListItem, VisualizationStage } from './vis_types/vis_type_alias_registry'; export { VISUALIZE_ENABLE_LABS_SETTING } from '../common/constants'; +export { SavedVisState } from '../common'; diff --git a/src/plugins/visualizations/public/legacy/vis_update_state.d.ts b/src/plugins/visualizations/public/legacy/vis_update_state.d.ts index f3643ad6adcbe..0d871b3b1dea4 100644 --- a/src/plugins/visualizations/public/legacy/vis_update_state.d.ts +++ b/src/plugins/visualizations/public/legacy/vis_update_state.d.ts @@ -6,7 +6,7 @@ * Public License, v 1. */ -import { SavedVisState } from '../types'; +import { SavedVisState } from '../../common'; declare function updateOldState(oldState: unknown): SavedVisState; diff --git a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references.test.ts b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references.test.ts index c858306968ad8..a85a1d453a939 100644 --- a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references.test.ts +++ b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references.test.ts @@ -7,7 +7,8 @@ */ import { extractReferences, injectReferences } from './saved_visualization_references'; -import { VisSavedObject, SavedVisState } from '../types'; +import { VisSavedObject } from '../types'; +import { SavedVisState } from '../../common'; describe('extractReferences', () => { test('extracts nothing if savedSearchId is empty', () => { diff --git a/src/plugins/visualizations/public/types.ts b/src/plugins/visualizations/public/types.ts index 2e57cd00486f7..dc9ca49840561 100644 --- a/src/plugins/visualizations/public/types.ts +++ b/src/plugins/visualizations/public/types.ts @@ -7,15 +7,12 @@ */ import { SavedObject } from '../../../plugins/saved_objects/public'; -import { - AggConfigOptions, - SearchSourceFields, - TimefilterContract, -} from '../../../plugins/data/public'; +import { SearchSourceFields, TimefilterContract } from '../../../plugins/data/public'; import { ExpressionAstExpression } from '../../expressions/public'; -import { SerializedVis, Vis, VisParams } from './vis'; +import { SerializedVis, Vis } from './vis'; import { ExprVis } from './expressions/vis'; +import { SavedVisState, VisParams } from '../common/types'; export { Vis, SerializedVis, VisParams }; @@ -30,13 +27,6 @@ export type VisualizationControllerConstructor = new ( vis: ExprVis ) => VisualizationController; -export interface SavedVisState { - title: string; - type: string; - params: VisParams; - aggs: AggConfigOptions[]; -} - export interface ISavedVis { id?: string; title: string; diff --git a/src/plugins/visualizations/public/vis.ts b/src/plugins/visualizations/public/vis.ts index 58bcdb9ea49c6..56a151fb82ed3 100644 --- a/src/plugins/visualizations/public/vis.ts +++ b/src/plugins/visualizations/public/vis.ts @@ -30,6 +30,7 @@ import { AggConfigOptions, SearchSourceFields, } from '../../../plugins/data/public'; +import { VisParams } from '../common/types'; export interface SerializedVisData { expression?: string; @@ -56,10 +57,6 @@ export interface VisData { savedSearchId?: string; } -export interface VisParams { - [key: string]: any; -} - const getSearchSource = async (inputSearchSource: ISearchSource, savedSearchId?: string) => { const searchSource = inputSearchSource.createCopy(); if (savedSearchId) {