From df305c2a972fbc40e6ef3494a97158e1061c779b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Sat, 19 Apr 2025 12:35:55 -0700 Subject: [PATCH] fix(): null-proof/nan-proof --- apps/api/src/lib/extract/extraction-service.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/api/src/lib/extract/extraction-service.ts b/apps/api/src/lib/extract/extraction-service.ts index 652cc5c0..fce96584 100644 --- a/apps/api/src/lib/extract/extraction-service.ts +++ b/apps/api/src/lib/extract/extraction-service.ts @@ -75,6 +75,8 @@ export class CostLimitExceededError extends Error { } } +const nanProof = (n: number | null | undefined) => isNaN(n ?? 0) ? 0 : (n ?? 0); + export class CostTracking { calls: { type: "smartScrape" | "other", @@ -109,10 +111,10 @@ export class CostTracking { calls: this.calls, smartScrapeCallCount: this.calls.filter(c => c.type === "smartScrape").length, - smartScrapeCost: this.calls.filter(c => c.type === "smartScrape").reduce((acc, c) => acc + c.cost, 0), + smartScrapeCost: this.calls.filter(c => c.type === "smartScrape").reduce((acc, c) => acc + nanProof(c.cost), 0), otherCallCount: this.calls.filter(c => c.type === "other").length, - otherCost: this.calls.filter(c => c.type === "other").reduce((acc, c) => acc + c.cost, 0), - totalCost: this.calls.reduce((acc, c) => acc + c.cost, 0), + otherCost: this.calls.filter(c => c.type === "other").reduce((acc, c) => acc + nanProof(c.cost), 0), + totalCost: this.calls.reduce((acc, c) => acc + nanProof(c.cost), 0), } } }