Skip to content

Commit

Permalink
fix: ensure to treat timestamp properly (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored May 29, 2024
1 parent e8657af commit 6e61720
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions src/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand All @@ -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
*
Expand Down
8 changes: 4 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}

Expand Down
18 changes: 4 additions & 14 deletions test/stats.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

require('set-now')

const { addDays } = require('date-fns')
const { randomUUID } = require('crypto')
const Redis = require('ioredis')
Expand Down Expand Up @@ -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 },
Expand All @@ -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 },
Expand Down

0 comments on commit 6e61720

Please sign in to comment.