2020-02-06 11:04:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-02-06 13:12:25 +00:00
|
|
|
#
|
|
|
|
# Generate a dot file to create graph of RERO-ils data (orga, lib and users)
|
|
|
|
#
|
2020-02-06 11:04:27 +00:00
|
|
|
|
2020-02-07 09:04:48 +00:00
|
|
|
# Usage:
|
|
|
|
# bash gen.sh output.dot
|
|
|
|
# where `output.dot` is the output file that permit to generate a graph.
|
|
|
|
# To generate the graph:
|
|
|
|
# dot -Tsvg output.dot -o organisations.svg
|
|
|
|
# where `output.dot` is the previous file. And `organisations.svg` the final schema you want to display.
|
|
|
|
|
2020-02-06 18:32:37 +00:00
|
|
|
# CONFIG
|
2020-02-06 13:12:25 +00:00
|
|
|
RERO_DIR="${HOME}/projets/rero/rero-ils"
|
2020-02-07 09:45:16 +00:00
|
|
|
# pastel colors (Cf. https://www.color-hex.com/color-palette/5361)
|
|
|
|
COLOR1="#ffb3ba"
|
|
|
|
COLOR2="#ffdfba"
|
|
|
|
COLOR3="#ffffba"
|
|
|
|
COLOR4="#baffc9"
|
|
|
|
COLOR5="#bae1ff"
|
|
|
|
COMP_COLOR4="#ffd8ba"
|
|
|
|
# colors you choose for different objects
|
|
|
|
ORGA_COLOR="${COLOR1}" # organisations
|
|
|
|
LIB_COLOR="${COLOR2}" # libraries
|
|
|
|
PT_COLOR="${COLOR3}" # patron_types
|
|
|
|
USER_COLOR="${COLOR5}" # users/patrons
|
2020-02-06 11:04:27 +00:00
|
|
|
|
2020-02-06 18:32:37 +00:00
|
|
|
# Built variables
|
2020-02-07 10:01:48 +00:00
|
|
|
outputfile="$1"
|
2020-02-06 13:12:25 +00:00
|
|
|
SRC_DIR="${RERO_DIR}/data/"
|
2020-02-07 11:20:33 +00:00
|
|
|
tmpl_gizmo="templates/gizmo.tmpl" # A colored polygon. Represents an entity.
|
|
|
|
tmpl_link="templates/link.tmpl" # A link between two entities.
|
|
|
|
tmpl_label="templates/label_default.tmpl" # Default label for entities.
|
2020-02-06 11:04:27 +00:00
|
|
|
|
2020-02-06 18:32:37 +00:00
|
|
|
# TESTS
|
2020-02-07 10:01:48 +00:00
|
|
|
if [[ -z "${outputfile}" ]]; then
|
2020-02-06 18:32:37 +00:00
|
|
|
echo "No output file given"
|
|
|
|
exit 1
|
2020-02-07 10:01:48 +00:00
|
|
|
elif [[ -f "${outputfile}" ]]; then
|
2020-02-06 18:32:37 +00:00
|
|
|
echo "File already exists!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-02-07 10:01:48 +00:00
|
|
|
# FUNCTIONS
|
2020-02-07 11:20:33 +00:00
|
|
|
# WRITE first argument in output file
|
|
|
|
send() {
|
2020-02-07 10:01:48 +00:00
|
|
|
echo -e "$1" >> "$outputfile"
|
|
|
|
}
|
2020-02-07 11:20:33 +00:00
|
|
|
# render: SHOW template (given in $1) after variables REPLACEMENTS
|
|
|
|
render() {
|
|
|
|
template="$(cat $1)"
|
|
|
|
eval "echo -e \"${template}\""
|
|
|
|
}
|
|
|
|
# save: WRITE template (given in $1) in outputfile after variables REPLACEMENTS
|
|
|
|
save() {
|
|
|
|
rendered_template="$(render $1)"
|
|
|
|
send "${rendered_template}"
|
|
|
|
}
|
2020-02-07 10:01:48 +00:00
|
|
|
|
|
|
|
# Output file initialization
|
|
|
|
echo "" > "$outputfile" # flush output file
|
2020-02-07 11:20:33 +00:00
|
|
|
save "templates/header.tmpl"
|
2020-02-06 11:04:27 +00:00
|
|
|
|
|
|
|
# ORGANISATIONS
|
2020-02-06 13:12:25 +00:00
|
|
|
orga_file="${SRC_DIR}organisations.json"
|
2020-02-07 11:20:33 +00:00
|
|
|
shape="box"
|
|
|
|
color="${ORGA_COLOR}"
|
2020-02-06 11:04:27 +00:00
|
|
|
cat "${orga_file}"|jq -c '.[] | {
|
|
|
|
name,
|
|
|
|
pid,
|
|
|
|
code }'| while read orga
|
|
|
|
do
|
|
|
|
# take important info
|
|
|
|
pid=$(echo $orga|jq -r .pid)
|
|
|
|
code=$(echo $orga|jq -r .code)
|
|
|
|
name=$(echo $orga|jq -r .name)
|
|
|
|
# write result in output
|
2020-02-07 11:20:33 +00:00
|
|
|
identifier="Orga${pid}"
|
|
|
|
label="$(render ${tmpl_label})"
|
|
|
|
save "${tmpl_gizmo}"
|
2020-02-06 11:04:27 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# LIBRARIES
|
2020-02-06 13:12:25 +00:00
|
|
|
lib_file="${SRC_DIR}libraries.json"
|
2020-02-07 11:20:33 +00:00
|
|
|
shape="house"
|
|
|
|
color="${LIB_COLOR}"
|
2020-02-06 11:04:27 +00:00
|
|
|
cat "${lib_file}"|jq -c '.[] | {
|
|
|
|
name,
|
|
|
|
pid,
|
2020-02-06 13:12:25 +00:00
|
|
|
code,
|
2020-02-06 11:04:27 +00:00
|
|
|
organisation: .organisation."$ref"}'|while read lib
|
|
|
|
do
|
|
|
|
pid=$(echo $lib|jq -r .pid)
|
|
|
|
name=$(echo $lib|jq -r .name)
|
2020-02-06 13:12:25 +00:00
|
|
|
code=$(echo $lib|jq -r .code)
|
2020-02-06 11:04:27 +00:00
|
|
|
orga=$(echo $lib|jq -r .organisation)
|
|
|
|
orga_pid=$(echo $orga|rev|cut -d "/" -f 1|rev)
|
|
|
|
# write result in output
|
2020-02-07 11:20:33 +00:00
|
|
|
identifier="Lib${pid}"
|
|
|
|
label="$(render ${tmpl_label})"
|
|
|
|
save "${tmpl_gizmo}"
|
|
|
|
relation="Orga${orga_pid}"
|
|
|
|
save "${tmpl_link}"
|
2020-02-06 11:04:27 +00:00
|
|
|
done
|
|
|
|
|
2020-02-07 09:44:20 +00:00
|
|
|
# PATRON_TYPES
|
|
|
|
pt_file="${SRC_DIR}patron_types.json"
|
2020-02-07 11:20:33 +00:00
|
|
|
shape="polygon"
|
|
|
|
color="${PT_COLOR}"
|
|
|
|
additionals="sides=7"
|
2020-02-07 09:44:20 +00:00
|
|
|
cat "${pt_file}" |jq -c '.[] | {
|
|
|
|
pid,
|
|
|
|
name,
|
|
|
|
organisation: .organisation."$ref"}'|while read pt
|
|
|
|
do
|
|
|
|
pid=$(echo $pt|jq -r .pid)
|
|
|
|
name=$(echo $pt|jq -r .name)
|
|
|
|
orga=$(echo $pt|jq -r .organisation)
|
|
|
|
# write result in output
|
2020-02-07 11:20:33 +00:00
|
|
|
identifier="Type${pid}"
|
|
|
|
label="$(render templates/label_patron_types.tmpl)"
|
|
|
|
save "${tmpl_gizmo}"
|
|
|
|
|
2020-02-07 09:44:20 +00:00
|
|
|
# Make a link with organisation if present
|
|
|
|
if [[ -n "${orga}" ]]; then
|
|
|
|
orga_pid=$(echo $orga|rev|cut -d "/" -f 1|rev)
|
2020-02-07 11:20:33 +00:00
|
|
|
relation="Orga${orga_pid}"
|
|
|
|
save "${tmpl_link}"
|
2020-02-07 09:44:20 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-02-06 11:04:27 +00:00
|
|
|
# USERS
|
2020-02-06 13:12:25 +00:00
|
|
|
user_file="${SRC_DIR}users.json"
|
2020-02-07 11:20:33 +00:00
|
|
|
shape="ellipse"
|
|
|
|
additionals=''
|
|
|
|
color="${USER_COLOR}"
|
2020-02-06 11:04:27 +00:00
|
|
|
cat "${user_file}"|jq -c '.[] | {
|
|
|
|
email,
|
|
|
|
first_name,
|
|
|
|
last_name,
|
2020-02-06 18:32:37 +00:00
|
|
|
barcode,
|
|
|
|
roles,
|
2020-02-07 09:44:20 +00:00
|
|
|
library: .library."$ref",
|
|
|
|
pt: .patron_type."$ref"}'|while read user
|
2020-02-06 11:04:27 +00:00
|
|
|
do
|
|
|
|
email=$(echo $user|jq -r .email)
|
|
|
|
first_name=$(echo $user|jq -r .first_name)
|
|
|
|
last_name=$(echo $user|jq -r .last_name)
|
2020-02-06 18:32:37 +00:00
|
|
|
barcode=$(echo $user|jq -r .barcode)
|
|
|
|
roles=$(echo $user|jq -r .roles[])
|
2020-02-06 11:04:27 +00:00
|
|
|
library=$(echo $user|jq -r .library)
|
|
|
|
library_pid=$(echo $library|rev|cut -d "/" -f 1|rev)
|
2020-02-07 09:44:20 +00:00
|
|
|
pt=$(echo $user|jq -r .pt)
|
2020-02-06 18:32:37 +00:00
|
|
|
|
2020-02-07 11:20:33 +00:00
|
|
|
# Prepare additional info
|
2020-02-06 18:32:37 +00:00
|
|
|
if [[ "${roles}" != "null" ]]; then
|
2020-02-07 09:45:16 +00:00
|
|
|
displayed_roles="roles: "
|
2020-02-06 18:32:37 +00:00
|
|
|
for role in ${roles}; do
|
2020-02-07 09:45:16 +00:00
|
|
|
displayed_roles="${displayed_roles}<font color='red' >${role}</font>, "
|
2020-02-06 18:32:37 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
# Don't display barcode if no one
|
|
|
|
info=""
|
|
|
|
if [[ "${barcode}" != "null" ]]; then
|
|
|
|
info="${info}<br/>${barcode}"
|
|
|
|
fi
|
|
|
|
# Same for roles
|
|
|
|
if [[ -n "${displayed_roles}" ]]; then
|
|
|
|
info="${info}<br/>${displayed_roles}"
|
|
|
|
fi
|
|
|
|
|
2020-02-07 11:20:33 +00:00
|
|
|
# write result in output
|
|
|
|
identifier="User_${email}"
|
|
|
|
label="$(render templates/label_users.tmpl)"
|
|
|
|
save "${tmpl_gizmo}"
|
2020-02-06 18:32:37 +00:00
|
|
|
|
|
|
|
# Display a link if library_pid is not null
|
|
|
|
if [[ "${library_pid}" != "null" ]]; then
|
2020-02-07 11:20:33 +00:00
|
|
|
relation="Lib${library_pid}"
|
|
|
|
save "${tmpl_link}"
|
2020-02-06 18:32:37 +00:00
|
|
|
fi
|
2020-02-07 09:44:20 +00:00
|
|
|
|
|
|
|
# Display a link if patron_type is not null
|
|
|
|
if [[ "$pt" != "null" ]]; then
|
|
|
|
pt_id=$(echo $pt|rev| cut -d "/" -f 1|rev)
|
2020-02-07 11:20:33 +00:00
|
|
|
relation="Type${pt_id}"
|
|
|
|
save "${tmpl_link}"
|
2020-02-07 09:44:20 +00:00
|
|
|
fi
|
2020-02-06 11:04:27 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# end of file
|
2020-02-07 11:20:33 +00:00
|
|
|
save "templates/footer.tmpl"
|
2020-02-06 11:04:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# END of program
|
|
|
|
exit 0
|