add referrers bar chart

This commit is contained in:
Emily
2024-06-10 17:04:12 +02:00
parent 0c3a25b7e0
commit 7cc7a3ab8d
13 changed files with 247 additions and 18 deletions

View File

@@ -7,7 +7,7 @@ export type MetricsTimeline = {
count: number
}
export async function getTimeline(model: Model<any>, project_id: string, slice: 'hour' | 'day' | 'month' | 'year' = 'day', duration?: number, customOptions: AggregateOptions = {}, customGroup: Object = {}, customProjection: Object = {}, customGroupId: Object = {}) {
export async function getTimeline(model: Model<any>, project_id: string, slice: 'hour' | 'day' | 'month' | 'year' = 'day', duration?: number, customOptions: AggregateOptions = {}, customGroup: Object = {}, customProjection: Object = {}, customGroupId: Object = {}, customMatch: Object = {}) {
const groupId: any = {};
const sort: any = {};
@@ -53,7 +53,8 @@ export async function getTimeline(model: Model<any>, project_id: string, slice:
{
$match: {
project_id: new Types.ObjectId(project_id),
created_at: { $gte: from, $lte: to }
created_at: { $gte: from, $lte: to },
...customMatch
}
},
{ $group: { _id: { ...groupId, ...customGroupId }, count: { $sum: 1 }, ...customGroup } },
@@ -61,6 +62,7 @@ export async function getTimeline(model: Model<any>, project_id: string, slice:
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count", ...customProjection } }
]
const result: MetricsTimeline[] = await model.aggregate(aggregation, customOptions);
return { data: result, from, to };

View File

@@ -0,0 +1,30 @@
import { getTimeline } from "./generic";
import { VisitModel } from "@schema/metrics/VisitSchema";
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
if (!project_id) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const { slice, duration, referrer } = await readBody(event);
// return await Redis.useCache({ key: `timeline:referrers:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
const timelineReferrers = await getTimeline(VisitModel, project_id, slice, duration,
{},
{},
{ referrer: "$_id.referrer" },
{ referrer: "$referrer" },
{ referrer }
);
return timelineReferrers;
// });
});