populate-stats.sh (2552B)
1 #!/bin/bash 2 3 # This script populates the stats table, to test the /monitor API. 4 5 usage() { 6 echo "Usage: ./populate-stats.sh CONFIG_FILE [--one]" 7 echo 8 echo "Populates the stats table with random data" 9 echo 10 echo Parameters: 11 echo 12 echo --one instead of random amounts, it always uses 1.0 13 } 14 15 # Detecting the help case. 16 if test "$1" = "--help" -o "$1" = "-h" -o -z ${1:-}; 17 then usage 18 exit 19 fi 20 21 HAS_ONE=0 22 if test "$2" = "--one"; 23 then HAS_ONE=1 24 fi 25 26 set -eu 27 DB_NAME=$(taler-exchange-config -c $1 -s libeufin-bankdb-postgres -o config) 28 echo Running on the database: $DB_NAME 29 30 # random number in range $1-$2 31 rnd () { 32 shuf -i $1-$2 -n1 33 } 34 35 insert_stat_one () { 36 echo " 37 SET search_path TO libeufin_bank; 38 CALL libeufin_bank.stats_register_payment ( 39 'taler_in'::text 40 ,TO_TIMESTAMP($1)::timestamp 41 ,(1, 0)::taler_amount 42 ,null 43 ); 44 CALL libeufin_bank.stats_register_payment ( 45 'taler_out'::text 46 ,TO_TIMESTAMP($1)::timestamp 47 ,(1, 0)::taler_amount 48 ,null 49 ); 50 CALL libeufin_bank.stats_register_payment ( 51 'cashin'::text 52 ,TO_TIMESTAMP($1)::timestamp 53 ,(1, 0)::taler_amount 54 ,(1, 0)::taler_amount 55 ); 56 CALL libeufin_bank.stats_register_payment ( 57 'cashout'::text 58 ,TO_TIMESTAMP($1)::timestamp 59 ,(1, 0)::taler_amount 60 ,(1, 0)::taler_amount 61 );" 62 } 63 64 insert_stat_rand () { 65 echo " 66 SET search_path TO libeufin_bank; 67 CALL libeufin_bank.stats_register_payment ( 68 'taler_in'::text 69 ,TO_TIMESTAMP($1)::timestamp 70 ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount 71 ,null 72 ); 73 CALL libeufin_bank.stats_register_payment ( 74 'taler_out'::text 75 ,TO_TIMESTAMP($1)::timestamp 76 ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount 77 ,null 78 ); 79 CALL libeufin_bank.stats_register_payment ( 80 'cashin'::text 81 ,TO_TIMESTAMP($1)::timestamp 82 ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount 83 ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount 84 ); 85 CALL libeufin_bank.stats_register_payment ( 86 'cashout'::text 87 ,TO_TIMESTAMP($1)::timestamp 88 ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount 89 ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount 90 );" 91 } 92 93 for n_hour_ago in `seq 1 100`; do 94 echo -n . 95 TIMESTAMP=$(date --date="${n_hour_ago} hour ago" +%s) 96 if test $HAS_ONE = 1; then 97 psql $DB_NAME -c "$(insert_stat_one ${TIMESTAMP})" > /dev/null 98 else 99 psql $DB_NAME -c "$(insert_stat_rand ${TIMESTAMP})" > /dev/null 100 fi 101 done