This commit is contained in:
Emily
2024-06-21 16:56:44 +02:00
parent 08ca6cf7ea
commit d8445b3f1a
5 changed files with 122 additions and 17 deletions

View File

@@ -0,0 +1,80 @@
import dayjs from 'dayjs';
export type Slice = 'day' | 'hour' | 'month' | 'year';
class DateService {
getQueryDateRange(slice: Slice) {
const group: Record<string, any> = {}
const sort: Record<string, any> = {}
const fromParts: Record<string, any> = {}
switch (slice) {
case 'hour':
group.hour = { $hour: '$created_at' }
sort['_id.hour'] = 1;
fromParts.hour = "$_id.hour";
case 'day':
group.day = { $dayOfMonth: '$created_at' }
sort['_id.day'] = 1;
fromParts.day = "$_id.day";
case 'month':
group.month = { $month: '$created_at' }
sort['_id.month'] = 1;
fromParts.month = "$_id.month";
case 'year':
group.year = { $year: '$created_at' }
sort['_id.year'] = 1;
fromParts.year = "$_id.year";
}
return { group, sort, fromParts }
}
prepareDateRange(from: string, to: string, slice: Slice) {
let fromDate = dayjs(from).minute(0).second(0).millisecond(0);
let toDate = dayjs(to).minute(0).second(0).millisecond(0);
switch (slice) {
case 'day':
fromDate = fromDate.hour(0);
toDate = toDate.hour(0);
break;
case 'hour':
break;
}
return {
from: fromDate.toDate(),
to: toDate.toDate()
}
}
fillDates(dates: string[], slice: Slice) {
const allDates: dayjs.Dayjs[] = [];
const firstDate = dayjs(dates.at(0));
const lastDate = dayjs(dates.at(-1));
let currentDate = firstDate.clone();
while (currentDate.isBefore(lastDate)) {
currentDate = currentDate.add(1, slice);
allDates.push(currentDate);
}
return allDates;
}
mergeFilledDates<T extends Record<string, any>, K extends keyof T>(dates: dayjs.Dayjs[], items: T[], dateField: K, slice: Slice, fillData: Omit<T, K>) {
const result = new Array<T>();
for (const date of dates) {
const item = items.find(e => dayjs(e[dateField]).isSame(date), slice);
result.push(item ?? { ...fillData, [dateField]: date.format() } as T);
}
return result;
}
}
const dateServiceInstance = new DateService();
export default dateServiceInstance;