import { OpenSearchDashboardsConfigType } from 'src/core/server/opensearch_dashboards_config' ; import { BehaviorSubject } from 'rxjs' ; import { Logger } from '../../../logging' ; import { IndexMapping , SavedObjectsTypeMappingDefinitions } from '../../mappings' ; import { SavedObjectUnsanitizedDoc , SavedObjectsSerializer } from '../../serialization' ; import { buildActiveMappings , IndexMigrator , MigrationResult , MigrationStatus } from '../core' ; import { DocumentMigrator , VersionedTransformer } from '../core/document_migrator' ; import { MigrationOpenSearchClient } from '../core/' ; import { createIndexMap } from '../core/build_index_map' ; import { SavedObjectsMigrationConfigType } from '../../saved_objects_config' ; import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry' ; import { SavedObjectsType } from '../../types' ; export interface OpenSearchDashboardsMigratorOptions { client : MigrationOpenSearchClient ; typeRegistry : ISavedObjectTypeRegistry ; savedObjectsConfig : SavedObjectsMigrationConfigType ; opensearchDashboardsConfig : OpenSearchDashboardsConfigType ; opensearchDashboardsVersion : string ; logger : Logger ; } export type IOpenSearchDashboardsMigrator = Pick < OpenSearchDashboardsMigrator , keyof OpenSearchDashboardsMigrator > ; export interface OpenSearchDashboardsMigratorStatus { status : MigrationStatus ; result ? : MigrationResult [ ] ; } /**
 * Manages the shape of mappings and documents in the OpenSearch Dashboards index.
 */ export class OpenSearchDashboardsMigrator { private readonly client : MigrationOpenSearchClient ; private readonly savedObjectsConfig : SavedObjectsMigrationConfigType ; private readonly documentMigrator : VersionedTransformer ; private readonly opensearchDashboardsConfig : OpenSearchDashboardsConfigType ; private readonly log : Logger ; private readonly mappingProperties : SavedObjectsTypeMappingDefinitions ; private readonly typeRegistry : ISavedObjectTypeRegistry ; private readonly serializer : SavedObjectsSerializer ; private migrationResult ? : Promise < MigrationResult [ ] > ; private readonly status$ = new BehaviorSubject < OpenSearchDashboardsMigratorStatus > ( { status : 'waiting' , } ) ; private readonly activeMappings : IndexMapping ; /**
     * Creates an instance of OpenSearchDashboardsMigrator.
     */ constructor ( { client , typeRegistry , opensearchDashboardsConfig , savedObjectsConfig , opensearchDashboardsVersion , logger , } ) { this . client = client ; this . opensearchDashboardsConfig = opensearchDashboardsConfig ; this . savedObjectsConfig = savedObjectsConfig ; this . typeRegistry = typeRegistry ; this . serializer = new SavedObjectsSerializer ( this . typeRegistry ) ; this . mappingProperties = mergeTypes ( this . typeRegistry . getAllTypes ( ) ) ; this . log = logger ; this . documentMigrator = new DocumentMigrator ( { opensearchDashboardsVersion , typeRegistry , log : this . log , } ) ; this . activeMappings = buildActiveMappings ( this . mappingProperties ) ; } param rerun remarks returns public runMigrations ( { rerun = false } = { } ) { if ( this . migrationResult === undefined || rerun ) { if ( ! rerun ) { this . status$ . next ( { status : 'running' } ) ; } this . migrationResult = this . runMigrationsInternal ( ) . then ( ( result ) => { if ( ! rerun ) { this . status$ . next ( { status : 'completed' , result } ) ; } return result ; } ) ; } return this . migrationResult ; } public getStatus$ ( ) { return this . status$ . asObservable ( ) ; } private runMigrationsInternal ( ) { const opensearchDashboardsIndexName = this . opensearchDashboardsConfig . index ; const indexMap = createIndexMap ( { opensearchDashboardsIndexName , indexMap : this . mappingProperties , registry : this . typeRegistry , } ) ; const migrators = Object . keys ( indexMap ) . map ( ( index ) => { return new IndexMigrator ( { batchSize : this . savedObjectsConfig . batchSize , client : this . client , documentMigrator : this . documentMigrator , index , log : this . log , mappingProperties : indexMap [ index ] . typeMappings , pollInterval : this . savedObjectsConfig . pollInterval , scrollDuration : this . savedObjectsConfig . scrollDuration , serializer : this . serializer , obsoleteIndexTemplatePattern : index === opensearchDashboardsIndexName ? 'opensearch_dashboards_index_template*' : undefined , convertToAliasScript : indexMap [ index ] . script , } ) ; } ) ; return Promise . all ( migrators . map ( ( migrator ) => migrator . migrate ( ) ) ) ; } /**
     * Gets all the index mappings defined by OpenSearchDashboards's enabled plugins.
     *
     */ public getActiveMappings ( ) { return this . activeMappings ; } param doc returns public migrateDocument ( doc ) { return this . documentMigrator . migrate ( doc ) ; } } /**
 * Merges savedObjectMappings properties into a single object, verifying that
 * no mappings are redefined.
 */ export function mergeTypes ( types ) { return types . reduce ( ( acc , { name : type , mappings } ) => { const duplicate = acc . hasOwnProperty ( type ) ; if ( duplicate ) { throw new Error ( `Type ${ type } is already defined.` ) ; } return { ... acc , [ type ] : mappings , } ; } , { } ) ; }