This commit is contained in:
2025-03-13 20:06:58 +08:00
parent b8ea4e7097
commit 72c040cf19
4 changed files with 381 additions and 14 deletions

View File

@@ -113,6 +113,77 @@ export class AnalyticsController {
}, 500);
}
}
/**
* Get KOL conversion funnel data
* Returns user counts and conversion rates for each funnel stage
*
* @param c Hono Context
* @returns Response with funnel data
*/
async getKolFunnel(c: Context) {
const requestId = `req_${Date.now()}_${Math.random().toString(36).substring(2, 10)}`;
const startTime = Date.now();
try {
// Get query parameters
const timeRange = c.req.query('timeRange') || '30'; // Default to 30 days
const projectId = c.req.query('projectId'); // Optional project filter
const debug = c.req.query('debug') || 'false'; // Debug mode
logger.info(`[${requestId}] KOL funnel request received`, {
timeRange,
projectId,
debug,
userAgent: c.req.header('user-agent'),
ip: c.req.header('x-forwarded-for') || 'unknown'
});
// Validate time range
if (!['7', '30', '90'].includes(timeRange)) {
logger.warn(`[${requestId}] Invalid timeRange: ${timeRange}`);
return c.json({
success: false,
error: 'Invalid timeRange. Must be 7, 30, or 90.'
}, 400);
}
// Get funnel data from service
const data = await analyticsService.getKolFunnel(
parseInt(timeRange, 10),
projectId
);
// Debug mode - log additional data
if (debug.toLowerCase() === 'true' && process.env.NODE_ENV !== 'production') {
await analyticsService.debugEventData();
}
// Log successful response
const duration = Date.now() - startTime;
logger.info(`[${requestId}] KOL funnel response sent successfully`, {
duration,
stageCount: data.stages.length
});
// Return the data
return c.json({
success: true,
data
});
} catch (error) {
// Log error
const duration = Date.now() - startTime;
logger.error(`[${requestId}] Error fetching KOL funnel (${duration}ms)`, error);
// Return error response
return c.json({
success: false,
error: 'Failed to fetch KOL funnel data',
message: error instanceof Error ? error.message : 'Unknown error'
}, 500);
}
}
}
// Export singleton instance