add cap to dates on slices

This commit is contained in:
Emily
2024-09-23 15:01:14 +02:00
parent 3b6a202538
commit 5b7e93bcbb
8 changed files with 96 additions and 72 deletions

View File

@@ -1,36 +0,0 @@
class PerformanceThing {
public min: number = Infinity;
public max: number = -Infinity;
private things: number[] = [];
private slice: number = 0;
constructor(public id: string, private maxThings: number) { }
start() { this.slice = performance.now(); }
stop() {
const time = performance.now() - this.slice;
if (time > this.max) this.max = time;
if (time < this.min) this.min = time;
this.things.push(time);
if (this.things.length > this.maxThings) {
this.things.shift();
}
return time;
}
avg() {
return this.things.reduce((a, e) => a + e, 0) / this.things.length;
}
print() {
console.log(`${this.id} | Avg: ${this.avg().toFixed(0)} ms | Min: ${this.min.toFixed(0)} ms | Max: ${this.max.toFixed(0)} ms`)
}
get data() { return this.things; }
}
export class PerformanceService {
static create(id: string, maxThings: number = 100) {
const thing = new PerformanceThing(id, maxThings);
return thing;
}
}

View File

@@ -29,6 +29,17 @@ export async function executeAdvancedTimelineAggregation<T = {}>(options: Advanc
const { group, sort, fromParts } = DateService.getQueryDateRange(options.slice);
if (!sort) throw Error('Slice is probably not correct');
const dateDistDays = (new Date(options.to).getTime() - new Date(options.from).getTime()) / (1000 * 60 * 60 * 24)
// 15 Days
if (options.slice === 'hour' && (dateDistDays > 15)) throw Error('Date gap too big for this slice');
// 1 Year
if (options.slice === 'day' && (dateDistDays > 365)) throw Error('Date gap too big for this slice');
// 3 Years
if (options.slice === 'month' && (dateDistDays > 365 * 3)) throw Error('Date gap too big for this slice');
const aggregation = [
{
$match: {