mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
change in progress
This commit is contained in:
34
dashboard/server/api/data/browsers.ts
Normal file
34
dashboard/server/api/data/browsers.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { getRequestData } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, { requireSchema: false });
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, project_id, limit } = data;
|
||||
|
||||
const cacheKey = `browsers:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const result = await VisitModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
}
|
||||
},
|
||||
{ $group: { _id: "$browser", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: limit }
|
||||
]);
|
||||
|
||||
return result as { _id: string, count: number }[];
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,57 +1,28 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
|
||||
import type { Model } from "mongoose";
|
||||
|
||||
|
||||
const allowedModels: Record<string, { model: Model<any>, field: string }> = {
|
||||
'events': {
|
||||
model: EventModel,
|
||||
field: 'name'
|
||||
}
|
||||
}
|
||||
|
||||
type TModelName = keyof typeof allowedModels;
|
||||
import { getRequestData } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const project_id = getHeader(event, 'x-pid');
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const from = getRequestHeader(event, 'x-from');
|
||||
const to = getRequestHeader(event, 'x-to');
|
||||
const data = await getRequestData(event);
|
||||
if (!data) return;
|
||||
|
||||
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to are required');
|
||||
const { schemaName, pid, from, to, model, project_id } = data;
|
||||
|
||||
const schemaName = getRequestHeader(event, 'x-schema');
|
||||
if (!schemaName) return setResponseStatus(event, 400, 'x-schema is required');
|
||||
|
||||
if (!Object.keys(allowedModels).includes(schemaName)) return setResponseStatus(event, 400, 'x-schema value is not valid');
|
||||
|
||||
const cacheKey = `count:${schemaName}:${project_id}:${from}:${to}`;
|
||||
const cacheKey = `count:${schemaName}:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async (noStore, updateExp) => {
|
||||
|
||||
const { model } = allowedModels[schemaName as TModelName];
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const result = await model.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
created_at: {
|
||||
$gte: new Date(from),
|
||||
$lte: new Date(to)
|
||||
}
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
}
|
||||
},
|
||||
{
|
||||
$count: 'total'
|
||||
}
|
||||
{ $count: 'count' },
|
||||
]);
|
||||
|
||||
return result;
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
|
||||
import type { Model } from "mongoose";
|
||||
|
||||
|
||||
const allowedModels: Record<string, { model: Model<any>, field: string }> = {
|
||||
'events': {
|
||||
model: EventModel,
|
||||
field: 'name'
|
||||
}
|
||||
}
|
||||
|
||||
type TModelName = keyof typeof allowedModels;
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const project_id = getHeader(event, 'x-pid');
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const from = getRequestHeader(event, 'x-from');
|
||||
const to = getRequestHeader(event, 'x-to');
|
||||
|
||||
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to are required');
|
||||
|
||||
const schemaName = getRequestHeader(event, 'x-schema');
|
||||
if (!schemaName) return setResponseStatus(event, 400, 'x-schema is required');
|
||||
|
||||
if (!Object.keys(allowedModels).includes(schemaName)) return setResponseStatus(event, 400, 'x-schema value is not valid');
|
||||
|
||||
const limitHeader = getRequestHeader(event, 'x-query-limit');
|
||||
const limitNumber = parseInt(limitHeader || '10');
|
||||
const limit = isNaN(limitNumber) ? 10 : limitNumber;
|
||||
|
||||
const cacheKey = `${schemaName}:${project_id}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async (noStore, updateExp) => {
|
||||
|
||||
const { model } = allowedModels[schemaName as TModelName];
|
||||
|
||||
const result = await model.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
created_at: {
|
||||
$gte: new Date(from),
|
||||
$lte: new Date(to)
|
||||
}
|
||||
}
|
||||
},
|
||||
{ $group: { _id: "$name", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: limit }
|
||||
]);
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -7,21 +7,14 @@ import mongoose from "mongoose";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const project_id = getHeader(event, 'x-pid');
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
const data = await getRequestData(event, { requireSchema: false, requireSlice: true });
|
||||
if (!data) return;
|
||||
|
||||
const from = getRequestHeader(event, 'x-from');
|
||||
const to = getRequestHeader(event, 'x-to');
|
||||
const { pid, from, to, slice, project_id } = data;
|
||||
|
||||
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to are required');
|
||||
|
||||
const slice = getRequestHeader(event, 'x-slice');
|
||||
|
||||
const cacheKey = `bouncing_rate:${project_id}:${from}:${to}`;
|
||||
const cacheKey = `timeline:bouncing_rate:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60 * 60; //1 hour
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async (noStore, updateExp) => {
|
||||
@@ -37,14 +30,14 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const allDates = DateService.createBetweenDates(from, to, slice as any);
|
||||
|
||||
const result: { date: string, value: number }[] = [];
|
||||
const result: { _id: string, count: number }[] = [];
|
||||
|
||||
for (const date of allDates.dates) {
|
||||
|
||||
const visits = await VisitModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
project_id: project_id,
|
||||
created_at: {
|
||||
$gte: date.startOf(slice as any).toDate(),
|
||||
$lte: date.endOf(slice as any).toDate()
|
||||
@@ -57,22 +50,30 @@ export default defineEventHandler(async event => {
|
||||
const sessions = await SessionModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
project_id: project_id,
|
||||
created_at: {
|
||||
$gte: date.startOf(slice as any).toDate(),
|
||||
$lte: date.endOf(slice as any).toDate()
|
||||
}
|
||||
},
|
||||
},
|
||||
{ $group: { _id: "$session", count: { $sum: 1, }, duration: { $sum: '$duration' } } },
|
||||
{
|
||||
$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({ date: date.toISOString(), value: bouncing_rate });
|
||||
result.push({
|
||||
_id: date.toISOString(),
|
||||
count: bouncing_rate
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
29
dashboard/server/api/timeline/sessions.ts
Normal file
29
dashboard/server/api/timeline/sessions.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, { requireSchema: false, requireSlice: true });
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id } = data;
|
||||
|
||||
const cacheKey = `timeline:sessions:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: SessionModel,
|
||||
from, to, slice,
|
||||
});
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, slice, from, to);
|
||||
return timelineFilledMerged;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
35
dashboard/server/api/timeline/sessions_duration.ts
Normal file
35
dashboard/server/api/timeline/sessions_duration.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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, { requireSchema: false, requireSlice: true });
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id } = data;
|
||||
|
||||
const cacheKey = `timeline:sessions_duration:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeAdvancedTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: SessionModel,
|
||||
from, to, slice,
|
||||
customGroup: {
|
||||
duration: { $sum: '$duration' }
|
||||
},
|
||||
customProjection: {
|
||||
count: { $divide: ["$duration", "$count"] }
|
||||
},
|
||||
});
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, slice, from, to);
|
||||
return timelineFilledMerged;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
29
dashboard/server/api/timeline/visits.ts
Normal file
29
dashboard/server/api/timeline/visits.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, { requireSchema: false, requireSlice: true });
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id } = data;
|
||||
|
||||
const cacheKey = `timeline:visits:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: VisitModel,
|
||||
from, to, slice,
|
||||
});
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, slice, from, to);
|
||||
return timelineFilledMerged;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
16
dashboard/server/services/DataService.ts
Normal file
16
dashboard/server/services/DataService.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
|
||||
import type { Model } from "mongoose";
|
||||
|
||||
|
||||
export type TModelName = keyof typeof allowedModels;
|
||||
|
||||
export const allowedModels: Record<string, { model: Model<any> }> = {
|
||||
'events': {
|
||||
model: EventModel,
|
||||
},
|
||||
'visits': {
|
||||
model: VisitModel,
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import type mongoose from "mongoose";
|
||||
|
||||
|
||||
export type TimelineAggregationOptions = {
|
||||
projectId: mongoose.Schema.Types.ObjectId,
|
||||
projectId: mongoose.Schema.Types.ObjectId | mongoose.Types.ObjectId,
|
||||
model: mongoose.Model<any>,
|
||||
from: string | number,
|
||||
to: string | number,
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import type { AuthContext } from "../middleware/01-authorization";
|
||||
import type { EventHandlerRequest, H3Event } from 'h3'
|
||||
import { allowedModels, TModelName } from "../services/DataService";
|
||||
import { LITLYX_PROJECT_ID } from "@data/LITLYX";
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import { Model, Types } from "mongoose";
|
||||
import { TeamMemberModel } from "@schema/TeamMemberSchema";
|
||||
import { Slice } from "@services/DateService";
|
||||
|
||||
export function getRequestUser(event: H3Event<EventHandlerRequest>) {
|
||||
if (!event.context.auth) return;
|
||||
@@ -14,4 +20,79 @@ export function getRequestProjectId(event: H3Event<EventHandlerRequest>) {
|
||||
export function getRequestAddress(event: H3Event<EventHandlerRequest>) {
|
||||
if (process.dev) return '127.0.0.1';
|
||||
return event.headers.get('x-real-ip') || event.headers.get('X-Forwarded-For') || '0.0.0.0';
|
||||
}
|
||||
|
||||
|
||||
|
||||
export type GetRequestDataOptions = {
|
||||
allowGuests?: boolean,
|
||||
requireSchema?: boolean,
|
||||
allowLitlyx?: boolean,
|
||||
requireSlice?: boolean
|
||||
}
|
||||
|
||||
async function hasAccessToProject(user_id: string, project: TProject) {
|
||||
if (!project) return [false, 'NONE'];
|
||||
const owner = project.owner.toString();
|
||||
const project_id = project._id;
|
||||
if (owner === user_id) return [true, 'OWNER'];
|
||||
const isGuest = await TeamMemberModel.exists({ project_id, user_id });
|
||||
if (isGuest) return [true, 'GUEST'];
|
||||
return [false, 'NONE'];
|
||||
}
|
||||
|
||||
|
||||
export async function getRequestData(event: H3Event<EventHandlerRequest>, options?: GetRequestDataOptions) {
|
||||
|
||||
const requireSchema = options?.requireSchema || false;
|
||||
const allowGuests = options?.allowGuests || true;
|
||||
const allowLitlyx = options?.allowLitlyx || true;
|
||||
const requireSlice = options?.requireSlice || false;
|
||||
|
||||
const pid = getHeader(event, 'x-pid');
|
||||
if (!pid) return setResponseStatus(event, 400, 'x-pid is required');
|
||||
|
||||
const slice = getHeader(event, 'x-slice') as Slice;
|
||||
if (!slice && requireSlice) return setResponseStatus(event, 400, 'x-slice is required');
|
||||
|
||||
const from = getRequestHeader(event, 'x-from');
|
||||
const to = getRequestHeader(event, 'x-to');
|
||||
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to are required');
|
||||
|
||||
|
||||
let model: (Model<any> | undefined) = undefined;
|
||||
|
||||
const schemaName = getRequestHeader(event, 'x-schema');
|
||||
if (requireSchema) {
|
||||
if (!schemaName) return setResponseStatus(event, 400, 'x-schema is required');
|
||||
if (!Object.keys(allowedModels).includes(schemaName)) return setResponseStatus(event, 400, 'x-schema value is not valid');
|
||||
const allowedModel = allowedModels[schemaName as TModelName];
|
||||
model = allowedModel.model;
|
||||
}
|
||||
|
||||
|
||||
const limitHeader = getRequestHeader(event, 'x-limit');
|
||||
const limitNumber = parseInt(limitHeader as string);
|
||||
const limit = isNaN(limitNumber) ? 10 : limitNumber;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
|
||||
if (!user || !user.logged) return setResponseStatus(event, 403, 'you must be logged');
|
||||
|
||||
const project_id = new Types.ObjectId(pid);
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'project not found');
|
||||
|
||||
|
||||
if (pid !== LITLYX_PROJECT_ID) {
|
||||
const [hasAccess, role] = await hasAccessToProject(user.id, project);
|
||||
if (!hasAccess) return setResponseStatus(event, 400, 'no access to project');
|
||||
if (role === 'GUEST' && !allowGuests) return setResponseStatus(event, 403, 'only owner can access this');
|
||||
} else {
|
||||
if (!allowLitlyx) return setResponseStatus(event, 400, 'no access to project');
|
||||
}
|
||||
|
||||
return { from, to, pid, project_id, project, user, limit, slice, schemaName, model }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user