-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Files] Adds bulk get method #155636
[Files] Adds bulk get method #155636
Conversation
Pinging @elastic/appex-sharedux (Team:SharedUX) |
@elasticmachine merge upstream |
@@ -120,6 +121,10 @@ export class EsIndexFilesMetadataClient<M = unknown> implements FileMetadataClie | |||
}; | |||
} | |||
|
|||
async bulkGet({ ids }: BulkGetArg): Promise<FileDescriptor[]> { | |||
return await Promise.all(ids.map((id) => this.get({ id }))); |
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 don't believe cases will be using this adapter but could this use elasticsearch's mget API?
If that's not an option, we've used p-map in cases to limit the number of concurrent requests: https://github.com/sindresorhus/p-map
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.
Good point. I've added p-limit
in 0a65355
const metadatas = await this.metadataClient.bulkGet({ ids }); | ||
const result = metadatas.map(({ id, metadata }) => { | ||
if (metadata.Status === 'DELETED') { | ||
throw new FileNotFoundError('File has been deleted'); |
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 don't think this will provide the functionality we need in cases. Cases is intentionally doing a promise settled to allow for some files to not exist but still not have the entire api call throw an error.
This way we can continue with our deletion logic even when some files don't exist. Could we still return normally here but provide a way to see which ids failed and which succeeded?
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.
Good points 👍
In c1030a0 I've added 2 options:
throwIfNotFound
- If set tofalse
thennull
is returnedformat
("array" | "map")
- If set tomap
then a map of id/File (ornull
if throwIfNotFound isfalse
) is returned
it('returns the files under a map of id/File', async () => {
const file1 = await createDisposableFile({ fileKind, name: 'test' });
const unknownID = 'foo';
const myFiles = await fileService.bulkGetById({
ids: [file1.id, unknownID],
throwIfNotFound: false, // do not throw
format: 'map', // returns a map
});
expect(myFiles[file1?.id]?.id).toBe(file1?.id); // true
expect(myFiles[unknownID]).toBe(null); // true
});
would that work?
}); | ||
return result; | ||
} catch (e) { | ||
if (SavedObjectsErrorHelpers.isNotFoundError(e)) { |
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.
Will the saved object client throw an error if one of the files does not exist? I think it will just mark that item in the array with the error
field.
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.
Yes you are right, that's how it is returned from the saved object bulkGet
if (docNotFound) {
return {
id,
type,
error: errorContent(SavedObjectsErrorHelpers.createGenericNotFoundError(type, id)),
} as any as SavedObject<T>;
}
In b97a0f1 I removed this condition and made sure we return null
in case of an error when fetching one of the saved object.
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.
Great job @vadimkibana ! I made some changes based on @jonathan-buttner feedback. Jonathan can you have another look and let me know if the new options would help for cases? Cheers 👍
@@ -120,6 +121,10 @@ export class EsIndexFilesMetadataClient<M = unknown> implements FileMetadataClie | |||
}; | |||
} | |||
|
|||
async bulkGet({ ids }: BulkGetArg): Promise<FileDescriptor[]> { | |||
return await Promise.all(ids.map((id) => this.get({ id }))); |
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.
Good point. I've added p-limit
in 0a65355
const metadatas = await this.metadataClient.bulkGet({ ids }); | ||
const result = metadatas.map(({ id, metadata }) => { | ||
if (metadata.Status === 'DELETED') { | ||
throw new FileNotFoundError('File has been deleted'); |
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.
Good points 👍
In c1030a0 I've added 2 options:
throwIfNotFound
- If set tofalse
thennull
is returnedformat
("array" | "map")
- If set tomap
then a map of id/File (ornull
if throwIfNotFound isfalse
) is returned
it('returns the files under a map of id/File', async () => {
const file1 = await createDisposableFile({ fileKind, name: 'test' });
const unknownID = 'foo';
const myFiles = await fileService.bulkGetById({
ids: [file1.id, unknownID],
throwIfNotFound: false, // do not throw
format: 'map', // returns a map
});
expect(myFiles[file1?.id]?.id).toBe(file1?.id); // true
expect(myFiles[unknownID]).toBe(null); // true
});
would that work?
}); | ||
return result; | ||
} catch (e) { | ||
if (SavedObjectsErrorHelpers.isNotFoundError(e)) { |
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.
Yes you are right, that's how it is returned from the saved object bulkGet
if (docNotFound) {
return {
id,
type,
error: errorContent(SavedObjectsErrorHelpers.createGenericNotFoundError(type, id)),
} as any as SavedObject<T>;
}
In b97a0f1 I removed this condition and made sure we return null
in case of an error when fetching one of the saved object.
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.
Thanks for the changes!
💚 Build Succeeded
Metrics [docs]Public APIs missing comments
Public APIs missing exports
Unknown metric groupsAPI count
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
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.
Approving!
## Summary Closes elastic#155369 ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### For maintainers - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit a0cd724)
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
4 similar comments
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
Summary
Closes #155369
Checklist
Delete any items that are not applicable to this PR.
For maintainers