Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decouple anomaly job creation action from Embeddable framework #176869

Merged
merged 26 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
55f14d7
decoupld anomaly job creation action from Embeddable framework
nreese Feb 13, 2024
13a1aaf
finish conversion and typing
nreese Feb 15, 2024
a032c54
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Feb 15, 2024
c0e2286
merge with main
nreese Feb 15, 2024
80a079b
move MapsApi into maps plugin
nreese Feb 15, 2024
c91a509
move LensApi to has_lens_config
nreese Feb 15, 2024
dc059a6
clean-up
nreese Feb 15, 2024
a4c8686
Merge branch 'main' into ml_job_creation_action
kibanamachine Feb 20, 2024
f11b045
merge with main
nreese Feb 21, 2024
e0e271c
Merge branch 'main' into ml_job_creation_action
kibanamachine Feb 22, 2024
b729f06
use getPanelTitle utility method to read panel titles
nreese Feb 22, 2024
e1d233d
merge with main
nreese Mar 5, 2024
835e3ae
add title and search api guards to isLensApi type guard
nreese Mar 5, 2024
c8c6a4c
add type guard to map API
nreese Mar 5, 2024
1b3f3e3
review feedback
nreese Mar 5, 2024
b82062c
eslint
nreese Mar 5, 2024
514010c
clean up
nreese Mar 5, 2024
3df9ef8
Merge branch 'main' into ml_job_creation_action
kibanamachine Mar 5, 2024
1d61828
clean up
nreese Mar 5, 2024
dfbb86b
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Mar 5, 2024
0057af8
merge with main
nreese Mar 8, 2024
f5ad2f5
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Mar 8, 2024
6e6fa3e
Merge branch 'main' into ml_job_creation_action
kibanamachine Mar 11, 2024
3bfa986
move DashboardApi interface into existing DashboardAPI interface
nreese Mar 11, 2024
a5ef271
update DashboardAPI
nreese Mar 11, 2024
ce78326
eslint
nreese Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@

import type { DataView } from '@kbn/data-views-plugin/public';
import { CanDuplicatePanels, CanExpandPanels, TracksOverlays } from '@kbn/presentation-containers';
import { HasTypeDisplayName, PublishesSavedObjectId } from '@kbn/presentation-publishing';
import {
HasType,
HasTypeDisplayName,
PublishesLocalUnifiedSearch,
PublishesPanelTitle,
PublishesSavedObjectId,
} from '@kbn/presentation-publishing';
import { DashboardPanelState } from '../../../common';
import { DashboardContainer } from '../embeddable/dashboard_container';

// TODO lock down DashboardAPI
export type DashboardAPI = DashboardContainer;
export type DashboardAPI = DashboardContainer &
Partial<
HasType<'dashboard'> &
PublishesLocalUnifiedSearch &
PublishesPanelTitle &
PublishesSavedObjectId
>;
export type AwaitingDashboardAPI = DashboardAPI | null;

export const buildApiFromDashboardContainer = (container?: DashboardContainer) => container ?? null;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/embeddable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

export * from './embeddable';

export { type HasLensConfig, apiHasLensConfig } from './interfaces/has_lens_config';
export { type LensApi, isLensApi } from './interfaces/lens_api';

This file was deleted.

38 changes: 38 additions & 0 deletions x-pack/plugins/lens/public/embeddable/interfaces/lens_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type {
HasParentApi,
HasType,
PublishesLocalUnifiedSearch,
PublishesPanelTitle,
} from '@kbn/presentation-publishing';
import {
apiIsOfType,
apiPublishesLocalUnifiedSearch,
apiPublishesPanelTitle,
} from '@kbn/presentation-publishing';
import { LensSavedObjectAttributes } from '../embeddable';

export type HasLensConfig = HasType<'lens'> & {
getSavedVis: () => Readonly<LensSavedObjectAttributes | undefined>;
};

export type LensApi = HasLensConfig &
PublishesPanelTitle &
PublishesLocalUnifiedSearch &
Partial<HasParentApi<unknown>>;

export const isLensApi = (api: unknown): api is LensApi => {
return Boolean(
api &&
apiIsOfType(api, 'lens') &&
typeof (api as HasLensConfig).getSavedVis === 'function' &&
apiPublishesPanelTitle(api) &&
apiPublishesLocalUnifiedSearch(api)
);
};
4 changes: 2 additions & 2 deletions x-pack/plugins/lens/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { LensPlugin } from './plugin';

export { apiHasLensConfig } from './embeddable/interfaces/has_lens_config';
export { isLensApi } from './embeddable/interfaces/lens_api';
export type {
EmbeddableComponentProps,
EmbeddableComponent,
Expand Down Expand Up @@ -110,7 +110,7 @@ export type {
export type { InlineEditLensEmbeddableContext } from './trigger_actions/open_lens_config/in_app_embeddable_edit/types';

export type {
HasLensConfig,
LensApi,
LensEmbeddableInput,
LensSavedObjectAttributes,
Embeddable,
Expand Down
37 changes: 37 additions & 0 deletions x-pack/plugins/maps/public/embeddable/map_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type {
HasParentApi,
HasType,
PublishesDataViews,
PublishesPanelTitle,
PublishesLocalUnifiedSearch,
} from '@kbn/presentation-publishing';
import {
apiIsOfType,
apiPublishesLocalUnifiedSearch,
apiPublishesPanelTitle,
} from '@kbn/presentation-publishing';
import type { ILayer } from '../classes/layers/layer';

export type MapApi = HasType<'map'> & {
getLayerList: () => ILayer[];
} & PublishesDataViews &
PublishesPanelTitle &
PublishesLocalUnifiedSearch &
Partial<HasParentApi<unknown>>;

export const isMapApi = (api: unknown): api is MapApi => {
return Boolean(
api &&
apiIsOfType(api, 'map') &&
typeof (api as MapApi).getLayerList === 'function' &&
apiPublishesPanelTitle(api) &&
apiPublishesLocalUnifiedSearch(api)
);
};
1 change: 1 addition & 0 deletions x-pack/plugins/maps/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type {
export type { MapsSetupApi, MapsStartApi } from './api';

export type { MapEmbeddable, MapEmbeddableInput, MapEmbeddableOutput } from './embeddable';
export { type MapApi, isMapApi } from './embeddable/map_api';

export type { EMSTermJoinConfig, SampleValuesConfig } from './ems_autosuggest';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { apiIsOfType } from '@kbn/presentation-publishing';
import { apiHasVisualizeConfig } from '@kbn/visualizations-plugin/public';
import { apiHasLensConfig } from '@kbn/lens-plugin/public';
import { isLensApi } from '@kbn/lens-plugin/public';
import { MAP_SAVED_OBJECT_TYPE } from '../../../common/constants';
import { isLegacyMapApi } from '../../legacy_visualizations/is_legacy_map';
import { mapEmbeddablesSingleton } from '../../embeddable/map_embeddables_singleton';
Expand All @@ -19,7 +19,7 @@ export function isCompatible(api: SynchronizeMovementActionApi) {
}
return (
apiIsOfType(api, MAP_SAVED_OBJECT_TYPE) ||
(apiHasLensConfig(api) && api.getSavedVis()?.visualizationType === 'lnsChoropleth') ||
(isLensApi(api) && api.getSavedVis()?.visualizationType === 'lnsChoropleth') ||
(apiHasVisualizeConfig(api) && isLegacyMapApi(api))
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import type { HasType } from '@kbn/presentation-publishing';
import type { HasLensConfig } from '@kbn/lens-plugin/public';
import type { LensApi } from '@kbn/lens-plugin/public';
import type { HasVisualizeConfig } from '@kbn/visualizations-plugin/public';

export type SynchronizeMovementActionApi =
| HasType<'map' | 'visualization' | 'lens'>
| Partial<HasLensConfig | HasVisualizeConfig>;
| Partial<LensApi | HasVisualizeConfig>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* 2.0.
*/

export { QuickJobCreatorBase, isLensEmbeddable, isMapEmbeddable } from './quick_create_job_base';
export { QuickJobCreatorBase } from './quick_create_job_base';
export type { CreateState } from './quick_create_job_base';
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import type { IUiSettingsClient } from '@kbn/core/public';
import type { TimefilterContract } from '@kbn/data-plugin/public';
import { firstValueFrom } from 'rxjs';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { DashboardLocatorParams, DashboardStart } from '@kbn/dashboard-plugin/public';
import type {
DashboardAPI,
DashboardLocatorParams,
DashboardStart,
} from '@kbn/dashboard-plugin/public';
import { getPanelTitle } from '@kbn/presentation-publishing';
import type { Filter, Query, DataViewBase } from '@kbn/es-query';
import { FilterStateStore } from '@kbn/es-query';
import type { Embeddable } from '@kbn/lens-plugin/public';
import type { MapEmbeddable } from '@kbn/maps-plugin/public';
import type { ErrorType } from '@kbn/ml-error-utils';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import type { MlApiServices } from '../../../services/ml_api_service';
Expand All @@ -25,16 +28,6 @@ import type { CREATED_BY_LABEL } from '../../../../../common/constants/new_job';
import { createQueries } from '../utils/new_job_utils';
import { createDatafeedId } from '../../../../../common/util/job_utils';

export function isLensEmbeddable(arg: any): arg is Embeddable {
return arg.hasOwnProperty('type') && arg.type === 'lens';
}

export function isMapEmbeddable(arg: any): arg is MapEmbeddable {
return arg.hasOwnProperty('type') && arg.type === 'map';
}

export type Dashboard = Embeddable['parent'];

interface CreationState {
success: boolean;
error?: ErrorType;
Expand Down Expand Up @@ -85,7 +78,7 @@ export class QuickJobCreatorBase {
end: number | undefined;
startJob: boolean;
runInRealTime: boolean;
dashboard?: Dashboard;
dashboard?: DashboardAPI;
}) {
const datafeedId = createDatafeedId(jobId);
const datafeed = { ...datafeedConfig, job_id: jobId, datafeed_id: datafeedId };
Expand Down Expand Up @@ -232,23 +225,14 @@ export class QuickJobCreatorBase {
return mergedQueries;
}

private async createDashboardLink(dashboard: Dashboard, datafeedConfig: estypes.MlDatafeed) {
const dashboardTitle = dashboard?.getTitle();
if (dashboardTitle === undefined || dashboardTitle === '') {
// embeddable may have not been in a dashboard
// and my not have been given a title as it is unsaved.
return null;
}

const findDashboardsService = await this.dashboardService.findDashboardsService();
nreese marked this conversation as resolved.
Show resolved Hide resolved
// find the dashboard from the dashboard service as the dashboard passed in may not have the correct id
const foundDashboard = await findDashboardsService.findByTitle(dashboardTitle);
if (foundDashboard === undefined) {
private async createDashboardLink(dashboard: DashboardAPI, datafeedConfig: estypes.MlDatafeed) {
const savedObjectId = dashboard.savedObjectId?.value;
if (!savedObjectId) {
return null;
}

const params: DashboardLocatorParams = {
dashboardId: foundDashboard.id,
dashboardId: savedObjectId,
timeRange: {
from: '$earliest$',
to: '$latest$',
Expand All @@ -270,13 +254,13 @@ export class QuickJobCreatorBase {
const url = `${location.app}${location.path}`;
const urlName = i18n.translate('xpack.ml.newJob.fromLens.createJob.namedUrlDashboard', {
defaultMessage: 'Open {dashboardTitle}',
values: { dashboardTitle },
values: { dashboardTitle: getPanelTitle(dashboard) ?? 'dashboard' },
});

return { url_name: urlName, url_value: url, time_range: 'auto' };
}

private async getCustomUrls(dashboard: Dashboard, datafeedConfig: estypes.MlDatafeed) {
private async getCustomUrls(dashboard: DashboardAPI, datafeedConfig: estypes.MlDatafeed) {
const customUrls = await this.createDashboardLink(dashboard, datafeedConfig);
return dashboard !== undefined && customUrls !== null ? { custom_urls: [customUrls] } : {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export {
getJobsItemsFromEmbeddable,
isCompatibleVisualizationType,
redirectToADJobWizards,
getChartInfoFromVisualization,
} from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { i18n } from '@kbn/i18n';
import type {
ChartInfo,
Embeddable,
LensPublicStart,
LensSavedObjectAttributes,
} from '@kbn/lens-plugin/public';
Expand All @@ -17,7 +16,7 @@ import type { TimefilterContract } from '@kbn/data-plugin/public';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import type { Filter, Query } from '@kbn/es-query';
import type { DashboardStart } from '@kbn/dashboard-plugin/public';

import type { LensApi } from '@kbn/lens-plugin/public';
import type { JobCreatorType } from '../common/job_creator';
import { createEmptyJob, createEmptyDatafeed } from '../common/job_creator/util/default_configs';
import { stashJobForCloning } from '../common/job_creator/util/general';
Expand Down Expand Up @@ -51,7 +50,7 @@ export class QuickLensJobCreator extends QuickJobCreatorBase {
public async createAndSaveJob(
jobId: string,
bucketSpan: string,
embeddable: Embeddable,
embeddable: LensApi,
startJob: boolean,
runInRealTime: boolean,
layerIndex: number
Expand Down
Loading