From 0e8228a0bb21769078c30ebf02941a06c2d42b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sat, 3 May 2025 15:15:39 +0400 Subject: [PATCH] Fuzzing for dates In addition to fuzzing amounts, this shifts all dates to the past by a random number of days (between 7 and 14). --- generate-stats.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/generate-stats.ts b/generate-stats.ts index 9259a68..31653e6 100644 --- a/generate-stats.ts +++ b/generate-stats.ts @@ -100,6 +100,16 @@ function getFuzzFactor(amount) { return normalized * 0.02 - 0.01; // -1% to +1% for larger amounts } +// Randomly choose a date shift between 7 and 14 days +const dateShiftDays = Math.floor(Math.random() * 8) + 7; + +// Shift date to the past by a number of days +function shiftDate(date, days) { + const shifted = new Date(date); + shifted.setDate(date.getDate() - days); // Subtract days to shift to past + return shifted.toISOString().split("T")[0]; // YYYY-MM-DD +} + // Process a single user and return CSV rows async function processUser(userId, client) { // Check the number of settled invoices for the user @@ -131,10 +141,10 @@ async function processUser(userId, client) { // Aggregate daily statistics using BigInt const dailyData = {}; - let runningBalance = BigInt(0); // Initialize as BigInt + let runningBalance = BigInt(0); for (const invoice of invoices) { - const day = invoice.settled_at.toISOString().split("T")[0]; // YYYY-MM-DD + const day = shiftDate(invoice.settled_at, dateShiftDays); // Get fuzzing factor for this invoice const fuzzFactor = getFuzzFactor(Number(invoice.amount));