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

[ML] Decouple open-in-anomaly-explorer action #178729

Merged

Conversation

darnautov
Copy link
Contributor

@darnautov darnautov commented Mar 14, 2024

Summary

Decouples open-in-anomaly-explorer UI action from embeddable framework.

  • Modifies and exports helper utils from the embeddable plugin to convert embeddable inputs and outputs to APIs
  • Updates anomaly swim lane and anomaly charts embeddables to expose required API for the "Open in Anomaly Explorer" action

Part of #178375
Part of #174967

@darnautov darnautov requested a review from a team as a code owner March 14, 2024 13:45
@darnautov darnautov added the :ml label Mar 14, 2024
@darnautov darnautov self-assigned this Mar 14, 2024
@darnautov darnautov added Feature:UIActions UI actions. These are client side only, not related to the server side actions.. Team:ML Team label for ML (also use :ml) v8.14.0 labels Mar 14, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/ml-ui (:ml)

@darnautov darnautov added the release_note:skip Skip the PR/issue when compiling release notes label Mar 14, 2024
@darnautov darnautov marked this pull request as draft March 14, 2024 15:32
Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, in x-pack/plugins/ml/public/ui_actions/open_in_anomaly_explorer_action.tsx, the action can no longer use embeddable.getInput and embeddable.getOutput. Instead, action will have to read values directly from embeddable. For example embeddable.jobIds. All embeddables that support this action will have to expose jobIds.

@darnautov darnautov marked this pull request as ready for review March 14, 2024 21:23
@darnautov darnautov requested a review from nreese March 14, 2024 21:23

const { jobIds, timeRange, viewBy } = embeddable.getInput();
const { perPage, fromPage } = embeddable.getOutput();
const { localTimeRange, viewBy, jobIds, perPage, fromPage } = embeddable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update from main, localTimeRange has been renamed to timeRange$

Copy link
Contributor

@nreese nreese Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing. embeddable.getInput().timeRange combined dashboard time range and any custom time range stored with the panel. I think when reading this you will need
const timeRange = embeddable.timeRange$?.value ?? embeddable.parentApi?.timeRange$?.value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4211c14

constructor(
initialInput: AnomalySwimlaneEmbeddableInput,
public services: [CoreStart, MlDependencies, AnomalySwimlaneServices],
parent?: IContainer
) {
super(initialInput, services[2].anomalyDetectorService, services[1].data.dataViews, parent);

this.apiSubscriptions.add(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you will want to add distinctUntilKeyChanged so that BehaviorSubject is only updated when the key changes, and not everytime the output or input changes. You should just be able to use embeddableInputToSubject and embeddableOutputToSubject utility methods from src/plugins/embeddable/public/lib/embeddables/compatibility/embeddable_compatibility_utils.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in fd352ad

@darnautov darnautov requested a review from a team as a code owner March 18, 2024 11:45
@botelastic botelastic bot added the Feature:Embedding Embedding content via iFrame label Mar 18, 2024
@darnautov darnautov requested a review from nreese March 18, 2024 11:46
Copy link
Contributor

@ThomThomson ThomThomson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! The actions are decoupled as expected, and the changes to the embeddable plugin look good.

Left a few small nits and cleanups. LGTM!

@@ -41,12 +42,37 @@ export class AnomalySwimlaneEmbeddable extends AnomalyDetectionEmbeddable<
private reload$ = new Subject<void>();
public readonly type: string = ANOMALY_SWIMLANE_EMBEDDABLE_TYPE;

// API
public viewBy = new BehaviorSubject<string | undefined>(this.getInput().viewBy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. If you're initializing these in the constructor via embeddableInputToSubject or embeddableOutputToSubject, you don't need to initialize them via new here. The helpers will handle that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, leftovers from the original implementation, updated in 48d9c50

public perPage = new BehaviorSubject<number | undefined>(this.getOutput().perPage);
public fromPage = new BehaviorSubject<number | undefined>(this.getOutput().fromPage);

private apiSubscriptions = new Subscription();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I pass it to embeddableInputToSubject and embeddableOutputToSubject to populate subscriptions and unsubscribe on destroy

@@ -40,12 +41,25 @@ export class AnomalyChartsEmbeddable extends AnomalyDetectionEmbeddable<
private reload$ = new Subject<void>();
public readonly type: string = ANOMALY_EXPLORER_CHARTS_EMBEDDABLE_TYPE;

// API
public entityFields = new BehaviorSubject<MlEntityField[] | undefined>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use embeddableOutputToSubject?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it for one embeddable and forgot the other 🤦🏻 Updated in d4debca

}

export interface SwimLaneDrilldownApi {
// Props from embeddable input
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these comments? Ideally soon these won't have any connection to the legacy embeddable's input / output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the cleanup in 22df822

embeddable,
}: SwimLaneDrilldownContext | AnomalyChartsFieldSelectionContext) {
async isCompatible({ embeddable }: EmbeddableApiContext) {
if (!isApiCompatible(embeddable)) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Here, wouldn't it be simpler to call your more specific type guards like:

return isSwimLaneEmbeddableContext(context) || isAnomalyChartsEmbeddableContext(context)? That way you wouldn't need to have the separate isApiCompatible method which checks the same things?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first wrote this isCompatible method looking at the example and added custom type guards later. Updated in dfaef99

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #84 / serverless observability UI Dataset Quality Dataset quality summary updates active datasets and estimated data KPIs

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
embeddable 451 460 +9

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
ml 3.7MB 3.7MB +1.4KB

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
embeddable 9 11 +2

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
embeddable 65.6KB 66.1KB +436.0B
ml 78.4KB 78.5KB +139.0B
total +575.0B
Unknown metric groups

API count

id before after diff
embeddable 553 562 +9

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @darnautov

Copy link
Contributor

@peteharverson peteharverson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the 'Open in Anomaly Explorer' action with embedded swim lane and anomaly charts in dashboards and cases and LGTM

@darnautov darnautov merged commit a9968cb into elastic:main Mar 19, 2024
17 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Mar 19, 2024
@darnautov darnautov deleted the ml-migrate-open-in-anomaly-explorer-action branch March 19, 2024 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Embedding Embedding content via iFrame Feature:UIActions UI actions. These are client side only, not related to the server side actions.. :ml project:embeddableRebuild release_note:skip Skip the PR/issue when compiling release notes Team:ML Team label for ML (also use :ml) v8.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants