diff --git a/proxy_docker/app/script/getactivewatches.sh b/proxy_docker/app/script/getactivewatches.sh index c93012c..c401244 100644 --- a/proxy_docker/app/script/getactivewatches.sh +++ b/proxy_docker/app/script/getactivewatches.sh @@ -4,60 +4,60 @@ . ./sql.sh getactivewatches() { - trace "Entering getactivewatches()..." + trace "Entering getactivewatches()..." - local watches - watches=$(sql "SELECT id, address, imported, callback0conf, callback1conf, inserted_ts FROM watching WHERE watching AND NOT calledback1conf") - returncode=$? - trace_rc ${returncode} + local watches + watches=$(sql "SELECT id, address, imported, callback0conf, callback1conf, inserted_ts FROM watching WHERE watching AND NOT calledback1conf") + returncode=$? + trace_rc ${returncode} - local id - local address - local imported - local inserted - local cb0conf_url - local cb1conf_url - local timestamp - local notfirst=false + local id + local address + local imported + local inserted + local cb0conf_url + local cb1conf_url + local timestamp + local notfirst=false - echo -n "{\"watches\":[" + echo -n "{\"watches\":[" - local IFS=$'\n' - for row in ${watches} - do - if ${notfirst}; then - echo "," - else - notfirst=true - fi - trace "[getactivewatches] row=${row}" - id=$(echo "${row}" | cut -d '|' -f1) - trace "[getactivewatches] id=${id}" - address=$(echo "${row}" | cut -d '|' -f2) - trace "[getactivewatches] address=${address}" - imported=$(echo "${row}" | cut -d '|' -f3) - trace "[getactivewatches] imported=${imported}" - cb0conf_url=$(echo "${row}" | cut -d '|' -f4) - trace "[getactivewatches] cb0conf_url=${cb0conf_url}" - cb1conf_url=$(echo "${row}" | cut -d '|' -f5) - trace "[getactivewatches] cb1conf_url=${cb1conf_url}" - timestamp=$(echo "${row}" | cut -d '|' -f6) - trace "[getactivewatches] timestamp=${timestamp}" + local IFS=$'\n' + for row in ${watches} + do + if ${notfirst}; then + echo "," + else + notfirst=true + fi + trace "[getactivewatches] row=${row}" + id=$(echo "${row}" | cut -d '|' -f1) + trace "[getactivewatches] id=${id}" + address=$(echo "${row}" | cut -d '|' -f2) + trace "[getactivewatches] address=${address}" + imported=$(echo "${row}" | cut -d '|' -f3) + trace "[getactivewatches] imported=${imported}" + cb0conf_url=$(echo "${row}" | cut -d '|' -f4) + trace "[getactivewatches] cb0conf_url=${cb0conf_url}" + cb1conf_url=$(echo "${row}" | cut -d '|' -f5) + trace "[getactivewatches] cb1conf_url=${cb1conf_url}" + timestamp=$(echo "${row}" | cut -d '|' -f6) + trace "[getactivewatches] timestamp=${timestamp}" - data="{\"id\":\"${id}\"," - data="${data}\"address\":\"${address}\"," - data="${data}\"imported\":\"${imported}\"," - data="${data}\"unconfirmedCallbackURL\":\"${cb0conf_url}\"," - data="${data}\"confirmedCallbackURL\":\"${cb1conf_url}\"," - data="${data}\"watching_since\":\"${timestamp}\"}" - trace "[getactivewatches] data=${data}" + data="{\"id\":\"${id}\"," + data="${data}\"address\":\"${address}\"," + data="${data}\"imported\":\"${imported}\"," + data="${data}\"unconfirmedCallbackURL\":\"${cb0conf_url}\"," + data="${data}\"confirmedCallbackURL\":\"${cb1conf_url}\"," + data="${data}\"watching_since\":\"${timestamp}\"}" + trace "[getactivewatches] data=${data}" - echo -n "${data}" - done + echo -n "${data}" + done - echo "]}" + echo "]}" - return ${returncode} + return ${returncode} } getactivewatchesbyxpub() { @@ -68,7 +68,7 @@ getactivewatchesbyxpub() { getactivewatchesxpub "pub32" ${xpub} returncode=$? - trace_rc ${returncode} + trace_rc ${returncode} return ${returncode} } @@ -81,7 +81,7 @@ getactivewatchesbylabel() { getactivewatchesxpub "label" ${label} returncode=$? - trace_rc ${returncode} + trace_rc ${returncode} return ${returncode} } @@ -93,64 +93,32 @@ getactivewatchesxpub() { trace "[getactivewatchesxpub] where=${where}" local value=${2} trace "[getactivewatchesxpub] value=${value}" - local watches - watches=$(sql "SELECT id, address, imported, callback0conf, callback1conf, inserted_ts, derivation_path, pub32_index FROM watching, watching_by_pub32 w32 WHERE watching_by_pub32_id = w32.id AND ${where} = \"${value}\" AND watching AND NOT calledback1conf") - returncode=$? - trace_rc ${returncode} + local watches - local id - local address - local imported - local inserted - local cb0conf_url - local cb1conf_url - local timestamp - local derivation_path - local pub32_index - local notfirst=false + # Let's build the string directly with sqlite instead of manipulating multiple strings afterwards, it's faster. + # {"id":"${id}","address":"${address}","imported":"${imported}","unconfirmedCallbackURL":"${cb0conf_url}","confirmedCallbackURL":"${cb1conf_url}","watching_since":"${timestamp}","derivation_path":"${derivation_path}","pub32_index":"${pub32_index}"} + watches=$(sql "SELECT '{\"id\":\"' || w.id || '\",\"address\":\"' || address || '\",\"imported\":\"' || imported || '\",\"unconfirmedCallbackURL\":\"' || w.callback0conf || '\",\"confirmedCallbackURL\":\"' || w.callback1conf || '\",\"watching_since\":\"' || w.inserted_ts || '\",\"derivation_path\":\"' || derivation_path || '\",\"pub32_index\":\"' || pub32_index || '\"}' FROM watching w, watching_by_pub32 w32 WHERE watching_by_pub32_id = w32.id AND ${where} = \"${value}\" AND w.watching AND NOT calledback1conf") + returncode=$? + trace_rc ${returncode} - echo -n "{\"watches\":[" + local notfirst=false - local IFS=$'\n' - for row in ${watches} - do - if ${notfirst}; then - echo "," - else - notfirst=true - fi - trace "[getactivewatchesxpub] row=${row}" - id=$(echo "${row}" | cut -d '|' -f1) - trace "[getactivewatchesxpub] id=${id}" - address=$(echo "${row}" | cut -d '|' -f2) - trace "[getactivewatchesxpub] address=${address}" - imported=$(echo "${row}" | cut -d '|' -f3) - trace "[getactivewatchesxpub] imported=${imported}" - cb0conf_url=$(echo "${row}" | cut -d '|' -f4) - trace "[getactivewatchesxpub] cb0conf_url=${cb0conf_url}" - cb1conf_url=$(echo "${row}" | cut -d '|' -f5) - trace "[getactivewatchesxpub] cb1conf_url=${cb1conf_url}" - timestamp=$(echo "${row}" | cut -d '|' -f6) - trace "[getactivewatchesxpub] timestamp=${timestamp}" - derivation_path=$(echo "${row}" | cut -d '|' -f7) - trace "[getactivewatchesxpub] derivation_path=${derivation_path}" - pub32_index=$(echo "${row}" | cut -d '|' -f8) - trace "[getactivewatchesxpub] pub32_index=${pub32_index}" + echo -n "{\"watches\":[" - data="{\"id\":\"${id}\"," - data="${data}\"address\":\"${address}\"," - data="${data}\"imported\":\"${imported}\"," - data="${data}\"unconfirmedCallbackURL\":\"${cb0conf_url}\"," - data="${data}\"confirmedCallbackURL\":\"${cb1conf_url}\"," - data="${data}\"watching_since\":\"${timestamp}\"," - data="${data}\"derivation_path\":\"${derivation_path}\"," - data="${data}\"pub32_index\":\"${pub32_index}\"}" - trace "[getactivewatchesxpub] data=${data}" + local IFS=$'\n' + for row in ${watches} + do + if ${notfirst}; then + echo "," + else + notfirst=true + fi + trace "[getactivewatchesxpub] row=${row}" - echo -n "${data}" - done + echo -n "${row}" + done - echo "]}" + echo "]}" - return ${returncode} + return ${returncode} }