From 3dc056d82afceb6360cfd0cc0a0f3f94a03fb98e Mon Sep 17 00:00:00 2001 From: Vinit khandal <111434418+vinit717@users.noreply.github.com> Date: Sat, 18 Jan 2025 23:49:37 +0530 Subject: [PATCH] fix date query to fetch logs (#2348) * chore: fix date query to fetch logs * chore: remove id for logs * chore: fix logs order query * chore: add test for logs modal * chore: fix failing test * chore: fix failing test by reverting it * chore: remove nanoseconds precision --- models/logs.js | 14 ++++-- test/unit/models/logs.test.js | 87 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/models/logs.js b/models/logs.js index f673d5b27..4c8b2b1cf 100644 --- a/models/logs.js +++ b/models/logs.js @@ -193,20 +193,24 @@ const fetchAllLogs = async (query) => { throw error; } - const buildTimestamp = (date) => ({ - _seconds: Math.floor(date / 1000), - _nanoseconds: 0, + const buildTimestamp = (milliseconds) => ({ + _seconds: Math.floor(milliseconds / 1000), + _nanoseconds: (milliseconds % 1000) * 1000000, }); if (startDate) { - requestQuery = requestQuery.where("timestamp", ">=", buildTimestamp(startDate)); + const startTimestamp = buildTimestamp(startDate); + requestQuery = requestQuery.where("timestamp._seconds", ">=", startTimestamp._seconds); } + if (endDate) { - requestQuery = requestQuery.where("timestamp", "<=", buildTimestamp(endDate)); + const endTimestamp = buildTimestamp(endDate); + requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds); } } requestQuery = requestQuery.orderBy("timestamp", "desc"); + let requestQueryDoc = requestQuery; if (prev) { diff --git a/test/unit/models/logs.test.js b/test/unit/models/logs.test.js index d0840d508..68b319fea 100644 --- a/test/unit/models/logs.test.js +++ b/test/unit/models/logs.test.js @@ -198,6 +198,93 @@ describe("Logs", function () { expect(Array.from(uniqueTypes)[0]).to.equal("REQUEST_CREATED"); }); + it("Should throw error when start date is greater than end date in dev mode", async function () { + await cleanDb(); + + const startDate = Date.now(); + const endDate = startDate - 86400000; + + try { + await logsQuery.fetchAllLogs({ + dev: "true", + startDate: startDate.toString(), + endDate: endDate.toString(), + size: 3, + }); + throw new Error("Expected fetchAllLogs to throw an error, but it did not."); + } catch (error) { + expect(error).to.be.instanceOf(Error); + expect(error.message).to.equal("Start date cannot be greater than end date."); + expect(error).to.have.property("statusCode", 400); + } + }); + + it("Should return logs within the specified date range in dev mode", async function () { + await cleanDb(); + + const endDate = Date.now(); + const startDate = endDate - 86400000 * 7; + + const result = await logsQuery.fetchAllLogs({ + dev: "true", + startDate: startDate.toString(), + endDate: endDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + if (result.allLogs.length > 0) { + result.allLogs.forEach((log) => { + expect(log).to.have.property("timestamp"); + }); + } + }); + + it("Should ignore date filters when not in dev mode", async function () { + const endDate = Date.now(); + const startDate = endDate - 86400000 * 7; + + const result = await logsQuery.fetchAllLogs({ + dev: "false", + startDate: startDate.toString(), + endDate: endDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + expect(result).to.have.property("prev"); + expect(result).to.have.property("next"); + expect(result).to.have.property("page"); + }); + + it("Should handle only start date filter in dev mode", async function () { + const startDate = Date.now() - 86400000 * 14; + + const result = await logsQuery.fetchAllLogs({ + dev: "true", + startDate: startDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + expect(result).to.have.property("prev"); + expect(result).to.have.property("next"); + }); + + it("Should handle only end date filter in dev mode", async function () { + const endDate = Date.now(); + + const result = await logsQuery.fetchAllLogs({ + dev: "true", + endDate: endDate.toString(), + size: 3, + }); + + expect(result).to.have.property("allLogs"); + expect(result).to.have.property("prev"); + expect(result).to.have.property("next"); + }); + it("Should return null if no logs are presnet the logs for specific types", async function () { await cleanDb(); const result = await logsQuery.fetchAllLogs({});