merchant landing: recent activity split GOA/CHF, 5 each

Stats script emits recent_activity_by_currency (limit 5); UI shows
side-by-side columns instead of one long mixed list.
This commit is contained in:
Hernâni Marques 2026-07-10 20:19:59 +02:00
parent f22158e369
commit 458472e177
No known key found for this signature in database
2 changed files with 183 additions and 82 deletions

View file

@ -206,18 +206,22 @@ for item in $SCHEMAS; do
REFUNDS=$((REFUNDS + r))
done
# --- recent activity ---
PAY_LIMIT=10
REF_LIMIT=5
{
echo "SELECT * FROM ("
echo "("
echo "SELECT * FROM ("
first=1
for item in $SCHEMAS; do
sch=${item%%:*}
if [ "$first" = 1 ]; then first=0; else echo " UNION ALL "; fi
cat <<SQL
# --- recent activity: 5 latest events per currency (GOA + CHF), payments + refunds ---
ACT_PER_CURRENCY="${ACT_PER_CURRENCY:-5}"
# Query latest ACT_PER_CURRENCY events for one currency code (payments refunds).
# Writes TSV rows: ts kind order_id amount summary status
query_activity_for_currency() {
local cur="$1"
local out="$2"
{
echo "SELECT * FROM ("
echo "SELECT * FROM ("
first=1
for item in $SCHEMAS; do
sch=${item%%:*}
if [ "$first" = 1 ]; then first=0; else echo " UNION ALL "; fi
cat <<SQL
SELECT creation_time AS ts,
'payment'::text AS kind,
order_id,
@ -226,21 +230,18 @@ SELECT creation_time AS ts,
CASE WHEN wired THEN 'wired' ELSE 'paid' END AS status
FROM ${sch}.merchant_contract_terms
WHERE paid
AND upper(split_part(coalesce(contract_terms->>'amount',''), ':', 1)) = upper('${cur}')
SQL
done
if [ "$first" = 1 ]; then
echo "SELECT 0::bigint AS ts, ''::text AS kind, ''::text AS order_id, ''::text AS amount, ''::text AS summary, ''::text AS status WHERE false"
fi
echo ") pay ORDER BY ts DESC LIMIT ${PAY_LIMIT}"
echo ")"
echo "UNION ALL"
echo "("
echo "SELECT * FROM ("
first=1
for item in $SCHEMAS; do
sch=${item%%:*}
if [ "$first" = 1 ]; then first=0; else echo " UNION ALL "; fi
cat <<SQL
done
if [ "$first" = 1 ]; then
echo "SELECT 0::bigint AS ts, ''::text AS kind, ''::text AS order_id, ''::text AS amount, ''::text AS summary, ''::text AS status WHERE false"
fi
echo " UNION ALL "
first=1
for item in $SCHEMAS; do
sch=${item%%:*}
if [ "$first" = 1 ]; then first=0; else echo " UNION ALL "; fi
cat <<SQL
SELECT r.refund_timestamp AS ts,
'refund'::text AS kind,
ct.order_id,
@ -255,17 +256,80 @@ SELECT r.refund_timestamp AS ts,
'refunded'::text AS status
FROM ${sch}.merchant_refunds r
JOIN ${sch}.merchant_contract_terms ct ON ct.order_serial = r.order_serial
WHERE upper((r.refund_amount).curr) = upper('${cur}')
SQL
done
if [ "$first" = 1 ]; then
echo "SELECT 0::bigint AS ts, ''::text AS kind, ''::text AS order_id, ''::text AS amount, ''::text AS summary, ''::text AS status WHERE false"
done
if [ "$first" = 1 ]; then
echo "SELECT 0::bigint AS ts, ''::text AS kind, ''::text AS order_id, ''::text AS amount, ''::text AS summary, ''::text AS status WHERE false"
fi
echo ") u ORDER BY ts DESC LIMIT ${ACT_PER_CURRENCY}"
echo ") act;"
} | psql_pipe >"$out" || abort "activity query failed for $cur"
}
# Build JSON array of activity items from a TSV file
activity_tsv_to_json() {
local tsv_file="$1"
local json="["
local af=1
local ts kind oid amt sum st sec human_fmt iso
while IFS=$'\t' read -r ts kind oid amt sum st; do
[ -z "${ts:-}" ] && continue
[ "$ts" = "0" ] && [ -z "$kind" ] && continue
sec=$(awk -v t="$ts" 'BEGIN{printf "%d", int(t/1000000)}')
human_fmt=$(date -d "@${sec}" +"%Y-%m-%d %H:%M %Z" 2>/dev/null || echo "$sec")
iso=$(date -d "@${sec}" +"%Y-%m-%dT%H:%M:%S%z" 2>/dev/null | sed -E 's/([+-][0-9]{2})([0-9]{2})$/\1:\2/' || true)
if [ "$af" = 1 ]; then af=0; else json="${json},"; fi
json="${json}
{
\"ts_us\": ${ts:-0},
\"ts\": $(json_str "$iso"),
\"ts_human\": $(json_str "$human_fmt"),
\"kind\": $(json_str "$kind"),
\"order_id\": $(json_str "$oid"),
\"amount\": $(json_str "$amt"),
\"summary\": $(json_str "$sum"),
\"status\": $(json_str "$st")
}"
done <"$tsv_file"
json="${json}
]"
printf '%s' "$json"
}
query_activity_for_currency GOA "/tmp/merch_act_goa_$$.tsv"
query_activity_for_currency CHF "/tmp/merch_act_chf_$$.tsv"
ACT_GOA_JSON=$(activity_tsv_to_json "/tmp/merch_act_goa_$$.tsv")
ACT_CHF_JSON=$(activity_tsv_to_json "/tmp/merch_act_chf_$$.tsv")
rm -f "/tmp/merch_act_goa_$$.tsv" "/tmp/merch_act_chf_$$.tsv"
# Flat recent_activity: GOA then CHF (compat; landing prefers by_currency)
ACT_JSON="["
af=1
for block in "$ACT_GOA_JSON" "$ACT_CHF_JSON"; do
# strip outer [ ] and inject if non-empty
inner=$(printf '%s' "$block" | sed '1s/^\s*\[//; $s/\]\s*$//')
# empty array → skip
if printf '%s' "$inner" | grep -q '"kind"'; then
if [ "$af" = 1 ]; then af=0; else ACT_JSON="${ACT_JSON},"; fi
ACT_JSON="${ACT_JSON}${inner}"
fi
echo ") ref ORDER BY ts DESC LIMIT ${REF_LIMIT}"
echo ")"
echo ") act ORDER BY ts DESC;"
} | psql_pipe >"/tmp/merch_act_$$.tsv" || abort "activity query failed"
ACT_TSV=$(cat "/tmp/merch_act_$$.tsv" 2>/dev/null || true)
rm -f "/tmp/merch_act_$$.tsv"
done
ACT_JSON="${ACT_JSON}
]"
ACT_BY_CUR_JSON="[
{
\"currency\": \"GOA\",
\"limit\": ${ACT_PER_CURRENCY},
\"items\": ${ACT_GOA_JSON}
},
{
\"currency\": \"CHF\",
\"limit\": ${ACT_PER_CURRENCY},
\"items\": ${ACT_CHF_JSON}
}
]"
# by_currency JSON — GOA then CHF then rest
CUR_JSON="["
@ -297,30 +361,6 @@ done
CUR_JSON="${CUR_JSON}
]"
ACT_JSON="["
af=1
while IFS=$'\t' read -r ts kind oid amt sum st; do
[ -z "${ts:-}" ] && continue
[ "$ts" = "0" ] && [ -z "$kind" ] && continue
sec=$(awk -v t="$ts" 'BEGIN{printf "%d", int(t/1000000)}')
human_fmt=$(date -d "@${sec}" +"%Y-%m-%d %H:%M %Z" 2>/dev/null || echo "$sec")
iso=$(date -d "@${sec}" +"%Y-%m-%dT%H:%M:%S%z" 2>/dev/null | sed -E 's/([+-][0-9]{2})([0-9]{2})$/\1:\2/' || true)
if [ "$af" = 1 ]; then af=0; else ACT_JSON="${ACT_JSON},"; fi
ACT_JSON="${ACT_JSON}
{
\"ts_us\": ${ts:-0},
\"ts\": $(json_str "$iso"),
\"ts_human\": $(json_str "$human_fmt"),
\"kind\": $(json_str "$kind"),
\"order_id\": $(json_str "$oid"),
\"amount\": $(json_str "$amt"),
\"summary\": $(json_str "$sum"),
\"status\": $(json_str "$st")
}"
done <<<"$ACT_TSV"
ACT_JSON="${ACT_JSON}
]"
GEN=$(now_iso)
HUMAN=$(now_human)
@ -374,6 +414,8 @@ cat >"$TMP" <<EOF
"deposits": ${DEPOSITS:-0},
"refunds": ${REFUNDS:-0},
"by_currency": $CUR_JSON,
"recent_activity_limit_per_currency": ${ACT_PER_CURRENCY},
"recent_activity_by_currency": $ACT_BY_CUR_JSON,
"recent_activity": $ACT_JSON,
"performance": {
"config_http": $(json_str "$CONFIG_HTTP"),