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

Refactor loading saved objects #9406

Merged
merged 11 commits into from
Dec 19, 2016

Conversation

stacey-gammon
Copy link
Contributor

Every saved object type repeated essentially the same code to find and retrieve objects from the server. This refactors that out into a common place. Some saved objects did need to overwrite the default functions when there was some slight deviation from the norm.

Note this change includes a fix for #9402 and also changes the timelion default retrieval size to 100 to match the rest of the saved objects (used to be 1000 but I couldn't find any good reason for the discrepancy).

@thomasneirynck
Copy link
Contributor

jenkins, test this

Copy link
Contributor

@thomasneirynck thomasneirynck left a comment

Choose a reason for hiding this comment

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

Don't have much to add :) I tested this with a few dashboards/vizzes/saved searches. Works!

this is also cool:
image

FWIW, since this is normal OO, I would use JS's built-in grammar to define the type, instead of recreating the methods inside the constructor function each time it's called.

either the old fashioned way, which Kibana frequently uses:

function SavedObjectSearch(){
 ...
}
SavedObjectSearch.prototype.get .= function()...
SavedObjectSearch.prototype.urlFor = function()...

or with ES6 class syntax.

class SavedObjectSearch(){
   constructor(...){...}
   get(){...}
   urlFor(){...}
}

There's a million ways to define types in JS (sadly), so I like keeping it conventional.

Anyway, that's minor.

LGTM.

}, (hit) => this.mapHits(hit));
};

this.find = (searchString, size = 100) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI: there is an issue about this hardcoded limit. #8044

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah cool, added a comment and a TODO to keep it on the radar.

@stacey-gammon
Copy link
Contributor Author

Switched to es6 classes! I actually meant to use the revealing module pattern but did it wrong, doh. After some more thought I decided I like es6 classes better, and even though it doesn't give you true private scope, it's clean and looks nice. :) Not much in that file is private anyhow.

@tsullivan tsullivan self-assigned this Dec 8, 2016
const source = hit._source;
source.id = hit._id;
source.url = this.urlFor(hit._id);
return source;
Copy link
Member

Choose a reason for hiding this comment

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

This mutates the hit parameter by adding ._source.id and ._source.url. Maybe the declaration of const source should clone hit._source first?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, I'm not 100% convinced. I don't think the code is relying on hit._source being modified, but I don't know for sure. Also, creating the clone is inefficient, unnecessary, and potentially dangerous without fully understanding what is in ._source (deep clone via json parse/stringify or shallow via object.assign? any circular references? Are any class types important to preserve?).

I'm not super confident that making a clone wouldn't create subtle bugs since I don't fully understand what ._source is (or might be), so at this moment, I don't think creating a clone isn't worth the additional complexity and would prefer to leave the logic as is and make this a refactor only PR (well, minus the two situations that produced wrong results - the 1000 -> 100 change and getting rid of allowing wildcards for visualizations).

But I'm open to changing it if you feel strongly, or I am wrong in any of my thoughts above, or if you have a better understanding of what ._source is. Maybe I'm overcomplicating it. :)

I did add a comment, anyhow, so it's clear that hit._source is being modified and returned.

Copy link
Member

Choose a reason for hiding this comment

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

I don't feel strongly about it, so keeping it as-is is fine with me. I think the added comment is very helpful

index: this.kbnIndex,
type: this.type.toLowerCase(),
body: body,
size: size
Copy link
Member

Choose a reason for hiding this comment

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

I think we should use the ES6 object literal shortcut for these fields, just

{
  ...
  body,
  size
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@stacey-gammon
Copy link
Contributor Author

@tsullivan all comments should be addressed, lmk if there is anything else!

@tsullivan
Copy link
Member

LGTM

@stacey-gammon
Copy link
Contributor Author

jenkins test this

@stacey-gammon stacey-gammon merged commit 1924d28 into elastic:master Dec 19, 2016
elastic-jasper added a commit that referenced this pull request Dec 19, 2016
Backports PR #9406

**Commit 1:**
Refactor code that loads saved objects

* Original sha: ddcdb6a
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-07T20:01:51Z

**Commit 2:**
couple fixes and comments

cleanup & comments

cleanup

* Original sha: d1bd310
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-07T20:51:31Z

**Commit 3:**
Use es6 classes, make a note about getting rid of the 100 hard limit on results

* Original sha: 78791b3
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-08T20:37:24Z

**Commit 4:**
Address comments

* Original sha: f8f842f
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-09T14:24:52Z

**Commit 5:**
Merge branch 'master' into saved-object-find-refactor

* Original sha: 1ee49af
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-14T15:43:04Z
* Committed by GitHub <noreply@github.com> on 2016-12-14T15:43:04Z

**Commit 6:**
fix linter

* Original sha: 6ce00ba
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-14T15:54:25Z

**Commit 7:**
lint fixes with { } spacing

* Original sha: 12ec497
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-15T16:43:07Z

**Commit 8:**
manual merge with master after fixing conflicts

* Original sha: c8f5701
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-15T16:55:01Z

**Commit 9:**
Fix linter

* Original sha: eafcf64
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-15T16:56:22Z

**Commit 10:**
fix forgotten merge conflict

* Original sha: 406de73
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-15T17:03:09Z

**Commit 11:**
and linter

* Original sha: e1b72cf
* Authored by Stacey Gammon <gammon@elastic.co> on 2016-12-15T17:03:20Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants