Skip to content

Commit

Permalink
use redis for hitcount queries
Browse files Browse the repository at this point in the history
  • Loading branch information
boehs committed Apr 1, 2024
1 parent 1f21b10 commit f266aec
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/pages/api/websites/[id]/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
import { parseDateRangeQuery } from 'lib/query';
import { getWebsiteStats } from 'queries';
import redis from '@umami/redis-client';

export interface WebsiteStatsRequestQuery {
id: string;
Expand Down Expand Up @@ -91,23 +92,31 @@ export default async (
city,
};

const metrics = await getWebsiteStats(websiteId, { ...filters, startDate, endDate });
const getStats = async () => {
const metrics = await getWebsiteStats(websiteId, { ...filters, startDate, endDate });

const prevPeriod = await getWebsiteStats(websiteId, {
...filters,
startDate: prevStartDate,
endDate: prevEndDate,
});
const prevPeriod = await getWebsiteStats(websiteId, {
...filters,
startDate: prevStartDate,
endDate: prevEndDate,
});

const stats = Object.keys(metrics[0]).reduce((obj, key) => {
obj[key] = {
value: Number(metrics[0][key]) || 0,
change: Number(metrics[0][key]) - Number(prevPeriod[0][key]) || 0,
};
return obj;
}, {});
return Object.keys(metrics[0]).reduce((obj, key) => {
obj[key] = {
value: Number(metrics[0][key]) || 0,
change: Number(metrics[0][key]) - Number(prevPeriod[0][key]) || 0,
};
return obj;
}, {});
};

return ok(res, stats);
if (redis.enabled && Object.keys(req.query).length == 1 && req.query.url != undefined) {
return ok(
res,
await redis.client.getCache(`hitcount:${req.query.url}`, () => getStats(), 60),
);
}
return ok(res, await getStats());
}

return methodNotAllowed(res);
Expand Down

0 comments on commit f266aec

Please sign in to comment.