Skip to content

Commit

Permalink
[Discover] Add migrations for visContextJSON. Create saved search pac…
Browse files Browse the repository at this point in the history
…kage for common code.
  • Loading branch information
jughosta committed Feb 8, 2024
1 parent 0e922c2 commit f8f00ce
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 60 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@
"@kbn/saved-objects-settings": "link:packages/kbn-saved-objects-settings",
"@kbn/saved-objects-tagging-oss-plugin": "link:src/plugins/saved_objects_tagging_oss",
"@kbn/saved-objects-tagging-plugin": "link:x-pack/plugins/saved_objects_tagging",
"@kbn/saved-search": "link:packages/kbn-saved-search",
"@kbn/saved-search-plugin": "link:src/plugins/saved_search",
"@kbn/saved-search-so-plugin": "link:src/plugins/saved_search_so",
"@kbn/screenshot-mode-example-plugin": "link:examples/screenshot_mode_example",
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-saved-search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/saved-search

Package for extracted saved search plugin logic
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
* Side Public License, v 1.
*/

export { MIN_SAVED_SEARCH_SAMPLE_SIZE, MAX_SAVED_SEARCH_SAMPLE_SIZE, VIEW_MODE } from './constants';
export {
MIN_SAVED_SEARCH_SAMPLE_SIZE,
MAX_SAVED_SEARCH_SAMPLE_SIZE,
VIEW_MODE,
} from './src/constants';
13 changes: 13 additions & 0 deletions packages/kbn-saved-search/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-saved-search'],
};
5 changes: 5 additions & 0 deletions packages/kbn-saved-search/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/saved-search",
"owner": "@elastic/kibana-data-discovery"
}
7 changes: 7 additions & 0 deletions packages/kbn-saved-search/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/saved-search",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0",
"sideEffects": false
}
File renamed without changes.
11 changes: 11 additions & 0 deletions packages/kbn-saved-search/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"include": ["*.ts", "src/**/*", "__mocks__/**/*.ts"],
"compilerOptions": {
"outDir": "target/types"
},
"exclude": [
"target/**/*"
],
"kbn_references": []
}
46 changes: 46 additions & 0 deletions packages/kbn-saved-search/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { RefreshInterval, TimeRange } from '@kbn/data-plugin/common';
import type { SerializableRecord } from '@kbn/utility-types';
import { VIEW_MODE } from './src/constants';

export interface DiscoverGridSettings extends SerializableRecord {
columns?: Record<string, DiscoverGridSettingsColumn>;
}

export interface DiscoverGridSettingsColumn extends SerializableRecord {
width?: number;
}

/** @internal **/
export interface SavedSearchAttributes {
title: string;
sort: Array<[string, string]>;
columns: string[];
description: string;
grid: DiscoverGridSettings;
hideChart: boolean;
isTextBasedQuery: boolean;
usesAdHocDataView?: boolean;
kibanaSavedObjectMeta: {
searchSourceJSON: string;
};
viewMode?: VIEW_MODE;
hideAggregatedPreview?: boolean;
rowHeight?: number;

timeRestore?: boolean;
timeRange?: Pick<TimeRange, 'from' | 'to'>;
refreshInterval?: RefreshInterval;

rowsPerPage?: number;
sampleSize?: number;
breakdownField?: string;
visContextJSON?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import {
updateOptionsSchema,
createResultSchema,
} from '@kbn/content-management-utils';
import {
MIN_SAVED_SEARCH_SAMPLE_SIZE,
MAX_SAVED_SEARCH_SAMPLE_SIZE,
} from '@kbn/saved-search-so-plugin/common';
import { MIN_SAVED_SEARCH_SAMPLE_SIZE, MAX_SAVED_SEARCH_SAMPLE_SIZE } from '@kbn/saved-search';

const sortSchema = schema.arrayOf(schema.string(), { maxSize: 2 });

Expand Down
12 changes: 6 additions & 6 deletions src/plugins/saved_search/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export {
VIEW_MODE,
MIN_SAVED_SEARCH_SAMPLE_SIZE,
MAX_SAVED_SEARCH_SAMPLE_SIZE,
} from '@kbn/saved-search-so-plugin/common';
export { getSavedSearchUrl, getSavedSearchFullPathUrl } from './saved_searches_url';
export { fromSavedSearchAttributes } from './saved_searches_utils';

} from '@kbn/saved-search';
export type {
DiscoverGridSettings,
DiscoverGridSettingsColumn,
SavedSearch,
SavedSearchAttributes,
} from './types';
} from '@kbn/saved-search/types';
export { getSavedSearchUrl, getSavedSearchFullPathUrl } from './saved_searches_url';
export { fromSavedSearchAttributes } from './saved_searches_utils';

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

export { SavedSearchType, LATEST_VERSION } from './constants';
export { getKibanaContextFn } from './expressions/kibana_context';
39 changes: 2 additions & 37 deletions src/plugins/saved_search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,8 @@
import type { ISearchSource, RefreshInterval, TimeRange } from '@kbn/data-plugin/common';
import type { SavedObjectReference } from '@kbn/core-saved-objects-server';
import type { SavedObjectsResolveResponse } from '@kbn/core/server';
import type { SerializableRecord } from '@kbn/utility-types';
import { VIEW_MODE } from '.';

export interface DiscoverGridSettings extends SerializableRecord {
columns?: Record<string, DiscoverGridSettingsColumn>;
}

export interface DiscoverGridSettingsColumn extends SerializableRecord {
width?: number;
}

/** @internal **/
export interface SavedSearchAttributes {
title: string;
sort: Array<[string, string]>;
columns: string[];
description: string;
grid: DiscoverGridSettings;
hideChart: boolean;
isTextBasedQuery: boolean;
usesAdHocDataView?: boolean;
kibanaSavedObjectMeta: {
searchSourceJSON: string;
};
viewMode?: VIEW_MODE;
hideAggregatedPreview?: boolean;
rowHeight?: number;

timeRestore?: boolean;
timeRange?: Pick<TimeRange, 'from' | 'to'>;
refreshInterval?: RefreshInterval;

rowsPerPage?: number;
sampleSize?: number;
breakdownField?: string;
visContextJSON?: string;
}
import { VIEW_MODE } from '@kbn/saved-search';
import { DiscoverGridSettings } from '@kbn/saved-search/types';

/** @internal **/
export type SortOrder = [string, string];
Expand Down
11 changes: 9 additions & 2 deletions src/plugins/saved_search_so/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@

import { CoreSetup, Plugin } from '@kbn/core/server';
import type { PluginSetup as DataPluginSetup } from '@kbn/data-plugin/server';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import { getSavedSearchObjectType } from './saved_objects';

/**
* Saved search SO plugin server Setup contract
*/
export interface SavedSearchSOPublicSetupDependencies {
data: DataPluginSetup;
lens: LensServerPluginSetup;
}

export class SavedSearchSOServerPlugin implements Plugin<object, object, object> {
constructor() {}

public setup(core: CoreSetup, { data }: SavedSearchSOPublicSetupDependencies) {
public setup(core: CoreSetup, { data, lens }: SavedSearchSOPublicSetupDependencies) {
const searchSource = data.search.searchSource;

const getSearchSourceMigrations = searchSource.getAllMigrations.bind(searchSource);
core.savedObjects.registerType(getSavedSearchObjectType(getSearchSourceMigrations));
core.savedObjects.registerType(
getSavedSearchObjectType({
getSearchSourceMigrations,
lensEmbeddableFactory: lens.lensEmbeddableFactory,
})
);

return {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/saved_search_so/server/saved_objects/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
MIN_SAVED_SEARCH_SAMPLE_SIZE,
MAX_SAVED_SEARCH_SAMPLE_SIZE,
VIEW_MODE,
} from '../../common';
} from '@kbn/saved-search';

const SCHEMA_SEARCH_BASE = {
// General
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/saved_search_so/server/saved_objects/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import { ANALYTICS_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { SavedObjectsType } from '@kbn/core/server';
import { MigrateFunctionsObject } from '@kbn/kibana-utils-plugin/common';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import { getAllMigrations } from './search_migrations';
import { SCHEMA_SEARCH_V8_8_0, SCHEMA_SEARCH_V8_12_0, SCHEMA_SEARCH_V8_13_0 } from './schema';

export function getSavedSearchObjectType(
getSearchSourceMigrations: () => MigrateFunctionsObject
): SavedObjectsType {
export function getSavedSearchObjectType({
getSearchSourceMigrations,
lensEmbeddableFactory,
}: {
getSearchSourceMigrations: () => MigrateFunctionsObject;
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
}): SavedObjectsType {
return {
name: 'search',
indexPattern: ANALYTICS_SAVED_OBJECT_INDEX,
Expand Down Expand Up @@ -47,6 +52,10 @@ export function getSavedSearchObjectType(
'8.12.0': SCHEMA_SEARCH_V8_12_0,
'8.13.0': SCHEMA_SEARCH_V8_13_0,
},
migrations: () => getAllMigrations(getSearchSourceMigrations()), // TODO: add lens embeddable migrations for `visContextJSON`
migrations: () =>
getAllMigrations({
searchSourceMigrations: getSearchSourceMigrations(),
lensEmbeddableFactory,
}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { mergeSavedObjectMigrationMaps } from '@kbn/core/server';
import { DEFAULT_QUERY_LANGUAGE } from '@kbn/data-plugin/server';
import { MigrateFunctionsObject, MigrateFunction } from '@kbn/kibana-utils-plugin/common';
import { isSerializedSearchSource, SerializedSearchSourceFields } from '@kbn/data-plugin/common';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import { getLensVisContextMigrations } from './search_vis_migrations';

export interface SavedSearchMigrationAttributes extends SavedObjectAttributes {
kibanaSavedObjectMeta: {
Expand Down Expand Up @@ -168,11 +170,18 @@ export const searchMigrations = {
'7.9.3': flow(migrateMatchAllQuery),
};

export const getAllMigrations = (
searchSourceMigrations: MigrateFunctionsObject
): SavedObjectMigrationMap => {
export const getAllMigrations = ({
searchSourceMigrations,
lensEmbeddableFactory,
}: {
searchSourceMigrations: MigrateFunctionsObject;
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
}): SavedObjectMigrationMap => {
return mergeSavedObjectMigrationMaps(
searchMigrations,
getSearchSourceMigrations(searchSourceMigrations) as SavedObjectMigrationMap
mergeSavedObjectMigrationMaps(
searchMigrations,
getSearchSourceMigrations(searchSourceMigrations) as SavedObjectMigrationMap
),
getLensVisContextMigrations(lensEmbeddableFactory)
);
};
Loading

0 comments on commit f8f00ce

Please sign in to comment.