mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
change in progress
This commit is contained in:
34
dashboard/server/api/data/browsers.ts
Normal file
34
dashboard/server/api/data/browsers.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { getRequestData } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, { requireSchema: false });
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, project_id, limit } = data;
|
||||
|
||||
const cacheKey = `browsers:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const result = await VisitModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
}
|
||||
},
|
||||
{ $group: { _id: "$browser", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: limit }
|
||||
]);
|
||||
|
||||
return result as { _id: string, count: number }[];
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user