Skip to content

perf(10749): move tasks_by_contact view to offline-only design document#10776

Open
sh1vam31 wants to merge 18 commits intomedic:masterfrom
sh1vam31:10749-move-tasks-by-contact-to-offline-clients
Open

perf(10749): move tasks_by_contact view to offline-only design document#10776
sh1vam31 wants to merge 18 commits intomedic:masterfrom
sh1vam31:10749-move-tasks-by-contact-to-offline-clients

Conversation

@sh1vam31
Copy link
Copy Markdown
Contributor

Description

fixes: #10749

moving the tasks_by_contact view from the server-managed design documents to a strictly offline-only design document.

Problem:

The tasks_by_contact view is a high-volume index that is only used by the rules engine on offline clients (mobile/web). When defined in the medic-client design document, the CouchDB server builds and stores this index, consuming significant disk space and processing power for zero benefit on the server.

Solution:

We moved the view definition into the client-side bootstrapper (webapp/src/js/bootstrapper/offline-ddocs). This ensures the view is only created on the client's PouchDB.

Changes:

Moved tasks_by_contact view from server ddocs to webapp/src/js/bootstrapper/offline-ddocs/medic-offline-tasks.
Updated @medic/rules-engine to query the new _design/medic-offline-tasks location.
Cleaned up the medic-client server-side design document by removing the redundant view.
Updated @medic/memdown test harness to correctly load the new offline-only views for integration testing.

Testing:

1. Rules Engine Integration Tests (220 Passing)

image

2. Manual Bootstrapper Verification

image

Code review checklist

  • UI/UX backwards compatible: Test it works for the new design (enabled by default). And test it works in the old design, enable can_view_old_navigation permission to see the old design. Test it has appropriate design for RTL languages.
  • Readable: Concise, well named, follows the style guide
  • Documented: Configuration and user documentation on cht-docs
  • Tested: Unit and/or e2e where appropriate
  • Internationalised: All user facing text
  • Backwards compatible: Works with existing data and configuration or includes a migration. Any breaking changes documented in the release notes.
  • AI disclosure: Please disclose use of AI per the guidelines.

License

The software is provided under AGPL-3.0. Contributions to this project are accepted under the same license.

@sh1vam31 sh1vam31 force-pushed the 10749-move-tasks-by-contact-to-offline-clients branch 2 times, most recently from 4908220 to 5b87f24 Compare March 28, 2026 23:07
@sh1vam31 sh1vam31 force-pushed the 10749-move-tasks-by-contact-to-offline-clients branch from 5b87f24 to 67e737e Compare March 28, 2026 23:10
@sh1vam31
Copy link
Copy Markdown
Contributor Author

Hi @witash,

I have updated the PR to move the tasks_by_contact view from the server-managed design document to an offline-only location (_design/medic-offline-tasks).

This change ensures that the high-volume index is only built on the client devices, reducing unnecessary server-side disk and processing overhead as requested in #10749. I have also updated the Rules Engine to correctly query this new location.

Looking forward to your feedback

@sh1vam31 sh1vam31 changed the title 10749 move tasks by contact to offline clients perf(10749): move tasks_by_contact view to offline-only design document Mar 28, 2026
@witash witash self-requested a review March 30, 2026 11:30
@witash witash self-assigned this Mar 30, 2026
Copy link
Copy Markdown
Contributor

@witash witash left a comment

Choose a reason for hiding this comment

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

Looks like there's some lint failures, you can run npm run lint locally to check (it will be fast).
They are in api/src/services/authorization.js, which separately I don't think needs to be changed.

Other than that looks great.
this will be a huge improvement on larger deployments

Comment thread api/src/services/authorization.js Outdated
const validatedIds = [MEDIC_CLIENT_DDOC, getUserSettingsId(authCtx.userCtx.name)];
const validatedIds = [
MEDIC_CLIENT_DDOC,
MEDIC_OFFLINE_TASKS_DDOC,
Copy link
Copy Markdown
Contributor

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 change is necessarry? This list is of documents on the server side that will be replicated to offline clients...but MEDIC_OFFLINE_TASKS_DDOC doesn't exist on the server at all; we don't need to add medic-offline-freetext here so also shouldn't for medic-offline-tasks

@sh1vam31
Copy link
Copy Markdown
Contributor Author

sh1vam31 commented Apr 7, 2026

Hi @witash,
Thanks for catching that! You're completely right — since the medic-offline-tasks design document is built locally on the client and doesn't exist on the server, we don't need to authorise it to be replicated. I've reverted the changes to api/src/services/authorization.js, which also resolved the lint errors.

Let me know if everything looks good to go.

@sh1vam31 sh1vam31 requested a review from witash April 7, 2026 19:27
@witash
Copy link
Copy Markdown
Contributor

witash commented Apr 8, 2026

thanks @sh1vam31 this is almost ready...hopefully one last thing this failure appears to be a real test failure, not flakiness (these tests historically have been very flaky, but it didn't pass after multiple retries, and I was able to reproduce the failure locally).
The issue is at https://github.com/sh1vam31/cht-core/blob/5d3ed0fa9da95d224520acbb6160be259305fa59/tests/e2e/default/db/initial-replication.wdio-spec.js#L10 where its listing the local only docs for the assertions, but only medic-offline-freetext.
I was able to get it to pass locally after adding medic-offline-tasks also.
const LOCAL_ONLY_DOC_IDS = ['_design/medic-offline-freetext', '_design/medic-offline-tasks'];
Once that's fixed, this should be ready to merge

@sh1vam31
Copy link
Copy Markdown
Contributor Author

sh1vam31 commented Apr 9, 2026

Hi @witash,
Thanks for reproducing that centrally. You were totally right: dropping _design/medic-offline-tasks into the LOCAL_ONLY_DOC_IDS assertion was all it needed.

I've pushed the fix to the test file.

Copy link
Copy Markdown
Contributor

@witash witash left a comment

Choose a reason for hiding this comment

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

HI @sh1vam31 just one more change I think;
there's two unrelated changes still in this branch; we really want to keep only changes related to the issue in this branch; test fixes for changes in the branch are ok, but test fixes for other flaky tests need to be in their own branch.

}
}
}
}, { ignoreReload: true }));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks unrelated, can you please remove it from this branch if so

};

const INDETERMINATE_FIELDS = ['current', 'uptime', 'date', 'fragmentation', 'node', 'sizes', 'file_size'];
const INDETERMINATE_FIELDS = ['current', 'uptime', 'date', 'fragmentation', 'node', 'sizes', 'file_size', 'doc_count'];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks unrelated, can you please remove it from this branch if so

@sh1vam31
Copy link
Copy Markdown
Contributor Author

Hi @witash,

Thank you for the feedback.

I have removed the two unrelated changes you pointed out:

Restored the { ignoreReload: true } setting in records.spec.js.
Removed 'doc_count' from INDETERMINATE_FIELDS in monitoring.spec.js.
I've also double-checked the branch to ensure no other unrelated flaky test fixes or chores remain. This PR should now be focused strictly on moving the tasks_by_contact view to the offline design document.

@sh1vam31 sh1vam31 requested a review from witash April 10, 2026 19:30
Comment thread tests/utils/index.js Outdated
ok: response.ok,
headers: response.headers
};
const request = async (options, { debug, maxRetries = 3, initialDelay = 100 } = {}) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@sh1vam31 these look like unrelated changes. The tests that were specifically for this issue are passing, so lets not add test fixes for other things; and in any case, I wouldn't automatically retry failed requests in this very heavily used file

Copy link
Copy Markdown
Contributor Author

@sh1vam31 sh1vam31 Apr 15, 2026

Choose a reason for hiding this comment

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

@sh1vam31 these look like unrelated changes. The tests that were specifically for this issue are passing, so lets not add test fixes for other things; and in any case, I wouldn't automatically retry failed requests in this very heavily used file

Hi @witash,
Thanks for the feedback! You're absolutely right — the retry logic in request() was leftover from debugging CI failures during development and isn't related to this issue. I've reverted tests/utils/index.js back to its original form. Sorry for the noise!

@sh1vam31
Copy link
Copy Markdown
Contributor Author

Hi @witash
I've addressed your feedback and reverted the unrelated retry logic from tests/utils/index.js. All 25 CI checks are now passing.

Could you please take another look when you get a chance?

Thanks so much for your time and guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move tasks_by_contact to offline clients only.

2 participants