mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +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;
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,22 +1,22 @@
|
||||
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, ['SLICE', 'DOMAIN', 'RANGE', 'OFFSET'], ['EVENTS']);
|
||||
if (!data) return;
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice','permission:webAnalytics', 'flag:allowShare');
|
||||
|
||||
const { pid, from, to, slice, project_id, timeOffset, domain } = data;
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
|
||||
const cacheKey = `timeline:events:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: EventModel,
|
||||
from, to, slice, timeOffset, domain
|
||||
from, to, slice, domain
|
||||
});
|
||||
return timelineData;
|
||||
});
|
||||
|
||||
@@ -1,27 +1,51 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeAdvancedTimelineAggregation } from "~/server/services/TimelineService";
|
||||
import { EventModel } from "~/shared/schema/metrics/EventSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, ['RANGE', 'SLICE', 'DOMAIN'], ['EVENTS']);
|
||||
if (!data) return;
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice', 'permission:events');
|
||||
|
||||
const { from, to, slice, project_id, timeOffset, domain } = data;
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
|
||||
return await Redis.useCache({ key: `timeline:events_stacked:${project_id}:${slice}:${from || 'none'}:${to || 'none'}:${domain}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
|
||||
const cacheKey = `timeline:events_stacked:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
const timelineStackedEvents = await executeAdvancedTimelineAggregation<{ name: String }>({
|
||||
model: EventModel,
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
|
||||
const timelineData = await executeAdvancedTimelineAggregation({
|
||||
projectId: project_id,
|
||||
from, to, slice,
|
||||
customProjection: { name: "$_id.name" },
|
||||
customIdGroup: { name: '$name' },
|
||||
timeOffset,
|
||||
domain
|
||||
})
|
||||
model: EventModel,
|
||||
from, to, slice, domain,
|
||||
customIdGroup: {
|
||||
event: '$name'
|
||||
},
|
||||
customProjection: {
|
||||
events: 1
|
||||
},
|
||||
customQueries: [
|
||||
{
|
||||
index: 2, query: {
|
||||
$group: {
|
||||
_id: {
|
||||
date: "$_id.date"
|
||||
},
|
||||
events: {
|
||||
$push: {
|
||||
name: "$_id.event",
|
||||
count: "$count"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
return timelineData;
|
||||
|
||||
return timelineStackedEvents.filter(e => e.name != undefined);
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
30
dashboard/server/api/timeline/live_users.ts
Normal file
30
dashboard/server/api/timeline/live_users.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeAdvancedTimelineAggregation } from "~/server/services/TimelineService";
|
||||
import { VisitModel } from "~/shared/schema/metrics/VisitSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice', 'permission:webAnalytics');
|
||||
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
|
||||
const cacheKey = `timeline:events:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeAdvancedTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: VisitModel,
|
||||
from, to, slice, domain,
|
||||
customIdGroup: {
|
||||
session: "$session"
|
||||
}
|
||||
});
|
||||
return timelineData;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
@@ -1,27 +1,24 @@
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
|
||||
import { sessionController } from "~/server/controllers/SessionController";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, ['SLICE', 'DOMAIN', 'RANGE', 'OFFSET'], ['WEB']);
|
||||
if (!data) return;
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice', 'permission:webAnalytics', 'flag:allowShare');
|
||||
|
||||
const { pid, from, to, slice, project_id, timeOffset, domain } = data;
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
|
||||
const cacheKey = `timeline:sessions:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
const cacheExp = 20;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: SessionModel,
|
||||
from, to, slice, timeOffset, domain
|
||||
});
|
||||
return timelineData;
|
||||
if (getHeader(event, 'x-dev') === 'true') await Redis.del(cacheKey);
|
||||
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
const { data, time } = await sessionController.executeDynamic({ project_id: project_id.toString(), from, to, slice, domain });
|
||||
setHeader(event, 'x-time', time.toFixed());
|
||||
return data;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
@@ -1,55 +1,21 @@
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeAdvancedTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
|
||||
import { durationController } from "~/server/controllers/DurationController";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, ['SLICE', 'DOMAIN', 'RANGE'], ['WEB']);
|
||||
if (!data) return;
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice', 'permission:webAnalytics', 'flag:allowShare');
|
||||
|
||||
const { pid, from, to, slice, project_id, domain } = data;
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
|
||||
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;
|
||||
if (getHeader(event, 'x-dev') === 'true') await Redis.del(cacheKey);
|
||||
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
const { data, time } = await durationController.executeDynamic({ project_id: project_id.toString(), from, to, slice, domain });
|
||||
setHeader(event, 'x-time', time.toFixed());
|
||||
return data;
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -1,29 +1,17 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { visitController } from "~/server/controllers/VisitController";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeAdvancedTimelineAggregation } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, ['SLICE', 'DOMAIN', 'RANGE', 'OFFSET'], ['WEB']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id, timeOffset, domain } = data;
|
||||
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'slice', 'permission:webAnalytics', 'flag:allowShare');
|
||||
const { pid, project_id, domain, from, to, slice } = ctx;
|
||||
const cacheKey = `timeline:visits:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 20;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeAdvancedTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: VisitModel,
|
||||
from, to, slice, timeOffset, domain
|
||||
});
|
||||
|
||||
return timelineData;
|
||||
|
||||
if (getHeader(event, 'x-dev') === 'true') await Redis.del(cacheKey);
|
||||
return await Redis.useCache(cacheKey, cacheExp, async () => {
|
||||
const { data, time } = await visitController.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