Skip to content

Commit

Permalink
Add saved objects status api
Browse files Browse the repository at this point in the history
Signed-off-by: Bandini Bhopi <[email protected]>
  • Loading branch information
bandinib-amzn committed Jul 26, 2023
1 parent c3ea363 commit 96cc898
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ export class LegacyService implements CoreService {
registerType: setupDeps.core.savedObjects.registerType,
getImportExportObjectLimit: setupDeps.core.savedObjects.getImportExportObjectLimit,
setRepositoryFactoryProvider: setupDeps.core.savedObjects.setRepositoryFactoryProvider,
setStatus: () => {
throw new Error(`core.savedObjects.setStatus is unsupported in legacy`);
},
},
status: {
isStatusPageAnonymous: setupDeps.core.status.isStatusPageAnonymous,
Expand Down
1 change: 1 addition & 0 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export function createPluginSetupContext<TPlugin, TPluginDependencies>(
registerType: deps.savedObjects.registerType,
getImportExportObjectLimit: deps.savedObjects.getImportExportObjectLimit,
setRepositoryFactoryProvider: deps.savedObjects.setRepositoryFactoryProvider,
setStatus: deps.savedObjects.setStatus,
},
status: {
core$: deps.status.core$,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const createSetupContractMock = () => {
registerType: jest.fn(),
getImportExportObjectLimit: jest.fn(),
setRepositoryFactoryProvider: jest.fn(),
setStatus: jest.fn(),
};

setupContract.getImportExportObjectLimit.mockReturnValue(100);
Expand Down
53 changes: 46 additions & 7 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
* under the License.
*/

import { Subject, Observable } from 'rxjs';
import { first, filter, take, switchMap } from 'rxjs/operators';
import { Subject, Observable, BehaviorSubject } from 'rxjs';
import { first, filter, take, switchMap, map } from 'rxjs/operators';

import { CoreService } from '../../types';
import {
SavedObjectsClient,
Expand Down Expand Up @@ -62,7 +63,7 @@ import { Logger } from '../logging';
import { SavedObjectTypeRegistry, ISavedObjectTypeRegistry } from './saved_objects_type_registry';
import { SavedObjectsSerializer } from './serialization';
import { registerRoutes } from './routes';
import { ServiceStatus } from '../status';
import { ServiceStatus, ServiceStatusLevels } from '../status';
import { calculateStatus$ } from './status';
import { createMigrationOpenSearchClient } from './migrations/core/';
/**
Expand Down Expand Up @@ -175,6 +176,12 @@ export interface SavedObjectsServiceSetup {
setRepositoryFactoryProvider: (
respositoryFactoryProvider: SavedObjectRepositoryFactoryProvider
) => void;

/**
* Allows a plugin to specify a custom status dependent on its own criteria.
* Completely overrides the default status.
*/
setStatus(status$: Observable<ServiceStatus<SavedObjectStatusMeta>>): void;
}

/**
Expand Down Expand Up @@ -301,6 +308,11 @@ export class SavedObjectsService
private started = false;

private respositoryFactoryProvider?: SavedObjectRepositoryFactoryProvider;
private savedObjectServiceCustomStatus$?: Observable<ServiceStatus<SavedObjectStatusMeta>>;
private savedObjectServiceStatus$ = new BehaviorSubject<ServiceStatus<SavedObjectStatusMeta>>({
level: ServiceStatusLevels.unavailable,
summary: `waiting`,
});

constructor(private readonly coreContext: CoreContext) {
this.logger = coreContext.logger.get('savedobjects-service');
Expand Down Expand Up @@ -329,10 +341,7 @@ export class SavedObjectsService
});

return {
status$: calculateStatus$(
this.migrator$.pipe(switchMap((migrator) => migrator.getStatus$())),
setupDeps.opensearch.status$
),
status$: this.savedObjectServiceStatus$.asObservable(),
setClientFactoryProvider: (provider) => {
if (this.started) {
throw new Error('cannot call `setClientFactoryProvider` after service startup.');
Expand Down Expand Up @@ -368,6 +377,15 @@ export class SavedObjectsService
}
this.respositoryFactoryProvider = repositoryProvider;
},
setStatus: (status$) => {
if (this.started) {
throw new Error('cannot call `setRepositoryFactoryProvider` after service startup.');
}
if (this.savedObjectServiceCustomStatus$) {
throw new Error('custom repository factory is already set, and can only be set once');
}
this.savedObjectServiceCustomStatus$ = status$;
},
};
}

Expand All @@ -381,6 +399,27 @@ export class SavedObjectsService

this.logger.debug('Starting SavedObjects service');

if (this.savedObjectServiceCustomStatus$) {
this.savedObjectServiceCustomStatus$
.pipe(
map((savedObjectServiceCustomStatus) => {
return savedObjectServiceCustomStatus;
})
)
.subscribe((value) => this.savedObjectServiceStatus$.next(value));
} else {
calculateStatus$(
this.migrator$.pipe(switchMap((migrator) => migrator.getStatus$())),
this.setupDeps.opensearch.status$
)
.pipe(
map((defaultstatus) => {
return defaultstatus;
})
)
.subscribe((value) => this.savedObjectServiceStatus$.next(value));
}

const opensearchDashboardsConfig = await this.coreContext.configService
.atPath<OpenSearchDashboardsConfigType>('opensearchDashboards')
.pipe(first())
Expand Down

0 comments on commit 96cc898

Please sign in to comment.