From 08c1de5d9d49e2f8a5688b08de8e5b007faaff69 Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Fri, 10 Jan 2025 18:35:50 -0800 Subject: [PATCH 1/2] container: lib_taxes: optimize record printing, add checks and logging - When applicable, as the stream progresses, exit each successive gawk iteration when no applicable records are present (no taxable events found for the given tag in the given year). Previously, when no records were found, a reports file was produced with either a single header-only row or junk data that resulted from the assumption there would be real data. A preexisting hack prevented the junk data from being written after production but this commit will resolve that hack by preventing the printing of junk data while also preventing the production of a header-only file. - Print warnings when no taxable events are found for the given year. - Add checks: * Only verify patched files if the patched files are not empty. * Empty variable checks. --- .../src/finance/lib/internal/lib_taxes.bash | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/container/src/finance/lib/internal/lib_taxes.bash b/container/src/finance/lib/internal/lib_taxes.bash index fc8dafb..a447029 100644 --- a/container/src/finance/lib/internal/lib_taxes.bash +++ b/container/src/finance/lib/internal/lib_taxes.bash @@ -446,6 +446,8 @@ function lib_taxes::__taxes_print() printf "\n" } { + if (!NR) { exit } + Date = $1 Action = $2 Account = $3 @@ -625,6 +627,9 @@ function lib_taxes::__taxes_print() END { print prev }' \ | gawk -M -v PREC=100 \ '{ + # NOTE: if previous only produces OFS, this will exit + if (!$NF) { exit } + # If more than triplet form, support quadruple form # TODO: support more than 4 if necessary if ($13 == $10) @@ -839,7 +844,7 @@ function lib_taxes::__taxes_print() print $0 exit } - }' | grep -vE "(,BUY,,,,USD,0,,|,,,,,,,,,,,0.000000000000000000)" # TODO: HACK + }' fi # @@ -861,6 +866,8 @@ function lib_taxes::__taxes_print() | sort -u \ | gawk \ '{ + if (!NR) { exit } + txid = $1; FeeCurrency = $5; Fee = $6 a[$1]=a[$1] ? a[$1] OFS txid OFS FeeCurrency OFS Fee : $0 } END { for(i in a) {print a[i]} }' FS=, OFS=, \ @@ -956,12 +963,24 @@ function lib_taxes::__taxes_print() if (NR == 1) { Header=$0 - print Header } + # Save non-header row(s) if ($0 != Header) { - print + Data[i++] = $0 } + } + END { + # Only print header if non-header row(s) exist + if (length(Data)) + { + print Header + } + # Print all non-header row(s) + for (i=0; i < length(Data); i++) + { + print Data[i] + } }' FS=, OFS=, # WARNING: # - Do not run unique (sort -ru)! There are legitimate income/trade entries @@ -1026,8 +1045,14 @@ function lib_taxes::__taxes_write() lib_utils::print_custom " \e[32m├─\e[34m\e[1;3m ${_arg_tag} (full)\e[0m\n" local _base_path="${_tax_root_dir}/${_arg_tag}/${_tax_year}_${_arg_tag}" - lib_taxes::__taxes_print "$_tax_year" "$_arg_tag" >"${_base_path}-${_ext_full}" + local _out_file="${_base_path}-${_ext_full}" + + lib_taxes::__taxes_print "$_tax_year" "$_arg_tag" >"${_out_file}" lib_utils::catch $? + + if [[ ! -f "$_out_file" || ! -s "$_out_file" ]]; then + lib_utils::print_warning "Nothing generated for '${_arg_tag}' (no taxable event found for the year ${global_arg_year})" + fi done # Patch transparent (full) reports @@ -1207,6 +1232,9 @@ function lib_taxes::__reports_patch() # Verify success of patches # + lib_utils::print_custom " \e[32m│\e[0m\t\e[37;2m ... Verifying patches (full) ...\e[0m\n" + lib_utils::print_custom " \e[32m│\e[0m\n" + lib_taxes::__reports_patch_verify "$_income" "${_spends_tags[@]}" "${_trades_tags[@]}" lib_taxes::__reports_patch_verify "$_spends" "${_trades_tags[@]}" "${_income_tags[@]}" lib_taxes::__reports_patch_verify "$_trades" "${_income_tags[@]}" "${_spends_tags[@]}" @@ -1217,16 +1245,20 @@ function lib_taxes::__reports_patch_verify() local _file="$1" local _tags=("${@:2}") - [ ! -f "$_file" ] && lib_utils::die_fatal "$_file not found" + [[ -z "$_file" || -z "${_tags[*]}" ]] && lib_utils::die_fatal - xsv select "Action" "$_file" \ - | sort -u \ - | tail -n +2 \ - | while read _line; do - for _tag in "${_tags[@]}"; do - [[ "$_line" != "$_tag" ]] || lib_utils::die_fatal "Bad entry in $_file" + [ ! -f "$_file" ] && lib_utils::die_fatal "File not found: '${_file}'" + + if [ -s "$_file" ]; then + xsv select "Action" "$_file" \ + | sort -u \ + | tail -n +2 \ + | while read _line; do + for _tag in "${_tags[@]}"; do + [[ "$_line" != "$_tag" ]] || lib_utils::die_fatal "Bad entry in $_file" + done done - done + fi } # From 21967e711d3a17d46643ef59a3a6352fc93af04f Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Fri, 10 Jan 2025 19:04:13 -0800 Subject: [PATCH 2/2] container: lib_taxes: remove unused arg expansion, appease the linter shellcheck will complain about the need for expansions but the asked-for expansions will not actually be used (thus the TODO). --- container/src/finance/lib/internal/lib_taxes.bash | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/container/src/finance/lib/internal/lib_taxes.bash b/container/src/finance/lib/internal/lib_taxes.bash index a447029..502abcd 100644 --- a/container/src/finance/lib/internal/lib_taxes.bash +++ b/container/src/finance/lib/internal/lib_taxes.bash @@ -31,7 +31,7 @@ source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_utils.bash function lib_taxes::taxes() { lib_taxes::__parse_args "$@" - lib_taxes::__taxes "$@" + lib_taxes::__taxes lib_utils::catch $? } @@ -1285,6 +1285,8 @@ function lib_taxes::__reports_obfs() lib_taxes::__reports_obfs_gen } +# TODO: refactor to remove shellcheck +# shellcheck disable=SC2120 function lib_taxes::__reports_obfs_gen() { # Temp storage