From dcf5336a752f1475033c82b066bca50cfb7a87c3 Mon Sep 17 00:00:00 2001 From: olli Date: Thu, 17 Oct 2024 22:39:31 +0200 Subject: [PATCH] speed optimations --- dabo/functions/currency_converter.sh | 62 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/dabo/functions/currency_converter.sh b/dabo/functions/currency_converter.sh index 0eacbd0..21026d2 100644 --- a/dabo/functions/currency_converter.sh +++ b/dabo/functions/currency_converter.sh @@ -29,7 +29,7 @@ function currency_converter { unset f_currency_converter_result - local f_line f_rate f_histfile f_date_array f_stablecoin f_reverse f_file f_link_file + local f_line f_rate f_histfile f_date_array f_stablecoin f_reverse f_file f_link_file f_timeframe if [[ $f_currency_target =~ ^20.*-.*: ]] then @@ -63,7 +63,7 @@ function currency_converter { do # Link USD Stablecoin files to USD cd "$f_asset_histories" - find . -name "*${f_stablecoin}.history.*.csv" | while read f_file + find . -maxdepth 1 -mindepth 1 -name "*${f_stablecoin}.history.*.csv" | while read f_file do f_link_file=${f_file/${f_stablecoin}/USD} ln -sf "$f_file" "$f_link_file" @@ -86,38 +86,47 @@ function currency_converter { f_currency_converter_result=$f_currency_amount return 0 fi - - # try direct pair - [ "${f_currency}" = "USD" ] && get_marketdata_coinmarketcap "${f_currency_target}-${f_currency}" "${f_currency_target}${f_currency}" - [ "${f_currency_target}" = "USD" ] && get_marketdata_coinmarketcap "${f_currency}-${f_currency_target}" "${f_currency}${f_currency_target}" + + # define possiblefiles local f_histfile_default="${f_asset_histories}${f_currency_target}${f_currency}.history" - local f_histfile_coinmarketcap="${f_asset_histories}${f_currency_target}${f_currency}.history" - # reverse as backup local f_histfile_default_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history" - local f_histfile_coinmarketcap_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history" - # search for rate by date - for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse" "$f_histfile_coinmarketcap" "$f_histfile_coinmarketcap_reverse" + for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse" do # search for most precise date - f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | tail -n1) - set -x + f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1) [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_hour" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1) [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_day" "$f_histfile"*h.csv 2>/dev/null | sort | tail -n1) - [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_month" "$f_histfile"*.csv 2>/dev/null | sort | tail -n1) - [ -n "$f_line" ] && f_rate=$(echo "$f_line" | cut -d, -f2) - set +x - f_reverse=false - if [ -n "$f_rate" ] - then - [[ $f_histfile =~ ${f_currency}${f_currency_target} ]] && f_reverse=true - [ $f_currency_target = "USD" ] && f_reverse=true - [ $f_currency = "USD" ] && f_reverse=false - [ $f_currency_target = "EUR" ] && [ $f_currency = "USD" ] && f_reverse=true - [[ $f_line =~ ^$f_currency_date_hour ]] && break - fi done + # download from coinmarketcap if nothing found + [ -z "$f_line" ] && for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse" + do + # download data from coinmarketcap + for f_timeframe in 1d 1w + do + [ "${f_currency}" = "USD" ] && get_marketdata_coinmarketcap "${f_currency_target}-${f_currency}" "${f_currency_target}${f_currency}" $f_timeframe + [ "${f_currency_target}" = "USD" ] && get_marketdata_coinmarketcap "${f_currency}-${f_currency_target}" "${f_currency}${f_currency_target}" + done + f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1) + [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_hour" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1) + [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_day" "$f_histfile"*h.csv 2>/dev/null | sort | tail -n1) + [ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_month" "$f_histfile"*.csv 2>/dev/null | sort | tail -n1) + [ -n "$f_line" ] && break + done + + # extract rate/price + [ -n "$f_line" ] && f_rate=$(echo "$f_line" | cut -d, -f2) + f_reverse=false + if [ -n "$f_rate" ] + then + [[ $f_histfile =~ ${f_currency}${f_currency_target} ]] && f_reverse=true + [ $f_currency_target = "USD" ] && f_reverse=true + [ $f_currency = "USD" ] && f_reverse=false + [ $f_currency_target = "EUR" ] && [ $f_currency = "USD" ] && f_reverse=true + [[ $f_line =~ ^$f_currency_date_hour ]] && break + fi + # end if no rate found if [ -z "$f_rate" ] then @@ -143,3 +152,6 @@ function currency_converter { } + + +