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

@@ -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;