Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix logs query for date filter #2374

Merged
merged 18 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions models/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,21 @@ const fetchAllLogs = async (query) => {
}

if (isDev && (startDate || endDate)) {
startDate = startDate ? parseInt(startDate) : null;
endDate = endDate ? parseInt(endDate) : null;
startDate = startDate ? parseInt(startDate, 10) * 1000 : null;
endDate = endDate ? parseInt(endDate, 10) * 1000 : null;

if (startDate && endDate && startDate > endDate) {
const error = new Error("Start date cannot be greater than end date.");
error.statusCode = 400;
throw error;
}

const buildTimestamp = (milliseconds) => ({
_seconds: Math.floor(milliseconds / 1000),
_nanoseconds: (milliseconds % 1000) * 1000000,
});

if (startDate) {
const startTimestamp = buildTimestamp(startDate);
requestQuery = requestQuery.where("timestamp._seconds", ">=", startTimestamp._seconds);
requestQuery = requestQuery.where("timestamp", ">=", admin.firestore.Timestamp.fromMillis(startDate));
}

if (endDate) {
const endTimestamp = buildTimestamp(endDate);
requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds);
requestQuery = requestQuery.where("timestamp", "<=", admin.firestore.Timestamp.fromMillis(endDate));
}
}

Expand Down Expand Up @@ -255,7 +248,6 @@ const fetchAllLogs = async (query) => {
page: page ? page + 1 : null,
};
}

if (format === "feed") {
const userList = await getUsersListFromLogs(allLogs);
const taskIdList = await getTasksFromLogs(allLogs);
Expand Down
8 changes: 4 additions & 4 deletions test/integration/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ describe("/logs", function () {

it("should return logs filtered by username, startDate, and endDate when dev flag is enabled", function (done) {
const username = "joygupta";
const startDate = 1729841400000;
const endDate = 1729841500000;
const startDate = 1729841400;
const endDate = 1729841500;
chai
.request(app)
.get(`/logs?username=${username}&startDate=${startDate}&endDate=${endDate}&dev=true`)
Expand Down Expand Up @@ -249,8 +249,8 @@ describe("/logs", function () {

it("should return an empty array if no logs match username and date range", function (done) {
const username = "nonexistentUser";
const startDate = 1729841400000;
const endDate = 1729841500000;
const startDate = 1729841400;
const endDate = 1729841500;

chai
.request(app)
Expand Down
35 changes: 25 additions & 10 deletions test/unit/models/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ describe("Logs", function () {
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;
const startDate = Math.floor(Date.now() / 1000);
const endDate = startDate - 86400;

try {
await logsQuery.fetchAllLogs({
Expand All @@ -222,9 +222,8 @@ describe("Logs", function () {
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 endDate = Math.floor(Date.now() / 1000);
const startDate = endDate - 86400 * 7;
const result = await logsQuery.fetchAllLogs({
dev: "true",
startDate: startDate.toString(),
Expand All @@ -235,14 +234,16 @@ describe("Logs", function () {
expect(result).to.have.property("allLogs");
if (result.allLogs.length > 0) {
result.allLogs.forEach((log) => {
expect(log).to.have.property("timestamp");
expect(log).to.have.property("timestamp").that.is.a("number");
expect(log.timestamp).to.be.at.least(startDate);
expect(log.timestamp).to.be.at.most(endDate);
});
}
});

it("Should ignore date filters when not in dev mode", async function () {
const endDate = Date.now();
const startDate = endDate - 86400000 * 7;
const endDate = Math.floor(Date.now() / 1000);
const startDate = endDate - 86400 * 7;

const result = await logsQuery.fetchAllLogs({
dev: "false",
Expand All @@ -258,7 +259,7 @@ describe("Logs", function () {
});

it("Should handle only start date filter in dev mode", async function () {
const startDate = Date.now() - 86400000 * 14;
const startDate = Math.floor(Date.now() / 1000) - 86400 * 14;

const result = await logsQuery.fetchAllLogs({
dev: "true",
Expand All @@ -269,10 +270,17 @@ describe("Logs", function () {
expect(result).to.have.property("allLogs");
expect(result).to.have.property("prev");
expect(result).to.have.property("next");

if (result.allLogs.length > 0) {
result.allLogs.forEach((log) => {
expect(log).to.have.property("timestamp").that.is.a("number");
expect(log.timestamp).to.be.at.least(startDate);
});
}
});

it("Should handle only end date filter in dev mode", async function () {
const endDate = Date.now();
const endDate = Math.floor(Date.now() / 1000);

const result = await logsQuery.fetchAllLogs({
dev: "true",
Expand All @@ -283,6 +291,13 @@ describe("Logs", function () {
expect(result).to.have.property("allLogs");
expect(result).to.have.property("prev");
expect(result).to.have.property("next");

if (result.allLogs.length > 0) {
result.allLogs.forEach((log) => {
expect(log).to.have.property("timestamp").that.is.a("number");
expect(log.timestamp).to.be.at.most(endDate);
});
}
});

it("Should return null if no logs are presnet the logs for specific types", async function () {
Expand Down
Loading