Skip to content

Commit

Permalink
#22 | add option to filter results
Browse files Browse the repository at this point in the history
if no filter is given, default to last 14 days
  • Loading branch information
kitsunekyo committed Aug 15, 2020
1 parent bdc2fc6 commit 2c9e450
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 4 additions & 1 deletion server/src/Speedtest/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const list = async (
next: any
): Promise<express.Response> => {
try {
const results = await service.list();
const start = req.query.start?.toString();
const end = req.query.end?.toString();

const results = await service.list(start, end);
return res.json(new SuccessResponse(results));
} catch (error) {
return next(error);
Expand Down
20 changes: 18 additions & 2 deletions server/src/Speedtest/resultDb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import monk from "monk";
import { Result } from "./Result";
import addDays from "date-fns/addDays";
import startOfDay from "date-fns/startOfDay";
import endOfDay from "date-fns/endOfDay";

const db = monk(process.env.MONGODB_CONNECTION_STRING || "");

Expand All @@ -10,8 +13,21 @@ const save = async (speedtest: Result): Promise<Result> => {
return speedtest;
};

const list = async (): Promise<Result[]> => {
return await results.find();
const list = async (
start: Date | string | null = null,
end: Date | string | null = null
): Promise<Result[]> => {
const startDate = start
? new Date(start)
: new Date(startOfDay(addDays(Date.now(), -14)));
const endDate = end ? new Date(end) : new Date(endOfDay(Date.now()));

return await results.find({
timestamp: {
$gte: startDate,
$lte: endDate,
},
});
};

export default {
Expand Down
7 changes: 5 additions & 2 deletions server/src/Speedtest/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ const save = async (speedtest: Result): Promise<Result> => {
return await resultDb.save(speedtest);
};

const list = async (): Promise<Result[]> => {
return await resultDb.list();
const list = async (
start: Date | string | null = null,
end: Date | string | null = null
): Promise<Result[]> => {
return await resultDb.list(start, end);
};

export default {
Expand Down

0 comments on commit 2c9e450

Please sign in to comment.