Skip to content

Commit 513fbf0

Browse files
committed
chore: add performance logging and fix data controller bug
- Add console.time benchmarks for Redis JSON ops, API auth, Logger, and CORS - Fix project ID reference in data.controller.js
1 parent 24697b2 commit 513fbf0

5 files changed

Lines changed: 22 additions & 4 deletions

File tree

backend/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ if (process.env.NODE_ENV === 'development') {
3030

3131
const adminCorsOptions = {
3232
origin: function (origin, callback) {
33+
console.time("cors for admin chk")
3334
// Allow requests with no origin (like mobile apps or curl)
3435
if (!origin || adminWhitelist.indexOf(origin) !== -1) {
3536
callback(null, true);
3637
} else {
3738
callback(new Error('Not allowed by CORS for Admin access'));
3839
}
40+
console.timeEnd("cors for admin chk")
3941
},
4042
credentials: true
4143
};

backend/controllers/data.controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports.insertData = async (req, res) => {
5656

5757
if (!project.resources.db.isExternal) {
5858
await Project.updateOne(
59-
{ _id: cachedProject._id },
59+
{ _id: project._id },
6060
{ $inc: { databaseUsed: docSize } }
6161
);
6262
}
@@ -70,6 +70,7 @@ module.exports.insertData = async (req, res) => {
7070
// GET ALL DATA
7171
module.exports.getAllData = async (req, res) => {
7272
try {
73+
console.time("getall")
7374
const { collectionName } = req.params;
7475
const project = req.project;
7576

@@ -80,6 +81,7 @@ module.exports.getAllData = async (req, res) => {
8081
const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal);
8182

8283
const data = await Model.find({}).limit(100).lean();
84+
console.timeEnd("getall")
8385
res.json(data);
8486
} catch (err) {
8587
res.status(500).json({ error: err.message });

backend/middleware/api_usage.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const rateLimit = require('express-rate-limit');
22
const Log = require('../models/Log');
33

44
// Rate Limiter
5+
56
const limiter = rateLimit({
67
windowMs: 15 * 60 * 1000,
78
max: 100,
@@ -14,6 +15,7 @@ const limiter = rateLimit({
1415

1516
// Logger
1617
const logger = (req, res, next) => {
18+
console.time("logger middleware")
1719
// Check for Data, Storage, AND UserAuth routes
1820
if (
1921
req.originalUrl.startsWith('/api/data') ||
@@ -38,6 +40,7 @@ const logger = (req, res, next) => {
3840
}
3941
});
4042
}
43+
console.timeEnd("logger middleware")
4144
next();
4245
};
4346

backend/middleware/verifyApiKey.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ const {
77

88
module.exports = async (req, res, next) => {
99
try {
10+
console.time("api chk middleware")
1011
const apiKey = req.header('x-api-key');
1112
if (!apiKey) {
1213
return res.status(401).json({ error: 'API key not found' });
1314
}
1415

1516
const hashedApi = hashApiKey(apiKey);
1617

18+
console.time("get project by api key cache")
1719
let project = await getProjectByApiKeyCache(hashedApi);
20+
console.timeEnd("get project by api key cache")
1821

1922
if (!project) {
23+
console.time("get project by api key from db")
2024
project = await Project.findOne({ apiKey: hashedApi })
2125
.select(`
2226
owner
@@ -29,7 +33,7 @@ module.exports = async (req, res, next) => {
2933
`)
3034
.populate('owner', 'isVerified')
3135
.lean();
32-
36+
console.timeEnd("get project by api key from db")
3337

3438
if (!project) {
3539
return res.status(401).json({
@@ -55,6 +59,7 @@ module.exports = async (req, res, next) => {
5559

5660
req.project = project;
5761
req.hashedApiKey = hashedApi;
62+
console.timeEnd("api chk middleware")
5863
next();
5964
} catch (err) {
6065
res.status(500).json({ error: err.message });

backend/services/redisCaching.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ const redis = require("../config/redis");
22

33
async function setProjectByApiKeyCache(api, project) {
44
try {
5+
console.time("json");
6+
const data = JSON.stringify(project);
7+
console.timeEnd("json");
58
await redis.set(
69
`project:apikey:${api}`,
7-
JSON.stringify(project),
10+
data,
811
'EX',
912
60 * 60 * 2
1013
);
@@ -17,7 +20,10 @@ async function getProjectByApiKeyCache(api) {
1720
try {
1821
const data = await redis.get(`project:apikey:${api}`);
1922
if (!data) return null;
20-
return JSON.parse(data);
23+
console.time("json parse")
24+
const parsedData = JSON.parse(data)
25+
console.timeEnd("json parse")
26+
return parsedData;
2127
} catch (err) {
2228
console.log(err);
2329
return null;

0 commit comments

Comments
 (0)