Fix requesting longer time periods and revert time range regression

This commit is contained in:
2024-12-27 11:18:44 -05:00
parent 09cc3e2e5c
commit 59fdb221cb
2 changed files with 66 additions and 36 deletions

View File

@@ -165,8 +165,12 @@ export class ReportingService {
return []; return [];
} }
const campaignDetails = await Promise.all( const fetchWithTimeout = async (campaignId, retries = 3) => {
campaignIds.map(async (campaignId) => { for (let i = 0; i < retries; i++) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
const response = await fetch( const response = await fetch(
`${this.baseUrl}/campaigns/${campaignId}?include=campaign-messages`, `${this.baseUrl}/campaigns/${campaignId}?include=campaign-messages`,
{ {
@@ -174,10 +178,13 @@ export class ReportingService {
'Accept': 'application/json', 'Accept': 'application/json',
'Authorization': `Klaviyo-API-Key ${this.apiKey}`, 'Authorization': `Klaviyo-API-Key ${this.apiKey}`,
'revision': this.apiRevision 'revision': this.apiRevision
} },
signal: controller.signal
} }
); );
clearTimeout(timeoutId);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch campaign ${campaignId}: ${response.status}`); throw new Error(`Failed to fetch campaign ${campaignId}: ${response.status}`);
} }
@@ -199,8 +206,31 @@ export class ReportingService {
subject: message?.attributes?.content?.subject subject: message?.attributes?.content?.subject
} }
}; };
}) } catch (error) {
if (i === retries - 1) throw error;
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))); // Exponential backoff
}
}
};
// Process in smaller chunks to avoid overwhelming the API
const chunkSize = 10;
const campaignDetails = [];
for (let i = 0; i < campaignIds.length; i += chunkSize) {
const chunk = campaignIds.slice(i, i + chunkSize);
const results = await Promise.all(
chunk.map(id => fetchWithTimeout(id).catch(error => {
console.error(`Failed to fetch campaign ${id}:`, error);
return null;
}))
); );
campaignDetails.push(...results.filter(Boolean));
if (i + chunkSize < campaignIds.length) {
await new Promise(resolve => setTimeout(resolve, 1000)); // 1 second delay between chunks
}
}
return campaignDetails; return campaignDetails;
} }

View File

@@ -2,12 +2,12 @@ export const TIME_RANGES = [
{ value: 'today', label: 'Today' }, { value: 'today', label: 'Today' },
{ value: 'yesterday', label: 'Yesterday' }, { value: 'yesterday', label: 'Yesterday' },
{ value: 'last7days', label: 'Last 7 Days' }, { value: 'last7days', label: 'Last 7 Days' },
{ value: 'last14days', label: 'Last 14 Days' },
{ value: 'last30days', label: 'Last 30 Days' }, { value: 'last30days', label: 'Last 30 Days' },
{ value: 'last90days', label: 'Last 90 Days' }, { value: 'last90days', label: 'Last 90 Days' },
{ value: 'monthToDate', label: 'Month to Date' }, { value: 'thisWeek', label: 'This Week' },
{ value: 'quarterToDate', label: 'Quarter to Date' }, { value: 'lastWeek', label: 'Last Week' },
{ value: 'yearToDate', label: 'Year to Date' }, { value: 'thisMonth', label: 'This Month' },
{ value: 'lastMonth', label: 'Last Month' }
]; ];
export const GROUP_BY_OPTIONS = [ export const GROUP_BY_OPTIONS = [