Skip to content

Commit 5092396

Browse files
committed
Added query ?stats at private path GET. Also private one-to-one GET gives ttl as JSON now
1 parent 4f0eedc commit 5092396

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

api/helper.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ export async function privateRefresh(privateKey){
113113
return redisData.expire(dbKey, ttl);
114114
}
115115

116+
export async function privateStats(privateKey){
117+
const publicKey = genPublicKey(privateKey);
118+
const dbKeyConsume = dbKeyPrefix.manyToOne + publicKey;
119+
const dbKeyPublish = dbKeyPrefix.oneToMany + publicKey;
120+
const countConsume = await redisData.llen(dbKeyConsume);
121+
const ttlConsume = await redisData.ttl(dbKeyConsume);
122+
const ttlPublish = await redisData.ttl(dbKeyPublish);
123+
return {
124+
consume: {
125+
count: countConsume,
126+
ttl: ttlConsume < 0 ? 0 : ttlConsume
127+
},
128+
publish: {
129+
ttl: ttlPublish < 0 ? 0 : ttlPublish
130+
}
131+
};
132+
}
133+
116134
export async function publicConsume(publicKey){
117135
const dbKey = dbKeyPrefix.oneToMany + publicKey;
118136
return redisData.get(dbKey);
@@ -132,14 +150,14 @@ export async function oneToOneConsume(publicKey, key){
132150
return redisData.hget(dbKey, field).then(redisData.hdel(dbKey, field));
133151
}
134152

135-
export async function oneToOneIsConsumed(privateKey, key){
153+
export async function oneToOneTTL(privateKey, key){
136154
const publicKey = genPublicKey(privateKey);
137155
const dbKey = dbKeyPrefix.oneToOne + publicKey;
138156
const field = key;
139157
const bool = await redisData.hexists(dbKey, field);
140158
if (bool) {
141-
return "Not consumed yet.";
159+
return {ttl: await redisData.ttl(dbKey)};
142160
} else {
143-
return "Consumed.";
161+
return {ttl: 0};
144162
}
145163
}

api/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ fastify.post('/public/:publicKey', async (request, reply) => {
112112
fastify.get('/private/:privateKey', async (request, reply) => {
113113
const { privateKey } = request.params;
114114
const webhook = request.query.hook;
115+
const statsPresent = 'stats' in request.query;
115116
try {
116117
if (helper.validate(privateKey) !== 'private') throw 401;
117118
if (webhook == null) {
118119
await helper.cacheDel(privateKey, 'hook');
119120
} else {
120121
await helper.cacheSet(privateKey, {hook:webhook});
121122
}
123+
if (statsPresent) return helper.privateStats(privateKey);
122124
const dataArray = await helper.privateConsume(privateKey);
123125
if (!dataArray.length) throw 404;
124126
reply.send(dataArray);
@@ -260,7 +262,7 @@ fastify.get('/private/:privateKey/:key', async (request, reply) => {
260262
try {
261263
if (key.substr(0,fieldLimit) !== key) throw 400;
262264
if (helper.validate(privateKey) !== 'private') throw 401;
263-
reply.send(await helper.oneToOneIsConsumed(privateKey, key));
265+
return helper.oneToOneTTL(privateKey, key);
264266
} catch (err) {
265267
if (err == 400) {
266268
callBadRequest(reply, 'Provided field is too long');

0 commit comments

Comments
 (0)