Archived
1
0

Orgas: Add template system to render graph in output file

This commit is contained in:
Olivier DOSSMANN 2020-02-07 12:20:33 +01:00
parent ba8093b2b0
commit 3be903d794
8 changed files with 59 additions and 24 deletions

View File

@ -28,6 +28,9 @@ USER_COLOR="${COLOR5}" # users/patrons
# Built variables # Built variables
outputfile="$1" outputfile="$1"
SRC_DIR="${RERO_DIR}/data/" SRC_DIR="${RERO_DIR}/data/"
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.
# TESTS # TESTS
if [[ -z "${outputfile}" ]]; then if [[ -z "${outputfile}" ]]; then
@ -39,19 +42,29 @@ elif [[ -f "${outputfile}" ]]; then
fi fi
# FUNCTIONS # FUNCTIONS
# write first argument in output file # WRITE first argument in output file
output() { send() {
echo -e "$1" >> "$outputfile" echo -e "$1" >> "$outputfile"
} }
# 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}"
}
# Output file initialization # Output file initialization
echo "" > "$outputfile" # flush output file echo "" > "$outputfile" # flush output file
output "digraph {" save "templates/header.tmpl"
output "rankdir = RL;"
output "label = \"Link between organisations, libraries and users.\";"
# ORGANISATIONS # ORGANISATIONS
orga_file="${SRC_DIR}organisations.json" orga_file="${SRC_DIR}organisations.json"
shape="box"
color="${ORGA_COLOR}"
cat "${orga_file}"|jq -c '.[] | { cat "${orga_file}"|jq -c '.[] | {
name, name,
pid, pid,
@ -62,11 +75,15 @@ do
code=$(echo $orga|jq -r .code) code=$(echo $orga|jq -r .code)
name=$(echo $orga|jq -r .name) name=$(echo $orga|jq -r .name)
# write result in output # write result in output
output "Orga${pid} [shape=box color=\"transparent\" style=filled fillcolor=\"${ORGA_COLOR}\" label=<${name}<br/>(code: ${code})<br/>PID: ${pid}>]" identifier="Orga${pid}"
label="$(render ${tmpl_label})"
save "${tmpl_gizmo}"
done done
# LIBRARIES # LIBRARIES
lib_file="${SRC_DIR}libraries.json" lib_file="${SRC_DIR}libraries.json"
shape="house"
color="${LIB_COLOR}"
cat "${lib_file}"|jq -c '.[] | { cat "${lib_file}"|jq -c '.[] | {
name, name,
pid, pid,
@ -79,38 +96,44 @@ do
orga=$(echo $lib|jq -r .organisation) orga=$(echo $lib|jq -r .organisation)
orga_pid=$(echo $orga|rev|cut -d "/" -f 1|rev) orga_pid=$(echo $orga|rev|cut -d "/" -f 1|rev)
# write result in output # write result in output
l_id="Lib${pid}" identifier="Lib${pid}"
output "${l_id} [shape=house color=transparent style=filled fillcolor=\"${LIB_COLOR}\" label=<${name}<br/>(code: ${code})<br/>PID: ${pid}>]" label="$(render ${tmpl_label})"
output "${l_id} -> Orga${orga_pid}" save "${tmpl_gizmo}"
relation="Orga${orga_pid}"
save "${tmpl_link}"
done done
# PATRON_TYPES # PATRON_TYPES
pt_file="${SRC_DIR}patron_types.json" pt_file="${SRC_DIR}patron_types.json"
shape="polygon"
color="${PT_COLOR}"
additionals="sides=7"
cat "${pt_file}" |jq -c '.[] | { cat "${pt_file}" |jq -c '.[] | {
pid, pid,
name, name,
organisation: .organisation."$ref"}'|while read pt organisation: .organisation."$ref"}'|while read pt
do do
# # The char " is problematic. Delete it.
# pt=$(echo $pt| sed -e 's/^"//g' -e 's/"$//g')
# pid=$(echo $pt|cut -d "|" -f 1)
# name=$(echo $pt|cut -d "|" -f 2)
# orga=$(echo $pt|cut -d "|" -f 3)
pid=$(echo $pt|jq -r .pid) pid=$(echo $pt|jq -r .pid)
name=$(echo $pt|jq -r .name) name=$(echo $pt|jq -r .name)
orga=$(echo $pt|jq -r .organisation) orga=$(echo $pt|jq -r .organisation)
# write result in output # write result in output
p_id="Type${pid}" identifier="Type${pid}"
output "${p_id} [shape="polygon" sides=7 color=transparent style=filled fillcolor=\"${PT_COLOR}\" label=<${name}<br/>PID: ${pid}>]" label="$(render templates/label_patron_types.tmpl)"
save "${tmpl_gizmo}"
# Make a link with organisation if present # Make a link with organisation if present
if [[ -n "${orga}" ]]; then if [[ -n "${orga}" ]]; then
orga_pid=$(echo $orga|rev|cut -d "/" -f 1|rev) orga_pid=$(echo $orga|rev|cut -d "/" -f 1|rev)
output "${p_id} -> Orga${orga_pid}" relation="Orga${orga_pid}"
save "${tmpl_link}"
fi fi
done done
# USERS # USERS
user_file="${SRC_DIR}users.json" user_file="${SRC_DIR}users.json"
shape="ellipse"
additionals=''
color="${USER_COLOR}"
cat "${user_file}"|jq -c '.[] | { cat "${user_file}"|jq -c '.[] | {
email, email,
first_name, first_name,
@ -129,15 +152,13 @@ do
library_pid=$(echo $library|rev|cut -d "/" -f 1|rev) library_pid=$(echo $library|rev|cut -d "/" -f 1|rev)
pt=$(echo $user|jq -r .pt) pt=$(echo $user|jq -r .pt)
# Prepare additional info
if [[ "${roles}" != "null" ]]; then if [[ "${roles}" != "null" ]]; then
displayed_roles="roles: " displayed_roles="roles: "
for role in ${roles}; do for role in ${roles}; do
displayed_roles="${displayed_roles}<font color='red' >${role}</font>, " displayed_roles="${displayed_roles}<font color='red' >${role}</font>, "
done done
fi fi
# write result in output
u_id="User_${email}"
# Don't display barcode if no one # Don't display barcode if no one
info="" info=""
if [[ "${barcode}" != "null" ]]; then if [[ "${barcode}" != "null" ]]; then
@ -148,22 +169,27 @@ do
info="${info}<br/>${displayed_roles}" info="${info}<br/>${displayed_roles}"
fi fi
output "\"${u_id}\" [color=transparent style=filled fillcolor=\"${USER_COLOR}\" label=<${first_name} ${last_name}<br/>${email}${info}>]" # write result in output
identifier="User_${email}"
label="$(render templates/label_users.tmpl)"
save "${tmpl_gizmo}"
# Display a link if library_pid is not null # Display a link if library_pid is not null
if [[ "${library_pid}" != "null" ]]; then if [[ "${library_pid}" != "null" ]]; then
output "\"${u_id}\" -> Lib${library_pid}" relation="Lib${library_pid}"
save "${tmpl_link}"
fi fi
# Display a link if patron_type is not null # Display a link if patron_type is not null
if [[ "$pt" != "null" ]]; then if [[ "$pt" != "null" ]]; then
pt_id=$(echo $pt|rev| cut -d "/" -f 1|rev) pt_id=$(echo $pt|rev| cut -d "/" -f 1|rev)
output "\"${u_id}\" -> Type${pt_id}" relation="Type${pt_id}"
save "${tmpl_link}"
fi fi
done done
# end of file # end of file
output "}" save "templates/footer.tmpl"
# END of program # END of program

View File

@ -0,0 +1 @@
}

View File

@ -0,0 +1 @@
\"${identifier}\" [shape=${shape} color=\"transparent\" style=filled fillcolor=\"${color}\" label=${label} ${additionals}]

View File

@ -0,0 +1,3 @@
digraph {
rankdir = RL;
label = \"Link between organisations, libraries and users.\";

View File

@ -0,0 +1 @@
<${name}<br/>(code: ${code})<br/>PID: ${pid}>

View File

@ -0,0 +1 @@
<${name}<br/>PID: ${pid}>

View File

@ -0,0 +1 @@
<${first_name} ${last_name}<br/>${email}${info}>

View File

@ -0,0 +1 @@
\"${identifier}\" -> \"${relation}\"