-
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
[Dashboard First] Genericize Attribute Service #76057
[Dashboard First] Genericize Attribute Service #76057
Conversation
@elasticmachine merge upstream |
Pinging @elastic/kibana-app (Team:KibanaApp) |
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 getting this error when trying to add a book embeddable:
Uncaught (in promise) SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at Object.showSavedObject (visualize_embeddable_factory.tsx:50)
at saved_object_finder.tsx:53
at Array.filter (<anonymous>)
at saved_object_finder.tsx:49
So I couldn't test this. Love addition of unit tests for the attribute service!
Some general thoughts below
const savedItem = await this.savedObjectsClient.create(this.type, newAttributes); | ||
return { savedObjectId: savedItem.id } as RefType; | ||
const savedItem = this.options?.customSaveMethod | ||
? await this.options.customSaveMethod(this.type, newAttributes) |
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.
This assumes customSaveMethod
will take type
and attributes
parameters by default.
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.
customSaveMethod
currently uses this signature:
customSaveMethod?: (
type: string,
attributes: A,
savedObjectId?: string
) => Promise<{ id: string }>;
It could easily be changed (and updated in the lens PR as well), but I can't think of a better signature to use.
> { | ||
private embeddableFactory: EmbeddableFactory; | ||
private embeddableFactory?: EmbeddableFactory; | ||
private attributesKey: AttributesKey; |
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.
Do we really need this? It feels like we're trying to be a bit too generic at this point and it's just adding a lot of code branching. Visualize embeddable can wrap its input in attributes
, and with having a custom save function, things should work fine. Ie.
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 made this for the case where the only difference between the regular attribute service and the embeddable input that will be using it is the name of the key. This may or may not be the case for Visualize, but I think it's a good feature to support, especially if the 'by value' paradigm will be used this way in many different embeddables. The only new logic this results in is the attribute key line:
this.attributesKey = options?.attributesKey || (ATTRIBUTE_SERVICE_DEFAULT_KEY as AttributesKey); |
Afterwards, the logic is all the same, accessing the attributes via:
input[this.attributesKey]
or setting the attributes via:
{ [this.attributesKey]: newAttributes }
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 actually ended up removing the attributesKey
to make this PR a little bit simpler. If we experiment with visualize and see that it could be helpful, it would be trivial to re-implement it in a followup PR.
await this.savedObjectsClient.update(this.type, savedObjectId, newAttributes); | ||
return { savedObjectId } as RefType; | ||
if (this.options?.customSaveMethod) { | ||
await this.options.customSaveMethod(this.type, newAttributes, savedObjectId); |
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.
Should this be a customUpdateMethod
?
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.
This is a good idea, and I considered it, but made the decision that the customSaveMethod
would handle both the update and save cases.
I thought that forgoing this bit of logic in the attribute service
"if you have a custom update method, use that, else if you have a custom save method, use that, else use the default".
... in favor of a little more logic in the custom save method i.e. this little bit of logic from the Lens saved object store:
const result = await (id
? this.safeUpdate(id, attributes, references)
: this.client.create(DOC_TYPE, attributes, {
references,
}));
... was a little bit cleaner. This all said, it would be possible to add the custom update method. What do you think works better?
I am unable to re-create the error on adding a book embeddable. I double checked creation of a new book and adding it to library, creation of a new book by value, and adding an existing book from the library. Not sure where the problem came from! |
@elasticmachine merge upstream |
…buteServiceCustomMethods
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.
code LGTM, didn't test out
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.
Checked on Mac/Chrome, LGTM.
…l title instead of embeddable
@elasticmachine merge upstream |
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.
Just a couple of observations. Tested with Safari 👍 .
src/plugins/dashboard/public/attribute_service/attribute_service.tsx
Outdated
Show resolved
Hide resolved
…omthomson/kibana into feature/attributeServiceCustomMethods
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.
LGTM
💚 Build SucceededBuild metricspage load bundle size
History
To update your PR or re-run it, just comment with: |
Genericized attribute service, with custom save and unwrap methods and added unit tests.
* master: (47 commits) Do not require id & description when creating a logstash pipeline (elastic#76616) Remove commented src/core/tsconfig file (elastic#76792) Replaced whitelistedHosts with allowedHosts in actions ascii docs (elastic#76731) [Dashboard First] Genericize Attribute Service (elastic#76057) [ci-metrics] unify distributable file count metrics (elastic#76448) [Security Solution][Detections] Handle conflicts on alert status update (elastic#75492) [eslint] convert to @typescript-eslint/no-unused-expressions (elastic#76471) [DOCS] Add default time range filter to advanced settings (elastic#76414) [Security Solution] Refactor NetworkTopNFlow to use Search Strategy (elastic#76249) [Dashboard] Update Index Patterns when Child Index Patterns Change (elastic#76356) [ML] Add option to Advanced Settings to set default time range filter for AD jobs (elastic#76347) Add CSM app to CODEOWNERS (elastic#76793) [Security Solution][Exceptions] - Updates exception item find sort field (elastic#76685) [Security Solution][Detections][Tech Debt] - Move to using common io-ts types (elastic#75009) [Lens] Drag dimension to replace (elastic#75895) URI encode the index names we fetch in the fetchIndices lib function. (elastic#76584) [Security Solution] Resolver retrieve entity id of documents without field mapped (elastic#76562) [Ingest Manager] validate agent route using AJV instead kbn-config-schema (elastic#76546) Updated non-dev usages of node-forge (elastic#76699) [Ingest Pipelines] Processor forms for processors K-S (elastic#75638) ...
Summary
This PR genericizes the Attribute service by adding an options prop.
The options prop contains:
savedObject
is transformed into the by value input. If left undefined, the attribute service returns theattributes
key from thesavedObject
savedObjectClient.update
orsavedObjectClient.create
. If provided, the attribute service will use the same custom save method in both the update and create cases.Using the AttributeService
To accept the defaults, you can get the attribute service via:
Getting an attribute service with custom save, and unwrap methods:
Checklist
Delete any items that are not applicable to this PR.
For maintainers