Fix time calcs and refund/cancellation cards
This commit is contained in:
@@ -193,18 +193,18 @@ export class EventsService {
|
||||
// Get period dates
|
||||
let periodStart, periodEnd, prevPeriodStart, prevPeriodEnd;
|
||||
if (params.startDate && params.endDate) {
|
||||
periodStart = this.timeManager.toDateTime(params.startDate);
|
||||
periodEnd = this.timeManager.toDateTime(params.endDate);
|
||||
periodStart = this.timeManager.getDayStart(this.timeManager.toDateTime(params.startDate));
|
||||
periodEnd = this.timeManager.getDayEnd(this.timeManager.toDateTime(params.endDate));
|
||||
const duration = periodEnd.diff(periodStart);
|
||||
prevPeriodStart = periodStart.minus(duration);
|
||||
prevPeriodEnd = periodStart.minus({ milliseconds: 1 });
|
||||
prevPeriodStart = this.timeManager.getDayStart(periodStart.minus(duration));
|
||||
prevPeriodEnd = this.timeManager.getDayEnd(periodStart.minus({ milliseconds: 1 }));
|
||||
} else if (params.timeRange) {
|
||||
const range = this.timeManager.getDateRange(params.timeRange);
|
||||
periodStart = range.start;
|
||||
periodEnd = range.end;
|
||||
const duration = periodEnd.diff(periodStart);
|
||||
prevPeriodStart = periodStart.minus(duration);
|
||||
prevPeriodEnd = periodStart.minus({ milliseconds: 1 });
|
||||
const prevRange = this.timeManager.getPreviousPeriod(params.timeRange);
|
||||
prevPeriodStart = prevRange.start;
|
||||
prevPeriodEnd = prevRange.end;
|
||||
}
|
||||
|
||||
// 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
|
||||
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;
|
||||
|
||||
// Initialize stats for this date if it doesn't exist
|
||||
@@ -481,7 +482,7 @@ export class EventsService {
|
||||
if (dayStats.revenue > bestDay.amount) {
|
||||
bestDay = {
|
||||
date: dateKey,
|
||||
displayDate: datetime.toFormat('LLL d, yyyy'),
|
||||
displayDate: dayStart.toFormat('LLL d, yyyy'),
|
||||
amount: dayStats.revenue,
|
||||
orderCount: dayStats.orders
|
||||
};
|
||||
@@ -827,7 +828,8 @@ export class EventsService {
|
||||
|
||||
// Track daily refunds
|
||||
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
|
||||
if (!dailyStats.has(dateKey)) {
|
||||
dailyStats.set(dateKey, {
|
||||
@@ -893,7 +895,8 @@ export class EventsService {
|
||||
|
||||
// Track daily cancellations
|
||||
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
|
||||
if (!dailyStats.has(dateKey)) {
|
||||
dailyStats.set(dateKey, {
|
||||
@@ -1274,43 +1277,13 @@ export class EventsService {
|
||||
|
||||
const results = await Promise.all(eventPromises);
|
||||
|
||||
// Transform and flatten the events into a single array
|
||||
const allEvents = [];
|
||||
// Transform results into a keyed object
|
||||
const batchResults = {};
|
||||
metrics.forEach((metric, index) => {
|
||||
const response = 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);
|
||||
}
|
||||
batchResults[metric] = results[index];
|
||||
});
|
||||
|
||||
// Sort all events by datetime in descending order
|
||||
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;
|
||||
return batchResults;
|
||||
} catch (error) {
|
||||
console.error('[EventsService] Error in batch metrics:', error);
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user