diff --git a/dashboard/server/LIVE_DEMO_DATA.ts b/dashboard/server/LIVE_DEMO_DATA.ts index e3d11ac..a896f2e 100644 --- a/dashboard/server/LIVE_DEMO_DATA.ts +++ b/dashboard/server/LIVE_DEMO_DATA.ts @@ -9,6 +9,7 @@ export async function getUserProjectFromId(project_id: string, user: AuthContext return project; } else { if (!user?.logged) return; + if (!project_id) return; const project = await ProjectModel.findById(project_id); if (!project) return; const [hasAccess, role] = await hasAccessToProject(user.id, project_id, project); diff --git a/dashboard/server/Logger.ts b/dashboard/server/Logger.ts index 273d7f9..3c7cd6a 100644 --- a/dashboard/server/Logger.ts +++ b/dashboard/server/Logger.ts @@ -23,6 +23,7 @@ export const logger = winston.createLogger({ ], transports: [ new winston.transports.Console({ + level: 'debug', format: combine( winston.format.colorize({ all: true }), errors({ stack: true }), @@ -36,6 +37,10 @@ export const logger = winston.createLogger({ }) ), }), - new winston.transports.File({ filename: 'winston-logs.ndjson' }) + new winston.transports.File({ filename: 'winston-logs.ndjson' }), + new winston.transports.File({ + level: 'debug', + filename: 'winston-debug.ndjson' + }) ] }); \ No newline at end of file diff --git a/dashboard/server/middleware/00-performance-start.ts b/dashboard/server/middleware/00-performance-start.ts new file mode 100644 index 0000000..626ef2e --- /dev/null +++ b/dashboard/server/middleware/00-performance-start.ts @@ -0,0 +1,7 @@ +import { logger } from "../Logger" + + +export default defineEventHandler(async (event) => { + const start = Date.now(); + event.context['performance-start'] = start.toString(); +}); \ No newline at end of file diff --git a/dashboard/server/middleware/01-authorization.ts b/dashboard/server/middleware/01-authorization.ts index 81c50ab..472145d 100644 --- a/dashboard/server/middleware/01-authorization.ts +++ b/dashboard/server/middleware/01-authorization.ts @@ -24,29 +24,21 @@ async function authorizationMiddleware(event: H3Event) { const authorization = event.headers.get('Authorization'); if (!authorization) { - event.context.auth = { logged: false, } + + 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'); } @@ -61,6 +53,7 @@ async function authorizationMiddleware(event: H3Event) { }, id: user._id.toString() } + event.context.auth = authContext; } diff --git a/dashboard/server/middleware/02-logging.ts b/dashboard/server/middleware/02-logging.ts new file mode 100644 index 0000000..4754bf2 --- /dev/null +++ b/dashboard/server/middleware/02-logging.ts @@ -0,0 +1,28 @@ +import { logger } from "../Logger" + + +export default defineEventHandler(async (event) => { + + const ip = getRequestAddress(event); + const user = getRequestUser(event); + + event.node.res.on('finish', () => { + if (!event.context['performance-start']) return; + const start = parseInt(event.context['performance-start']); + if (isNaN(start)) return; + + const end = Date.now(); + const duration = (end - start); + + if (!user) { + logger.debug('Request without user', { path: event.path, method: event.method, ip, duration }); + } else if (!user.logged) { + logger.debug('Request as guest', { path: event.path, method: event.method, ip, duration }); + } else { + logger.debug(`(${duration}ms) [${event.method}] ${event.path} { ${user.user.email} }`, { ip }); + } + + // event.node.res.setHeader('X-Total-Response-Time', `${duration.toFixed(2)} ms`); + }); + +}) \ No newline at end of file diff --git a/dashboard/server/services/CacheService.ts b/dashboard/server/services/CacheService.ts index 6d21e1d..9220bc2 100644 --- a/dashboard/server/services/CacheService.ts +++ b/dashboard/server/services/CacheService.ts @@ -4,15 +4,14 @@ import { createClient } from 'redis'; const runtimeConfig = useRuntimeConfig(); export const DATA_EXPIRE_TIME = 30; -export const TIMELINE_EXPIRE_TIME = 60 * 5; +export const TIMELINE_EXPIRE_TIME = 60; 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 const EVENT_METADATA_FIELDS_EXPIRE_TIME = 120; +export const EVENT_METADATA_FIELDS_EXPIRE_TIME = 30; export class Redis {