mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add dashboard
This commit is contained in:
77
dashboard/server/api/metrics/[project_id]/counts.ts
Normal file
77
dashboard/server/api/metrics/[project_id]/counts.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import { COUNTS_EXPIRE_TIME, COUNTS_SESSIONS_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
export type MetricsCounts = {
|
||||
eventsCount: number,
|
||||
visitsCount: number,
|
||||
sessionsVisitsCount: number,
|
||||
firstEventDate: number,
|
||||
firstViewDate: number,
|
||||
avgSessionDuration: number
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `counts:${project_id}`,
|
||||
exp: COUNTS_EXPIRE_TIME
|
||||
}, async () => {
|
||||
|
||||
|
||||
const count: { events: number, visits: number }[] = await ProjectCountModel.aggregate([
|
||||
{ $match: { project_id: project._id } },
|
||||
{
|
||||
$group: {
|
||||
_id: "$project_id",
|
||||
events: { $sum: "$events" },
|
||||
visits: { $sum: "$visits" }
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
|
||||
const sessionsVisitsCount: any[] = await Redis.useCache({
|
||||
key: `counts:${project_id}:sessions_count`,
|
||||
exp: COUNTS_SESSIONS_EXPIRE_TIME
|
||||
}, async () => {
|
||||
return await SessionModel.aggregate([
|
||||
{ $match: { project_id: project._id } },
|
||||
{ $group: { _id: "$session", time: { $sum: '$duration' }, count: { $sum: 1 } } },
|
||||
])
|
||||
});
|
||||
|
||||
const totalSessions = sessionsVisitsCount.length;
|
||||
const totalSessionsTime = sessionsVisitsCount.reduce((a, e) => a + e.time, 0);
|
||||
const avgSessionDuration = totalSessionsTime / totalSessions;
|
||||
|
||||
const year = new Date().getFullYear();
|
||||
const month = new Date().getMonth();
|
||||
|
||||
const firstEventDate = new Date(year, month, 0, 0, 0, 0, 0).getTime();
|
||||
const firstViewDate = new Date(year, month, 0, 0, 0, 0, 0).getTime();
|
||||
|
||||
return {
|
||||
eventsCount: count[0].events,
|
||||
visitsCount: count[0].visits,
|
||||
sessionsVisitsCount: totalSessions + (sessionsVisitsCount?.[0]?.count || 0),
|
||||
avgSessionDuration,
|
||||
firstEventDate,
|
||||
firstViewDate,
|
||||
} as MetricsCounts;
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
43
dashboard/server/api/metrics/[project_id]/data/browsers.ts
Normal file
43
dashboard/server/api/metrics/[project_id]/data/browsers.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type BrowsersAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `browsers:${project_id}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const browsers: BrowsersAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id }, },
|
||||
{ $group: { _id: "$browser", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return browsers;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
41
dashboard/server/api/metrics/[project_id]/data/countries.ts
Normal file
41
dashboard/server/api/metrics/[project_id]/data/countries.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type CountriesAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `countries:${project_id}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const countries: CountriesAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id, country: { $ne: null } }, },
|
||||
{ $group: { _id: "$country", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return countries;
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
41
dashboard/server/api/metrics/[project_id]/data/devices.ts
Normal file
41
dashboard/server/api/metrics/[project_id]/data/devices.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type DevicesAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `devices:${project_id}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const devices: DevicesAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id, device: { $ne: null } }, },
|
||||
{ $group: { _id: "$device", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return devices;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
42
dashboard/server/api/metrics/[project_id]/data/oss.ts
Normal file
42
dashboard/server/api/metrics/[project_id]/data/oss.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type OssAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `oss:${project_id}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const oss: OssAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id }, },
|
||||
{ $group: { _id: "$os", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return oss;
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
46
dashboard/server/api/metrics/[project_id]/data/pages.ts
Normal file
46
dashboard/server/api/metrics/[project_id]/data/pages.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type VisitsPageAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 websiteName = getRequestHeader(event, 'x-website-name');
|
||||
if (!websiteName) return [];
|
||||
const limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `pages:${project_id}:${websiteName}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const pages: VisitsPageAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id }, },
|
||||
{ $match: { website: websiteName, }, },
|
||||
{ $group: { _id: "$page", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return pages;
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
42
dashboard/server/api/metrics/[project_id]/data/referrers.ts
Normal file
42
dashboard/server/api/metrics/[project_id]/data/referrers.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type ReferrersAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `referrers:${project_id}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const referrers: ReferrersAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id }, },
|
||||
{ $group: { _id: "$referrer", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return referrers;
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
40
dashboard/server/api/metrics/[project_id]/data/websites.ts
Normal file
40
dashboard/server/api/metrics/[project_id]/data/websites.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { DATA_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
export type VisitsWebsiteAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 limit = getRequestHeader(event, 'x-query-limit');
|
||||
const numLimit = parseInt(limit || '10');
|
||||
|
||||
return await Redis.useCache({
|
||||
key: `websites:${project_id}:${numLimit}`,
|
||||
exp: DATA_EXPIRE_TIME
|
||||
}, async () => {
|
||||
const websites: VisitsWebsiteAggregated[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id }, },
|
||||
{ $group: { _id: "$website", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: numLimit }
|
||||
]);
|
||||
|
||||
return websites;
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
33
dashboard/server/api/metrics/[project_id]/events/names.ts
Normal file
33
dashboard/server/api/metrics/[project_id]/events/names.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { EVENT_NAMES_EXPIRE_TIME, Redis } from "~/server/services/CacheService";
|
||||
|
||||
|
||||
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 names: string[] = await Redis.useCache({
|
||||
key: `counts:${project_id}:event_names`,
|
||||
exp: EVENT_NAMES_EXPIRE_TIME
|
||||
}, async () => {
|
||||
|
||||
const namesAggregation = await EventModel.aggregate([
|
||||
{ $match: { project_id: project._id } },
|
||||
{ $group: { _id: "$name" } }
|
||||
]);
|
||||
|
||||
return namesAggregation.map(e => e._id);
|
||||
|
||||
});
|
||||
|
||||
return names;
|
||||
|
||||
});
|
||||
36
dashboard/server/api/metrics/[project_id]/events_pie.ts
Normal file
36
dashboard/server/api/metrics/[project_id]/events_pie.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||
|
||||
export type EventsPie = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 await Redis.useCache({
|
||||
key: `events_pie${project_id}`,
|
||||
exp: TIMELINE_EXPIRE_TIME
|
||||
}, async () => {
|
||||
|
||||
const eventsPie: EventsPie[] = await EventModel.aggregate([
|
||||
{ $match: { project_id: project._id } },
|
||||
{ $group: { _id: "$name", count: { $sum: 1 } } }
|
||||
]);
|
||||
|
||||
return eventsPie as EventsPie[];
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
|
||||
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 hasEvent = await EventModel.exists({ project_id: project._id });
|
||||
if (hasEvent) return true;
|
||||
const hasVisit = await VisitModel.exists({ project_id: project._id });
|
||||
if (hasVisit) return true;
|
||||
|
||||
return false;
|
||||
|
||||
});
|
||||
29
dashboard/server/api/metrics/[project_id]/live_users.ts
Normal file
29
dashboard/server/api/metrics/[project_id]/live_users.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Types } from "mongoose";
|
||||
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 online_users = await SessionModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: new Types.ObjectId(project_id),
|
||||
updated_at: { $gt: new Date(Date.now() - 1000 * 60 * 5) }
|
||||
}
|
||||
},
|
||||
{ $count: 'count' }
|
||||
]);
|
||||
|
||||
if (!online_users[0]) return 0;
|
||||
|
||||
return online_users[0].count;
|
||||
|
||||
});
|
||||
36
dashboard/server/api/metrics/[project_id]/query.ts
Normal file
36
dashboard/server/api/metrics/[project_id]/query.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
|
||||
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;
|
||||
const query = getQuery(event);
|
||||
|
||||
const { orderBy, order, page, limit, type } = query;
|
||||
|
||||
const limitValue = limit ? parseInt(limit.toString()) : 20;
|
||||
const skipValue = page ? (parseInt(page.toString()) - 1) * limitValue : 0;
|
||||
|
||||
if (type == '0') {
|
||||
const visits = await VisitModel.find({ project_id: project }, {}, {
|
||||
limit: limitValue,
|
||||
skip: skipValue,
|
||||
sort: { [(orderBy || '').toString()]: order == 'asc' ? 1 : -1 }
|
||||
});
|
||||
return visits;
|
||||
} else {
|
||||
const events = await EventModel.find({ project_id: project }, {}, {
|
||||
limit: limitValue,
|
||||
skip: skipValue,
|
||||
sort: { [(orderBy || '').toString()]: order == 'asc' ? 1 : -1 }
|
||||
});
|
||||
return events;
|
||||
}
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
29
dashboard/server/api/metrics/[project_id]/visits/events.ts
Normal file
29
dashboard/server/api/metrics/[project_id]/visits/events.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
|
||||
export type CustomEventsAggregated = {
|
||||
_id: string,
|
||||
count: number
|
||||
}
|
||||
|
||||
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 websites: CustomEventsAggregated[] = await EventModel.aggregate([
|
||||
{ $match: { project_id: project._id }, },
|
||||
{ $group: { _id: "$name", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } }
|
||||
]);
|
||||
|
||||
return websites;
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user