mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
.
This commit is contained in:
@@ -16,7 +16,8 @@ export function useFirstInteractionData() {
|
|||||||
return metricsInfo;
|
return metricsInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function useTimeline(endpoint: 'visits' | 'sessions', slice: Slice, fromDate?: string, toDate?: string) {
|
|
||||||
|
export async function useTimeline(endpoint: 'visits' | 'sessions' | 'referrers', slice: Slice, fromDate?: string, toDate?: string) {
|
||||||
|
|
||||||
const { from, to } = DateService.prepareDateRange(
|
const { from, to } = DateService.prepareDateRange(
|
||||||
fromDate || DateService.getDefaultRange(slice).from,
|
fromDate || DateService.getDefaultRange(slice).from,
|
||||||
@@ -36,6 +37,8 @@ export async function useTimeline(endpoint: 'visits' | 'sessions', slice: Slice,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function useTimelineDataRaw(timelineEndpointName: string, slice: SliceName) {
|
export async function useTimelineDataRaw(timelineEndpointName: string, slice: SliceName) {
|
||||||
const activeProject = useActiveProject();
|
const activeProject = useActiveProject();
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
|
|||||||
import DateService from "@services/DateService";
|
import DateService from "@services/DateService";
|
||||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||||
|
import { executeAdvancedTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService";
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
const project_id = getRequestProjectId(event);
|
const project_id = getRequestProjectId(event);
|
||||||
@@ -12,30 +13,27 @@ export default defineEventHandler(async event => {
|
|||||||
const project = await getUserProjectFromId(project_id, user);
|
const project = await getUserProjectFromId(project_id, user);
|
||||||
if (!project) return;
|
if (!project) return;
|
||||||
|
|
||||||
const { slice, referrer, from, to } = await readBody(event);
|
const { slice, from, to } = await readBody(event);
|
||||||
|
|
||||||
const { group, sort, fromParts } = DateService.getQueryDateRange(slice);
|
if (!from) return setResponseStatus(event, 400, 'from is required');
|
||||||
|
if (!from) return setResponseStatus(event, 400, 'to is required');
|
||||||
|
if (!from) return setResponseStatus(event, 400, 'slice is required');
|
||||||
|
|
||||||
const aggregation = [
|
return await Redis.useCache({
|
||||||
{
|
key: `timeline:referrers:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
||||||
$match: {
|
exp: TIMELINE_EXPIRE_TIME
|
||||||
project_id: project._id,
|
}, async () => {
|
||||||
created_at: {
|
const timelineData = await executeAdvancedTimelineAggregation({
|
||||||
$gte: new Date(from),
|
projectId: project._id,
|
||||||
$lte: new Date(to)
|
model: VisitModel,
|
||||||
},
|
from, to, slice,
|
||||||
referrer
|
customMatch: {
|
||||||
|
referrer: '$referrer'
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
{ $group: { _id: group, count: { $sum: 1 } } },
|
const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
|
||||||
{ $sort: sort },
|
return timelineFilledMerged;
|
||||||
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
|
});
|
||||||
]
|
|
||||||
|
|
||||||
const timelineReferrers: { _id: string, count: number }[] = await VisitModel.aggregate(aggregation);
|
|
||||||
|
|
||||||
const filledDates = DateService.fillDates(timelineReferrers.map(e => e._id), slice);
|
|
||||||
const merged = DateService.mergeFilledDates(filledDates, timelineReferrers, '_id', slice, { count: 0 });
|
|
||||||
return merged;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -2,7 +2,7 @@ import { getTimeline } from "./generic";
|
|||||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||||
import DateService from "@services/DateService";
|
import { executeTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService";
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
const project_id = getRequestProjectId(event);
|
const project_id = getRequestProjectId(event);
|
||||||
@@ -23,26 +23,13 @@ export default defineEventHandler(async event => {
|
|||||||
key: `timeline:sessions:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
key: `timeline:sessions:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
||||||
exp: TIMELINE_EXPIRE_TIME
|
exp: TIMELINE_EXPIRE_TIME
|
||||||
}, async () => {
|
}, async () => {
|
||||||
|
const timelineData = await executeTimelineAggregation({
|
||||||
const { group, sort, fromParts } = DateService.getQueryDateRange(slice);
|
projectId: project._id,
|
||||||
|
model: SessionModel,
|
||||||
const aggregation = [
|
from, to, slice
|
||||||
{
|
});
|
||||||
$match: {
|
const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
|
||||||
project_id: project._id,
|
return timelineFilledMerged;
|
||||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ $group: { _id: group, count: { $sum: 1 } } },
|
|
||||||
{ $sort: sort },
|
|
||||||
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
|
|
||||||
]
|
|
||||||
|
|
||||||
const timelineVisits: { _id: string, count: number }[] = await SessionModel.aggregate(aggregation);
|
|
||||||
const filledDates = DateService.fillDates(timelineVisits.map(e => e._id), slice);
|
|
||||||
const merged = DateService.mergeFilledDates(filledDates, timelineVisits, '_id', slice, { count: 0 });
|
|
||||||
return merged;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
|
|||||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||||
import DateService from "@services/DateService";
|
import DateService from "@services/DateService";
|
||||||
|
import { executeTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService";
|
||||||
|
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
@@ -23,28 +24,16 @@ export default defineEventHandler(async event => {
|
|||||||
key: `timeline:visits:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
key: `timeline:visits:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
|
||||||
exp: TIMELINE_EXPIRE_TIME
|
exp: TIMELINE_EXPIRE_TIME
|
||||||
}, async () => {
|
}, async () => {
|
||||||
|
const timelineData = await executeTimelineAggregation({
|
||||||
const { group, sort, fromParts } = DateService.getQueryDateRange(slice);
|
projectId: project._id,
|
||||||
|
model: VisitModel,
|
||||||
const aggregation = [
|
from, to, slice
|
||||||
{
|
});
|
||||||
$match: {
|
const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
|
||||||
project_id: project._id,
|
return timelineFilledMerged;
|
||||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ $group: { _id: group, count: { $sum: 1 } } },
|
|
||||||
{ $sort: sort },
|
|
||||||
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
|
|
||||||
]
|
|
||||||
|
|
||||||
const timelineVisits: { _id: string, count: number }[] = await VisitModel.aggregate(aggregation);
|
|
||||||
const filledDates = DateService.fillDates(timelineVisits.map(e => e._id), slice);
|
|
||||||
const merged = DateService.mergeFilledDates(filledDates, timelineVisits, '_id', slice, { count: 0 });
|
|
||||||
return merged;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
53
dashboard/server/services/TimelineService.ts
Normal file
53
dashboard/server/services/TimelineService.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
import { Slice } from "@services/DateService";
|
||||||
|
import DateService from "@services/DateService";
|
||||||
|
import type mongoose from "mongoose";
|
||||||
|
|
||||||
|
|
||||||
|
export type TimelineAggregationOptions = {
|
||||||
|
projectId: mongoose.Schema.Types.ObjectId,
|
||||||
|
model: mongoose.Model<any>,
|
||||||
|
from: string | number,
|
||||||
|
to: string | number,
|
||||||
|
slice: Slice
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AdvancedTimelineAggregationOptions = TimelineAggregationOptions & {
|
||||||
|
customMatch?: Record<string, any>
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function executeAdvancedTimelineAggregation(options: AdvancedTimelineAggregationOptions) {
|
||||||
|
|
||||||
|
options.customMatch = options.customMatch || {};
|
||||||
|
|
||||||
|
const { group, sort, fromParts } = DateService.getQueryDateRange(options.slice);
|
||||||
|
|
||||||
|
const aggregation = [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
project_id: options.projectId,
|
||||||
|
created_at: { $gte: new Date(options.from), $lte: new Date(options.to) },
|
||||||
|
...options.customMatch
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ $group: { _id: group, count: { $sum: 1 } } },
|
||||||
|
{ $sort: sort },
|
||||||
|
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
|
||||||
|
]
|
||||||
|
|
||||||
|
const timeline: { _id: string, count: number }[] = await options.model.aggregate(aggregation);
|
||||||
|
|
||||||
|
return timeline;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function executeTimelineAggregation(options: TimelineAggregationOptions) {
|
||||||
|
return executeAdvancedTimelineAggregation(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function fillAndMergeTimelineAggregation(timeline: { _id: string, count: number }[], slice: Slice) {
|
||||||
|
const filledDates = DateService.fillDates(timeline.map(e => e._id), slice);
|
||||||
|
const merged = DateService.mergeFilledDates(filledDates, timeline, '_id', slice, { count: 0 });
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user