make rewards query less aggressive
This commit is contained in:
parent
d2697477a0
commit
a85af82460
@ -4,9 +4,26 @@ import { serializeInvoicable } from './serial'
|
||||
import { ANON_USER_ID } from '../../lib/constants'
|
||||
import { getItem } from './item'
|
||||
|
||||
export default {
|
||||
Query: {
|
||||
rewards: async (parent, { when }, { models }) => {
|
||||
const rewardCache = new Map()
|
||||
|
||||
async function updateCachedRewards (when, models) {
|
||||
const rewards = await getRewards(when, models)
|
||||
rewardCache.set(when, { rewards, createdAt: Date.now() })
|
||||
return rewards
|
||||
}
|
||||
|
||||
async function getCachedRewards (staleIn, when, models) {
|
||||
if (rewardCache.has(when)) {
|
||||
const { rewards, createdAt } = rewardCache.get(when)
|
||||
const expired = createdAt + staleIn < Date.now()
|
||||
if (expired) updateCachedRewards(when, models).catch(console.error)
|
||||
return rewards // serve stale rewards
|
||||
}
|
||||
return await updateCachedRewards(when, models)
|
||||
}
|
||||
|
||||
async function getRewards (when, models) {
|
||||
console.log('getRewards', when)
|
||||
if (when) {
|
||||
if (when.length > 2) {
|
||||
throw new GraphQLError('too many dates', { extensions: { code: 'BAD_USER_INPUT' } })
|
||||
@ -63,7 +80,12 @@ export default {
|
||||
ORDER BY days_cte.day ASC`
|
||||
|
||||
return results.length ? results : [{ total: 0, time: '0', sources: [] }]
|
||||
},
|
||||
}
|
||||
|
||||
export default {
|
||||
Query: {
|
||||
rewards: async (parent, { when }, { models }) =>
|
||||
when ? await getRewards(when, models) : await getCachedRewards(5000, when, models),
|
||||
meRewards: async (parent, { when }, { me, models }) => {
|
||||
if (!me) {
|
||||
return null
|
||||
|
Loading…
x
Reference in New Issue
Block a user