Files
litlyx/dashboard/server/api/timeline/sessions_duration.ts
Emily afda29997d Fix
2025-03-14 16:40:50 +01:00

58 lines
1.9 KiB
TypeScript

import { SessionModel } from "@schema/metrics/SessionSchema";
import { Redis } from "~/server/services/CacheService";
import { executeAdvancedTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
export default defineEventHandler(async event => {
const data = await getRequestData(event, ['SLICE', 'DOMAIN', 'RANGE'], ['WEB']);
if (!data) return;
const { pid, from, to, slice, project_id, domain } = data;
const cacheKey = `timeline:sessions_duration:${pid}:${slice}:${from}:${to}:${domain}`;
const cacheExp = 60;
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
const timelineData = await executeAdvancedTimelineAggregation({
projectId: project_id,
model: SessionModel,
from, to, slice,
domain,
customGroup: {
duration: { $sum: '$duration' }
},
customProjection: {
count: { $divide: ["$duration", "$count"] }
},
customQueries: [
{
index: 1,
query: {
"$lookup": {
"from": "visits",
"localField": "session",
"foreignField": "session",
"as": "visits",
"pipeline": [{ "$limit": 1 }]
}
},
},
{
index: 2,
query: {
"$match": {
"visits.0": { "$exists": true }
}
}
}
]
});
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, slice, from, to);
return timelineFilledMerged;
});
});