cht-conf 3.9.1 → 3.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [3.9.2](https://github.com/medic/cht-conf/compare/v3.9.1...v3.9.2) (2021-12-08)
2
+
3
+
4
+ ### Performance Improvements
5
+
6
+ * **#432:** refactor the moving of reports to use a recursive function to improve performance ([c17b43b](https://github.com/medic/cht-conf/commit/c17b43b4d89b29b813db4be6e0f2d2c0a1e6c94d)), closes [#432](https://github.com/medic/cht-conf/issues/432)
7
+
1
8
  ## [3.9.1](https://github.com/medic/cht-conf/compare/v3.9.0...v3.9.1) (2021-11-16)
2
9
 
3
10
 
package/README.md CHANGED
@@ -282,7 +282,7 @@ To develop a new action or improve an existing one, check the ["Actions" doc](sr
282
282
 
283
283
  ## Testing
284
284
 
285
- Execute `npm test` to run static analysis checks and the test suite.
285
+ Execute `npm test` to run static analysis checks and the test suite. Requires Docker to run integration tests against a CouchDB instance.
286
286
 
287
287
  ## Executing your local branch
288
288
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cht-conf",
3
- "version": "3.9.1",
3
+ "version": "3.9.2",
4
4
  "description": "Configure CHT deployments",
5
5
  "main": "./src/lib/main.js",
6
6
  "engines": {
@@ -9,7 +9,9 @@
9
9
  "scripts": {
10
10
  "clean": "rm -rf ./build/",
11
11
  "eslint": "eslint src/**/*.js test/*.js test/**/*.js",
12
- "test": "npm run eslint && npm run clean && mkdir -p build/test && cp -r test/data build/test/data && cd build/test && nyc --reporter=html mocha --forbid-only ../../test/**/*.spec.js && cd ../..",
12
+ "docker-start-couchdb": "npm run docker-stop-couchdb && docker run -d -p 6984:5984 --rm --name cht-conf-couchdb couchdb:2.3.1 && sh test/scripts/wait_for_response_code.sh 6984 200 CouchDB",
13
+ "docker-stop-couchdb": "docker stop cht-conf-couchdb || true",
14
+ "test": "npm run eslint && npm run docker-start-couchdb && npm run clean && mkdir -p build/test && cp -r test/data build/test/data && cd build/test && nyc --reporter=html mocha --forbid-only ../../test/**/*.spec.js && cd ../.. && npm run docker-stop-couchdb",
13
15
  "semantic-release": "semantic-release"
14
16
  },
15
17
  "bin": {
@@ -10,6 +10,7 @@ const pouch = require('../lib/db');
10
10
  const { warn, trace, info, error } = require('../lib/log');
11
11
 
12
12
  const HIERARCHY_ROOT = 'root';
13
+ const BATCH_SIZE = 10000;
13
14
 
14
15
  module.exports = {
15
16
  requiresInstance: true,
@@ -49,19 +50,15 @@ const updateLineagesAndStage = async (options, db) => {
49
50
  trace(`Considering primary contact updates to ${ancestors.length} ancestor(s) of contact ${prettyPrintDocument(contactDoc)}.`);
50
51
  const updatedAncestors = replaceLineageInAncestors(descendantsAndSelf, ancestors);
51
52
 
52
- const reportsCreatedByDescendants = await fetch.reportsCreatedBy(db, descendantsAndSelf.map(descendant => descendant._id));
53
- trace(`${reportsCreatedByDescendants.length} report(s) created by these affected contact(s) will update`);
54
- const updatedReports = replaceLineageInReports(reportsCreatedByDescendants, replacementLineage, contactId);
53
+ minifyLineageAndWriteToDisk([...updatedDescendants, ...updatedAncestors], options);
55
54
 
56
- [...updatedDescendants, ...updatedReports, ...updatedAncestors].forEach(updatedDoc => {
57
- lineageManipulation.minifyLineagesInDoc(updatedDoc);
58
- writeDocumentToDisk(options, updatedDoc);
59
- });
55
+ const movedReportsCount = await moveReports(db, descendantsAndSelf, options, replacementLineage, contactId);
56
+ trace(`${movedReportsCount} report(s) created by these affected contact(s) will be updated`);
60
57
 
61
58
  affectedContactCount += updatedDescendants.length + updatedAncestors.length;
62
- affectedReportCount += updatedReports.length;
59
+ affectedReportCount += movedReportsCount;
63
60
 
64
- info(`Staged updates to ${prettyPrintDocument(contactDoc)}. ${updatedDescendants.length} contact(s) and ${updatedReports.length} report(s).`);
61
+ info(`Staged updates to ${prettyPrintDocument(contactDoc)}. ${updatedDescendants.length} contact(s) and ${movedReportsCount} report(s).`);
65
62
  }
66
63
 
67
64
  info(`Staged changes to lineage information for ${affectedContactCount} contact(s) and ${affectedReportCount} report(s).`);
@@ -155,6 +152,31 @@ ${bold('OPTIONS')}
155
152
  `);
156
153
  };
157
154
 
155
+ const moveReports = async (db, descendantsAndSelf, writeOptions, replacementLineage, contactId) => {
156
+ const contactIds = descendantsAndSelf.map(contact => contact._id);
157
+
158
+ let skip = 0;
159
+ let reportDocsBatch;
160
+ do {
161
+ info(`Processing ${skip} to ${skip + BATCH_SIZE} report docs`);
162
+ reportDocsBatch = await fetch.reportsCreatedBy(db, contactIds, skip);
163
+
164
+ const updatedReports = replaceLineageInReports(reportDocsBatch, replacementLineage, contactId);
165
+ minifyLineageAndWriteToDisk(updatedReports, writeOptions);
166
+
167
+ skip += reportDocsBatch.length;
168
+ } while (reportDocsBatch.length >= BATCH_SIZE);
169
+
170
+ return skip;
171
+ };
172
+
173
+ const minifyLineageAndWriteToDisk = (docs, parsedArgs) => {
174
+ docs.forEach(doc => {
175
+ lineageManipulation.minifyLineagesInDoc(doc);
176
+ writeDocumentToDisk(parsedArgs, doc);
177
+ });
178
+ };
179
+
158
180
  const writeDocumentToDisk = ({ docDirectoryPath }, doc) => {
159
181
  const destinationPath = path.join(docDirectoryPath, `${doc._id}.doc.json`);
160
182
  if (fs.exists(destinationPath)) {
@@ -214,10 +236,12 @@ const fetch = {
214
236
  .filter(doc => doc && doc.type !== 'tombstone');
215
237
  },
216
238
 
217
- reportsCreatedBy: async (db, contactIds) => {
239
+ reportsCreatedBy: async (db, contactIds, skip) => {
218
240
  const reports = await db.query('medic-client/reports_by_freetext', {
219
241
  keys: contactIds.map(id => [`contact:${id}`]),
220
242
  include_docs: true,
243
+ limit: BATCH_SIZE,
244
+ skip: skip,
221
245
  });
222
246
 
223
247
  return reports.rows.map(row => row.doc);