diff --git a/package.json b/package.json index c1d5a0e..f666ec7 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,6 @@ "npm-check-updates": "latest", "postcss": "latest", "postcss-focus": "latest", - "set-now": "latest", "simple-git-hooks": "latest", "standard": "latest", "standard-version": "latest" diff --git a/src/stats.js b/src/stats.js index 11b9402..dcb570d 100644 --- a/src/stats.js +++ b/src/stats.js @@ -31,8 +31,8 @@ return current module.exports = ({ redis, prefix }) => { const prefixKey = key => `${prefix}stats:${key}` - const increment = (keyValue, quantity = 1) => - redis.eval(LUA_INCREMENT_AND_EXPIRE, 1, `${prefixKey(keyValue)}:${formatYYYMMDDDate()}`, TTL, quantity) + const increment = (keyValue, quantity = 1, date = new Date()) => + redis.eval(LUA_INCREMENT_AND_EXPIRE, 1, `${prefixKey(keyValue)}:${formatYYYMMDDDate(date)}`, TTL, quantity) /** * Get stats for a given key. diff --git a/src/usage.js b/src/usage.js index b76c8bd..2c0823d 100644 --- a/src/usage.js +++ b/src/usage.js @@ -26,10 +26,11 @@ module.exports = ({ plans, keys, redis, stats, prefix, serialize, deserialize }) * // } * */ - const increment = async (keyValue, { timestamp = Date.now(), quantity = 1, throwError = true } = {}) => { + const increment = async (keyValue, { date = new Date(), quantity = 1, throwError = true } = {}) => { const key = await keys.retrieve(keyValue, { throwError }) const plan = await plans.retrieve(key.plan, { throwError }) let usage = await deserialize(await redis.get(prefixKey(keyValue))) + const timestamp = date.getTime() if (usage === null) { usage = { @@ -47,7 +48,10 @@ module.exports = ({ plans, keys, redis, stats, prefix, serialize, deserialize }) const pending = quantity > 0 - ? Promise.all([redis.set(prefixKey(keyValue), await serialize(usage)), stats.increment(keyValue, quantity)]) + ? Promise.all([ + redis.set(prefixKey(keyValue), await serialize(usage)), + stats.increment(keyValue, quantity, date) + ]) : Promise.resolve([]) return { @@ -59,7 +63,7 @@ module.exports = ({ plans, keys, redis, stats, prefix, serialize, deserialize }) } /** - * Get usage for a given key. + * Get the current usage for a given key. * * @param {string} keyValue * diff --git a/src/util.js b/src/util.js index ed22daa..b722e11 100644 --- a/src/util.js +++ b/src/util.js @@ -20,10 +20,10 @@ const pick = (obj, keys) => { return result } -const formatYYYMMDDDate = (now = new Date()) => { - const year = now.getFullYear() - const month = String(now.getMonth() + 1).padStart(2, '0') - const day = String(now.getDate()).padStart(2, '0') +const formatYYYMMDDDate = (date = new Date()) => { + const year = date.getFullYear() + const month = String(date.getMonth() + 1).padStart(2, '0') + const day = String(date.getDate()).padStart(2, '0') return `${year}-${month}-${day}` } diff --git a/test/stats.js b/test/stats.js index 0367a6a..7f3cecb 100644 --- a/test/stats.js +++ b/test/stats.js @@ -1,7 +1,5 @@ 'use strict' -require('set-now') - const { addDays } = require('date-fns') const { randomUUID } = require('crypto') const Redis = require('ioredis') @@ -35,24 +33,20 @@ test.serial('.increment # by one', async t => { let data = await openkey.usage.increment(key.value) await data.pending - Date.setNow(addDays(Date.now(), 1)) await Promise.all( [...Array(10).keys()].map(async () => { - data = await openkey.usage.increment(key.value) + data = await openkey.usage.increment(key.value, { date: addDays(Date.now(), 1) }) await data.pending }) ) - Date.setNow(addDays(Date.now(), 1)) await Promise.all( [...Array(5).keys()].map(async () => { - data = await openkey.usage.increment(key.value) + data = await openkey.usage.increment(key.value, { date: addDays(Date.now(), 2) }) await data.pending }) ) - Date.setNow() - t.deepEqual(await openkey.stats(key.value), [ { date: openkey.stats.formatYYYMMDDDate(), count: 1 }, { date: openkey.stats.formatYYYMMDDDate(addDays(Date.now(), 1)), count: 10 }, @@ -71,16 +65,12 @@ test.serial('.increment # by more than one', async t => { const data = await openkey.usage.increment(key.value) await data.pending - Date.setNow(addDays(Date.now(), 1)) - await openkey.usage.increment(key.value, { quantity: 10 }) + await openkey.usage.increment(key.value, { quantity: 10, date: addDays(Date.now(), 1) }) await data.pending - Date.setNow(addDays(Date.now(), 1)) - await openkey.usage.increment(key.value, { quantity: 5 }) + await openkey.usage.increment(key.value, { quantity: 5, date: addDays(Date.now(), 2) }) await data.pending - Date.setNow() - t.deepEqual(await openkey.stats(key.value), [ { date: openkey.stats.formatYYYMMDDDate(), count: 1 }, { date: openkey.stats.formatYYYMMDDDate(addDays(Date.now(), 1)), count: 10 },