implementing snapshots

This commit is contained in:
Emily
2024-07-26 16:18:20 +02:00
parent fc78b3bb43
commit 229c341d7a
19 changed files with 293 additions and 165 deletions

View File

@@ -22,13 +22,25 @@ export default defineEventHandler(async event => {
const limit = getRequestHeader(event, 'x-query-limit');
const numLimit = parseInt(limit || '10');
const from = getRequestHeader(event, 'x-from');
const to = getRequestHeader(event, 'x-to');
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to headers missing');
return await Redis.useCache({
key: `referrers:${project_id}:${numLimit}`,
key: `referrers:${project_id}:${numLimit}:${from}:${to}`,
exp: DATA_EXPIRE_TIME
}, async () => {
const referrers: ReferrersAggregated[] = await VisitModel.aggregate([
{ $match: { project_id: project._id }, },
{
$match: {
project_id: project._id,
created_at: {
$gte: new Date(from),
$lte: new Date(to)
}
},
},
{ $group: { _id: "$referrer", count: { $sum: 1, } } },
{ $sort: { count: -1 } },
{ $limit: numLimit }