Skip to content
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

GROW-118 Dynamic loan urls and images from Ml recommender #1881

Merged
merged 14 commits into from
Jul 4, 2020

Conversation

ryan-ludwig
Copy link
Contributor

Couple of things to iron out still, but ready for some review.

To demo:

A few comments on the files below

@ryan-ludwig ryan-ludwig requested review from mcstover and emuvente July 1, 2020 19:25
try {
const loanData = await getRecommendedLoans('img', userId/* , offset */);
const loanCardImgBuffer = await drawLoanCard(loanData[0]); // TODO: offset?
// TODO: Add loanCard img to memcache?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add loanCard img to memcache here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to come up with a system to reduce the number of images that might be cached. As I mentioned before, rounding the currently funded about to the nearest 20th percent would be a good start. Then you could have cache keys for images like so... live-loan-##loanid##-0, live-loan-##loanid##-20, live-loan-##loanid##-40 etc...

This would at least give us a baseline of measurement to ensure we have caching instances that can hold enough entries.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then your process will be more like...
const loanCardCacheKey = live-loan-##loanid##-0 let cachedLoanCardImage = retrieveFromCache(live-loan-##loanid##-0) if (!cachedLoanCardImage) { cachedLoanCardImage = await drawLoanCard() } ... res.send(cachedLoanCardImage);

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could also just cache one image for a loan, and use a ttl rather than a cache key based on the loan progress. (The cached value could be json, allowing you to store and check the time it was cached)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@emuvente I'd be fine with that if we can build in a "delay" of sorts so that we're not re-generating the image for small changes in the funded amount. my interest is reduce re-generating the image repeatedly (aka load on the express object) and to prevent excessive cache entries for each loan...

Copy link
Collaborator

Choose a reason for hiding this comment

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

The ttl would handle that delay, as the image would not be regenerated while the cached image is young enough. It would also handle the excessive entries for each loan as only 1 image would ever be cached for a loan.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emuvente @mcstover To reiterate, we'll cache the generated image buffer to memcache for say 10(?) minutes, and serve that to anyone else who would see the same loan. We'll also add a max-age header for also 10(?) minutes to the image response.

return path.join(__dirname, '../../../src/assets/fonts', name);
}
registerFont(fontFile('PostGrotesk-Medium.ttf'), { family: 'Kiva Post Grot', weight: '400' });
registerFont(fontFile('PostGrotesk-MediumItalic.ttf'), { family: 'Kiva Post Grot', weight: '400', style: 'italic' });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: Having issues with node-canvas and registering fonts. So far only the first font is able to be used. Still working on this but I don't think it'll be a dealbreaker.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What if you used a different font family name for each one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No luck there, I think I may need to patch the fonts or wait for a new release of node-canvas - Automattic/node-canvas#1572

@ryan-ludwig ryan-ludwig marked this pull request as ready for review July 1, 2020 19:30
server/live-loan-router.js Outdated Show resolved Hide resolved
@emuvente
Copy link
Collaborator

emuvente commented Jul 2, 2020

To ensure that the data is accurate, I would recommend against using different graphql queries for the url and img handling. There should be a single get recommended loans call that is cached (old dynamic loan code would cache this data for 30 minutes, but I could see 10 minutes or less being ok too) and used by both the url and img handlers, as that prevents the situation of the url query getting a different loan than the image query.

@mcstover
Copy link
Collaborator

mcstover commented Jul 2, 2020

To ensure that the data is accurate, I would recommend against using different graphql queries for the url and img handling. There should be a single get recommended loans call that is cached (old dynamic loan code would cache this data for 30 minutes, but I could see 10 minutes or less being ok too) and used by both the url and img handlers, as that prevents the situation of the url query getting a different loan than the image query.

With regard to caching the loan query...at first I was thinking we wouldn't want to...and that may still be the case... This is a user level query so what's the likely hood of the cache result ever getting used?

@emuvente
Copy link
Collaborator

emuvente commented Jul 2, 2020

This is a user level query so what's the likely hood of the cache result ever getting used?

It's going to be used at least once, because the query is called once for the image, and then a second time for the url when the user clicks on the image and needs to be redirected.

Copy link
Collaborator

@mcstover mcstover left a comment

Choose a reason for hiding this comment

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

I say roll with it if things are close. We run some internal tests and tweak as needed in a hotfix

@ryan-ludwig
Copy link
Contributor Author

Going to merge this and work on cache improvements next week as hotfixes.

@ryan-ludwig ryan-ludwig merged commit 74de75b into master Jul 4, 2020
@ryan-ludwig ryan-ludwig deleted the GROW-118_dynamic-ml-image branch August 18, 2020 18:02
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.

3 participants