From e94934738dd31af66c226df79057c7715ee67502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjarne=20=C3=98verli?= Date: Fri, 1 Oct 2021 14:21:11 +0200 Subject: [PATCH] Add secrets created statistics (#32) --- package-lock.json | 11 +++++++++++ package.json | 1 + src/server/services/redis.js | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/package-lock.json b/package-lock.json index 97c203fd..33a9f918 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "aws-sdk": "^2.935.0", "bcrypt": "^5.0.1", "config": "^3.3.6", + "dayjs": "^1.10.7", "email-validator": "^2.0.4", "fastify": "^3.17.0", "fastify-cors": "^6.0.1", @@ -7200,6 +7201,11 @@ "node": ">=10" } }, + "node_modules/dayjs": { + "version": "1.10.7", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.7.tgz", + "integrity": "sha512-P6twpd70BcPK34K26uJ1KT3wlhpuOAPoMwJzpsIWUxHZ7wpmbdZL/hQqBDfz7hGurYSa5PhzdhDHtt319hL3ig==" + }, "node_modules/debug": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", @@ -31088,6 +31094,11 @@ "whatwg-url": "^8.0.0" } }, + "dayjs": { + "version": "1.10.7", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.7.tgz", + "integrity": "sha512-P6twpd70BcPK34K26uJ1KT3wlhpuOAPoMwJzpsIWUxHZ7wpmbdZL/hQqBDfz7hGurYSa5PhzdhDHtt319hL3ig==" + }, "debug": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", diff --git a/package.json b/package.json index eb6b08be..26cb94c2 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "aws-sdk": "^2.935.0", "bcrypt": "^5.0.1", "config": "^3.3.6", + "dayjs": "^1.10.7", "email-validator": "^2.0.4", "fastify": "^3.17.0", "fastify-cors": "^6.0.1", diff --git a/src/server/services/redis.js b/src/server/services/redis.js index 99f9d9b8..f6fb71a3 100644 --- a/src/server/services/redis.js +++ b/src/server/services/redis.js @@ -1,5 +1,6 @@ const config = require('config'); const asyncRedis = require('async-redis'); +const dayjs = require('dayjs'); const { nanoid } = require('nanoid'); const isValidTTL = require('../helpers/validate-ttl'); @@ -41,6 +42,8 @@ function createSecret(data, ttl) { prepare.push(...['file', JSON.stringify(data.file)]); } + createStatistics('secrets_created'); + client .multi() .hmset(prepare) @@ -124,6 +127,16 @@ async function createRateLimit(ip) { return false; } +async function createStatistics(type = '') { + const types = ['secrets_created']; + + if (types.indexOf(type) === -1) { + console.log(` [*] Type "${type}" not supported`); + } + + return await client.incr(`statistics:${type}:${dayjs().format('YYYY-MM-DD')}`); +} + module.exports = { createSecret, getSecret, @@ -134,4 +147,5 @@ module.exports = { getUser, deleteUser, createRateLimit, + createStatistics, };