mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-14 00:58:36 +01:00
new selfhosted version
This commit is contained in:
@@ -1,73 +1,21 @@
|
||||
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { bouncingController } from "~/server/controllers/BouncingController";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import DateService from "@services/DateService";
|
||||
|
||||
import { checkSliceValidity } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice', 'permission:webAnalytics', 'flag:allowShare');
|
||||
|
||||
const data = await getRequestData(event, ['SLICE', 'RANGE', 'DOMAIN'], ['WEB']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id, domain } = data;
|
||||
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
|
||||
const cacheKey = `timeline:bouncing_rate:${pid}:${slice}:${from}:${to}`;
|
||||
const cacheExp = 60 * 60; //1 hour
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const [sliceValid, errorOrDays] = checkSliceValidity(from, to, slice);
|
||||
if (!sliceValid) throw Error(errorOrDays);
|
||||
|
||||
const allDates = DateService.generateDateSlices(slice, new Date(from), new Date(to));
|
||||
|
||||
const result: { _id: string, count: number }[] = [];
|
||||
|
||||
for (const date of allDates) {
|
||||
|
||||
const visits = await VisitModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project_id,
|
||||
created_at: {
|
||||
$gte: DateService.startOfSlice(date, slice),
|
||||
$lte: DateService.endOfSlice(date, slice)
|
||||
},
|
||||
website: domain
|
||||
},
|
||||
},
|
||||
{ $group: { _id: "$session", count: { $sum: 1, } } },
|
||||
]);
|
||||
|
||||
const sessions = await SessionModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project_id,
|
||||
created_at: {
|
||||
$gte: DateService.startOfSlice(date, slice),
|
||||
$lte: DateService.endOfSlice(date, slice)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$session", count: { $sum: 1, },
|
||||
duration: { $sum: '$duration' }
|
||||
}
|
||||
},
|
||||
]);
|
||||
|
||||
const total = visits.length;
|
||||
const bounced = sessions.filter(e => (e.duration / e.count) < 1).length;
|
||||
const bouncing_rate = 100 / total * bounced;
|
||||
result.push({ _id: date.toISOString(), count: bouncing_rate });
|
||||
}
|
||||
|
||||
return result;
|
||||
if (getHeader(event, 'x-dev') === 'true') await Redis.del(cacheKey);
|
||||
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
const { data, time } = await bouncingController.executeDynamic({ project_id: project_id.toString(), from, to, slice, domain });
|
||||
setHeader(event, 'x-time', time.toFixed());
|
||||
return data;
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user