Fix time calcs and refund/cancellation cards
This commit is contained in:
@@ -193,18 +193,18 @@ export class EventsService {
|
|||||||
// Get period dates
|
// Get period dates
|
||||||
let periodStart, periodEnd, prevPeriodStart, prevPeriodEnd;
|
let periodStart, periodEnd, prevPeriodStart, prevPeriodEnd;
|
||||||
if (params.startDate && params.endDate) {
|
if (params.startDate && params.endDate) {
|
||||||
periodStart = this.timeManager.toDateTime(params.startDate);
|
periodStart = this.timeManager.getDayStart(this.timeManager.toDateTime(params.startDate));
|
||||||
periodEnd = this.timeManager.toDateTime(params.endDate);
|
periodEnd = this.timeManager.getDayEnd(this.timeManager.toDateTime(params.endDate));
|
||||||
const duration = periodEnd.diff(periodStart);
|
const duration = periodEnd.diff(periodStart);
|
||||||
prevPeriodStart = periodStart.minus(duration);
|
prevPeriodStart = this.timeManager.getDayStart(periodStart.minus(duration));
|
||||||
prevPeriodEnd = periodStart.minus({ milliseconds: 1 });
|
prevPeriodEnd = this.timeManager.getDayEnd(periodStart.minus({ milliseconds: 1 }));
|
||||||
} else if (params.timeRange) {
|
} else if (params.timeRange) {
|
||||||
const range = this.timeManager.getDateRange(params.timeRange);
|
const range = this.timeManager.getDateRange(params.timeRange);
|
||||||
periodStart = range.start;
|
periodStart = range.start;
|
||||||
periodEnd = range.end;
|
periodEnd = range.end;
|
||||||
const duration = periodEnd.diff(periodStart);
|
const prevRange = this.timeManager.getPreviousPeriod(params.timeRange);
|
||||||
prevPeriodStart = periodStart.minus(duration);
|
prevPeriodStart = prevRange.start;
|
||||||
prevPeriodEnd = periodStart.minus({ milliseconds: 1 });
|
prevPeriodEnd = prevRange.end;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load both current and previous period data
|
// Load both current and previous period data
|
||||||
@@ -403,7 +403,7 @@ export class EventsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
currentDate = currentDate.plus({ days: 1 });
|
currentDate = this.timeManager.getDayStart(currentDate.plus({ days: 1 }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -428,7 +428,8 @@ export class EventsService {
|
|||||||
|
|
||||||
// Track daily stats
|
// Track daily stats
|
||||||
if (datetime) {
|
if (datetime) {
|
||||||
const dateKey = datetime.toFormat('yyyy-MM-dd');
|
const dayStart = this.timeManager.getDayStart(datetime);
|
||||||
|
const dateKey = dayStart.toFormat('yyyy-MM-dd');
|
||||||
const hourOfDay = datetime.hour;
|
const hourOfDay = datetime.hour;
|
||||||
|
|
||||||
// Initialize stats for this date if it doesn't exist
|
// Initialize stats for this date if it doesn't exist
|
||||||
@@ -481,7 +482,7 @@ export class EventsService {
|
|||||||
if (dayStats.revenue > bestDay.amount) {
|
if (dayStats.revenue > bestDay.amount) {
|
||||||
bestDay = {
|
bestDay = {
|
||||||
date: dateKey,
|
date: dateKey,
|
||||||
displayDate: datetime.toFormat('LLL d, yyyy'),
|
displayDate: dayStart.toFormat('LLL d, yyyy'),
|
||||||
amount: dayStats.revenue,
|
amount: dayStats.revenue,
|
||||||
orderCount: dayStats.orders
|
orderCount: dayStats.orders
|
||||||
};
|
};
|
||||||
@@ -827,7 +828,8 @@ export class EventsService {
|
|||||||
|
|
||||||
// Track daily refunds
|
// Track daily refunds
|
||||||
if (datetime) {
|
if (datetime) {
|
||||||
const dateKey = datetime.toFormat('yyyy-MM-dd');
|
const dayStart = this.timeManager.getDayStart(datetime);
|
||||||
|
const dateKey = dayStart.toFormat('yyyy-MM-dd');
|
||||||
// Initialize stats for this date if it doesn't exist
|
// Initialize stats for this date if it doesn't exist
|
||||||
if (!dailyStats.has(dateKey)) {
|
if (!dailyStats.has(dateKey)) {
|
||||||
dailyStats.set(dateKey, {
|
dailyStats.set(dateKey, {
|
||||||
@@ -893,7 +895,8 @@ export class EventsService {
|
|||||||
|
|
||||||
// Track daily cancellations
|
// Track daily cancellations
|
||||||
if (datetime) {
|
if (datetime) {
|
||||||
const dateKey = datetime.toFormat('yyyy-MM-dd');
|
const dayStart = this.timeManager.getDayStart(datetime);
|
||||||
|
const dateKey = dayStart.toFormat('yyyy-MM-dd');
|
||||||
// Initialize stats for this date if it doesn't exist
|
// Initialize stats for this date if it doesn't exist
|
||||||
if (!dailyStats.has(dateKey)) {
|
if (!dailyStats.has(dateKey)) {
|
||||||
dailyStats.set(dateKey, {
|
dailyStats.set(dateKey, {
|
||||||
@@ -1274,43 +1277,13 @@ export class EventsService {
|
|||||||
|
|
||||||
const results = await Promise.all(eventPromises);
|
const results = await Promise.all(eventPromises);
|
||||||
|
|
||||||
// Transform and flatten the events into a single array
|
// Transform results into a keyed object
|
||||||
const allEvents = [];
|
const batchResults = {};
|
||||||
metrics.forEach((metric, index) => {
|
metrics.forEach((metric, index) => {
|
||||||
const response = results[index];
|
batchResults[metric] = results[index];
|
||||||
const events = response?.data || [];
|
|
||||||
if (Array.isArray(events)) {
|
|
||||||
const transformedEvents = events.map(event => ({
|
|
||||||
...event,
|
|
||||||
metric_id: metric,
|
|
||||||
datetime: event.attributes?.datetime || event.datetime,
|
|
||||||
event_properties: {
|
|
||||||
...event.event_properties,
|
|
||||||
datetime: event.attributes?.datetime || event.datetime,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
allEvents.push(...transformedEvents);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sort all events by datetime in descending order
|
return batchResults;
|
||||||
allEvents.sort((a, b) => {
|
|
||||||
const dateA = new Date(a.datetime);
|
|
||||||
const dateB = new Date(b.datetime);
|
|
||||||
return dateB - dateA;
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = { data: allEvents };
|
|
||||||
|
|
||||||
// Cache the result
|
|
||||||
try {
|
|
||||||
const ttl = this.redisService._getTTL(params.timeRange);
|
|
||||||
await this.redisService.set(`${cacheKey}:feed`, result, ttl);
|
|
||||||
} catch (cacheError) {
|
|
||||||
console.warn('[EventsService] Cache set error:', cacheError);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[EventsService] Error in batch metrics:', error);
|
console.error('[EventsService] Error in batch metrics:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user