mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 00:08:37 +01:00
add dashboard
This commit is contained in:
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;
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user