speed optimations

This commit is contained in:
olli 2024-10-17 22:39:31 +02:00
parent f36485f1eb
commit dcf5336a75

View File

@ -29,7 +29,7 @@ function currency_converter {
unset f_currency_converter_result 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.*-.*: ]] if [[ $f_currency_target =~ ^20.*-.*: ]]
then then
@ -63,7 +63,7 @@ function currency_converter {
do do
# Link USD Stablecoin files to USD # Link USD Stablecoin files to USD
cd "$f_asset_histories" 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 do
f_link_file=${f_file/${f_stablecoin}/USD} f_link_file=${f_file/${f_stablecoin}/USD}
ln -sf "$f_file" "$f_link_file" ln -sf "$f_file" "$f_link_file"
@ -87,26 +87,36 @@ function currency_converter {
return 0 return 0
fi fi
# try direct pair # define possiblefiles
[ "${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}"
local f_histfile_default="${f_asset_histories}${f_currency_target}${f_currency}.history" 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_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 do
# search for most precise date # search for most precise date
f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | tail -n1) f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1)
set -x [ -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)
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_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_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) [ -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) [ -n "$f_line" ] && f_rate=$(echo "$f_line" | cut -d, -f2)
set +x
f_reverse=false f_reverse=false
if [ -n "$f_rate" ] if [ -n "$f_rate" ]
then then
@ -116,7 +126,6 @@ function currency_converter {
[ $f_currency_target = "EUR" ] && [ $f_currency = "USD" ] && f_reverse=true [ $f_currency_target = "EUR" ] && [ $f_currency = "USD" ] && f_reverse=true
[[ $f_line =~ ^$f_currency_date_hour ]] && break [[ $f_line =~ ^$f_currency_date_hour ]] && break
fi fi
done
# end if no rate found # end if no rate found
if [ -z "$f_rate" ] if [ -z "$f_rate" ]
@ -143,3 +152,6 @@ function currency_converter {
} }