fix dashboard charts

This commit is contained in:
Emily
2024-06-24 14:51:15 +02:00
parent 67c027d720
commit 75090c809c
5 changed files with 90 additions and 27 deletions

View File

@@ -2,6 +2,7 @@ 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";
import { executeTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService";
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
@@ -11,11 +12,25 @@ export default defineEventHandler(async event => {
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const { slice, duration } = await readBody(event);
const { slice, from, to } = 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;
if (!from) return setResponseStatus(event, 400, 'from is required');
if (!from) return setResponseStatus(event, 400, 'to is required');
if (!from) return setResponseStatus(event, 400, 'slice is required');
return await Redis.useCache({
key: `timeline:events:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
exp: TIMELINE_EXPIRE_TIME
}, async () => {
const timelineData = await executeTimelineAggregation({
projectId: project._id,
model: EventModel,
from, to, slice
});
const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
return timelineFilledMerged;
});
});

View File

@@ -2,6 +2,7 @@ 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";
import { executeAdvancedTimelineAggregation, executeTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService";
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
@@ -11,16 +12,44 @@ export default defineEventHandler(async event => {
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const { slice, duration } = await readBody(event);
// 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;
// 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;
// });
const { slice, from, to } = await readBody(event);
if (!from) return setResponseStatus(event, 400, 'from is required');
if (!from) return setResponseStatus(event, 400, 'to is required');
if (!from) return setResponseStatus(event, 400, 'slice is required');
return await Redis.useCache({
key: `timeline:sessions_duration:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
exp: TIMELINE_EXPIRE_TIME
}, async () => {
const timelineData = await executeAdvancedTimelineAggregation({
projectId: project._id,
model: SessionModel,
from, to, slice,
customGroup: {
duration: { $sum: '$duration' }
},
customProjection: {
count: { $divide: ["$duration", "$count"] }
},
});
const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
return timelineFilledMerged;
});
});

View File

@@ -9,16 +9,21 @@ export type TimelineAggregationOptions = {
model: mongoose.Model<any>,
from: string | number,
to: string | number,
slice: Slice
slice: Slice,
debug?: boolean
}
export type AdvancedTimelineAggregationOptions = TimelineAggregationOptions & {
customMatch?: Record<string, any>
customMatch?: Record<string, any>,
customGroup?: Record<string, any>,
customProjection?: Record<string, any>
}
export async function executeAdvancedTimelineAggregation(options: AdvancedTimelineAggregationOptions) {
options.customMatch = options.customMatch || {};
options.customGroup = options.customGroup || {};
options.customProjection = options.customProjection || {};
const { group, sort, fromParts } = DateService.getQueryDateRange(options.slice);
@@ -30,11 +35,15 @@ export async function executeAdvancedTimelineAggregation(options: AdvancedTimeli
...options.customMatch
}
},
{ $group: { _id: group, count: { $sum: 1 } } },
{ $group: { _id: group, count: { $sum: 1 }, ...options.customGroup } },
{ $sort: sort },
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count" } }
{ $project: { _id: { $dateFromParts: fromParts }, count: "$count", ...options.customProjection } }
]
if (options.debug === true) {
console.log(JSON.stringify(aggregation, null, 2));
}
const timeline: { _id: string, count: number }[] = await options.model.aggregate(aggregation);
return timeline;