-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
feat(#9238): add functionality of getting people as an AsyncGenerator in cht-datasource #9281
Merged
sugat009
merged 57 commits into
9193-api-endpoints-for-getting-contacts-by-type
from
9238-add-functionality-of-getting-people-as-an-iterator-in-cht-datasource
Aug 12, 2024
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
08e2b27
feat(#9237): Add functionality of getting people with pagination in c…
sugat009 ab92f0b
Update shared-libs/cht-datasource/src/local/libs/doc.ts
sugat009 de63197
feat(#9237): Address PR comments
sugat009 105d5db
Update shared-libs/cht-datasource/src/qualifier.ts
sugat009 2b92b4c
Update shared-libs/cht-datasource/test/local/person.spec.ts
sugat009 ba2e27a
Update shared-libs/cht-datasource/src/person.ts
sugat009 15068fa
Update shared-libs/cht-datasource/src/remote/person.ts
sugat009 a83a259
Update shared-libs/cht-datasource/src/person.ts
sugat009 29a3ad5
Update shared-libs/cht-datasource/src/index.ts
sugat009 8fbc245
feat(#9237): Address PR comments
sugat009 a99de72
feat(#9237): Add unit tests for getResources in remote mode
sugat009 d87c258
feat(#9237): Address PR comments
sugat009 c34716a
Write getAll generator in src/person.ts
sugat009 a9fb5eb
(#feat): Minor fix
sugat009 7b94447
Refactor the AsyncIterator
sugat009 4b534f3
Add documentation for new functions
sugat009 eff3972
Add unit tests
sugat009 9a94d36
Add comment on unit tests for clarity
sugat009 141cfab
Update shared-libs/cht-datasource/src/person.ts
sugat009 5daa8cf
Update shared-libs/cht-datasource/src/person.ts
sugat009 9015353
Address PR comments
sugat009 8be7b70
Avoid using spread operator in getDocumentStream function
sugat009 54da9fb
Fix documentation of previously changed functions
sugat009 6064c99
Update shared-libs/cht-datasource/src/person.ts
sugat009 9fb6c7b
Update shared-libs/cht-datasource/test/local/libs/lineage.spec.ts
sugat009 2364d82
Update shared-libs/cht-datasource/test/local/libs/lineage.spec.ts
sugat009 1b87d4a
Update shared-libs/cht-datasource/test/local/person.spec.ts
sugat009 9a828a6
feat(#9237): Address PR comments
sugat009 cd81f2d
Write getAll generator in src/person.ts
sugat009 cd344d5
Refactor the AsyncIterator
sugat009 5d7fc98
Add documentation for new functions
sugat009 4279c60
Add unit tests
sugat009 190f69e
Address PR comments
sugat009 cb34075
Fix conflict with target branch
sugat009 154b295
Fix conflict with target branch
sugat009 c233d0b
Update shared-libs/cht-datasource/src/person.ts
sugat009 a9cf536
Address PR comments
sugat009 318c1a4
Update shared-libs/cht-datasource/src/index.ts
sugat009 bc92ff3
Update shared-libs/cht-datasource/src/index.ts
sugat009 0e6e9ea
Update shared-libs/cht-datasource/src/person.ts
sugat009 d14ce9e
Remove limit and skip as arguments for getDocumentStream function
sugat009 36a5a87
Remove duplicate test case added during conflict resolution
sugat009 d87d293
Cleanup after refactoring
sugat009 2867a80
Implement cursor based pagination
sugat009 84c30c9
Fix unit tests according to implementation of cursor pagination
sugat009 d45f0d9
Add unit tests
sugat009 563ac81
Address PR comments
sugat009 fd72695
Avoid using spread operator in getDocumentStream function
sugat009 bab3c1e
Fix conflict with target branch
sugat009 0d305ff
Remove limit and skip as arguments for getDocumentStream function
sugat009 15b6e39
Cleanup after refactoring
sugat009 1d62657
Refactor getDocumentStream w.r.t. the cursor based pagination in getP…
sugat009 1057fe4
Fix merge conflicts with target branch
sugat009 88f39d7
Update shared-libs/cht-datasource/src/libs/core.ts
sugat009 60604a5
Change skip to cursor in all places and refactor fetchAndFilter funct…
sugat009 3eeb049
Fix eslint issues
sugat009 7e0355f
Merge branch '9193-api-endpoints-for-getting-contacts-by-type' into 9…
sugat009 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,8 +63,8 @@ export namespace v1 { | |
export const getPage = ({ medicDb, settings }: LocalDataContext) => { | ||
return async ( | ||
personType: ContactTypeQualifier, | ||
cursor: string, | ||
limit: number, | ||
skip: number | ||
): Promise<Page<Person.v1.Person>> => { | ||
const personTypes = contactTypeUtils.getPersonTypes(settings.getAll()); | ||
const personTypesIds = personTypes.map((item) => item.id); | ||
|
@@ -73,43 +73,45 @@ export namespace v1 { | |
throw new Error(`Invalid person type: ${personType.contactType}`); | ||
} | ||
|
||
// Adding a number skip variable here so as not to confuse ourselves | ||
const skip = Number(cursor) || 0; | ||
const getDocsByPage = queryDocsByKey(medicDb, 'medic-client/contacts_by_type'); | ||
|
||
const fetchAndFilter = async ( | ||
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. Solid logic here! 👍 I think we can tighten up the cursor calculation a bit (at least reduce the nesting if-statements): const fetchAndFilter = async (
currentLimit: number,
currentSkip: number,
currentPersons: Person.v1.Person[] = []
): Promise<Page<Person.v1.Person>> => {
const docs = await getDocsByPage([personType.contactType], currentLimit, currentSkip);
const noMoreResults = docs.length < currentLimit;
const newPersons = docs.filter((doc): doc is Person.v1.Person => isPerson(settings, doc, doc?._id));
// Need to slice here because we retrieve extra docs on re-fetches and may end up with too many.
const overFetchCount = currentPersons.length + newPersons.length - limit || 0;
const totalPersons = [...currentPersons, ...newPersons].slice(0, limit);
if (noMoreResults) {
return { data: totalPersons };
}
if (totalPersons.length === limit) {
const nextSkip = currentSkip + currentLimit - overFetchCount;
return { data: totalPersons, cursor: (nextSkip).toString() };
}
// Re-fetch twice as many docs as we need to limit number of recursions
const missingCount = currentLimit - newPersons.length;
logger.debug(`Found [${missingCount}] invalid persons. Re-fetching additional records.`);
const nextLimit = missingCount * 2;
const nextSkip = currentSkip + currentLimit;
return fetchAndFilter(nextLimit, nextSkip, totalPersons);
};
return fetchAndFilter(limit, skip); |
||
currentLimit: number, | ||
currentSkip: number, | ||
personDocs: Person.v1.Person[], | ||
totalDocsFetched = 0, | ||
currentPersonDocs: Person.v1.Person[] = [], | ||
): Promise<Page<Person.v1.Person>> => { | ||
const docs = await getDocsByPage([personType.contactType], currentLimit, currentSkip); | ||
if (docs.length === 0) { | ||
return { data: personDocs, cursor: '-1' }; | ||
} | ||
const noMoreResults = docs.length < currentLimit; | ||
const newPersonDocs = docs.filter((doc): doc is Person.v1.Person => isPerson(settings, doc, doc?._id)); | ||
const overFetchCount = currentPersonDocs.length + newPersonDocs.length - limit || 0; | ||
const totalPeople = [...currentPersonDocs, ...newPersonDocs].slice(0, limit); | ||
|
||
const tempFilteredDocs = docs.filter((doc): doc is Person.v1.Person => isPerson(settings, doc, doc?._id)); | ||
if (noMoreResults) { | ||
return { data: totalPeople, cursor: '-1' }; | ||
} | ||
|
||
personDocs.push(...tempFilteredDocs); | ||
totalDocsFetched += docs.length; | ||
if (totalPeople.length === limit) { | ||
const nextSkip = currentSkip + currentLimit - overFetchCount; | ||
|
||
if (personDocs.length >= limit) { | ||
let cursor: number; | ||
if (docs.length < currentLimit) { | ||
cursor = -1; | ||
} else { | ||
cursor = skip + totalDocsFetched - (personDocs.length - limit); | ||
} | ||
return { data: personDocs.slice(0, limit), cursor: cursor.toString() }; | ||
return { data: totalPeople, cursor: nextSkip.toString() }; | ||
} | ||
|
||
// Re-fetch twice as many docs as we need to limit number of recursions | ||
const missingCount = currentLimit - newPersonDocs.length; | ||
logger.debug(`Found [${missingCount.toString()}] invalid persons. Re-fetching additional records.`); | ||
const nextLimit = missingCount * 2; | ||
const nextSkip = currentSkip + currentLimit; | ||
|
||
return fetchAndFilter( | ||
(currentLimit - tempFilteredDocs.length) * 2, | ||
currentSkip + currentLimit, | ||
personDocs, | ||
totalDocsFetched | ||
nextLimit, | ||
nextSkip, | ||
totalPeople, | ||
); | ||
}; | ||
|
||
return fetchAndFilter(limit, skip, []); | ||
return fetchAndFilter(limit, skip); | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we know that we have reached the end of the results, we should not return a cursor value.
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 catch! I believe
cursor
's type should beThat's more explicit to express "you've reached the end"
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 think we should be more explicit and return
"-1"
and not nothing to indicate the end of iteration.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.
@m5r and I commented simultaneously,
null
should do as well.