chore: cleaning shitty code

This commit is contained in:
2026-01-12 10:47:29 +01:00
parent 584a38062c
commit be4313cf2a

View File

@@ -6,58 +6,34 @@ interface InteractionEvent {
metadata?: Record<string, any>; metadata?: Record<string, any>;
} }
export async function dumpKafkaTopic( const dumpKafkaTopic = async (backendUrl: string, topic: string) => {
backendUrl: string,
topic: string
): Promise<any[]> {
const resp = await fetch(`${backendUrl}/api/kafka/dump?topic=${topic}`); const resp = await fetch(`${backendUrl}/api/kafka/dump?topic=${topic}`);
if (!resp.ok) { if (!resp.ok) throw new Error(`Kafka dump failed: ${resp.status}`);
throw new Error(`Kafka dump failed: ${resp.status}`); const { messages = [] } = await resp.json();
} return messages as any[];
const data = await resp.json(); };
return data.messages || [];
}
export async function waitForInteractionEvent( export const waitForInteractionEvent = async (
backendUrl: string, backendUrl: string,
sessionId: string, sessionId: string,
eventType: string, eventType: string,
maxRetries: number = 10, maxRetries = 10,
pollInterval: number = 500 pollInterval = 500
): Promise<InteractionEvent | null> { ): Promise<InteractionEvent | null> => {
for (let i = 0; i < maxRetries; i++) { for (let i = 0; i < maxRetries; i++) {
const msgs = await dumpKafkaTopic(backendUrl, 'user-interactions'); const msgs = await dumpKafkaTopic(backendUrl, "user-interactions");
const hit = msgs.find(m => m.sessionId === sessionId && m.event === eventType);
for (const msg of msgs) { if (hit) return hit as InteractionEvent;
if (msg.sessionId === sessionId && msg.event === eventType) { await new Promise<void>(r => setTimeout(r, pollInterval));
return msg as InteractionEvent;
} }
}
await new Promise(r => setTimeout(r, pollInterval));
}
return null; return null;
} };
export async function countProductViews( export const countProductViews = async (backendUrl: string, productId: string) =>
backendUrl: string, (await dumpKafkaTopic(backendUrl, "user-interactions")).reduce(
productId: string (n, m) => n + (m.productId === productId && m.event === "view_item_page" ? 1 : 0),
): Promise<number> { 0
const msgs = await dumpKafkaTopic(backendUrl, 'user-interactions'); );
return msgs.reduce((cnt, msg) => { export const getSessionEvents = async (backendUrl: string, sessionId: string) =>
if (msg.productId === productId && msg.event === 'view_item_page') { (await dumpKafkaTopic(backendUrl, "user-interactions")).filter(m => m.sessionId === sessionId);
return cnt + 1;
}
return cnt;
}, 0);
}
export async function getSessionEvents(
backendUrl: string,
sessionId: string
): Promise<InteractionEvent[]> {
const msgs = await dumpKafkaTopic(backendUrl, 'user-interactions');
return msgs.filter(m => m.sessionId === sessionId);
}