Skip to content

Commit

Permalink
[CM] - lens (#155700)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored May 3, 2023
1 parent 93256a7 commit 2c491b5
Show file tree
Hide file tree
Showing 27 changed files with 854 additions and 97 deletions.
20 changes: 20 additions & 0 deletions x-pack/plugins/lens/common/content_management/cm_services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

import type {
ContentManagementServicesDefinition as ServicesDefinition,
Version,
} from '@kbn/object-versioning';

// We export the versioned service definition from this file and not the barrel to avoid adding
// the schemas in the "public" js bundle

import { serviceDefinition as v1 } from './v1/cm_services';

export const cmServicesDefinition: { [version: Version]: ServicesDefinition } = {
1: v1,
};
10 changes: 10 additions & 0 deletions x-pack/plugins/lens/common/content_management/constants.ts
Original file line number Diff line number Diff line change
@@ -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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const LATEST_VERSION = 1;

export const CONTENT_ID = 'lens';
31 changes: 31 additions & 0 deletions x-pack/plugins/lens/common/content_management/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

export { LATEST_VERSION, CONTENT_ID } from './constants';

export type { LensContentType } from './types';

export type {
LensSavedObject,
PartialLensSavedObject,
LensSavedObjectAttributes,
LensGetIn,
LensGetOut,
LensCreateIn,
LensCreateOut,
CreateOptions,
LensUpdateIn,
LensUpdateOut,
UpdateOptions,
LensDeleteIn,
LensDeleteOut,
LensSearchIn,
LensSearchOut,
LensSearchQuery,
} from './latest';

export * as LensV1 from './v1';
8 changes: 8 additions & 0 deletions x-pack/plugins/lens/common/content_management/latest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

export * from './v1';
8 changes: 8 additions & 0 deletions x-pack/plugins/lens/common/content_management/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

export type LensContentType = 'lens';
141 changes: 141 additions & 0 deletions x-pack/plugins/lens/common/content_management/v1/cm_services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/
import { schema } from '@kbn/config-schema';
import type { ContentManagementServicesDefinition as ServicesDefinition } from '@kbn/object-versioning';

const apiError = schema.object({
error: schema.string(),
message: schema.string(),
statusCode: schema.number(),
metadata: schema.object({}, { unknowns: 'allow' }),
});

const referenceSchema = schema.object(
{
name: schema.maybe(schema.string()),
type: schema.string(),
id: schema.string(),
},
{ unknowns: 'forbid' }
);

const referencesSchema = schema.arrayOf(referenceSchema);

const lensAttributesSchema = schema.object(
{
title: schema.string(),
description: schema.maybe(schema.string()),
visualizationType: schema.maybe(schema.string()),
state: schema.maybe(schema.any()),
uiStateJSON: schema.maybe(schema.string()),
visState: schema.maybe(schema.string()),
savedSearchRefName: schema.maybe(schema.string()),
},
{ unknowns: 'forbid' }
);

const lensSavedObjectSchema = schema.object(
{
id: schema.string(),
type: schema.string(),
version: schema.maybe(schema.string()),
createdAt: schema.maybe(schema.string()),
updatedAt: schema.maybe(schema.string()),
error: schema.maybe(apiError),
attributes: lensAttributesSchema,
references: referencesSchema,
namespaces: schema.maybe(schema.arrayOf(schema.string())),
originId: schema.maybe(schema.string()),
},
{ unknowns: 'allow' }
);

const getResultSchema = schema.object(
{
item: lensSavedObjectSchema,
meta: schema.object(
{
outcome: schema.oneOf([
schema.literal('exactMatch'),
schema.literal('aliasMatch'),
schema.literal('conflict'),
]),
aliasTargetId: schema.maybe(schema.string()),
aliasPurpose: schema.maybe(
schema.oneOf([
schema.literal('savedObjectConversion'),
schema.literal('savedObjectImport'),
])
),
},
{ unknowns: 'forbid' }
),
},
{ unknowns: 'forbid' }
);

const createOptionsSchema = schema.object({
overwrite: schema.maybe(schema.boolean()),
references: schema.maybe(referencesSchema),
});

// Content management service definition.
// We need it for BWC support between different versions of the content
export const serviceDefinition: ServicesDefinition = {
get: {
out: {
result: {
schema: getResultSchema,
},
},
},
create: {
in: {
options: {
schema: createOptionsSchema,
},
data: {
schema: lensAttributesSchema,
},
},
out: {
result: {
schema: schema.object(
{
item: lensSavedObjectSchema,
},
{ unknowns: 'forbid' }
),
},
},
},
update: {
in: {
options: {
schema: createOptionsSchema, // same schema as "create"
},
data: {
schema: lensAttributesSchema,
},
},
},
search: {
in: {
options: {
schema: schema.maybe(
schema.object(
{
searchFields: schema.maybe(schema.arrayOf(schema.string())),
types: schema.maybe(schema.arrayOf(schema.string())),
},
{ unknowns: 'forbid' }
)
),
},
},
},
};
26 changes: 26 additions & 0 deletions x-pack/plugins/lens/common/content_management/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

export type {
LensSavedObject,
PartialLensSavedObject,
LensSavedObjectAttributes,
LensGetIn,
LensGetOut,
LensCreateIn,
LensCreateOut,
CreateOptions,
LensUpdateIn,
LensUpdateOut,
UpdateOptions,
LensDeleteIn,
LensDeleteOut,
LensSearchIn,
LensSearchOut,
LensSearchQuery,
Reference,
} from './types';
111 changes: 111 additions & 0 deletions x-pack/plugins/lens/common/content_management/v1/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

import {
GetIn,
CreateIn,
SearchIn,
UpdateIn,
DeleteIn,
DeleteResult,
SearchResult,
GetResult,
CreateResult,
UpdateResult,
} from '@kbn/content-management-plugin/common';

import { LensContentType } from '../types';

export interface Reference {
type: string;
id: string;
name: string;
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type LensSavedObjectAttributes = {
title: string;
description?: string;
visualizationType: string | null;
state?: unknown;
};

export interface LensSavedObject {
id: string;
type: string;
version?: string;
updatedAt?: string;
createdAt?: string;
attributes: LensSavedObjectAttributes;
references: Reference[];
namespaces?: string[];
originId?: string;
error?: {
error: string;
message: string;
statusCode: number;
metadata?: Record<string, unknown>;
};
}

export type PartialLensSavedObject = Omit<LensSavedObject, 'attributes' | 'references'> & {
attributes: Partial<LensSavedObjectAttributes>;
references: Reference[] | undefined;
};
// ----------- GET --------------

export type LensGetIn = GetIn<LensContentType>;

export type LensGetOut = GetResult<
LensSavedObject,
{
outcome: 'exactMatch' | 'aliasMatch' | 'conflict';
aliasTargetId?: string;
aliasPurpose?: 'savedObjectConversion' | 'savedObjectImport';
}
>;

// ----------- CREATE --------------

export interface CreateOptions {
/** If a document with the given `id` already exists, overwrite it's contents (default=false). */
overwrite?: boolean;
/** Array of referenced saved objects. */
references?: Reference[];
}

export type LensCreateIn = CreateIn<LensContentType, LensSavedObjectAttributes, CreateOptions>;

export type LensCreateOut = CreateResult<LensSavedObject>;

// ----------- UPDATE --------------

export interface UpdateOptions {
/** Array of referenced saved objects. */
references?: Reference[];
}

export type LensUpdateIn = UpdateIn<LensContentType, LensSavedObjectAttributes, UpdateOptions>;

export type LensUpdateOut = UpdateResult<PartialLensSavedObject>;

// ----------- DELETE --------------

export type LensDeleteIn = DeleteIn<LensContentType>;

export type LensDeleteOut = DeleteResult;

// ----------- SEARCH --------------

export interface LensSearchQuery {
types?: string[];
searchFields?: string[];
}

export type LensSearchIn = SearchIn<LensContentType, {}>;

export type LensSearchOut = SearchResult<LensSavedObject>;
1 change: 1 addition & 0 deletions x-pack/plugins/lens/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eventAnnotation",
"unifiedSearch",
"unifiedFieldList",
"contentManagement"
],
"optionalPlugins": [
"expressionLegacyMetricVis",
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { act } from 'react-dom/test-utils';
import { App } from './app';
import { LensAppProps, LensAppServices } from './types';
import { EditorFrameInstance, EditorFrameProps } from '../types';
import { Document } from '../persistence';
import { Document, SavedObjectIndexStore } from '../persistence';
import {
visualizationMap,
datasourceMap,
Expand Down Expand Up @@ -87,6 +87,11 @@ describe('Lens App', () => {
topNavMenuEntryGenerators: [],
theme$: new Observable(),
coreStart: coreMock.createStart(),
savedObjectStore: {
save: jest.fn(),
load: jest.fn(),
search: jest.fn(),
} as unknown as SavedObjectIndexStore,
};
}

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function App({
initialContext,
theme$,
coreStart,
savedObjectStore,
}: LensAppProps) {
const lensAppServices = useKibana<LensAppServices>().services;

Expand Down
Loading

0 comments on commit 2c491b5

Please sign in to comment.