aggregation optimization

This commit is contained in:
Emily
2024-09-02 18:36:52 +02:00
parent d499aa2f39
commit c003b655ec
6 changed files with 32 additions and 21 deletions

View File

@@ -31,4 +31,8 @@ logs
out.pdf out.pdf
# TESTS - TO REMOVE # TESTS - TO REMOVE
tests tests
# EXPLAINS MONGODB
explains

View File

@@ -100,7 +100,8 @@ function goToUpgrade() {
Limit reached Limit reached
</div> </div>
<div class="poppins text-[#fbbf24]"> <div class="poppins text-[#fbbf24]">
Litlyx cannot receive new data as you reached your plan's limit. Resume all the great features and collect even more data with a higher plan. Litlyx cannot receive new data as you reached your plan's limit. Resume all the great
features and collect even more data with a higher plan.
</div> </div>
</div> </div>
<div> <div>
@@ -111,7 +112,7 @@ function goToUpgrade() {
</div> </div>
<DashboardTopSection></DashboardTopSection> <DashboardTopSection></DashboardTopSection>
<DashboardTopCards :key="refreshKey"></DashboardTopCards> <DashboardTopCards :key="refreshKey"></DashboardTopCards>
@@ -143,7 +144,7 @@ function goToUpgrade() {
</div> </div>
</CardTitled> --> </CardTitled> -->
</div> </div>
<div class="flex w-full justify-center mt-6 px-6"> <div class="flex w-full justify-center mt-6 px-6">
<div class="flex w-full gap-6 flex-col xl:flex-row"> <div class="flex w-full gap-6 flex-col xl:flex-row">
@@ -151,13 +152,13 @@ function goToUpgrade() {
<DashboardWebsitesBarCard :key="refreshKey"></DashboardWebsitesBarCard> <DashboardWebsitesBarCard :key="refreshKey"></DashboardWebsitesBarCard>
</div> </div>
<div class="flex-1"> <div class="flex-1">
<DashboardEventsBarCard :key="refreshKey"></DashboardEventsBarCard> <DashboardEventsBarCard :key="refreshKey"></DashboardEventsBarCard>
</div> </div>
</div> </div>
</div> </div>
<div class="flex w-full justify-center mt-6 px-6"> <div class="flex w-full justify-center mt-6 px-6">
<div class="flex w-full gap-6 flex-col xl:flex-row"> <div class="flex w-full gap-6 flex-col xl:flex-row">
<div class="flex-1"> <div class="flex-1">
<DashboardReferrersBarCard :key="refreshKey"></DashboardReferrersBarCard> <DashboardReferrersBarCard :key="refreshKey"></DashboardReferrersBarCard>
@@ -187,7 +188,7 @@ function goToUpgrade() {
<div class="flex-1"> <div class="flex-1">
</div> </div>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -4,6 +4,7 @@ import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import DateService from "@services/DateService"; import DateService from "@services/DateService";
import { executeTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService"; import { executeTimelineAggregation, fillAndMergeTimelineAggregation } from "~/server/services/TimelineService";
import fs from 'fs';
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event); const project_id = getRequestProjectId(event);
@@ -20,19 +21,24 @@ export default defineEventHandler(async event => {
if (!to) return setResponseStatus(event, 400, 'to is required'); if (!to) return setResponseStatus(event, 400, 'to is required');
if (!slice) return setResponseStatus(event, 400, 'slice is required'); if (!slice) return setResponseStatus(event, 400, 'slice is required');
return await Redis.useCache({ // return await Redis.useCache({
key: `timeline:visits:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`, // key: `timeline:visits:${project_id}:${slice}:${from || 'none'}:${to || 'none'}`,
exp: TIMELINE_EXPIRE_TIME // exp: TIMELINE_EXPIRE_TIME,
}, async () => { // }, async () => {
const timelineData = await executeTimelineAggregation({ const timelineData = await executeTimelineAggregation({
projectId: project._id, projectId: project._id,
model: VisitModel, model: VisitModel,
from, to, slice from, to, slice,
}); debug: true
const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
return timelineFilledMerged;
}); });
console.log(timelineData);
fs.writeFileSync('explains/timeline-visits.json', JSON.stringify(timelineData));
// const timelineFilledMerged = fillAndMergeTimelineAggregation(timelineData, slice);
// return timelineFilledMerged;
// });

View File

@@ -46,7 +46,7 @@ export async function executeAdvancedTimelineAggregation<T = {}>(options: Advanc
console.log(JSON.stringify(aggregation, null, 2)); console.log(JSON.stringify(aggregation, null, 2));
} }
const timeline: { _id: string, count: number & T }[] = await options.model.aggregate(aggregation); const timeline: { _id: string, count: number & T }[] = await options.model.aggregate(aggregation, { explain: options.debug === true });
return timeline; return timeline;

View File

@@ -15,7 +15,7 @@ const EventSchema = new Schema<TEvent>({
metadata: Schema.Types.Mixed, metadata: Schema.Types.Mixed,
session: { type: String }, session: { type: String },
flowHash: { type: String }, flowHash: { type: String },
created_at: { type: Date, default: () => Date.now() }, created_at: { type: Date, default: () => Date.now(), index: true },
}) })
export const EventModel = model<TEvent>('events', EventSchema); export const EventModel = model<TEvent>('events', EventSchema);

View File

@@ -16,7 +16,7 @@ const SessionSchema = new Schema<TSession>({
flowHash: { type: String }, flowHash: { type: String },
duration: { type: Number, required: true, default: 0 }, duration: { type: Number, required: true, default: 0 },
updated_at: { type: Date, default: () => Date.now() }, updated_at: { type: Date, default: () => Date.now() },
created_at: { type: Date, default: () => Date.now() }, created_at: { type: Date, default: () => Date.now(), index: true },
}) })
export const SessionModel = model<TSession>('sessions', SessionSchema); export const SessionModel = model<TSession>('sessions', SessionSchema);