Skip to content

Commit

Permalink
fix date query to fetch logs (#2348)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vinit717 authored and yesyash committed Jan 18, 2025
1 parent c79e3cc commit 3dc056d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
14 changes: 9 additions & 5 deletions models/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
87 changes: 87 additions & 0 deletions test/unit/models/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down

0 comments on commit 3dc056d

Please sign in to comment.