mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-09 23:48:36 +01:00
change in progress
This commit is contained in:
@@ -1,57 +1,28 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
|
||||
import type { Model } from "mongoose";
|
||||
|
||||
|
||||
const allowedModels: Record<string, { model: Model<any>, field: string }> = {
|
||||
'events': {
|
||||
model: EventModel,
|
||||
field: 'name'
|
||||
}
|
||||
}
|
||||
|
||||
type TModelName = keyof typeof allowedModels;
|
||||
import { getRequestData } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const project_id = getHeader(event, 'x-pid');
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const from = getRequestHeader(event, 'x-from');
|
||||
const to = getRequestHeader(event, 'x-to');
|
||||
const data = await getRequestData(event);
|
||||
if (!data) return;
|
||||
|
||||
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to are required');
|
||||
const { schemaName, pid, from, to, model, project_id } = data;
|
||||
|
||||
const schemaName = getRequestHeader(event, 'x-schema');
|
||||
if (!schemaName) return setResponseStatus(event, 400, 'x-schema is required');
|
||||
|
||||
if (!Object.keys(allowedModels).includes(schemaName)) return setResponseStatus(event, 400, 'x-schema value is not valid');
|
||||
|
||||
const cacheKey = `count:${schemaName}:${project_id}:${from}:${to}`;
|
||||
const cacheKey = `count:${schemaName}:${pid}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async (noStore, updateExp) => {
|
||||
|
||||
const { model } = allowedModels[schemaName as TModelName];
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const result = await model.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
created_at: {
|
||||
$gte: new Date(from),
|
||||
$lte: new Date(to)
|
||||
}
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
}
|
||||
},
|
||||
{
|
||||
$count: 'total'
|
||||
}
|
||||
{ $count: 'count' },
|
||||
]);
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user