Skip to content

Commit 3dc056d

Browse files
vinit717yesyash
authored andcommitted
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
1 parent c79e3cc commit 3dc056d

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

models/logs.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,24 @@ const fetchAllLogs = async (query) => {
193193
throw error;
194194
}
195195

196-
const buildTimestamp = (date) => ({
197-
_seconds: Math.floor(date / 1000),
198-
_nanoseconds: 0,
196+
const buildTimestamp = (milliseconds) => ({
197+
_seconds: Math.floor(milliseconds / 1000),
198+
_nanoseconds: (milliseconds % 1000) * 1000000,
199199
});
200200

201201
if (startDate) {
202-
requestQuery = requestQuery.where("timestamp", ">=", buildTimestamp(startDate));
202+
const startTimestamp = buildTimestamp(startDate);
203+
requestQuery = requestQuery.where("timestamp._seconds", ">=", startTimestamp._seconds);
203204
}
205+
204206
if (endDate) {
205-
requestQuery = requestQuery.where("timestamp", "<=", buildTimestamp(endDate));
207+
const endTimestamp = buildTimestamp(endDate);
208+
requestQuery = requestQuery.where("timestamp._seconds", "<=", endTimestamp._seconds);
206209
}
207210
}
208211

209212
requestQuery = requestQuery.orderBy("timestamp", "desc");
213+
210214
let requestQueryDoc = requestQuery;
211215

212216
if (prev) {

test/unit/models/logs.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,93 @@ describe("Logs", function () {
198198
expect(Array.from(uniqueTypes)[0]).to.equal("REQUEST_CREATED");
199199
});
200200

201+
it("Should throw error when start date is greater than end date in dev mode", async function () {
202+
await cleanDb();
203+
204+
const startDate = Date.now();
205+
const endDate = startDate - 86400000;
206+
207+
try {
208+
await logsQuery.fetchAllLogs({
209+
dev: "true",
210+
startDate: startDate.toString(),
211+
endDate: endDate.toString(),
212+
size: 3,
213+
});
214+
throw new Error("Expected fetchAllLogs to throw an error, but it did not.");
215+
} catch (error) {
216+
expect(error).to.be.instanceOf(Error);
217+
expect(error.message).to.equal("Start date cannot be greater than end date.");
218+
expect(error).to.have.property("statusCode", 400);
219+
}
220+
});
221+
222+
it("Should return logs within the specified date range in dev mode", async function () {
223+
await cleanDb();
224+
225+
const endDate = Date.now();
226+
const startDate = endDate - 86400000 * 7;
227+
228+
const result = await logsQuery.fetchAllLogs({
229+
dev: "true",
230+
startDate: startDate.toString(),
231+
endDate: endDate.toString(),
232+
size: 3,
233+
});
234+
235+
expect(result).to.have.property("allLogs");
236+
if (result.allLogs.length > 0) {
237+
result.allLogs.forEach((log) => {
238+
expect(log).to.have.property("timestamp");
239+
});
240+
}
241+
});
242+
243+
it("Should ignore date filters when not in dev mode", async function () {
244+
const endDate = Date.now();
245+
const startDate = endDate - 86400000 * 7;
246+
247+
const result = await logsQuery.fetchAllLogs({
248+
dev: "false",
249+
startDate: startDate.toString(),
250+
endDate: endDate.toString(),
251+
size: 3,
252+
});
253+
254+
expect(result).to.have.property("allLogs");
255+
expect(result).to.have.property("prev");
256+
expect(result).to.have.property("next");
257+
expect(result).to.have.property("page");
258+
});
259+
260+
it("Should handle only start date filter in dev mode", async function () {
261+
const startDate = Date.now() - 86400000 * 14;
262+
263+
const result = await logsQuery.fetchAllLogs({
264+
dev: "true",
265+
startDate: startDate.toString(),
266+
size: 3,
267+
});
268+
269+
expect(result).to.have.property("allLogs");
270+
expect(result).to.have.property("prev");
271+
expect(result).to.have.property("next");
272+
});
273+
274+
it("Should handle only end date filter in dev mode", async function () {
275+
const endDate = Date.now();
276+
277+
const result = await logsQuery.fetchAllLogs({
278+
dev: "true",
279+
endDate: endDate.toString(),
280+
size: 3,
281+
});
282+
283+
expect(result).to.have.property("allLogs");
284+
expect(result).to.have.property("prev");
285+
expect(result).to.have.property("next");
286+
});
287+
201288
it("Should return null if no logs are presnet the logs for specific types", async function () {
202289
await cleanDb();
203290
const result = await logsQuery.fetchAllLogs({});

0 commit comments

Comments
 (0)