first implementation of elasticity demand computation

This commit is contained in:
2025-11-25 22:27:38 +01:00
parent 8b76d24ade
commit c639d99be2
8 changed files with 616 additions and 38 deletions

View File

@@ -13,17 +13,6 @@ export async function GET(req: NextRequest) {
const experimentId = searchParams.get('experimentId');
const storeMode = process.env.NEXT_PUBLIC_STORE_MODE || 'shop';
// log in dev
if (process.env.NODE_ENV === 'development') {
console.log('[pricing-api]', {
productId,
sessionId,
experimentId,
storeMode,
timestamp: new Date().toISOString(),
});
}
if (!productId) {
return NextResponse.json(
{ error: 'productId is required' },
@@ -34,11 +23,46 @@ export async function GET(req: NextRequest) {
// stub: call external pricing provider (random for now)
const basePrice = 100 + Math.random() * 900; // 100-1000 range
const price = Math.round(basePrice * 100) / 100;
const timestamp = new Date().toISOString();
// log price to kafka for elasticity computation
if (sessionId) {
const backendUrl = process.env.BACKEND_URL || 'http://localhost:5000';
try {
await fetch(`${backendUrl}/api/kafka/price-log`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
productId,
price,
sessionId,
experimentId: experimentId || undefined,
storeMode,
ts: timestamp,
}),
});
} catch (err) {
console.error('[price-log-error]', err);
// don't fail the pricing request if logging fails
}
}
// log in dev
if (process.env.NODE_ENV === 'development') {
console.log('[pricing-api]', {
productId,
sessionId,
experimentId,
storeMode,
price,
timestamp,
});
}
const response: PricingResponse = {
price,
currency: 'EUR',
cachedAt: new Date().toISOString(),
cachedAt: timestamp,
};
return NextResponse.json(response);