Skip to content

Commit

Permalink
[Discover] Extend ebt context with a list of activated context-aware …
Browse files Browse the repository at this point in the history
…profiles
  • Loading branch information
jughosta committed Sep 13, 2024
1 parent 3cc99fc commit af9fc84
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/plugins/discover/public/context_awareness/profiles_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import type { DataTableRecord } from '@kbn/discover-utils';
import { isOfAggregateQueryType } from '@kbn/es-query';
import { isEqual } from 'lodash';
import { BehaviorSubject, combineLatest, map } from 'rxjs';
import { BehaviorSubject, combineLatest, map, tap } from 'rxjs';
import { DataSourceType, isDataSourceType } from '../../common/data_sources';
import { addLog } from '../utils/add_log';
import type {
Expand Down Expand Up @@ -39,6 +39,9 @@ interface DataTableRecordWithContext extends DataTableRecord {
context: ContextWithProfileId<DocumentContext>;
}

export type ProfilesManagerEbtContext = BehaviorSubject<{ dscActiveProfiles: string[] }>;
export const EBT_NO_ACTIVE_PROFILES = ['default'];

/**
* Options for the `getProfiles` method
*/
Expand All @@ -52,6 +55,7 @@ export interface GetProfilesOptions {
export class ProfilesManager {
private readonly rootContext$: BehaviorSubject<ContextWithProfileId<RootContext>>;
private readonly dataSourceContext$: BehaviorSubject<ContextWithProfileId<DataSourceContext>>;
private readonly ebtContext$: ProfilesManagerEbtContext | undefined;

private prevRootProfileParams?: SerializedRootProfileParams;
private prevDataSourceProfileParams?: SerializedDataSourceProfileParams;
Expand All @@ -61,10 +65,12 @@ export class ProfilesManager {
constructor(
private readonly rootProfileService: RootProfileService,
private readonly dataSourceProfileService: DataSourceProfileService,
private readonly documentProfileService: DocumentProfileService
private readonly documentProfileService: DocumentProfileService,
ebtContext$: ProfilesManagerEbtContext | undefined
) {
this.rootContext$ = new BehaviorSubject(rootProfileService.defaultContext);
this.dataSourceContext$ = new BehaviorSubject(dataSourceProfileService.defaultContext);
this.ebtContext$ = ebtContext$;
}

/**
Expand Down Expand Up @@ -191,6 +197,16 @@ export class ProfilesManager {
*/
public getProfiles$(options: GetProfilesOptions = {}) {
return combineLatest([this.rootContext$, this.dataSourceContext$]).pipe(
tap(() => {
const dscActiveProfiles = [
this.rootContext$.getValue().profileId,
this.dataSourceContext$.getValue().profileId,
];
// console.log(dscActiveProfiles);
this.ebtContext$?.next({
dscActiveProfiles,
});
}),
map(() => this.getProfiles(options))
);
}
Expand Down
40 changes: 35 additions & 5 deletions src/plugins/discover/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ import { DISCOVER_CELL_ACTIONS_TRIGGER } from './context_awareness/types';
import { RootProfileService } from './context_awareness/profiles/root_profile';
import { DataSourceProfileService } from './context_awareness/profiles/data_source_profile';
import { DocumentProfileService } from './context_awareness/profiles/document_profile';
import { ProfilesManager } from './context_awareness/profiles_manager';
import {
ProfilesManager,
ProfilesManagerEbtContext,
EBT_NO_ACTIVE_PROFILES,
} from './context_awareness/profiles_manager';

/**
* Contains Discover, one of the oldest parts of Kibana
Expand Down Expand Up @@ -149,6 +153,25 @@ export class DiscoverPlugin
this.urlTracker = { setTrackedUrl, restorePreviousUrl, setTrackingEnabled };
this.stopUrlTracking = stopUrlTracker;

const ebtContext$ = new BehaviorSubject({ dscActiveProfiles: EBT_NO_ACTIVE_PROFILES });

core.analytics.registerContextProvider({
name: 'dsc_active_profiles',
context$: ebtContext$,
schema: {
dscActiveProfiles: {
type: 'array',
items: {
type: 'keyword',
_meta: {
description:
'List of profiles which are activated by Discover Context Awareness logic',
},
},
},
},
});

core.application.register({
id: PLUGIN_ID,
title: 'Discover',
Expand Down Expand Up @@ -183,7 +206,7 @@ export class DiscoverPlugin
history: this.historyService.getHistory(),
scopedHistory: this.scopedHistory,
urlTracker: this.urlTracker!,
profilesManager: await this.createProfilesManager(),
profilesManager: await this.createProfilesManager({ ebtContext$ }),
setHeaderActionMenu: params.setHeaderActionMenu,
});

Expand Down Expand Up @@ -319,22 +342,28 @@ export class DiscoverPlugin
return { rootProfileService, dataSourceProfileService, documentProfileService };
});

private async createProfilesManager() {
private async createProfilesManager({
ebtContext$,
}: {
ebtContext$?: ProfilesManagerEbtContext;
} = {}) {
const { rootProfileService, dataSourceProfileService, documentProfileService } =
await this.createProfileServices();

return new ProfilesManager(
rootProfileService,
dataSourceProfileService,
documentProfileService
documentProfileService,
ebtContext$
);
}

private createEmptyProfilesManager() {
return new ProfilesManager(
new RootProfileService(),
new DataSourceProfileService(),
new DocumentProfileService()
new DocumentProfileService(),
undefined // TODO?
);
}

Expand Down Expand Up @@ -365,6 +394,7 @@ export class DiscoverPlugin
};
};

// TODO: enable for the embeddable too?
const getDiscoverServicesInternal = async () => {
const [coreStart, deps] = await core.getStartServices();
const profilesManager = await this.createProfilesManager();
Expand Down

0 comments on commit af9fc84

Please sign in to comment.