Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions dev_docs/lens/config_api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ tags: ['kibana', 'dev', 'lens', 'introduction']
The Lens Config Builder API provides a streamlined and flexible interface for developers to construct and integrate Lens configurations within their applications. Lens, as a powerful visualization tool in Kibana, enables users to create, visualize, and analyze data in diverse and complex ways. This API aims to simplify the process of generating these visualizations programmatically, allowing for dynamic and customizable integration points with Lens. By abstracting the complexities of Lens configuration details, developers can focus on delivering rich data visualization experiences tailored to their application's needs.

```
import LensConfigBuilder, LensConfig from '@kbn/lens-embeddable-utils/config_builder';
import { LensConfigBuilder, LensApiConfig } from '@kbn/lens-embeddable-utils';

const config: LensConfig = {
const config: LensApiConfig = {
chartType: 'metric',
title: 'my metric chart',
dataset: { esql: 'from my_index | stats sum=sum(my_field)'}
value: 'sum'
}

const configBuilder = new LensConfigBuilder(dataViewsAPI, lensFormulaAPI);
const lensConfig = configBuilder(config, {
const lensConfig = configBuilder(config, {
timeRange: { from: 'now-30d', to: 'now', type: 'relative' },
embeddable: true,
}
Expand All @@ -46,11 +46,11 @@ The constructor requires two parameters:

#### build Method

The `build` method is the primary method used to generate a Lens configuration. It accepts a `LensConfig` object, detailing the type of chart and its options, and an optional `LensConfigOptions` object for additional configuration options like filters, queries, and time ranges.
The `build` method is the primary method used to generate a Lens configuration. It accepts a `LensApiConfig` object, detailing the type of chart and its options, and an optional `LensConfigOptions` object for additional configuration options like filters, queries, and time ranges.

**Parameters:**

- `config`: A `LensConfig` object specifying the chart type and its configuration.
- `config`: A `LensApiConfig` object specifying the chart type and its configuration.
- `options` (optional): A `LensConfigOptions` object providing additional settings such as embeddable options, time range overrides, filters, and queries.

**Returns:** A Promise resolving to either `LensAttributes` or `LensEmbeddableInput`, depending on the options provided. This allows the generated configuration to be directly used within Kibana Lens visualizations.
Expand Down
4 changes: 2 additions & 2 deletions src/platform/packages/shared/kbn-lens-common-2/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @kbn/lens-common-2

Split off of `@kbn/lens-common` because of circular dependence issues importing LensApiSchemaType.
Split off of `@kbn/lens-common` because of circular dependence issues importing LensApiConfig.

```ts
import type { LensApiSchemaType } from '@kbn/lens-embeddable-utils';
import type { LensApiConfig } from '@kbn/lens-embeddable-utils';
```
4 changes: 2 additions & 2 deletions src/platform/packages/shared/kbn-lens-common-2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {
SerializedTitles,
SerializedTimeRange,
} from '@kbn/presentation-publishing';
import type { LensApiSchemaType } from '@kbn/lens-embeddable-utils';
import type { LensApiConfig } from '@kbn/lens-embeddable-utils';
import type { Simplify } from '@kbn/chart-expressions-common';
import type {
LensByValueBase,
Expand All @@ -51,7 +51,7 @@ type LensPersistableState = SerializedTitles & // title, description, hide_title

export type LensByValueSerializedAPIConfig = LensPersistableState & {
// Temporarily allow both old and new attributes until all chart types are supported and feature flag removed
attributes: LensApiSchemaType | LensByValueBase['attributes'];
attributes: LensApiConfig | LensByValueBase['attributes'];
ref_id?: string; // really should be never but creates type issues
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
fromAPItoLensState as fromDatatableAPItoLensState,
fromLensStateToAPI as fromDatatableLensStateToAPI,
} from './transforms/charts/datatable';
import type { LensApiState } from './schema';
import type { LensApiConfig } from './schema';
import { filtersAndQueryToApiFormat, filtersAndQueryToLensState } from './transforms/utils';
import { isLensLegacyFormat } from './utils';

Expand All @@ -75,7 +75,7 @@ const compatibilityMap: Record<string, string> = {
type ChartTypeLike =
| Pick<LensAttributes, 'visualizationType'>
| Pick<LensConfig, 'chartType'>
| Pick<LensApiState, 'type'>
| Pick<LensApiConfig, 'type'>
| { visualizationType: null | undefined }
| undefined;

Expand Down Expand Up @@ -225,7 +225,7 @@ export class LensConfigBuilder {
return chartState as LensAttributes;
}

fromAPIFormat(config: LensApiState): LensAttributes {
fromAPIFormat(config: LensApiConfig): LensAttributes {
const chartType = config.type;

if (!(chartType in this.apiConvertersByChart)) {
Expand Down Expand Up @@ -253,7 +253,7 @@ export class LensConfigBuilder {
};
}

toAPIFormat(config: LensAttributes): LensApiState {
toAPIFormat(config: LensAttributes): LensApiConfig {
const visType = config.visualizationType;
const type = compatibilityMap[visType] ?? visType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ export type {
LensBaseLayer,
LensXYConfigBase,
LensBreakdownConfig,
LensYBoundsConfig,
} from './types';

export { lensApiStateSchema, lensApiStateSchemaNoESQL } from './schema';
export type { LensApiState as LensApiSchemaType } from './schema';
export {
isLensESQLConfig,
isLensDSLConfig,
isLensAPIFormat,
isLensLegacyFormat,
isLensLegacyAttributes,
} from './utils';

export * from './schema';
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*/
import { AS_CODE_DATA_VIEW_REFERENCE_TYPE } from '@kbn/as-code-data-views-schema';
import { LENS_EMPTY_AS_NULL_DEFAULT_VALUE } from '../../transforms/columns/utils';
import type { DatatableState } from './datatable';
import { datatableStateSchema } from './datatable';
import type { DatatableConfig } from './datatable';
import { datatableConfigSchema } from './datatable';

type DefaultDatatableConfig = Pick<DatatableState, 'sampling' | 'ignore_global_filters'>;
type DatatableWithoutDefaultsConfig = Omit<DatatableState, 'sampling' | 'ignore_global_filters'>;
type DefaultDatatableConfig = Pick<DatatableConfig, 'sampling' | 'ignore_global_filters'>;
type DatatableWithoutDefaultsConfig = Omit<DatatableConfig, 'sampling' | 'ignore_global_filters'>;

describe('Datatable Schema', () => {
const baseDatatableConfig: Omit<DatatableWithoutDefaultsConfig, 'metrics'> = {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('Datatable Schema', () => {
],
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -80,7 +80,7 @@ describe('Datatable Schema', () => {
],
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -127,7 +127,7 @@ describe('Datatable Schema', () => {
},
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -174,7 +174,7 @@ describe('Datatable Schema', () => {
},
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -222,7 +222,7 @@ describe('Datatable Schema', () => {
},
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -275,7 +275,7 @@ describe('Datatable Schema', () => {
},
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});
});
Expand All @@ -300,7 +300,7 @@ describe('Datatable Schema', () => {
],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws on empty metrics for non-esql', () => {
Expand All @@ -318,7 +318,7 @@ describe('Datatable Schema', () => {
],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws on empty rows', () => {
Expand All @@ -344,7 +344,7 @@ describe('Datatable Schema', () => {
],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws on empty split_metrics_by', () => {
Expand All @@ -363,7 +363,7 @@ describe('Datatable Schema', () => {
split_metrics_by: [],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using invalid density height type', () => {
Expand All @@ -390,7 +390,7 @@ describe('Datatable Schema', () => {
},
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using invalid density mode', () => {
Expand All @@ -415,7 +415,7 @@ describe('Datatable Schema', () => {
},
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using invalid height type', () => {
Expand All @@ -436,7 +436,7 @@ describe('Datatable Schema', () => {
styling: { density: { height: { header: { type: 'invalid' } } } },
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when missing summary type', () => {
Expand All @@ -457,7 +457,7 @@ describe('Datatable Schema', () => {
],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using term buckets operation in an esql configuration', () => {
Expand All @@ -480,7 +480,7 @@ describe('Datatable Schema', () => {
rows: [{ operation: 'terms', fields: ['geo.dest'], limit: 10 }],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when esql datatable has no metrics and no rows', () => {
Expand All @@ -492,7 +492,7 @@ describe('Datatable Schema', () => {
},
};

expect(() => datatableStateSchema.validate(input)).toThrow(
expect(() => datatableConfigSchema.validate(input)).toThrow(
'Datatable must have at least one column'
);
});
Expand All @@ -512,7 +512,7 @@ describe('Datatable Schema', () => {
],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws on empty rows array for esql', () => {
Expand All @@ -530,7 +530,7 @@ describe('Datatable Schema', () => {
rows: [],
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using invalid sorting index', () => {
Expand Down Expand Up @@ -576,7 +576,7 @@ describe('Datatable Schema', () => {
},
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using invalid sorting index for pivoted_metric', () => {
Expand Down Expand Up @@ -623,7 +623,7 @@ describe('Datatable Schema', () => {
},
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});

it('throws when using invalid values length for pivoted_metric', () => {
Expand Down Expand Up @@ -675,7 +675,7 @@ describe('Datatable Schema', () => {
},
};

expect(() => datatableStateSchema.validate(input)).toThrow();
expect(() => datatableConfigSchema.validate(input)).toThrow();
});
});

Expand Down Expand Up @@ -764,7 +764,7 @@ describe('Datatable Schema', () => {
],
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -852,7 +852,7 @@ describe('Datatable Schema', () => {
],
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});

Expand Down Expand Up @@ -890,7 +890,7 @@ describe('Datatable Schema', () => {
],
};

const validated = datatableStateSchema.validate(input);
const validated = datatableConfigSchema.validate(input);
expect(validated).toEqual({ ...defaultValues, ...input });
});
});
Expand Down
Loading
Loading