From 0f71a486a5e86d0be0dcbdd25c6f0c49c758e9c4 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 25 Nov 2022 14:13:22 +0100 Subject: [PATCH] log affected accounts & some fixes --- database/log/.gitignore | 2 + .../0054-recalculate_balance_and_decay.ts | 52 +++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 database/log/.gitignore diff --git a/database/log/.gitignore b/database/log/.gitignore new file mode 100644 index 000000000..c96a04f00 --- /dev/null +++ b/database/log/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/database/migrations/0054-recalculate_balance_and_decay.ts b/database/migrations/0054-recalculate_balance_and_decay.ts index fe9896db1..6614b4a52 100644 --- a/database/migrations/0054-recalculate_balance_and_decay.ts +++ b/database/migrations/0054-recalculate_balance_and_decay.ts @@ -13,6 +13,7 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ +import fs from 'fs' import Decimal from 'decimal.js-light' // Set precision value @@ -86,30 +87,63 @@ function calculateDecay( } export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // Write log file + const logFile = 'log/0054-recalculate_balance_and_decay.log.csv' + await fs.writeFile( + logFile, + `email;first_name;last_name;affected_transactions;new_balance;new_decay;old_balance;old_decay;delta;\n`, + (err) => { + if (err) throw err + }, + ) + // Find all users & loop over them const users = await queryFn('SELECT user_id FROM transactions GROUP BY user_id;') for (let u = 0; u < users.length; u++) { + const userId = users[u].user_id // find all transactions for a user const transactions = await queryFn( - `SELECT * FROM transactions WHERE user_id = ${users[u].user_id} ORDER BY balance_date ASC;`, + `SELECT *, CONVERT(balance, CHAR) as dec_balance, CONVERT(decay, CHAR) as dec_decay FROM transactions WHERE user_id = ${userId} ORDER BY balance_date ASC;`, ) + let previous = null + let affectedTransactions = 0 let balance = new Decimal(0) for (let t = 0; t < transactions.length; t++) { const transaction = transactions[t] - const decayStartDate = previous ? previous.balance_date : transaction.balance_date const amount = new Decimal(transaction.amount) const decay = calculateDecay(balance, decayStartDate, transaction.balance_date) balance = decay.balance.add(amount) - // Update - await queryFn(` - UPDATE transactions SET - balance = ${balance}, - decay = ${decay.decay ? decay.decay : 0} - WHERE id = ${transaction.id}; - `) + const userContact = await queryFn( + `SELECT email, first_name, last_name FROM users LEFT JOIN user_contacts ON users.email_id = user_contacts.id WHERE users.id = ${userId}`, + ) + const userEmail = userContact.length === 1 ? userContact[0].email : userId + const userFirstName = userContact.length === 1 ? userContact[0].first_name : '' + const userLastName = userContact.length === 1 ? userContact[0].last_name : '' + + // Update if needed + if (!balance.eq(transaction.dec_balance)) { + await queryFn(` + UPDATE transactions SET + balance = ${balance}, + decay = ${decay.decay ? decay.decay : 0} + WHERE id = ${transaction.id}; + `) + affectedTransactions++ + + // Log on last entry + if (t === transactions.length - 1) { + fs.appendFile( + logFile, + `${userEmail};${userFirstName};${userLastName};${affectedTransactions};${balance};${decay.decay ? decay.decay : 0};${transaction.dec_balance};${transaction.dec_decay};${balance.sub(transaction.dec_balance)};\n`, + (err) => { + if (err) throw err + }, + ) + } + } // previous previous = transaction