add dashboard

This commit is contained in:
Litlyx
2024-06-01 15:27:40 +02:00
parent 75f0787c3b
commit df4faf366f
201 changed files with 91267 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import { EventModel } from "@schema/metrics/EventSchema";
import { getTimeline } from "./generic";
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 } = await readBody(event);
return await Redis.useCache({ key: `timeline:events:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
const timelineEvents = await getTimeline(EventModel, project_id, slice, duration);
return timelineEvents;
});
});

View File

@@ -0,0 +1,27 @@
import { EventModel } from "@schema/metrics/EventSchema";
import { getTimeline } from "./generic";
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 } = await readBody(event);
return await Redis.useCache({ key: `timeline:events_stacked:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
const timelineStackedEvents = await getTimeline(EventModel, project_id, slice, duration,
{},
{},
{ name: "$_id.name" },
{ name: '$name' }
);
return timelineStackedEvents;
});
});

View File

@@ -0,0 +1,79 @@
import { AggregateOptions, Model, Types } from "mongoose";
import { ProjectModel } from "@schema/ProjectSchema";
export type MetricsTimeline = {
_id: string,
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 = {}) {
const groupId: any = {};
const sort: any = {};
const fromParts: any = {};
const from = new Date();
const to = new Date();
from.setMinutes(0, 0, 0);
to.setMinutes(0, 0, 0);
switch (slice) {
case 'day':
from.setDate(from.getDate() - (duration || 7));
from.setHours(0);
to.setHours(0);
break;
case 'hour':
from.setHours(from.getHours() - (duration || 24));
break;
}
switch (slice) {
case 'hour':
groupId.hour = { $hour: '$created_at' }
sort['_id.hour'] = 1;
fromParts.hour = "$_id.hour";
case 'day':
groupId.day = { $dayOfMonth: '$created_at' }
sort['_id.day'] = 1;
fromParts.day = "$_id.day";
case 'month':
groupId.month = { $month: '$created_at' }
sort['_id.month'] = 1;
fromParts.month = "$_id.month";
case 'year':
groupId.year = { $year: '$created_at' }
sort['_id.year'] = 1;
fromParts.year = "$_id.year";
}
const aggregation: any[] = [
{
$match: {
project_id: new Types.ObjectId(project_id),
created_at: { $gte: from, $lte: to }
}
},
{ $group: { _id: { ...groupId, ...customGroupId }, count: { $sum: 1 }, ...customGroup } },
{ $sort: sort },
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count", ...customProjection } }
]
const result: MetricsTimeline[] = await model.aggregate(aggregation, customOptions);
return { data: result, from, to };
}
export default defineEventHandler(async event => {
const user = getRequestUser(event);
if (!user?.logged) return;
const project_id = getRequestProjectId(event);
if (!project_id) return;
const project = await ProjectModel.findOne({ _id: project_id, owner: user.id });
if (!project) return;
return;
});

View File

@@ -0,0 +1,24 @@
import { getTimeline } from "./generic";
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import { SessionModel } from "@schema/metrics/SessionSchema";
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 } = await readBody(event);
return await Redis.useCache({ key: `timeline:sessions:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
const timelineSessions = await getTimeline(SessionModel, project_id, slice, duration);
return timelineSessions;
});
});

View File

@@ -0,0 +1,26 @@
import { getTimeline } from "./generic";
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import { SessionModel } from "@schema/metrics/SessionSchema";
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 } = await readBody(event);
return await Redis.useCache({ key: `timeline:sessions_duration:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
const timelineSessionsDuration = await getTimeline(SessionModel, project_id, slice, duration, {},
{ duration: { $sum: '$duration' } },
{ count: { $divide: ["$duration", "$count"] } }
);
return timelineSessionsDuration;
});
});

View File

@@ -0,0 +1,24 @@
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 } = await readBody(event);
return await Redis.useCache({ key: `timeline:visits:${project_id}:${slice}`, exp: TIMELINE_EXPIRE_TIME }, async () => {
const timelineVisits = await getTimeline(VisitModel, project_id, slice, duration);
return timelineVisits;
});
});