-
Notifications
You must be signed in to change notification settings - Fork 128
/
analysis.sh
executable file
·77 lines (65 loc) · 1.89 KB
/
analysis.sh
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
#!/bin/bash
set -e
SCRIPT_PATH=$(dirname ${BASH_SOURCE[0]})
source $SCRIPT_PATH/base.sh
function create_database() {
database_filename="$1"; shift
clean="$1"
rm -rf "$database_filename"
for table in boletim caso obito_cartorio caso_full; do
filename="data/output/${table}.csv.gz"
if [ "$clean" = "--clean" ]; then
rm -rf "$filename"
fi
if [ -e "$filename" ]; then
echo "Using already downloaded $filename as $table"
else
echo "Downloading $table"
url="https://data.brasil.io/dataset/covid19/${table}.csv.gz"
rm -rf "$filename"
wget -q -c -t 0 -O "$filename" "$url"
fi
rows csv2sqlite --schemas=schema/${table}.csv "$filename" "$database_filename"
done
rows csv2sqlite \
--schemas=schema/{populacao-por-municipio,epidemiological-week}.csv \
data/{epidemiological-week,populacao-por-municipio-2020}.csv \
$database_filename
}
function execute_sql_file_no_output() {
database=$1; shift
filename=$1
cat "$filename" | sqlite3 "$database"
}
function execute_sql_file_with_output() {
database=$1; shift
sql_filename=$1; shift
output_filename=$1
rows query \
"$(cat $sql_filename)" \
"$database" \
--output="$output_filename"
}
function setup_database() {
database=$1; shift
for filename in sql/*setup.sql; do
echo "Running setup $filename"
execute_sql_file_no_output "$database" "$filename"
done
}
function execute_sql_files() {
database=$1; shift
files=$@
for filename in $files; do
if [[ "$(basename $filename)" != *"setup.sql" ]]; then
echo "Executing $filename and exporting data"
output="data/analysis/$(basename $filename | sed 's/\.sql$/.csv.gz/')"
execute_sql_file_with_output "$DATABASE" "$filename" "$output"
fi
done
}
DATABASE="$SCRIPT_PATH/data/covid19.sqlite"
mkdir -p "$SCRIPT_PATH/data/analysis" "$SCRIPT_PATH/data/output"
create_database "$DATABASE" "$1"
setup_database "$DATABASE"
execute_sql_files "$DATABASE" $SCRIPT_PATH/sql/*.sql