mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 00:08:37 +01:00
new selfhosted version
This commit is contained in:
35
dashboard/server/api/share/live_users.ts
Normal file
35
dashboard/server/api/share/live_users.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import dayjs from "dayjs";
|
||||
import { VisitModel } from "~/shared/schema/metrics/VisitSchema";
|
||||
import { ProjectShareModel } from "~/shared/schema/project/ProjectShareSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const ctx = await getRequestContext(event, 'pid', 'flag:allowShare');
|
||||
const { linkId } = getQuery(event);
|
||||
if (!linkId) return;
|
||||
|
||||
const target = await ProjectShareModel.findOne({ link: linkId.toString() });
|
||||
if (!target) return;
|
||||
|
||||
const timespan = dayjs(Date.now() - 1000 * 60 * 5).utc().toDate()
|
||||
|
||||
const live_users = await VisitModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: target._id,
|
||||
created_at: { $gte: timespan }
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$session',
|
||||
session: { $sum: 1 }
|
||||
}
|
||||
},
|
||||
{
|
||||
$count: 'sessions'
|
||||
}
|
||||
]);
|
||||
|
||||
return live_users[0]?.sessions || 0 as number;
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user