Skip to content

Commit

Permalink
Avoid primary contact lookup when redundant
Browse files Browse the repository at this point in the history
This is a very minor performance tweak to avoid an unnecessary PouchDB.get() for a place's primary contact when the document is already in the place's children. In my limited exposure to project data, the primary contact is often a child of the place.

Looks like the in-memory lookup is ~0-1ms and PouchDB lookup ranges from 10-45ms. Not impressive... but I'm here cause it's on my list, and it doesn't add much compexity. Very open to just closing this as won't fix due to insufficient gains.

"The data fetched in right-panel step R6 was already fetched in step R4. Can we consolidate?" from #4445
  • Loading branch information
kennsippell authored and garethbowen committed Dec 16, 2018
1 parent 919a270 commit 72ccf5a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
48 changes: 29 additions & 19 deletions webapp/src/js/services/contact-view-model-generator.js
Expand Up @@ -96,41 +96,51 @@ angular.module('inboxServices').factory('ContactViewModelGenerator',
});
};

var getPrimaryContact = function(doc) {
const getPrimaryContact = function(doc, children) {
var contactId = doc && doc.contact && doc.contact._id;
if (!contactId) {
return $q.resolve();
}
return DB().get(contactId).catch(function(err) {
if (err.status === 404 || err.error === 'not_found') {
return;
}
throw err;
});

const persons = children.persons || [];
const idx = _.findIndex(persons, person => person.doc._id === contactId);
if (idx !== -1) {
return $q.resolve({
idx,
doc: persons[idx].doc
});
}

// If the primary contact is not a child, fetch the document
return DB().get(contactId)
.then(doc => ({ idx, doc }))
.catch(function(err) {
if (err.status === 404 || err.error === 'not_found') {
return;
}
throw err;
});
};

var sortPrimaryContactToTop = function(model, children) {
return getPrimaryContact(model.doc)
.then(function(primaryContact) {
return getPrimaryContact(model.doc, children)
.then(function (primaryContact) {
if (!primaryContact) {
return;
}
var newChild = {
id: primaryContact._id,
doc: primaryContact,
const newChild = {
id: primaryContact.doc._id,
doc: primaryContact.doc,
isPrimaryContact: true
};
if (!children.persons) {
children.persons = [ newChild ];
return;
}
var persons = children.persons;
const persons = children.persons;
// remove existing child
var primaryContactIdx = _.findIndex(persons, function(child) {
return child.doc._id === primaryContact._id;
});
if (primaryContactIdx !== -1) {
persons.splice(primaryContactIdx, 1);
if (primaryContact.idx !== -1) {
persons.splice(primaryContact.idx, 1);
}
// push the primary contact on to the start of the array
persons.unshift(newChild);
Expand Down Expand Up @@ -205,7 +215,7 @@ angular.module('inboxServices').factory('ContactViewModelGenerator',
report.heading = getHeading(dataRecord);
}
});

return reports;
});
};
Expand Down
10 changes: 10 additions & 0 deletions webapp/tests/karma/unit/services/contact-view-model-generator.js
Expand Up @@ -157,6 +157,16 @@ describe('ContactViewModelGenerator service', () => {
});
});

it('contact person loaded from children', () => {
return runPlaceTest([childContactPerson], []).then(model => {
assert.equal(dbGet.callCount, 0);
assert.equal(model.children.persons.length, 1);
const firstPerson = model.children.persons[0];
assert.deepEqual(firstPerson.doc, childContactPerson);
assert(firstPerson.isPrimaryContact, 'has isPrimaryContact flag');
});
});

it('if no contact in parent, persons still get displayed', () => {
delete doc.contact;
return runPlaceTest([childPerson, childContactPerson]).then(model => {
Expand Down

0 comments on commit 72ccf5a

Please sign in to comment.