Skip to content

Commit

Permalink
[Logs and Metrics UI] Make spaces optional plugin (elastic#151053)
Browse files Browse the repository at this point in the history
Closes elastic#149973 

# Summary

This PR makes `spaces` an optional plugin. In Logs UI we use only the
space id and there we want to fallback to the default space id when the
space plugin is disabled. The difference to the changes in the other PRs
is that we will consider the default space as "active" space if the
space plugin is disabled: so in this case we will return the default
space id as this is the only property we need from the `getActiveSpace`
response.

# Testing
1. In the `kibana.dev.yaml` add `xpack.spaces.enabled: false`
2. Before we have this
[PR](elastic#151147) merged you should do
[this
change](https://github.com/elastic/kibana/pull/151147/files#diff-1b17eae66f358505fae8d86df37e155a25e8db996fce93ee6016582fb341092e)
on the branch while testing
3. Inside Logs the `Anomalies` and `Categories` pages should load the
default space

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
2 people authored and justinkambic committed Feb 23, 2023
1 parent 10f152a commit 46c8c44
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/infra/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"share",
"features",
"usageCollection",
"spaces",
"embeddable",
"data",
"dataViews",
Expand All @@ -28,6 +27,7 @@
"unifiedSearch"
],
"optionalPlugins": [
"spaces",
"ml",
"home",
"embeddable",
Expand Down
14 changes: 12 additions & 2 deletions x-pack/plugins/infra/public/hooks/use_kibana_space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@
* 2.0.
*/

import useAsync from 'react-use/lib/useAsync';
import type { Space } from '@kbn/spaces-plugin/public';
import useAsync from 'react-use/lib/useAsync';
import { useKibanaContextForPlugin } from './use_kibana';

export type ActiveSpace =
| { isLoading: true; error: undefined; space: undefined }
| { isLoading: false; error: Error; space: undefined }
| { isLoading: false; error: undefined; space: Space };

// Fallback to default if spaces plugin is not available
const getDefaultSpaceAsPromise = () =>
Promise.resolve({
id: 'default',
name: 'Default',
disabledFeatures: [],
});

export const useActiveKibanaSpace = (): ActiveSpace => {
const kibana = useKibanaContextForPlugin();
const getActiveSpaceOrDefault =
kibana.services?.spaces?.getActiveSpace ?? getDefaultSpaceAsPromise;

const asyncActiveSpace = useAsync(kibana.services.spaces.getActiveSpace);
const asyncActiveSpace = useAsync(getActiveSpaceOrDefault);

if (asyncActiveSpace.loading) {
return {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/infra/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface InfraClientStartDeps {
unifiedSearch: UnifiedSearchPublicPluginStart;
dataViews: DataViewsPublicPluginStart;
observability: ObservabilityPublicStart;
spaces: SpacesPluginStart;
spaces?: SpacesPluginStart;
triggersActionsUi: TriggersAndActionsUIPublicPluginStart;
usageCollection: UsageCollectionStart;
ml: MlPluginStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/serve
import { CoreSetup, IRouter, KibanaRequest, RequestHandler, RouteMethod } from '@kbn/core/server';
import { UI_SETTINGS } from '@kbn/data-plugin/server';
import { TimeseriesVisData } from '@kbn/vis-type-timeseries-plugin/server';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import { TSVBMetricModel } from '../../../../common/inventory_models/types';
import { InfraConfig } from '../../../plugin';
import type { InfraPluginRequestHandlerContext } from '../../../types';
Expand Down Expand Up @@ -213,17 +214,7 @@ export class KibanaFramework {
}

public getSpaceId(request: KibanaRequest): string {
const spacesPlugin = this.plugins.spaces;

if (
spacesPlugin &&
spacesPlugin.spacesService &&
typeof spacesPlugin.spacesService.getSpaceId === 'function'
) {
return spacesPlugin.spacesService.getSpaceId(request);
} else {
return 'default';
}
return this.plugins.spaces?.spacesService?.getSpaceId(request) ?? DEFAULT_SPACE_ID;
}

public async makeTSVBRequest(
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/infra/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { handleEsError } from '@kbn/es-ui-shared-plugin/server';
import { i18n } from '@kbn/i18n';
import { Logger } from '@kbn/logging';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import { LOGS_FEATURE_ID, METRICS_FEATURE_ID } from '../common/constants';
import { defaultLogViewsStaticConfig } from '../common/log_views';
import { publicConfigKeys } from '../common/plugin_config_types';
Expand Down Expand Up @@ -196,7 +197,7 @@ export class InfraServerPlugin
const soClient = (await context.core).savedObjects.client;
const mlSystem = plugins.ml?.mlSystemProvider(request, soClient);
const mlAnomalyDetectors = plugins.ml?.anomalyDetectorsProvider(request, soClient);
const spaceId = plugins.spaces?.spacesService.getSpaceId(request) || 'default';
const spaceId = plugins.spaces?.spacesService.getSpaceId(request) ?? DEFAULT_SPACE_ID;

return {
mlAnomalyDetectors,
Expand Down

0 comments on commit 46c8c44

Please sign in to comment.