aboutsummaryrefslogtreecommitdiff
path: root/contrib/populate-stats.sh
blob: 319ffd9cfaa889b8849406b7b2e84cee90d7e689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash

# This script populates the stats table, to test the /monitor API.

usage() {
  echo "Usage: ./populate-stats.sh CONFIG_FILE [--one]"
  echo
  echo "Populates the stats table with random data"
  echo
  echo Parameters:
  echo
  echo --one instead of random amounts, it always uses 1.0
}

# Detecting the help case.
if test "$1" = "--help" -o "$1" = "-h" -o -z ${1:-};
  then usage
  exit
fi

HAS_ONE=0
if test "$2" = "--one";
  then HAS_ONE=1
fi

set -eu
DB_NAME=$(taler-config -c $1 -s libeufin-bankdb-postgres -o config)
echo Running on the database: $DB_NAME

# random number in range $1-$2
rnd () {
  shuf -i $1-$2 -n1
}

insert_stat_one () {
  echo "
    SET search_path TO libeufin_bank;
    CALL libeufin_bank.stats_register_payment (
      'taler_in'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,(1, 0)::taler_amount
      ,null
    );
    CALL libeufin_bank.stats_register_payment (
      'taler_out'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,(1, 0)::taler_amount
      ,null
    );
    CALL libeufin_bank.stats_register_payment (
      'cashin'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,(1, 0)::taler_amount
      ,(1, 0)::taler_amount
    );
    CALL libeufin_bank.stats_register_payment (
      'cashout'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,(1, 0)::taler_amount
      ,(1, 0)::taler_amount
    );"
}

insert_stat_rand () {
  echo "
    SET search_path TO libeufin_bank;
    CALL libeufin_bank.stats_register_payment (
      'taler_in'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount
      ,null
    );
    CALL libeufin_bank.stats_register_payment (
      'taler_out'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount
      ,null
    );
    CALL libeufin_bank.stats_register_payment (
      'cashin'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount
      ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount
    );
    CALL libeufin_bank.stats_register_payment (
      'cashout'::text
      ,TO_TIMESTAMP($1)::timestamp
      ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount
      ,($(rnd 0 99999999), $(rnd 0 99999999))::taler_amount
    );"
}

for n_hour_ago in `seq 1 100`; do
  echo -n .
  TIMESTAMP=$(date --date="${n_hour_ago} hour ago" +%s)
  if test $HAS_ONE = 1; then
    psql $DB_NAME -c "$(insert_stat_one ${TIMESTAMP})" > /dev/null
  else
    psql $DB_NAME -c "$(insert_stat_rand ${TIMESTAMP})" > /dev/null
  fi
done