mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add dashboard
This commit is contained in:
31
dashboard/server/AuthManager.ts
Normal file
31
dashboard/server/AuthManager.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
const { AUTH_JWT_SECRET } = useRuntimeConfig();
|
||||
|
||||
function createJwt(data: Object) {
|
||||
return jwt.sign(data, AUTH_JWT_SECRET, { expiresIn: '30d' });
|
||||
}
|
||||
|
||||
function readJwt(data: string) {
|
||||
try {
|
||||
return jwt.verify(data, AUTH_JWT_SECRET);
|
||||
} catch (ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export type TUserJwt = {
|
||||
email: string,
|
||||
name: string
|
||||
}
|
||||
|
||||
export function readUserJwt(raw: string) {
|
||||
const data = readJwt(raw);
|
||||
return data as TUserJwt | undefined;
|
||||
}
|
||||
|
||||
export function createUserJwt(data: TUserJwt) {
|
||||
return createJwt(data);
|
||||
}
|
||||
16
dashboard/server/LIVE_DEMO_DATA.ts
Normal file
16
dashboard/server/LIVE_DEMO_DATA.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { AuthContext } from "./middleware/01-authorization";
|
||||
import { ProjectModel } from "~/../shared/schema/ProjectSchema";
|
||||
import { LITLYX_PROJECT_ID } from '@data/LITLYX'
|
||||
|
||||
export async function getUserProjectFromId(project_id: string, user: AuthContext | undefined) {
|
||||
if (project_id == LITLYX_PROJECT_ID) {
|
||||
const project = await ProjectModel.findOne({ _id: project_id });
|
||||
return project;
|
||||
} else {
|
||||
if (!user?.logged) return;
|
||||
const project = await ProjectModel.findOne({ _id: project_id, owner: user.id });
|
||||
return project;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
107
dashboard/server/api/admin/projects.ts
Normal file
107
dashboard/server/api/admin/projects.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
|
||||
export type AdminProjectsList = {
|
||||
premium: boolean,
|
||||
created_at: Date,
|
||||
project_name: string,
|
||||
user: {
|
||||
name: string,
|
||||
email: string,
|
||||
given_name: string,
|
||||
picture: string,
|
||||
created_at: Date
|
||||
},
|
||||
total_visits: number,
|
||||
total_events: number
|
||||
}
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return;
|
||||
if (!userData.user.roles.includes('ADMIN')) return;
|
||||
|
||||
const data: AdminProjectsList[] = await ProjectModel.aggregate([
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "owner",
|
||||
foreignField: "_id",
|
||||
as: "user"
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "visits",
|
||||
let: { projectId: "$_id" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$eq: ["$project_id", "$$projectId"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$count: "total_visits"
|
||||
}
|
||||
],
|
||||
as: "visits"
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "events",
|
||||
let: { projectId: "$_id" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$eq: ["$project_id", "$$projectId"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$count: "total_events"
|
||||
}
|
||||
],
|
||||
as: "events"
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
project_name: "$name",
|
||||
premium: 1,
|
||||
created_at: 1,
|
||||
user: {
|
||||
$first: "$user"
|
||||
},
|
||||
total_visits: {
|
||||
$ifNull: [
|
||||
{
|
||||
$arrayElemAt: [
|
||||
"$visits.total_visits",
|
||||
0
|
||||
]
|
||||
},
|
||||
0
|
||||
]
|
||||
},
|
||||
total_events: {
|
||||
$ifNull: [
|
||||
{
|
||||
$arrayElemAt: [
|
||||
"$events.total_events",
|
||||
0
|
||||
]
|
||||
},
|
||||
0
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
return data;
|
||||
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||
import { sendMessageOnChat } from "~/server/services/AiService";
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
if (!event.context.params) return;
|
||||
const chat_id = event.context.params['chat_id'];
|
||||
|
||||
const chat = await AiChatModel.findOne({ _id: chat_id, project_id });
|
||||
if (!chat) return;
|
||||
|
||||
const messages = chat.messages.filter(e => {
|
||||
return (e.role == 'user' || (e.role == 'assistant' && e.content != undefined))
|
||||
}).map(e => {
|
||||
return { role: e.role, content: e.content }
|
||||
});
|
||||
|
||||
return messages;
|
||||
});
|
||||
17
dashboard/server/api/ai/[project_id]/chats_list.ts
Normal file
17
dashboard/server/api/ai/[project_id]/chats_list.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||
|
||||
|
||||
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 chatList = await AiChatModel.find({ project_id }, { _id: 1, title: 1 }, { sort: { updated_at: 1 } });
|
||||
|
||||
return chatList.map(e => e.toJSON());
|
||||
|
||||
});
|
||||
24
dashboard/server/api/ai/[project_id]/chats_remaining.ts
Normal file
24
dashboard/server/api/ai/[project_id]/chats_remaining.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
|
||||
import { checkProjectCount } from '@functions/UtilsProjectCounts';
|
||||
|
||||
export async function getAiChatRemainings(project_id: string) {
|
||||
const counts = await checkProjectCount(project_id)
|
||||
if (!counts) return 0;
|
||||
const chatsRemaining = counts.ai_limit - counts.ai_messages;
|
||||
if (isNaN(chatsRemaining)) return 0;
|
||||
return chatsRemaining;
|
||||
}
|
||||
|
||||
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 chatsRemaining = await getAiChatRemainings(project_id);
|
||||
return chatsRemaining;
|
||||
});
|
||||
27
dashboard/server/api/ai/[project_id]/send_message.post.ts
Normal file
27
dashboard/server/api/ai/[project_id]/send_message.post.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { sendMessageOnChat } from "~/server/services/AiService";
|
||||
import { getAiChatRemainings } from "./chats_remaining";
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
// if (!user?.logged) return;
|
||||
// if (!user.user.roles.includes('ADMIN')) return;
|
||||
|
||||
const { text, chat_id } = await readBody(event);
|
||||
if (!text) return setResponseStatus(event, 400, 'text parameter missing');
|
||||
|
||||
const chatsRemaining = await getAiChatRemainings(project_id);
|
||||
if (chatsRemaining <= 0) return setResponseStatus(event, 400, 'CHAT_LIMIT_REACHED');
|
||||
|
||||
const response = await sendMessageOnChat(text, project._id.toString(), chat_id);
|
||||
return response || 'Error getting response';
|
||||
});
|
||||
51
dashboard/server/api/ai/functions/AI_Events.ts
Normal file
51
dashboard/server/api/ai/functions/AI_Events.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
import OpenAI from "openai";
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
|
||||
|
||||
export const AI_EventsFunctions = {
|
||||
getEventsCount: ({ pid, from, to, name, metadata }: any) => {
|
||||
return getEventsCountForAI(pid, from, to, name, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const getEventsCountForAIDeclaration: OpenAI.Chat.Completions.ChatCompletionTool = {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'getEventsCount',
|
||||
description: 'Gets the number of events received on a date range, can also specify the event name and the metadata associated',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
from: { type: 'string', description: 'ISO string of start date including hours' },
|
||||
to: { type: 'string', description: 'ISO string of end date including hours' },
|
||||
name: { type: 'string', description: 'Name of the events to get' },
|
||||
metadata: { type: 'object', description: 'Metadata of events to get' },
|
||||
},
|
||||
required: ['from', 'to']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const AI_EventsTools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
|
||||
getEventsCountForAIDeclaration
|
||||
]
|
||||
|
||||
export async function getEventsCountForAI(project_id: string, from?: string, to?: string, name?: string, metadata?: string) {
|
||||
|
||||
const query: any = {
|
||||
project_id,
|
||||
created_at: {
|
||||
$gt: from ? new Date(from).getTime() : new Date(2023).getTime(),
|
||||
$lt: to ? new Date(to).getTime() : new Date().getTime(),
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata) query.metadata = metadata;
|
||||
if (name) query.name = name;
|
||||
|
||||
const result = await EventModel.countDocuments(query);
|
||||
|
||||
return { count: result };
|
||||
}
|
||||
14
dashboard/server/api/ai/functions/AI_Visits.ts
Normal file
14
dashboard/server/api/ai/functions/AI_Visits.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
|
||||
|
||||
|
||||
export async function getVisitsCountFromDateRange(project_id: string, from?: string, to?: string) {
|
||||
const result = await VisitModel.countDocuments({
|
||||
project_id,
|
||||
created_at: {
|
||||
$gt: from ? new Date(from).getTime() : new Date(2023).getTime(),
|
||||
$lt: to ? new Date(to).getTime() : new Date().getTime(),
|
||||
}
|
||||
});
|
||||
return { count: result };
|
||||
}
|
||||
55
dashboard/server/api/auth/google_login.post.ts
Normal file
55
dashboard/server/api/auth/google_login.post.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
import { createUserJwt } from '~/server/AuthManager';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
import { sendWelcomeEmail } from '@services/EmailService';
|
||||
|
||||
const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
|
||||
|
||||
const client = new OAuth2Client({
|
||||
clientId: GOOGLE_AUTH_CLIENT_ID,
|
||||
clientSecret: GOOGLE_AUTH_CLIENT_SECRET
|
||||
});
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const body = await readBody(event)
|
||||
|
||||
const origin = event.headers.get('origin');
|
||||
|
||||
const tokenResponse = await client.getToken({
|
||||
code: body.code,
|
||||
redirect_uri: origin || ''
|
||||
});
|
||||
|
||||
const tokens = tokenResponse.tokens;
|
||||
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken: tokens.id_token || '',
|
||||
audience: GOOGLE_AUTH_CLIENT_ID,
|
||||
});
|
||||
|
||||
const payload = ticket.getPayload();
|
||||
if (!payload) return { error: true, access_token: '' };
|
||||
|
||||
|
||||
const user = await UserModel.findOne({ email: payload.email });
|
||||
if (user) return { error: false, access_token: createUserJwt({ email: user.email, name: user.name }) }
|
||||
|
||||
const newUser = new UserModel({
|
||||
email: payload.email,
|
||||
given_name: payload.given_name,
|
||||
name: payload.name,
|
||||
locale: payload.locale,
|
||||
picture: payload.picture,
|
||||
created_at: Date.now()
|
||||
});
|
||||
|
||||
const savedUser = await newUser.save();
|
||||
|
||||
setImmediate(() => {
|
||||
if (payload.email) sendWelcomeEmail(payload.email);
|
||||
});
|
||||
|
||||
return { error: false, access_token: createUserJwt({ email: savedUser.email, name: savedUser.name }) }
|
||||
|
||||
});
|
||||
7
dashboard/server/api/live_demo/index.ts
Normal file
7
dashboard/server/api/live_demo/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const liveDemoProject = await ProjectModel.findById('6643cd08a1854e3b81722ab5');
|
||||
if (!liveDemoProject) return;
|
||||
return liveDemoProject.toJSON() as TProject;
|
||||
});
|
||||
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;
|
||||
|
||||
|
||||
});
|
||||
6
dashboard/server/api/metrics/test.ts
Normal file
6
dashboard/server/api/metrics/test.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
console.log('TEST');
|
||||
return;
|
||||
});
|
||||
23
dashboard/server/api/project/create.post.ts
Normal file
23
dashboard/server/api/project/create.post.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const body = await readBody(event);
|
||||
|
||||
const newProjectName = body.name;
|
||||
|
||||
if (!newProjectName) return setResponseStatus(event, 400, 'ProjectName too short');
|
||||
if (newProjectName.length < 2) return setResponseStatus(event, 400, 'ProjectName too short');
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const existingUserProjects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (existingUserProjects == 3) return setResponseStatus(event, 400, 'Already have 3 projects');
|
||||
|
||||
const newProject = new ProjectModel({ owner: userData.id, name: newProjectName });
|
||||
const saved = await newProject.save();
|
||||
|
||||
return saved.toJSON() as TProject;
|
||||
|
||||
});
|
||||
18
dashboard/server/api/project/delete.delete.ts
Normal file
18
dashboard/server/api/project/delete.delete.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const body = await readBody(event);
|
||||
|
||||
const projectId = body.project_id;
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const projects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project');
|
||||
|
||||
const deletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
return { ok: deletation.acknowledged };
|
||||
|
||||
});
|
||||
36
dashboard/server/api/project/generate_csv.ts
Normal file
36
dashboard/server/api/project/generate_csv.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
import { EventModel } from '@schema/metrics/EventSchema';
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const currentActiveProject = await UserSettingsModel.findOne({ user_id: userData.id });
|
||||
if (!currentActiveProject) return setResponseStatus(event, 400, 'You need to select a project');
|
||||
|
||||
const project_id = currentActiveProject.active_project_id;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not found');
|
||||
|
||||
|
||||
const eventsReportData = await EventModel.find({
|
||||
project_id: project._id,
|
||||
created_at: { $gt: Date.now() - 1000 * 60 * 60 * 24 * 7 + 30 }
|
||||
});
|
||||
|
||||
const csvLines: string[][] = [['name']];
|
||||
eventsReportData.forEach(e => {
|
||||
csvLines.push([e.name])
|
||||
});
|
||||
|
||||
return csvLines.map(row => {
|
||||
return row.join(',');
|
||||
}).join('\n');
|
||||
|
||||
|
||||
});
|
||||
164
dashboard/server/api/project/generate_pdf.ts
Normal file
164
dashboard/server/api/project/generate_pdf.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
|
||||
import pdfkit from 'pdfkit';
|
||||
|
||||
import { PassThrough } from 'node:stream';
|
||||
import fs from 'fs';
|
||||
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
import { VisitModel } from '@schema/metrics/VisitSchema';
|
||||
import { EventModel } from '@schema/metrics/EventSchema';
|
||||
|
||||
|
||||
type PDF_Data = {
|
||||
pageVisits: number, customEvents: number,
|
||||
visitsDay: number, eventsDay: number, visitsSessions: number,
|
||||
visitsSessionsDay: number
|
||||
}
|
||||
|
||||
function formatNumberK(value: string | number, decimals: number = 1) {
|
||||
const num = parseInt(value.toString());
|
||||
|
||||
if (num > 1_000_000) return (num / 1_000_000).toFixed(decimals) + ' M';
|
||||
if (num > 1_000) return (num / 1_000).toFixed(decimals) + ' K';
|
||||
return num.toFixed();
|
||||
|
||||
}
|
||||
|
||||
function createPdf(projectName: string, data: PDF_Data) {
|
||||
const pdf = new pdfkit({
|
||||
size: 'A4',
|
||||
margins: { top: 50, bottom: 50, left: 50, right: 50 },
|
||||
});
|
||||
|
||||
pdf.pipe(fs.createWriteStream('out.pdf'));
|
||||
|
||||
// Set up fonts and colors
|
||||
pdf
|
||||
.fillColor('#ffffff')
|
||||
.rect(0, 0, pdf.page.width, pdf.page.height)
|
||||
.fill('#000000');
|
||||
|
||||
// Title
|
||||
pdf
|
||||
.font('pdf_fonts/Poppins-Bold.ttf')
|
||||
.fontSize(26)
|
||||
.fillColor('#ffffff')
|
||||
.text(`Report of: ${projectName}`, 50, 50);
|
||||
|
||||
// Section 1
|
||||
pdf
|
||||
.font('pdf_fonts/Poppins-SemiBold.ttf')
|
||||
.fontSize(20)
|
||||
.fillColor('#ffffff')
|
||||
.text('-> This month has seen a lot of visits!', 50, 120);
|
||||
|
||||
pdf
|
||||
.image('pdf_images/d.png', 50, 160, { width: 300 })
|
||||
.font('pdf_fonts/Poppins-Bold.ttf')
|
||||
.fontSize(28)
|
||||
.fillColor('#ffffff')
|
||||
.text(`${formatNumberK(data.pageVisits, 2)}`, 400, 180)
|
||||
.text('WOW!', 400, 210);
|
||||
|
||||
// Section 2
|
||||
pdf
|
||||
.font('pdf_fonts/Poppins-SemiBold.ttf')
|
||||
.fontSize(20)
|
||||
.fillColor('#ffffff')
|
||||
.text('-> There are also many recorded events!', 50, 350);
|
||||
|
||||
pdf
|
||||
.image('pdf_images/c.png', 50, 390, { width: 300 })
|
||||
.font('pdf_fonts/Poppins-Bold.ttf')
|
||||
.fontSize(28)
|
||||
.fillColor('#ffffff')
|
||||
.text(`${formatNumberK(data.customEvents, 2)}`, 400, 420)
|
||||
.text('Let\'s go!', 400, 450);
|
||||
|
||||
// Final section
|
||||
pdf
|
||||
.font('pdf_fonts/Poppins-SemiBold.ttf')
|
||||
.fontSize(20)
|
||||
.fillColor('#ffffff')
|
||||
.text('This report is not final, it only serves to demonstrate the potential of this tool. LitLyx will improve soon! Stay tuned!', 50, 600);
|
||||
|
||||
pdf
|
||||
.font('pdf_fonts/Poppins-Regular.ttf')
|
||||
.fontSize(14)
|
||||
.fillColor('#ffffff')
|
||||
.text('Generated on litlyx.com', 50, 760);
|
||||
pdf
|
||||
.image('pdf_images/logo.png', 460, 700, { width: 100 }) // replace with the correct path to your Unsplash image
|
||||
|
||||
// End PDF creation and save to file
|
||||
pdf.end();
|
||||
return pdf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const currentActiveProject = await UserSettingsModel.findOne({ user_id: userData.id });
|
||||
if (!currentActiveProject) return setResponseStatus(event, 400, 'You need to select a project');
|
||||
|
||||
const project_id = currentActiveProject.active_project_id;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not found');
|
||||
|
||||
|
||||
|
||||
|
||||
const eventsCount = await EventModel.countDocuments({ project_id: project._id });
|
||||
const visitsCount = await VisitModel.countDocuments({ project_id: project._id });
|
||||
|
||||
const sessionsVisitsCount: any[] = await VisitModel.aggregate([
|
||||
{ $match: { project_id: project._id } },
|
||||
{ $group: { _id: "$session" } },
|
||||
{ $count: "count" }
|
||||
]);
|
||||
|
||||
const firstEventDate = await EventModel.findOne({ project_id: project._id }, { created_at: 1 }, { sort: { created_at: 1 } });
|
||||
const firstViewDate = await VisitModel.findOne({ project_id: project._id }, { created_at: 1 }, { sort: { created_at: 1 } });
|
||||
|
||||
|
||||
const avgEventsDay = () => {
|
||||
const days = (Date.now() - (firstEventDate?.created_at.getTime() || 0)) / 1000 / 60 / 60 / 24;
|
||||
const avg = eventsCount / Math.max(days, 1);
|
||||
return avg;
|
||||
};
|
||||
|
||||
const avgVisitDay = () => {
|
||||
const days = (Date.now() - (firstViewDate?.created_at.getTime() || 0)) / 1000 / 60 / 60 / 24;
|
||||
const avg = visitsCount / Math.max(days, 1);
|
||||
return avg;
|
||||
};
|
||||
|
||||
const avgVisitsSessionsDay = () => {
|
||||
const days = (Date.now() - (firstViewDate?.created_at.getTime() || 0)) / 1000 / 60 / 60 / 24;
|
||||
const avg = sessionsVisitsCount[0].count / Math.max(days, 1);
|
||||
return avg;
|
||||
};
|
||||
|
||||
const pdf = createPdf(
|
||||
project.name, {
|
||||
customEvents: eventsCount,
|
||||
eventsDay: avgEventsDay(),
|
||||
pageVisits: visitsCount,
|
||||
visitsDay: avgVisitDay(),
|
||||
visitsSessions: sessionsVisitsCount[0].count,
|
||||
visitsSessionsDay: avgVisitsSessionsDay()
|
||||
});
|
||||
|
||||
const passThrough = new PassThrough();
|
||||
pdf.pipe(passThrough);
|
||||
await sendStream(event, passThrough);
|
||||
});
|
||||
12
dashboard/server/api/project/list.ts
Normal file
12
dashboard/server/api/project/list.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return [];
|
||||
|
||||
|
||||
const userProjects = await ProjectModel.find({ owner: userData.id });
|
||||
return userProjects.map(e => e.toJSON()) as TProject[];
|
||||
|
||||
});
|
||||
43
dashboard/server/api/project/plan.ts
Normal file
43
dashboard/server/api/project/plan.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
|
||||
const { BROKER_UPDATE_EXPIRE_TIME_PATH } = useRuntimeConfig();
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const currentActiveProject = await UserSettingsModel.findOne({ user_id: userData.id });
|
||||
if (!currentActiveProject) return setResponseStatus(event, 400, 'You need to select a project');
|
||||
|
||||
const project_id = currentActiveProject.active_project_id;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not found');
|
||||
|
||||
|
||||
let projectCounts = await ProjectCountModel.findOne({ project_id }, {}, {
|
||||
sort: { billing_expire_at: -1 }
|
||||
});
|
||||
|
||||
if (!projectCounts || Date.now() > new Date(projectCounts.billing_expire_at).getTime()) {
|
||||
await fetch(BROKER_UPDATE_EXPIRE_TIME_PATH + project._id.toString());
|
||||
projectCounts = await ProjectCountModel.findOne({ project_id }, {}, { sort: { billing_expire_at: -1 } });
|
||||
}
|
||||
|
||||
if (!projectCounts) return setResponseStatus(event, 400, 'Project counts not found');
|
||||
|
||||
const result = {
|
||||
premium: project.premium,
|
||||
premium_type: project.premium_type,
|
||||
billing_start_at: projectCounts.billing_start_at,
|
||||
billing_expire_at: projectCounts.billing_expire_at,
|
||||
limit: projectCounts.limit,
|
||||
count: projectCounts.events + projectCounts.visits,
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
19
dashboard/server/api/user/active_project.ts
Normal file
19
dashboard/server/api/user/active_project.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { AuthContext, AuthContextLogged } from "~/server/middleware/01-authorization";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return;
|
||||
|
||||
const userSettings = await UserSettingsModel.findOne({ user_id: userData.id }, {
|
||||
active_project_id: 1
|
||||
});
|
||||
|
||||
if (!userSettings) {
|
||||
const projectOwned = await ProjectModel.findOne({ owner: userData.id }, { _id: 1 });
|
||||
return projectOwned?._id.toString();
|
||||
}
|
||||
return userSettings?.active_project_id.toString();
|
||||
});
|
||||
9
dashboard/server/api/user/is_first_time.ts
Normal file
9
dashboard/server/api/user/is_first_time.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { AuthContext } from "~/server/middleware/01-authorization";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const userData: AuthContext = getRequestUser(event) as any;
|
||||
if (!userData.logged) return;
|
||||
const userProjects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
return userProjects == 0;
|
||||
});
|
||||
6
dashboard/server/api/user/me.ts
Normal file
6
dashboard/server/api/user/me.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { AuthContext } from "~/server/middleware/01-authorization";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const userData: AuthContext = getRequestUser(event) as any;
|
||||
return userData;
|
||||
});
|
||||
23
dashboard/server/api/user/set_active_project.ts
Normal file
23
dashboard/server/api/user/set_active_project.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
if (!event.context) return;
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return;
|
||||
|
||||
const { project_id } = getQuery(event);
|
||||
|
||||
const hasAccess = await ProjectModel.exists({ owner: userData.id, _id: project_id });
|
||||
|
||||
if (!hasAccess) return setResponseStatus(event, 400, 'No access to project');
|
||||
|
||||
await UserSettingsModel.updateOne({ user_id: userData.id }, { active_project_id: project_id }, { upsert: true });
|
||||
|
||||
return;
|
||||
|
||||
});
|
||||
22
dashboard/server/init.ts
Normal file
22
dashboard/server/init.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import mongoose from "mongoose";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
const config = useRuntimeConfig();
|
||||
let connection: mongoose.Mongoose;
|
||||
|
||||
export default async () => {
|
||||
|
||||
console.log('[SERVER] Initializing');
|
||||
|
||||
if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) {
|
||||
console.log('[DATABASE] Connecting');
|
||||
connection = await mongoose.connect(config.MONGO_CONNECTION_STRING);
|
||||
console.log('[DATABASE] Connected');
|
||||
}
|
||||
|
||||
console.log('[REDIS] Connecting');
|
||||
await Redis.init();
|
||||
console.log('[REDIS] Connected');
|
||||
|
||||
console.log('[SERVER] Completed');
|
||||
|
||||
};
|
||||
71
dashboard/server/middleware/01-authorization.ts
Normal file
71
dashboard/server/middleware/01-authorization.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
|
||||
import { readUserJwt } from "../AuthManager";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
import { ADMIN_EMAILS } from '@data/ADMINS';
|
||||
|
||||
import type { H3Event, EventHandlerRequest } from 'h3';
|
||||
|
||||
export type AuthContextLogged = {
|
||||
id: string,
|
||||
logged: true,
|
||||
user: {
|
||||
email: string,
|
||||
name: string,
|
||||
roles: string[],
|
||||
picture?: string,
|
||||
}
|
||||
}
|
||||
|
||||
export type AuthContext = { logged: false } | AuthContextLogged;
|
||||
|
||||
|
||||
|
||||
async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
|
||||
const authorization = event.headers.get('Authorization');
|
||||
|
||||
if (!authorization) {
|
||||
event.context.auth = { logged: false, }
|
||||
} else {
|
||||
|
||||
const [type, token] = authorization.split(' ');
|
||||
const valid = readUserJwt(token);
|
||||
|
||||
if (!valid) return event.context.auth = { logged: false }
|
||||
|
||||
const user = await UserModel.findOne({ email: valid.email })
|
||||
|
||||
if (!user) return event.context.auth = { logged: false };
|
||||
|
||||
const premium: any = null;//await PremiumModel.findOne({ user_id: user.id });
|
||||
|
||||
const roles: string[] = [];
|
||||
|
||||
if (premium && premium.ends_at.getTime() < Date.now()) {
|
||||
// await PremiumModel.deleteOne({ user_id: user.id });
|
||||
} else if (premium) {
|
||||
roles.push('PREMIUM');
|
||||
roles.push('PREMIUM_' + premium.type);
|
||||
}
|
||||
|
||||
if (ADMIN_EMAILS.includes(user.email)) {
|
||||
roles.push('ADMIN');
|
||||
}
|
||||
|
||||
const authContext: AuthContext = {
|
||||
logged: true,
|
||||
user: {
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
picture: user.picture || `https://robohash.org/${user.email}?set=set4`,
|
||||
roles
|
||||
},
|
||||
id: user._id.toString()
|
||||
}
|
||||
event.context.auth = authContext;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await authorizationMiddleware(event);
|
||||
})
|
||||
144
dashboard/server/services/AiService.ts
Normal file
144
dashboard/server/services/AiService.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
|
||||
import { getVisitsCountFromDateRange } from '~/server/api/ai/functions/AI_Visits';
|
||||
|
||||
import OpenAI from "openai";
|
||||
import { AiChatModel } from '@schema/ai/AiChatSchema';
|
||||
import { AI_EventsFunctions, AI_EventsTools } from '../api/ai/functions/AI_Events';
|
||||
import { ProjectCountModel } from '@schema/ProjectsCounts';
|
||||
import { getCurrentProjectCountId } from '@functions/UtilsProjectCounts';
|
||||
|
||||
const { AI_ORG, AI_PROJECT, AI_KEY } = useRuntimeConfig();
|
||||
|
||||
const openai = new OpenAI({
|
||||
organization: AI_ORG,
|
||||
project: AI_PROJECT,
|
||||
apiKey: AI_KEY
|
||||
});
|
||||
|
||||
|
||||
// const get_current_date: OpenAI.Chat.Completions.ChatCompletionTool = {
|
||||
// type: 'function',
|
||||
// function: {
|
||||
// name: 'get_current_date',
|
||||
// description: 'Gets the current date as ISO string',
|
||||
// }
|
||||
// }
|
||||
|
||||
const get_visits_count_Schema: OpenAI.Chat.Completions.ChatCompletionTool = {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'get_visits_count',
|
||||
description: 'Gets the number of visits received on a date range',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
from: { type: 'string', description: 'ISO string of start date including hours' },
|
||||
to: { type: 'string', description: 'ISO string of end date including hours' }
|
||||
},
|
||||
required: ['from', 'to']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const tools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
|
||||
get_visits_count_Schema,
|
||||
...AI_EventsTools
|
||||
]
|
||||
|
||||
|
||||
const functions: any = {
|
||||
get_current_date: async ({ }) => {
|
||||
return new Date().toISOString();
|
||||
},
|
||||
get_visits_count: async ({ pid, from, to }: any) => {
|
||||
return await getVisitsCountFromDateRange(pid, from, to);
|
||||
},
|
||||
...AI_EventsFunctions
|
||||
}
|
||||
|
||||
|
||||
async function getMessagesFromChatId(chat_id?: string) {
|
||||
if (!chat_id) return;
|
||||
const chatItem = await AiChatModel.findById(chat_id);
|
||||
if (!chatItem) return;
|
||||
return chatItem.messages;
|
||||
}
|
||||
|
||||
async function addMessageToChat(message: any, chat_id?: string) {
|
||||
if (!chat_id) return;
|
||||
await AiChatModel.updateOne({ _id: chat_id }, { $push: { messages: message } });
|
||||
}
|
||||
|
||||
async function createChatIfNotExist(pid: string, chat_id?: string) {
|
||||
const chatItem = await AiChatModel.exists({ _id: chat_id });
|
||||
if (chatItem) return chatItem._id.toString();
|
||||
const newChatItem = await AiChatModel.create({ messages: [], project_id: pid, title: 'new chat' });
|
||||
return newChatItem._id.toString();
|
||||
}
|
||||
|
||||
async function setChatTitle(title: string, chat_id?: string) {
|
||||
if (!chat_id) return;
|
||||
await AiChatModel.updateOne({ _id: chat_id }, { title });
|
||||
}
|
||||
|
||||
export async function sendMessageOnChat(text: string, pid: string, initial_chat_id?: string) {
|
||||
|
||||
|
||||
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = []
|
||||
const chat_id = await createChatIfNotExist(pid, initial_chat_id);
|
||||
const chatMessages = await getMessagesFromChatId(chat_id);
|
||||
|
||||
if (chatMessages && chatMessages.length > 0) {
|
||||
messages.push(...chatMessages);
|
||||
} else {
|
||||
const roleMessage: OpenAI.Chat.Completions.ChatCompletionMessageParam = {
|
||||
role: 'system', content: "Today is " + new Date().toISOString()
|
||||
}
|
||||
messages.push(roleMessage);
|
||||
await addMessageToChat(roleMessage, chat_id);
|
||||
|
||||
await setChatTitle(text.substring(0, 110), chat_id);
|
||||
}
|
||||
|
||||
const userMessage: OpenAI.Chat.Completions.ChatCompletionMessageParam = {
|
||||
role: 'user', content: text
|
||||
}
|
||||
messages.push(userMessage);
|
||||
await addMessageToChat(userMessage, chat_id);
|
||||
|
||||
let response = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages, n: 1, tools });
|
||||
|
||||
let responseMessage = response.choices[0].message;
|
||||
let toolCalls = responseMessage.tool_calls;
|
||||
|
||||
await addMessageToChat(responseMessage, chat_id);
|
||||
messages.push(responseMessage);
|
||||
|
||||
|
||||
if (toolCalls) {
|
||||
console.log({ toolCalls: toolCalls.length });
|
||||
for (const toolCall of toolCalls) {
|
||||
const functionName = toolCall.function.name;
|
||||
const functionToCall = functions[functionName];
|
||||
const functionArgs = JSON.parse(toolCall.function.arguments);
|
||||
console.log('CALLING FUNCTION', functionName, 'WITH PARAMS', functionArgs);
|
||||
const functionResponse = await functionToCall({ pid, ...functionArgs });
|
||||
console.log('RESPONSE FUNCTION', functionName, 'WITH VALUE', functionResponse);
|
||||
messages.push({ tool_call_id: toolCall.id, role: "tool", content: JSON.stringify(functionResponse) });
|
||||
await addMessageToChat({ tool_call_id: toolCall.id, role: "tool", content: JSON.stringify(functionResponse) }, chat_id);
|
||||
}
|
||||
response = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages, n: 1, tools });
|
||||
responseMessage = response.choices[0].message;
|
||||
toolCalls = responseMessage.tool_calls;
|
||||
|
||||
await addMessageToChat(responseMessage, chat_id);
|
||||
|
||||
}
|
||||
|
||||
const currentCountId = await getCurrentProjectCountId(pid);
|
||||
if (!currentCountId) console.error('Project not exist');
|
||||
await ProjectCountModel.updateOne({ _id: currentCountId }, { $inc: { ai_messages: 1 } })
|
||||
|
||||
return responseMessage.content;
|
||||
}
|
||||
|
||||
63
dashboard/server/services/CacheService.ts
Normal file
63
dashboard/server/services/CacheService.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
import { createClient } from 'redis';
|
||||
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
export const DATA_EXPIRE_TIME = 30;
|
||||
export const TIMELINE_EXPIRE_TIME = 60 * 5;
|
||||
export const COUNTS_EXPIRE_TIME = 10;
|
||||
|
||||
export const COUNTS_OLD_SESSIONS_EXPIRE_TIME = 60 * 5;
|
||||
export const COUNTS_SESSIONS_EXPIRE_TIME = 60 * 3;
|
||||
|
||||
export const EVENT_NAMES_EXPIRE_TIME = 60;
|
||||
|
||||
|
||||
export class Redis {
|
||||
|
||||
private static client = createClient({
|
||||
url: runtimeConfig.REDIS_URL,
|
||||
username: runtimeConfig.REDIS_USERNAME,
|
||||
password: runtimeConfig.REDIS_PASSWORD,
|
||||
});
|
||||
|
||||
static async init() {
|
||||
await this.client.connect();
|
||||
}
|
||||
|
||||
static async setString(key: string, value: string, exp: number) {
|
||||
await this.client.set(key, value, { EX: exp });
|
||||
}
|
||||
|
||||
|
||||
static async set<T extends any>(key: string, value: T, exp: number) {
|
||||
const stringValue = JSON.stringify(value);
|
||||
this.setString(key, stringValue, exp);
|
||||
}
|
||||
|
||||
static async getString(key: string) {
|
||||
return await this.client.get(key);
|
||||
}
|
||||
|
||||
static async get<T extends any>(key: string): Promise<undefined | T> {
|
||||
const data = await this.getString(key);
|
||||
if (!data) return;
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
static async del(key: string) {
|
||||
await this.client.del(key);
|
||||
}
|
||||
|
||||
static async useCache<T extends any>(options: { key: string, exp: number }, action: (noStore: () => void) => Promise<T>): Promise<T> {
|
||||
const cached = await this.get<T>(options.key);
|
||||
if (cached) return cached;
|
||||
let storeResult = true;
|
||||
const noStore = () => storeResult = false;
|
||||
const result = await action(noStore);
|
||||
if (!storeResult) return result;
|
||||
await this.set(options.key, result, options.exp);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
36
dashboard/server/services/PerformanceService.ts
Normal file
36
dashboard/server/services/PerformanceService.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
|
||||
|
||||
class PerformanceThing {
|
||||
public min: number = Infinity;
|
||||
public max: number = -Infinity;
|
||||
private things: number[] = [];
|
||||
private slice: number = 0;
|
||||
constructor(public id: string, private maxThings: number) { }
|
||||
start() { this.slice = performance.now(); }
|
||||
stop() {
|
||||
const time = performance.now() - this.slice;
|
||||
if (time > this.max) this.max = time;
|
||||
if (time < this.min) this.min = time;
|
||||
this.things.push(time);
|
||||
if (this.things.length > this.maxThings) {
|
||||
this.things.shift();
|
||||
}
|
||||
return time;
|
||||
}
|
||||
avg() {
|
||||
return this.things.reduce((a, e) => a + e, 0) / this.things.length;
|
||||
}
|
||||
|
||||
print() {
|
||||
console.log(`${this.id} | Avg: ${this.avg().toFixed(0)} ms | Min: ${this.min.toFixed(0)} ms | Max: ${this.max.toFixed(0)} ms`)
|
||||
}
|
||||
get data() { return this.things; }
|
||||
}
|
||||
|
||||
export class PerformanceService {
|
||||
static create(id: string, maxThings: number = 100) {
|
||||
const thing = new PerformanceThing(id, maxThings);
|
||||
return thing;
|
||||
}
|
||||
}
|
||||
3
dashboard/server/tsconfig.json
Normal file
3
dashboard/server/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../.nuxt/tsconfig.server.json"
|
||||
}
|
||||
17
dashboard/server/utils/getRequestUser.ts
Normal file
17
dashboard/server/utils/getRequestUser.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { AuthContext } from "../middleware/01-authorization";
|
||||
import type { EventHandlerRequest, H3Event } from 'h3'
|
||||
|
||||
export function getRequestUser(event: H3Event<EventHandlerRequest>) {
|
||||
if (!event.context.auth) return;
|
||||
return event.context.auth as AuthContext;
|
||||
}
|
||||
|
||||
export function getRequestProjectId(event: H3Event<EventHandlerRequest>) {
|
||||
if (!event.context.params) return;
|
||||
return event.context.params['project_id'];
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
Reference in New Issue
Block a user