Skip to content

Commit

Permalink
chore: limit number of visits returned from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
JuroUhlar committed Jan 14, 2024
1 parent 39d40c2 commit 8bbffd5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/pages/api/bot-firewall/get-bot-visits.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { BotVisit, getBotVisits } from '../../../server/botd-firewall/botVisitDatabase';

export default async function handler(_req: NextApiRequest, res: NextApiResponse<BotVisit[]>) {
export default async function handler(req: NextApiRequest, res: NextApiResponse<BotVisit[]>) {
try {
const blockedIps = await getBotVisits();
res.status(200).json(blockedIps);
const limit = Number(req.query.limit);
const botVisits = await getBotVisits(limit);
res.status(200).json(botVisits);
} catch (error) {
console.error(error);
res.statusMessage = `Something went wrong ${error}`;
Expand Down
12 changes: 10 additions & 2 deletions src/server/botd-firewall/botVisitDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Attributes, DataTypes, InferAttributes, InferCreationAttributes, Model } from 'sequelize';
import { Attributes, DataTypes, FindOptions, InferAttributes, InferCreationAttributes, Model } from 'sequelize';
import { sequelize } from '../server';
import { EventResponseBotData } from '../../shared/types';

Expand Down Expand Up @@ -62,4 +62,12 @@ export const saveBotVisit = async (botData: EventResponseBotData, visitorId: str
});
};

export const getBotVisits = async () => await BotVisitDbModel.findAll({ order: [['timestamp', 'DESC']] });
export const getBotVisits = async (limit?: number) => {
const options: FindOptions = {
order: [['timestamp', 'DESC']],
};
if (limit) {
options.limit = limit;
}
return await BotVisitDbModel.findAll(options);
};

0 comments on commit 8bbffd5

Please sign in to comment.