-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Implementing ReferenceOrValueEmbeddable for visualize embeddable #76088
Changes from 17 commits
c014e35
dbafccb
7f2e4ad
3209dc2
9788589
2b6f075
e9dff6f
3c02a0e
f04c20f
d9e8bfb
11e878a
020c104
c04d0f7
db9adc0
1189f44
3e7668b
e645b98
5843645
cee9b81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { DashboardStart } from './plugin'; | ||
|
||
export type Start = jest.Mocked<DashboardStart>; | ||
|
||
const createStartContract = (): DashboardStart => { | ||
// @ts-ignore | ||
const startContract: DashboardStart = { | ||
getAttributeService: jest.fn(), | ||
}; | ||
|
||
return startContract; | ||
}; | ||
|
||
export const dashboardPluginMock = { | ||
createStartContract, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,11 +37,11 @@ export const defaultEmbeddableFactoryProvider = < | |
getExplicitInput: def.getExplicitInput | ||
? def.getExplicitInput.bind(def) | ||
: () => Promise.resolve({}), | ||
createFromSavedObject: | ||
def.createFromSavedObject ?? | ||
((savedObjectId: string, input: Partial<I>, parent?: IContainer) => { | ||
throw new Error(`Creation from saved object not supported by type ${def.type}`); | ||
}), | ||
createFromSavedObject: def.createFromSavedObject | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not quite sure why this change is required, why the bind, and why a ternary here instead of nullish coalescing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't bound properly and was causing an error when creating visualize embeddable from saved object: |
||
? def.createFromSavedObject.bind(def) | ||
: (savedObjectId: string, input: Partial<I>, parent?: IContainer) => { | ||
throw new Error(`Creation from saved object not supported by type ${def.type}`); | ||
}, | ||
create: def.create.bind(def), | ||
type: def.type, | ||
isEditable: def.isEditable.bind(def), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
"version": "kibana", | ||
"server": true, | ||
"ui": true, | ||
"requiredPlugins": ["data", "expressions", "uiActions", "embeddable", "usageCollection", "inspector"], | ||
"requiredPlugins": ["data", "expressions", "uiActions", "embeddable", "usageCollection", "inspector", "dashboard"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we certain the only way to address this is to introduce a required dependency between This doesn't feel right to me, but I'm also not familiar enough with the I just wanted to raise the question before we move forward with merging this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey Luke! This is a really good point. I made the decision to move The attributeService is an additional layer of abstraction on top of savedObjects. It provides a default implementation that embeddables can use to deal more easily with input that can be either 'by value' or 'by reference' . It also provides code for translating between the two types of input. The dashboard plugin is not its permanent home, however, it needs to stay somewhere until we find a better place for it. Do you have any thoughts on where it could live? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, right i didn't notice this ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Attribute service is indeed embeddable specific, but it's also only used within the dashboard (at least for now). I don't think saved objects plugin is the best place for it, since it's more than just saved objects. If there are major concerns about this living inside the dashboard, I think a separate plugin is the best way to go. |
||
"requiredBundles": ["kibanaUtils", "discover", "savedObjects"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import _, { get } from 'lodash'; | ||
import { Subscription } from 'rxjs'; | ||
import * as Rx from 'rxjs'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { VISUALIZE_EMBEDDABLE_TYPE } from './constants'; | ||
import { | ||
IIndexPattern, | ||
|
@@ -35,6 +36,8 @@ import { | |
Embeddable, | ||
IContainer, | ||
Adapters, | ||
SavedObjectEmbeddableInput, | ||
ReferenceOrValueEmbeddable, | ||
} from '../../../../plugins/embeddable/public'; | ||
import { | ||
IExpressionLoaderParams, | ||
|
@@ -47,6 +50,10 @@ import { getExpressions, getUiActions } from '../services'; | |
import { VIS_EVENT_TO_TRIGGER } from './events'; | ||
import { VisualizeEmbeddableFactoryDeps } from './visualize_embeddable_factory'; | ||
import { TriggerId } from '../../../ui_actions/public'; | ||
import { SavedObjectAttributes } from '../../../../core/types'; | ||
import { AttributeService } from '../../../dashboard/public'; | ||
import { SavedVisualizationsLoader } from '../saved_visualizations'; | ||
import { VisSavedObject } from '../types'; | ||
|
||
const getKeys = <T extends {}>(o: T): Array<keyof T> => Object.keys(o) as Array<keyof T>; | ||
|
||
|
@@ -75,9 +82,19 @@ export interface VisualizeOutput extends EmbeddableOutput { | |
visTypeName: string; | ||
} | ||
|
||
export type VisualizeSavedObjectAttributes = SavedObjectAttributes & { | ||
title: string; | ||
vis?: Vis; | ||
savedVis?: VisSavedObject; | ||
}; | ||
export type VisualizeByValueInput = { attributes: VisualizeSavedObjectAttributes } & VisualizeInput; | ||
export type VisualizeByReferenceInput = SavedObjectEmbeddableInput & VisualizeInput; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice looking input shapes! |
||
|
||
type ExpressionLoader = InstanceType<ExpressionsStart['ExpressionLoader']>; | ||
|
||
export class VisualizeEmbeddable extends Embeddable<VisualizeInput, VisualizeOutput> { | ||
export class VisualizeEmbeddable | ||
extends Embeddable<VisualizeInput, VisualizeOutput> | ||
implements ReferenceOrValueEmbeddable<VisualizeByValueInput, VisualizeByReferenceInput> { | ||
private handler?: ExpressionLoader; | ||
private timefilter: TimefilterContract; | ||
private timeRange?: TimeRange; | ||
|
@@ -93,11 +110,23 @@ export class VisualizeEmbeddable extends Embeddable<VisualizeInput, VisualizeOut | |
private abortController?: AbortController; | ||
private readonly deps: VisualizeEmbeddableFactoryDeps; | ||
private readonly inspectorAdapters?: Adapters; | ||
private attributeService?: AttributeService< | ||
VisualizeSavedObjectAttributes, | ||
VisualizeByValueInput, | ||
VisualizeByReferenceInput | ||
>; | ||
private savedVisualizationsLoader?: SavedVisualizationsLoader; | ||
|
||
constructor( | ||
timefilter: TimefilterContract, | ||
{ vis, editPath, editUrl, indexPatterns, editable, deps }: VisualizeEmbeddableConfiguration, | ||
initialInput: VisualizeInput, | ||
attributeService?: AttributeService< | ||
VisualizeSavedObjectAttributes, | ||
VisualizeByValueInput, | ||
VisualizeByReferenceInput | ||
>, | ||
savedVisualizationsLoader?: SavedVisualizationsLoader, | ||
parent?: IContainer | ||
) { | ||
super( | ||
|
@@ -118,6 +147,8 @@ export class VisualizeEmbeddable extends Embeddable<VisualizeInput, VisualizeOut | |
this.vis = vis; | ||
this.vis.uiState.on('change', this.uiStateChangeHandler); | ||
this.vis.uiState.on('reload', this.reload); | ||
this.attributeService = attributeService; | ||
this.savedVisualizationsLoader = savedVisualizationsLoader; | ||
|
||
this.autoRefreshFetchSubscription = timefilter | ||
.getAutoRefreshFetch$() | ||
|
@@ -381,4 +412,52 @@ export class VisualizeEmbeddable extends Embeddable<VisualizeInput, VisualizeOut | |
public supportedTriggers(): TriggerId[] { | ||
return this.vis.type.getSupportedTriggers?.() ?? []; | ||
} | ||
|
||
inputIsRefType = (input: VisualizeInput): input is VisualizeByReferenceInput => { | ||
if (!this.attributeService) { | ||
throw new Error('AttributeService must be defined for getInputAsRefType'); | ||
} | ||
return this.attributeService.inputIsRefType(input as VisualizeByReferenceInput); | ||
}; | ||
|
||
getInputAsValueType = async (): Promise<VisualizeByValueInput> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the attribute service here as well? The implementation is quite simple, but it could be more consistent that way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried, but it's honestly too much hassle. I'd need a |
||
const input = { | ||
savedVis: this.vis.serialize(), | ||
}; | ||
if (this.getTitle()) { | ||
input.savedVis.title = this.getTitle(); | ||
} | ||
delete input.savedVis.id; | ||
return new Promise<VisualizeByValueInput>((resolve) => { | ||
resolve({ ...(input as VisualizeByValueInput) }); | ||
}); | ||
}; | ||
|
||
getInputAsRefType = async (): Promise<VisualizeByReferenceInput> => { | ||
const savedVis = await this.savedVisualizationsLoader?.get({}); | ||
if (!savedVis) { | ||
throw new Error('Error creating a saved vis object'); | ||
} | ||
if (!this.attributeService) { | ||
throw new Error('AttributeService must be defined for getInputAsRefType'); | ||
} | ||
const saveModalTitle = this.getTitle() | ||
? this.getTitle() | ||
: i18n.translate('visualizations.embeddable.placeholderTitle', { | ||
defaultMessage: 'Placeholder Title', | ||
}); | ||
// @ts-ignore | ||
const attributes: VisualizeSavedObjectAttributes = { | ||
savedVis, | ||
vis: this.vis, | ||
title: this.vis.title, | ||
}; | ||
return this.attributeService.getInputAsRefType( | ||
{ | ||
id: this.id, | ||
attributes, | ||
}, | ||
{ showSaveModal: true, saveModalTitle } | ||
); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering about this case for when there isn't an id in the returned saved item - is this part of the error case that you've added to the API? If so, shouldn't we return the error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
id
check here is to make typescript happy. But good point that we need error handling for the scenario where saving a saved object failsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay yes, makes sense. You need the check because you changed the signature of the custom save method to send back an error case.